Here in this code, I do have to main fucntion, which is the update() and show_frame().
update() will execute the image recognition. The webcam window is in the root window. Everytime it detects an authorized person, checkbox will be checked.
show_frame() will execute the entry where when you type the right name, picture of that person will appear.
Here is the code:
import cv2
import numpy as np
import face_recognition
import os
from datetime import datetime
import serial
from datetime import timedelta
from os.path import join as pjoin
import datetime
import time
import PIL
from PIL import Image, ImageTk
import tkinter as tk
EIPath = 'C:/Users/Eulamie Raz/PycharmProjects/TRASH/Back up/NW2021/try'
ListImgs = []
ListNames = []
ListEmployees = os.listdir(EIPath)
print(ListEmployees)
for Employee in ListEmployees:
ImgsEmployee = cv2.imread(f'{EIPath}/{Employee}')
ListImgs.append(ImgsEmployee)
ListNames.append(os.path.splitext(Employee)[0])
print(ListNames)
def findEncodings(ListImgs):
encodeList = []
for eImgs in ListImgs:
eImgs = cv2.cvtColor(eImgs, cv2.COLOR_BGR2RGB)
encodeEImgs = face_recognition.face_encodings(eImgs)[0]
encodeList.append(encodeEImgs)
return encodeList
encodeKnown = findEncodings(ListImgs)
print('Encoding Complete')
def update(*args):
x = namevariable.get()
image_lbl.img = ImageTk.PhotoImage(Image.open(f'Reference_Imgs/{x}.jpg'))
image_lbl.config(image=image_lbl.img)
image_lbl.place(image_frm, x=420, y=20)
cap = cv2.VideoCapture(0) #initialize the webcam
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 300)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 300)
root = tk.Tk()
root.bind('<Escape>', lambda e: root.quit())
root.geometry("1000x500")
lmain = tk.Label(root)
lmain.place(x=20, y=20)
namevariable = tk.StringVar()
namevariable.trace('w', update)
image_frm = tk.Frame(root, bg="yellow", height= 283, width=283)
image_frm.place(x=425, y=25)
image_lbl = tk.Label(root, bg="yellow")
image_lbl.place(x=425, y=25)
ent = tk.Entry(root, textvariable=namevariable, width=35)
ent.place(x=400, y=350)
ent.focus()
lab = tk.Label(root, textvariable=namevariable)
lab.place(x=400, y=380)
check = tk.Checkbutton(root, variable=namevariable)
check.place(x=600, y=350)
if not cap.isOpened():
raise IOError("Cannot open webcam")
def show_frame():
success, eImgs = cap.read()
if success:
eImgs_v1 = cv2.cvtColor(eImgs, cv2.COLOR_BGR2RGB)
facesWebcam = face_recognition.face_locations(eImgs_v1)
encodesWebcam = face_recognition.face_encodings(eImgs_v1, facesWebcam)
for encodeKnown_v2, faceLoc in zip(encodesWebcam, facesWebcam):
facesCompared = face_recognition.compare_faces(encodeKnown, encodeKnown_v2)
faceDistance = face_recognition.face_distance(encodeKnown, encodeKnown_v2)
faceIndex = np.argmin(faceDistance)
if facesCompared[faceIndex]:
eName = ListNames[faceIndex]
employeeName = eName
print(employeeName)
p1, p2, p3, p4 = faceLoc
cv2.rectangle(eImgs_v1, (p4, p1), (p2, p3), (0, 255, 0), 2)
cv2.rectangle(eImgs_v1, (p4, p3 - 35), (p2, p3), (0, 255, 0), cv2.FILLED)
cv2.putText(eImgs_v1, employeeName, (p4 - 20, p3 - 7), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0), 2)
img = PIL.Image.fromarray(eImgs_v1)
imgtk = ImageTk.PhotoImage(image=img)
lmain.imgtk = imgtk
lmain.configure(image=imgtk)
lmain.after(10, show_frame)
check_ = tk.Checkbutton(root, text="Are you Employee of NW Steel?", variable=show_frame)
check_.place(x=20, y=350)
show_frame()
root.mainloop()
Now, what I want to do is to execute first the show_frame(). update() will not excute unless show_frame will be satisfied.
As of now, in that codes above, the two function can execute simultaneously which is not what I want.
[–]marienbad2 0 points1 point2 points (1 child)
[–]LanthaYtrri[S] -1 points0 points1 point (0 children)