Why gui stop capturing video from camera(opencv with python)?
I am new in field of programming, and the task of my project is to have gui using python and tkinter
:
- first key in menu should open the video when it pressed.
- second key should recognize the faces when it pressed and keep recognizing until the program stop.
Issue: when I run the code and press first button, code works fine and start to capture the video. But when I press second key, program stops showing the video (also rectangle around the face does not appear) but the program still gives me output message that I expect when it recognize known faces without giving any error messages.
Why does it stop showing the video? Do I need to create class for such project or what?
Thank you in advance.
Here is my code:
import cv2
import numpy as np
import dlib
import face_recognition
import os
import time
import playsound
from gtts import gTTS
import speech_recognition as sr
import winsound
from time import sleep
from tkinter import *
from PIL import Image
from PIL import ImageTk
faces = []
encode = []
known_faces = []
names = []
white = "#ffffff"
lightBlue2 = "#adc5ed"
font = "Constantia"
fontButtons = (font, 12)
maxWidth = 1000
maxHeight = 600
mainWindow = Tk()
mainWindow.title('SUTech')
mainWindow.configure(bg=lightBlue2)
mainWindow.geometry('%dx%d+%d+%d' % (maxWidth,maxHeight,0,0))
mainFrame = Frame(mainWindow)
mainFrame.place(x=480,y=0)
lmain = Label(mainFrame)
cap = cv2.VideoCapture(0)
def show_frame():
ret, frame = cap.read()
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(cv2image).resize((512, 512))
imgtk = ImageTk.PhotoImage(image=img)
lmain.grid(row=0, column=0)
lmain.grid(row=0, column=0)
lmain.imgtk = imgtk
lmain.configure(image=imgtk)
lmain.after(10, show_frame)
def facing():
x = 0
for filename in os.listdir('faces/'):
if filename.endswith('.jpg'):
faces.append(face_recognition.load_image_file(os.path.join('faces/',filename)))
encode.append(face_recognition.face_encodings(faces[x])[0])
known_faces.append(encode[x])
names.append(filename.replace('.jpg', ''))
x = x + 1
while(cap.isOpened()):
ret, frame = cap.read()
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
location = face_recognition.face_locations(img, number_of_times_to_upsample=1, model=0)
encodings = face_recognition.face_encodings(img, location)
name = 'Unkown'
for encoding in encodings:
match = face_recognition.compare_faces(known_faces, encoding,tolerance=0.9)
distance = face_recognition.face_distance(known_faces, encoding)
bestMatch = np.argmin(distance)
if match[bestMatch]:
name = names[bestMatch]
print('Detected face:'+ str(name))
for (x, y, w, h) in (location):
cv2.rectangle(frame,(h, x),(y, w), (0,255,0), 3)
cv2.rectangle(frame,(h , w+30),(y, w), (0,255,0), -1)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (h , w+25), cv2.FONT_HERSHEY_COMPLEX,1, (0,0,255), 2)
#file menu
menubar = Menu(mainWindow)
file = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='File', menu = file)
file.add_command(label ='New File', command = show_frame)
file.add_separator()
file.add_command(label ='Face_detection', command = facing)
file.add_separator()
file.add_command(label ='Exit', command = lambda:mainWindow.destroy())
mainWindow.config(menu = menubar)
mainWindow.mainloop()