Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import mysql.connector
- import face_recognition
- import cv2
- import numpy as np
- video_capture = cv2.VideoCapture(0) # capturam camera video
- mydb = mysql.connector.connect( # accesam baza de date
- host="localhost",
- user="root",
- password="",
- database="mydatabase"
- )
- lista_nume_DB = [] # liste pentru informatiile pe care le vom prelua din baza de date
- lista_imagini = []
- mycursor = mydb.cursor() # clasa cu pe care o utilizam pentru BD
- mycursor.execute("SELECT NumePrenume FROM facedetection ") # comanda pentru a prelua Numele studentului din baza de date
- myresult = mycursor.fetchall()
- lista_nume_DB = [item for t in myresult for item in t] # intrucat se preia ca si un tuplu, o convertim la lista
- mycursor.execute("SELECT locatieImagine FROM facedetection ") # comanda pentru a prelua path-ul pentru imaginea studentului
- myresult = mycursor.fetchall()
- lista_imagini = [item for t in myresult for item in t] # intrucat se preia ca si un tuplu, o convertim la lista
- listaFete_codificateDB = [] # codificam imaginile preluate din baza de date
- for i in range(len(lista_imagini)):
- imagine = lista_imagini[i] # luam fiecare imagine pe rand si o codificam
- imagine_incarcata = face_recognition.load_image_file(imagine)
- imagine_codificata = face_recognition.face_encodings(imagine_incarcata)[0]
- listaFete_codificateDB.append(imagine_codificata) # adaugam imaginile codificate la o lista
- print(listaFete_codificateDB[0])
- # liste unde se vor stoca informatii din fluxul video
- fetele_gasite = []
- fetele_codificate_gasite = []
- numele_fetelor_gasite = []
- process_this_frame = True
- while True:
- # preluam frame cu frame din video
- ret, frame = video_capture.read()
- # relizam un redimensionare a capturii, pentru a face recunoasterea faciala mai rapida
- small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
- # se converteste imaginea de la BGR, pe care OpenCV-ul o utilizeaza la RGB deoarece modulul face_recognition il utilizeaza
- rgb_small_frame = small_frame[:, :, ::-1]
- # Procesam frame cu frame
- if process_this_frame:
- # Gasim fetele, pe care le codificam
- fetele_gasite = face_recognition.face_locations(rgb_small_frame)
- fetele_codificate_gasite = face_recognition.face_encodings(rgb_small_frame, fetele_gasite)
- numele_fetelor_gasite = []
- for fata_codificata in fetele_codificate_gasite:
- # verificam daca fetele din fluxul video se regasesc in baza de date
- matches = face_recognition.compare_faces(listaFete_codificateDB, fata_codificata)
- name = "Unknown" # presupunem ca nu
- face_distances = face_recognition.face_distance(listaFete_codificateDB, fata_codificata)
- best_match_index = np.argmin(face_distances)
- if matches[best_match_index]: # in cazul in care se regaseste, adaugam numele in baza de date
- name = lista_nume_DB[best_match_index]
- numele_fetelor_gasite.append(name)
- process_this_frame = not process_this_frame # incheiem prelucrarea frame-ului curent
- # afisam rezultatul
- for (top, right, bottom, left), name in zip(fetele_gasite, numele_fetelor_gasite):
- top *= 4
- right *= 4
- bottom *= 4
- left *= 4
- # se deseneaza un patrat in jurul fetei
- cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
- # adaugam labe-ul cu numele patratului
- cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
- font = cv2.FONT_HERSHEY_DUPLEX
- cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
- # afisam in fluxul video
- cv2.imshow('Video', frame)
- # se apasa tasta 'q' pentru a iesi din program
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- # inchidem camera si fereastra
- video_capture.release()
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement