I followed the instructions correctly but i still got some errors please help thank you. I m trying to start front camera
from
kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics.texture import Texture
from kivy.uix.camera import Camera
import numpy as np
import cv2
from kivy.properties import ObjectProperty
from jnius import autoclass
from android.permissions import request_permissions, Permission
camerax = autoclass('android.hardware.Camera')
CameraInfo = autoclass('android.hardware.Camera$CameraInfo')
class AndroidCamera(Camera):
camera_resolution = (1280, 720)
index = CameraInfo.CAMERA_FACING_FRONT
def _camera_loaded(self, *largs):
self.texture = Texture.create(size=np.flip(self.camera_resolution), colorfmt='rgb')
self.texture_size = list(self.texture.size)
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)
def frame_from_buf(self):
w, h = self.resolution
frame = np.frombuffer(self._camera._buffer.tostring(), 'uint8').reshape((h + h // 2, w))
frame_bgr = cv2.cvtColor(frame, 93)
return np.rot90(frame_bgr, 3)
def frame_to_screen(self, frame):
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
flipped = np.flip(frame_rgb, 0)
print(flipped)
buf = flipped.tostring()
self.texture.blit_buffer(buf, colorfmt='rgb', bufferfmt='ubyte')
class Box(BoxLayout):
pass
class MainApp(App):
def build(self):
request_permissions([Permission.CAMERA])
return Box()
if __name__ == '__main__':
MainApp().run()