基维。形象在while循环中从另一个线程运行时不更新

2024-09-28 01:23:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个基维。形象我的类中的对象主图像显示了来自网络摄像头的主流,另一个显示了从主流收集的训练样本进程可以完成,但我们首先运行它,它阻止了我的应用程序/导致它冻结的线程,然后我介绍了它运行的线程,但它没有更新主流和显示正在收集的训练样本的流。你知道吗

我在python中尝试了多种多线程方式,但似乎我搞不懂

    from kivy.uix.floatlayout import FloatLayout
    from kivy.uix.button import Button
    from kivy.app import App
    import threading

    import cv2
    from kivy.uix.image import Image
    from Clock_In import face as f
    class example(App):

        def build(self):

            layout = FloatLayout()
            self.user = Image()
            self.trainingSample = Image(source="C://Users//SELINA//Project Clock-in//data//Images//face_avatar.jpg")

            self.user.allow_stretch = True
            self.user.keep_ratio = True

            self.user.size_hint_x = 0.5
            self.user.size_hint_y = 0.9


            self.user.pos_hint = {"x":0.06, "y":0.2}

            self.trainingSample.allow_stretch =False
            self.trainingSample.keep_ratio = False

            self.trainingSample.size_hint_x = None
            self.trainingSample.size_hint_y = None

            self.trainingSample.width =160
            self.trainingSample.height=160

            self.face_Recognition = f.Recognition()
            self.capture = cv2.VideoCapture(0)
            self.trainingSample.pos_hint = {"x":0.6, "y": 0.68}
    self.btnText = Button(text="Collect Data", font_size=12, size_hint_y=None, size_hint_x=None, width = 150, height = 30, pos_hint= {"x":0.4, "y":0.6})
            self.btnText.bind(on_press=self.cdata)



            layout.add_widget(self.user)
            layout.add_widget(self.trainingSample)
            layout.add_widget(self.btnText)

            return layout
     #I have tried this
        def cdata(self, instance):
            threading.Thread(
              target=self.another, daemon=True
            ).start()

def another(self, dt):
    (self.im_width, self.im_height) = (160, 160)



    self.count = 0
    self.count_max = 10



    while self.count < self.count_max:

        ret = False

        while(not ret):
            (ret, frame) = self.capture.read()
            if(not ret):
                print("Couldn't run trying again")


                height, width, channels = frame.shape

                frame = cv2.flip(frame, 1, 0)
                normal = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                normal = cv2.cvtColor(normal, cv2.COLOR_RGB2BGR)

                if self.face_Recognition.identify(frame) is None:
                    faces = None


                if self.face_Recognition.identify(frame) is not None:
                    faces, points = self.face_Recognition.identify(frame)



                if faces is not None:
                    for face in faces:
                            face_bb = face.bounding_box.astype(int)
                            yourface = normal[max(0, face_bb[1]):min(face_bb[3], normal.shape[0]-1), max(0, face_bb[0]):min(face_bb[2], normal.shape[1]-1)]


                            face_resize = cv2.resize(yourface, (self.im_width, self.im_height))
                            cv2.rectangle(frame, (face_bb[0], face_bb[1]), (face_bb[2], face_bb[3]),(220,20,60), 1)
                            cv2.putText(frame, 'user', (face_bb[0], face_bb[3] + 20),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (220,20,60),1, cv2.LINE_AA)




                            print("Saving training sample " + str(self.count)+"/"+str(self.count_max))


                            cv2.imwrite('example' + str(self.count) + '.png', face_resize)

                            self.trainingSample.source = 'example' + str(self.count) + '.png'

                            self.count += 1

                    buf1 = cv2.flip(frame, 0)

                    buf = buf1.tostring()

                    texture1 = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
                    texture1.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')


                    self.user.texture = texture1





if __name__ == '__main__':
    example().run()

我想要那个自我用户为了在while循环的另一个线程中更新为it-taking用户的面部图像,并在training sample Image中显示用户的面部图像样本,我使用了github的david sandberg facenet,如果您可以尝试使用haarcascade或一些简单的问题不是识别,而是kivy小部件在收集训练样本时实时更新给用户


Tags: fromimportselfnonesizecountcv2frame
1条回答
网友
1楼 · 发布于 2024-09-28 01:23:42

修改后的代码仍然没有真正使用多线程,因为新的coll()方法只是安排another()方法在主线程上运行。你知道吗

要实际使用多线程,请将guts放回coll()方法,以便它实际在新线程中运行。并用Clock.schedule_once()调用替换self.user.texture = texture1。我认为应该是这样的(但我还没有测试过这段代码):

def cdata(self, instance):
    threading.Thread(
      target=self.coll, daemon=True).start()    

def coll(self):
    (self.im_width, self.im_height) = (160, 160)    
    self.count = 0
    self.count_max = 10  

    while self.count < self.count_max:    
        ret = False

        while(not ret):
            (ret, frame) = self.capture.read()
            if(not ret):
                print("Couldn't run trying again") 
                height, width, channels = frame.shape
                frame = cv2.flip(frame, 1, 0)
                normal = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                normal = cv2.cvtColor(normal, cv2.COLOR_RGB2BGR)

                if self.face_Recognition.identify(frame) is None:
                    faces = None
                if self.face_Recognition.identify(frame) is not None:
                    faces, points = self.face_Recognition.identify(frame)
                if faces is not None:
                    for face in faces:
                            face_bb = face.bounding_box.astype(int)
                            yourface = normal[max(0, face_bb[1]):min(face_bb[3], normal.shape[0]-1), max(0, face_bb[0]):min(face_bb[2], normal.shape[1]-1)]
                            face_resize = cv2.resize(yourface, (self.im_width, self.im_height))
                            cv2.rectangle(frame, (face_bb[0], face_bb[1]), (face_bb[2], face_bb[3]),(220,20,60), 1)
                            cv2.putText(frame, 'user', (face_bb[0], face_bb[3] + 20),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (220,20,60),1, cv2.LINE_AA)
                            print("Saving training sample " + str(self.count)+"/"+str(self.count_max))
                            cv2.imwrite('example' + str(self.count) + '.png', face_resize)    
                            self.trainingSample.source = 'example' + str(self.count) + '.png'    
                            self.count += 1    
                    buf1 = cv2.flip(frame, 0)    
                    buf = buf1.tostring()    
                    texture1 = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
                    texture1.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')  
                    #self.user.texture = texture1
                    Clock.schedule_once(partial(self.setUserTexture, texture1))

def setUserTexture(self, newTexture, dt)
    self.user.texture = newTexture

这是显示GUI的图片

相关问题 更多 >

    热门问题