kivy and opencv android

94 views
Skip to first unread message

Degenerate Tech

unread,
Jun 25, 2021, 2:22:29 PM6/25/21
to Kivy users support
i have found this code working in android .but camera image is lagging .i think numpy operations taking huge time .is there any way to make it faster ?
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics.texture import Texture
from kivy.uix.camera import Camera
from kivy.lang import Builder
import numpy as np
import cv2
import logging

Builder.load_file("myapplayout.kv")

class AndroidCamera(Camera):
    camera_resolution = (640, 480)
    counter = 0


    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
        logging.info("######### H1")
        frame = self.frame_from_buf()
        logging.info("######### H2")

        self.frame_to_screen(frame)
        logging.info("######### H3")
        super(AndroidCamera, self).on_tex(*l)
        logging.info("######### H4")

    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)
        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__':
    MyApp().run()

Robert

unread,
Jun 29, 2021, 7:51:54 PM6/29/21
to Kivy users support
Use Xcamera.
Reply all
Reply to author
Forward
0 new messages