I tried to build my Kivy and Python files to become an APK file, however it gives me a "NoneType" object has no attribute 'blit buffer' error. I followed the solution in this link and I tried to re-build it again, but it is giving me the same error on the logcat.
I executed the android clean first before re-building the application, however no luck.
Here's my main.py:
import kivy
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics.texture import Texture
from kivy.uix.image import Image
from kivy.uix.camera import Camera
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.logger import Logger
from kivy.properties import ObjectProperty, NumericProperty
import numpy as np
import cv2
class CamApp(App):
pass
class AndroidCamera(Camera):
camera_resolution = (640, 480)
counter = 0
def on_tex(self, *l):
if self._camera._buffer is None:
return None
frame = self.frame_from_buf()
self.frame_to_screen(frame)
super(AndroidCamera, self).on_tex(*l)
self.texture = Texture.create(size=self.camera_resolution, colorfmt='rgb')
self.texture_size = list(self.texture.size)
def frame_from_buf(self):
w, h = self.resolution
frame = np.frombuffer(self._camera._buffer.tostring(), 'uint8').reshape((h + h // 2, w)) # TODO: try to understand why the frame size has nothing to do with the original resolution
print(f'{frame.shape = }')
frame_bgr = cv2.cvtColor(frame, cv2.COLOR_YUV2BGR_NV21)
return frame_bgr
def frame_to_screen(self, frame):
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
cv2.putText(frame_rgb, str(self.counter), (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
self.counter += 1
flipped = np.flip(frame_rgb, 0)
buf = flipped.tostring()
self.texture.blit_buffer(buf, colorfmt='rgb', bufferfmt='ubyte')
class MyLayout(BoxLayout):
pass
class MyApp(App):
def build(self):
return MyLayout()
if __name__ == '__main__':
Builder.load_file("myapplayout.kv")
MyApp().run()
Here's my myapplayout.kv:
<MyLayout>
orientation: 'horizontal'
size: root.width, root.height
AndroidCamera:
index: 0
resolution: self.camera_resolution
allow_stretch: True
play: True
I specified python3, kivy, opencv, and numpy in the requirements section of the buildozer and as well as the CAMERA in the android permission section.