Posts: 3
Threads: 1
Joined: Jan 2021
Hi everyone !
I'm a beginner in python. I would like to know how to use a facial recognition program that I made in a graphical interface with Tkinter?
Thank you for your help
Posts: 12,054
Threads: 488
Joined: Sep 2016
Posts: 3
Threads: 1
Joined: Jan 2021
Hi, I only have the code for recognition.
import cv2
import operator
face_cascade = cv2.CascadeClassifier("./haarcascade_frontalface_alt2.xml")
profile_cascade = cv2.CascadeClassifier("./haarcascade_profileface.xml")
webcam = cv2.VideoCapture(0)
width = int(webcam.get(3))
marge = 80
while True:
ret, frame = webcam.read()
tab_face = []
speed = cv2.getTickCount()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
face = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=4)
for x, y, w, h in face:
tab_face.append([x, y, x+w, y+h])
face = profile_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=4)
for x, y, w, h in face:
tab_face.append([x, y, x+w, y+h])
gray2 = cv2.flip(gray, 1)
face = profile_cascade.detectMultiScale(gray2, scaleFactor=1.2, minNeighbors=4)
for x, y, w, h in face:
tab_face.append([width - x, y, width - (x+w), y+h])
tab_face = sorted(tab_face, key=operator.itemgetter(0, 1))
index = 0
for x, y, x2, y2 in tab_face:
if not index or (x - tab_face[index - 1][0] > marge or y - tab_face[index - 1][1] > marge):
cv2.rectangle(frame, (x, y), (x2, y2), (0, 0, 255), 2)
index += 1
if cv2.waitKey(1) == ord('q'):
break
fps = cv2.getTickFrequency() / (cv2.getTickCount()-speed)
cv2.putText(frame, "FPS : {:05.2f}".format(fps), (10, 30), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 2)
cv2.imshow('video', frame)
webcam.release()
cv2.destroyAllWindows()
Posts: 3,458
Threads: 101
Joined: Sep 2016
Jan-29-2021, 06:58 PM
(This post was last modified: Jan-29-2021, 06:58 PM by nilamo.)
I don't have the time to try it right now, but the frame returned from webcam.read() is a numpy array of pixel colors, so it should be fairly easy to convert.
A quick google search shows something like this should work (PIL is part of the pillow package): import cv2
import tkinter as tk
import PIL
import PIL.Image
import PIL.ImageTk
app = tk.Tk()
webcam = cv2.VideoCapture(0)
width = webcam.get(cv2.CAP_PROP_FRAME_WIDTH)
height = webcam.get(cv2.CAP_PROP_FRAME_HEIGHT)
canvas = tk.Canvas(app, width=width, height=height)
canvas.pack()
def update():
ret, frame = webcam.read()
cv2.putText(frame, "example write, before converting to image",
(10, 30), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 2)
image = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(frame))
canvas.create_image(0, 0, image=image, anchor=tk.NW)
app.after(1000, update)
update()
app.mainloop()
webcam.release()
cv2.destroyAllWindows() That doesn't work (at least for me), but it should be very close to a working solution.
Posts: 3
Threads: 1
Joined: Jan 2021
Thank you very much for your answer.
I found this solution to work but I don't understand everything that is being done. I will inquire.
import tkinter as tk
import cv2
from PIL import Image, ImageTk
width, height = 800, 600
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
root = tk.Tk()
root.bind("<Escape>", lambda e: root.quit())
lmain = tk.Label(root)
lmain.pack()
def show_frame():
ref, frame = cap.read()
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)
show_frame()
root.mainloop()
|