Advertisement
Guest User

Untitled

a guest
Dec 31st, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. import sys
  2. import dlib
  3. import cv2
  4. import face_recognition
  5. import os
  6. import psycopg2
  7.  
  8. if len(sys.argv) < 2:
  9. print("Usage: face-find <image>")
  10. exit(1)
  11.  
  12. # Take the image file name from the command line
  13. file_name = sys.argv[1]
  14.  
  15. # Create a HOG face detector using the built-in dlib class
  16. face_detector = dlib.get_frontal_face_detector()
  17.  
  18. # Load the image
  19. image = cv2.imread(file_name)
  20. print(image)
  21.  
  22. # Run the HOG face detector on the image data
  23. detected_faces = face_detector(image, 1)
  24.  
  25. print("Found {} faces in the image file {}".format(len(detected_faces), file_name))
  26.  
  27. connection_db = psycopg2.connect("user='pr0n00gler' password='314159265' host='localhost' dbname='findface'")
  28. db=connection_db.cursor()
  29.  
  30. # Loop through each face we found in the image
  31. for i, face_rect in enumerate(detected_faces):
  32. # Detected faces are returned as an object with the coordinates
  33. # of the top, left, right and bottom edges
  34. print("- Face #{} found at Left: {} Top: {} Right: {} Bottom: {}".format(i, face_rect.left(), face_rect.top(),
  35. face_rect.right(), face_rect.bottom()))
  36. crop = image[face_rect.top():face_rect.bottom(), face_rect.left():face_rect.right()]
  37.  
  38. encodings = face_recognition.face_encodings(crop)
  39. if len(encodings) > 0:
  40. query = "SELECT user_id FROM face ORDER BY " + \
  41. "(CUBE(array[{}]) <-> vec_low) + (CUBE(array[{}]) <-> vec_high) ASC;".format(
  42. ','.join(str(s) for s in encodings[0][0:63]),
  43. ','.join(str(s) for s in encodings[0][64:127]),
  44. )
  45. print(query)
  46. db.execute(query)
  47. print("The number of parts: ", db.rowcount)
  48. row = db.fetchone()
  49.  
  50. while row is not None:
  51. print(row)
  52. row = db.fetchone()
  53.  
  54. db.close()
  55. else:
  56. print("No encodings")
  57.  
  58. if connection_db is not None:
  59. connection_db.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement