Wednesday, 6 October 2021

Kivy visualizing image from external USB camera in android phone

I have a python script that successfully reads images from my USB webcam on desktop. Currently, I am writting a mobile version of my script because my web camera is inteded to be used on my smartphone usb-c. Therefore, I'd like to know how can I specifically select my usb camera plugged to the phone using python/kivy. I know for instance, the video capture automatically opens a device that is detected as camera, but on phone I imagine it'll open the default camera(s), in which case i'd need to specify the camera.

from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.button import MDRaisedButton
from kivy.clock import Clock
from kivy.graphics.texture import Texture
import cv2
from kivy.uix.image import Image

class MainApp(MDApp):

    def build(self):
        layout = MDBoxLayout(orientation='vertical')
        self.image = Image()
        layout.add_widget(self.image)
        self.save_img_button = MDRaisedButton(
            text="Click here",
            pos_hint={'center_x': .5, 'center_y': .5},
            size_hint=(None, None))
        self.save_img_button.bind(on_press=self.take_picture)
        layout.add_widget(self.save_img_button)
        self.capture = cv2.VideoCapture(1)
        Clock.schedule_interval(self.load_video, 1.0/50.0)
        return layout

    def take_picture(self, *args):
        image_name = "captured_frame.png"
        cv2.imwrite(image_name, self.image_frame)


    def load_video(self, *args):
        ret, frame = self.capture.read()
        self.image_frame = frame
        buffer = cv2.flip(frame, 0).tostring()
        texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
        texture.blit_buffer(buffer, colorfmt='bgr', bufferfmt='ubyte')
        self.image.texture = texture

if __name__ == "__main__":
    MainApp().run()

My app currently loads the camera, so I can take a picture.

I need a Kivy solution that allows me to select an external USB camera connected to my android and visualize on the android screen the image from the web camera.



from Kivy visualizing image from external USB camera in android phone

No comments:

Post a Comment