Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么opencv要创建两个窗口?_Python_Opencv_Tensorflow_Tk - Fatal编程技术网

Python 为什么opencv要创建两个窗口?

Python 为什么opencv要创建两个窗口?,python,opencv,tensorflow,tk,Python,Opencv,Tensorflow,Tk,从Github()中找到用于情感检测的代码。我想在它做一些改变,并希望与Tkinter结合,以方便用户交互。如下图所示。 在这里,我为tk窗口编写了一些代码: win = Tk() win.title('EMOTIBOT') win.config(background = "#D9D9D9") win.resizable(width=FALSE, height=FALSE) #frame1 for webcamera frame1 = Frame(win, width=600, bg="bla

从Github()中找到用于情感检测的代码。我想在它做一些改变,并希望与Tkinter结合,以方便用户交互。如下图所示。 在这里,我为tk窗口编写了一些代码:

win = Tk()
win.title('EMOTIBOT')
win.config(background = "#D9D9D9")
win.resizable(width=FALSE, height=FALSE)

#frame1 for webcamera
frame1 = Frame(win, width=600, bg="black",height=300, padx=10, pady=10,highlightbackground="grey", highlightcolor="black", highlightthickness=5)
frame1.pack(side=LEFT ,fill=Y,padx=10, pady=10)
在这里,我对原始代码做了一些更改:

if mode == "display":
    model.load_weights('model.h5')

    # prevents openCL usage and unnecessary logging messages
    cv2.ocl.setUseOpenCL(False)

    # dictionary which assigns each label an emotion (alphabetical order)
    emotion_dict = {0: "Angry", 1: "Disgusted", 2: "Fearful", 3: "Happy", 4: "Neutral", 5: "Sad", 6: "Surprised"}

    # start the webcam feed
    cap = cv2.VideoCapture(0)

    def show_frame():
        _, frame = cap.read()
        frame = cv2.flip(frame, 1)
        #cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)

        #while True:
        facecasc = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
        cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = facecasc.detectMultiScale(cv2image,scaleFactor=1.3, minNeighbors=5)

        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y-50), (x+w, y+h+10), (255, 0, 0), 2)
            roi_gray = gray[y:y + h, x:x + w]
            cropped_img = np.expand_dims(np.expand_dims(cv2.resize(roi_gray, (48, 48)), -1), 0)
            prediction = model.predict(cropped_img)
            maxindex = int(np.argmax(prediction))
            cv2.putText(frame, emotion_dict[maxindex], (x+20, y-60), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)

        cv2.imshow('Video', cv2.resize(frame,(1600,960),interpolation = cv2.INTER_CUBIC))

        img = Image.fromarray(cv2image)
        imgtk = ImageTk.PhotoImage(image=img)
        lmain.imgtk = imgtk
        lmain.configure(image=imgtk)
        lmain.after(10, show_frame) 

    #Capture video frames
    lmain = Label(frame1)
    lmain.grid(row=0, column=0)

    show_frame()  #Display 2
    win.mainloop()
这是打开两个窗口。第一个窗口,Tk窗口,在检测到的情绪上没有矩形。第二个窗口显示检测到的情绪的矩形。为什么第一个tk窗口没有在面上显示矩形?

如果你需要我的脚本文件,就在这里。()


我对OpenCV和Tk都是新手。请帮助我。

将我们的讨论总结为一个答案:

  • 删除
    cv2.imshow
    以删除第二个窗口
  • 使用
    img=Image.fromarray(cv2.cvtColor(frame,cv2.COLOR\u BGR2RGBA))
    以正确的颜色显示带有检测到的人脸的帧

  • 我认为是
    cv2.imshow
    打开了第二个窗口,您可以尝试删除该行吗?如果您要从
    cv2image
    开始为tk窗口创建框架,您应该使用
    frame
    instead@BlackBear我删除了
    cv2.imshow
    ,它确实解决了我的错误。非常感谢。但是主Tk窗口中的cam提要没有检测到情绪。请尝试使用
    img=Image.fromarray(frame)
    @BlackBear能否详细说明在何处使用
    frame
    。谢谢