使用OpenCV2(面部识别)后,Tkinter窗口无法更新

时间:2017-09-25 21:19:16

标签: python opencv tkinter

我正在研究Tkinter和OpenCV 2(面部识别)。我制作了一个程序,当你按下一个按钮(来自Tkinter)时,程序开始识别过程,当用户按下" Escape"识别过程停止并在tkinter窗口中出现一个新标签,指示主题是谁。

这最后一件事是不行的。当我按下" Escape",以完成识别过程时,tkinter窗口不会使用新标签进行更新。

代码很大但是在这里:

import cv2, sys, numpy, os
from Tkinter import *

global size
global fn_haar
global fn_dir
global Welcome

os.system("sudo modprobe bcm2835-v4l2")
size = 5
fn_haar = '/usr/share/opencv/haarcascade/haarcascade_frontalface_alt.xml'
fn_dir = '/home/pi/Control'

def recognition():
    (images, lables, names, id) = ([], [], {}, 0)
    subdir = "Subject 1"
    names[id] = subdir
    subjectpath = os.path.join(fn_dir, subdir)
    for filename in os.listdir(subjectpath):
        f_name, f_extension = os.path.splitext(filename)
        if(f_extension.lower() not in ['.png','.jpg','.jpeg','.gif','.pgm']):
            continue
        path = subjectpath + '/' + filename
        lable = id
        images.append(cv2.imread(path, 0))
        lables.append(int(lable))
    (im_width, im_height) = (112, 92)
    (images, lables) = [numpy.array(lis) for lis in [images, lables]]
    model = cv2.createLBPHFaceRecognizer()
    model.train(images, lables)
    haar_cascade = cv2.CascadeClassifier(fn_haar)
    webcam = cv2.VideoCapture(0)
    while True:
        rval = False
        while(not rval):
            (rval, frame) = webcam.read()
            if(not rval):
                print "Failed to open webcam. Trying again..."

        frame=cv2.flip(frame,1,0)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        mini = cv2.resize(gray, (int(gray.shape[1] / size), int(gray.shape[0] / size)))
        faces = haar_cascade.detectMultiScale(mini)

        for i in range(len(faces)):
            face_i = faces[i]
            (x, y, w, h) = [v * size for v in face_i]
            face = gray[y:y + h, x:x + w]
            face_resize = cv2.resize(face, (im_width, im_height))
            prediction = model.predict(face_resize)
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)

            if prediction[1]<90:
                cv2.putText(frame,'%s - %.0f' %(names[prediction[0]],prediction[1]),(x-10, y-10),cv2.FONT_HERSHEY_PLAIN,1,(0, 255, 0))

            else:
                cv2.putText(frame,'Unknown',(x-10, y-10),cv2.FONT_HERSHEY_PLAIN,1,(0, 255, 0))

        cv2.imshow('OpenCV', frame)
        cv2.moveWindow('OpenCV', 350, 120)
        key = cv2.waitKey(10)
        if key == 27:
            cv2.startWindowThread()
            cv2.destroyWindow('OpenCV')
            L1 = Label(Welcome, text = "The subject is:" + names[prediction[0]]).pack()

Welcome = Tk()
Welcome.title ("Welcome")
Welcome.geometry ("300x100+1+1")
LabelWelcome = Label(Welcome, text = "Welcome to the facial recognitio program").pack()
B1 = Button(Welcome, text="START", command=recognition).pack()
Welcome.mainloop()

程序运行正常,直到我按下&#34; Escape&#34;,然后它没有用新标签更新窗口,窗口就冻结了。

所以,在做了一些测试后,我认为问题是&#34; cv2.destroyWindow(&#39; OpenCV&#39;)&#34;摧毁主循环或类似的东西。

我的问题是:

  • 这是问题吗? cv2.destroyWindow(&#39; OpenCV&#39;)也销毁了主循环?
  • 如果这是问题,我该怎么办?
  • 如果那不是问题,你知道会发生什么吗?
PD:我想,也许,如果问题出在cv2.destroyWindow上,我可以隐藏那些窗口(openCV)而不是关闭它(不知道这是否可能)。<登记/> PD2:我知道如果这段代码有效,我每次运行识别部分时都会添加一个标签,但我暂时还不错。

提前致谢。

0 个答案:

没有答案