Advertisement
barbos01

faceDetectionV3

Apr 4th, 2022
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.02 KB | None | 0 0
  1. import mysql.connector
  2. import face_recognition
  3. import cv2
  4. import numpy as np
  5.  
  6. video_capture = cv2.VideoCapture(0) # capturam camera video
  7.  
  8. mydb = mysql.connector.connect( # accesam baza de date
  9.   host="localhost",
  10.   user="root",
  11.   password="",
  12.   database="mydatabase"
  13. )
  14.  
  15. lista_nume_DB = [] # liste pentru informatiile pe care le vom prelua din baza de date
  16. lista_imagini = []
  17.  
  18. mycursor = mydb.cursor() # clasa cu pe care o utilizam pentru BD
  19.  
  20. mycursor.execute("SELECT NumePrenume FROM facedetection ")  # comanda pentru a prelua Numele studentului din baza de date
  21. myresult = mycursor.fetchall()
  22. lista_nume_DB = [item for t in myresult for item in t]  # intrucat se preia ca si un tuplu, o convertim la lista
  23.  
  24.  
  25. mycursor.execute("SELECT locatieImagine FROM facedetection ")   # comanda pentru a prelua path-ul pentru imaginea studentului
  26. myresult = mycursor.fetchall()
  27. lista_imagini = [item for t in myresult for item in t]  # intrucat se preia ca si un tuplu, o convertim la lista
  28.  
  29.  
  30. listaFete_codificateDB = []                                                     # codificam imaginile preluate din baza de date
  31. for i in range(len(lista_imagini)):
  32.   imagine = lista_imagini[i]                                                    # luam fiecare imagine pe rand si o codificam
  33.   imagine_incarcata = face_recognition.load_image_file(imagine)
  34.   imagine_codificata = face_recognition.face_encodings(imagine_incarcata)[0]
  35.   listaFete_codificateDB.append(imagine_codificata)                               # adaugam imaginile codificate la o lista
  36. print(listaFete_codificateDB[0])
  37.  
  38. #  liste unde se vor stoca informatii din fluxul video
  39. fetele_gasite = []
  40. fetele_codificate_gasite = []
  41. numele_fetelor_gasite = []
  42. process_this_frame = True
  43.  
  44. while True:
  45.     # preluam frame cu frame din video
  46.     ret, frame = video_capture.read()
  47.  
  48.     # relizam un redimensionare a capturii, pentru a face recunoasterea faciala mai rapida
  49.     small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
  50.  
  51.     # se converteste imaginea de la BGR, pe care OpenCV-ul o utilizeaza la RGB deoarece modulul face_recognition il utilizeaza
  52.     rgb_small_frame = small_frame[:, :, ::-1]
  53.  
  54.     # Procesam frame cu frame
  55.     if process_this_frame:
  56.         # Gasim fetele, pe care le codificam
  57.         fetele_gasite = face_recognition.face_locations(rgb_small_frame)
  58.         fetele_codificate_gasite = face_recognition.face_encodings(rgb_small_frame, fetele_gasite)
  59.  
  60.         numele_fetelor_gasite = []
  61.         for fata_codificata in fetele_codificate_gasite:
  62.             # verificam daca fetele din fluxul video se regasesc in baza de date
  63.             matches = face_recognition.compare_faces(listaFete_codificateDB, fata_codificata)
  64.             name = "Unknown" # presupunem ca nu
  65.             face_distances = face_recognition.face_distance(listaFete_codificateDB, fata_codificata)
  66.             best_match_index = np.argmin(face_distances)
  67.             if matches[best_match_index]: # in cazul in care se regaseste, adaugam numele in baza de date
  68.                 name = lista_nume_DB[best_match_index]
  69.             numele_fetelor_gasite.append(name)
  70.  
  71.     process_this_frame = not process_this_frame # incheiem prelucrarea frame-ului curent
  72.  
  73.  
  74.     # afisam rezultatul
  75.     for (top, right, bottom, left), name in zip(fetele_gasite, numele_fetelor_gasite):
  76.         top *= 4
  77.         right *= 4
  78.         bottom *= 4
  79.         left *= 4
  80.  
  81.         # se deseneaza un patrat in jurul fetei
  82.         cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
  83.  
  84.         # adaugam labe-ul cu numele patratului
  85.         cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
  86.         font = cv2.FONT_HERSHEY_DUPLEX
  87.         cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
  88.  
  89.     # afisam in fluxul video
  90.     cv2.imshow('Video', frame)
  91.  
  92.     # se apasa tasta 'q' pentru a iesi din program
  93.     if cv2.waitKey(1) & 0xFF == ord('q'):
  94.         break
  95.  
  96. # inchidem camera si fereastra
  97. video_capture.release()
  98. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement