其他分享
首页 > 其他分享> > kivy自定义相机

kivy自定义相机

作者:互联网

新版本自定相机 使用的时候一定要注意把所有方法和__init__ 放到你要使用的类中

class CustomCamera():
    def __init__(self):
        self.Flag_list = [0, 1]
        self.image_from_camera = []
        self.camera_flog = [False]
        self.detail_flog = [False]

    # 将numpy转为texture
    def numpy_to_texture(self, frame, colorfmt='bgr'):
        buf1 = cv2.flip(frame, 0)
        buf = buf1.tostring()
        image_texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt=colorfmt)
        image_texture.blit_buffer(buf, colorfmt=colorfmt, bufferfmt='ubyte')
        return image_texture    def cam(self, *kwargs):

        ret, frame = self.Flag_list[0].read()
        if ret:
            node1 = self.numpy_to_texture(frame[:, ::-1, :])
            self.image_from_camera.append(node1)
            self.camera_to_page()

    # 这就是动态显示的模板
    def camera_to_page(self):
        # 显示相机到页面
        if len(self.image_from_camera) >=1:
            print("的点点滴滴多多多多多多多多多多")
            node1 = self.image_from_camera.pop(0)
            self.Flag_list[1].clear_widgets()
            if self.camera_flog:
                node = Scatter(pos=self.Flag_list[1].pos, size=self.Flag_list[1].size)
                with node.canvas:
                    Rectangle(size=node.size, texture=node1)
                self.Flag_list[1].add_widget(node)
                # self.Flag_list[0].add_widget(node2)

    # 自定义相机
    def coustom_camera(self, par):
        self.Flag_list[1] = par
        self.camera_flog[0] = not self.camera_flog[0]
        if not self.camera_flog[0]:
            self.Flag_list[0].release()
            self.image_from_camera = []
            print("tun off")
        try:
            cap = cv2.VideoCapture(0)
        except:
            cap = cv2.VideoCapture(1)
        self.Flag_list[0] = cap        if not self.camera_flog[0]:
            cap.release()
            Clock.unschedule(self.cam)
        else:
            Clock.schedule_interval(self.cam, 1.0 / 20)

               

标签:自定义,self,list,texture,相机,Flag,camera,kivy,image
来源: https://blog.51cto.com/u_15177056/2726142