Come Usare La WEBCAM per il Riconoscimento Facciale con Python
Pubblicato il 09 Gennaio 2020 da Michele Saba
Ciao a tutti ragazzi e ragazze!
In questo video tutorial vedremo assieme come usare la WEBCAM per effettuare operazioni di riconoscimento facciale in tempo reale con Python.
Se non avete visto i due tutorial precedenti al riguardo dovreste decisamente andare a vedere prima quelli, in quanto diversamente quanto esposto qui potrebbe risultare molto avanzato.
Di seguito il codice sorgente utilizzato:
import face_recognition
import cv2 # pip install opencv-python
webcam = cv2.VideoCapture(0)
image_file = input("Target Image File > ")
target_image = face_recognition.load_image_file(image_file)
target_encoding = face_recognition.face_encodings(target_image)[0]
print("Image Loaded. 128-dimension Face Encoding Generated. \n")
target_name = input("Target Name > ")
process_this_frame = True
while True:
ret, frame = webcam.read()
small_frame = cv2.resize(frame, None, fx=0.20, fy=0.20)
rgb_small_frame = cv2.cvtColor(small_frame, 4)
if process_this_frame:
face_locations = face_recognition.face_locations(rgb_small_frame)
frame_encodings = face_recognition.face_encodings(rgb_small_frame)
if frame_encodings:
frame_face_encoding = frame_encodings[0]
match = face_recognition.compare_faces([target_encoding], frame_face_encoding)[0]
label = target_name if match else "Unknown"
process_this_frame = not process_this_frame
if face_locations:
top, right, bottom, left = face_locations[0]
top *= 5
right *= 5
bottom *= 5
left *= 5
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.rectangle(frame, (left, bottom - 30), (right, bottom), (0, 255, 0), cv2.FILLED)
label_font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, label, (left + 6, bottom - 6), label_font, 0.8, (255, 255, 255), 1)
cv2.imshow("Video Feed", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
webcam.release()
cv2.destroyAllWindows()
Oppure direttamente dal repo nel mio profilo GitHub
Happy Coding!