0

I have done real time face detection system, but I need to add gui for the program. Instead of using the solution from here. I does not want to read frame in a recursion way.

def show_frame():
    _,frame = cap.read()
    ...  #skip 
    lmain.after(10,show_frame)

It require a lot of refactoring in my previous code. So, I prefer read frame in a while loop way. But it does not work. Thanks for help.

import numpy as np
import cv2
import tkinter as tk
from PIL import Image, ImageTk

window = tk.Tk()
window.wm_title("Test")

imageFrame = tk.Frame(window, width=600, height=500)
imageFrame.grid(row=0, column=0, padx=10, pady=2)

#Capture video frames
lmain = tk.Label(imageFrame)
lmain.grid(row=0, column=0)
cap = cv2.VideoCapture(0)

def show_frame(frame):
    frame = cv2.flip(frame, 1)
    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
    img = Image.fromarray(cv2image)
    imgtk = ImageTk.PhotoImage(image=img)
    lmain.imgtk = imgtk
    lmain.configure(image=imgtk)
    # lmain.after(10, show_frame) 

while True:
    _,frame = cap.read()
    show_frame(frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
window.mainloop() 
oka96
  • 383
  • 2
  • 16
  • `after` is not recursion, it just schedules the same function to be dispatched at later time and ends – Dan Mašek Apr 21 '18 at 10:21
  • [This Q&A](https://stackoverflow.com/q/29158220/3962537) may shed some light on it, and I suggest doing some follow-up research on the topic as well. Your main issue right now seems lack of understanding of the basic principles of how GUI operates and should be programmed. This leads you to dismiss legitimate solutions due to misinterpretation, and trying to shoehorn it onto a regular command line application model. | e.g. `cv2.waitKey` has no business being here, if you want keyboard events, [capture them using tkinter](http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm). – Dan Mašek Apr 21 '18 at 10:36

0 Answers0