Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

face_recognition_svm.py 2.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  1. # Train multiple images per person
  2. # Find and recognize faces in an image using a SVC with scikit-learn
  3. """
  4. Structure:
  5. <test_image>.jpg
  6. <train_dir>/
  7. <person_1>/
  8. <person_1_face-1>.jpg
  9. <person_1_face-2>.jpg
  10. .
  11. .
  12. <person_1_face-n>.jpg
  13. <person_2>/
  14. <person_2_face-1>.jpg
  15. <person_2_face-2>.jpg
  16. .
  17. .
  18. <person_2_face-n>.jpg
  19. .
  20. .
  21. <person_n>/
  22. <person_n_face-1>.jpg
  23. <person_n_face-2>.jpg
  24. .
  25. .
  26. <person_n_face-n>.jpg
  27. """
  28. import face_recognition
  29. from sklearn import svm
  30. import os
  31. # Training the SVC classifier
  32. # The training data would be all the face encodings from all the known images and the labels are their names
  33. encodings = []
  34. names = []
  35. # Training directory
  36. train_dir = os.listdir('/train_dir/')
  37. # Loop through each person in the training directory
  38. for person in train_dir:
  39. pix = os.listdir("/train_dir/" + person)
  40. # Loop through each training image for the current person
  41. for person_img in pix:
  42. # Get the face encodings for the face in each image file
  43. face = face_recognition.load_image_file("/train_dir/" + person + "/" + person_img)
  44. face_bounding_boxes = face_recognition.face_locations(face)
  45. #If training image contains exactly one face
  46. if len(face_bounding_boxes) == 1:
  47. face_enc = face_recognition.face_encodings(face)[0]
  48. # Add face encoding for current image with corresponding label (name) to the training data
  49. encodings.append(face_enc)
  50. names.append(person)
  51. else:
  52. print(person + "/" + person_img + " was skipped and can't be used for training")
  53. # Create and train the SVC classifier
  54. clf = svm.SVC(gamma='scale')
  55. clf.fit(encodings,names)
  56. # Load the test image with unknown faces into a numpy array
  57. test_image = face_recognition.load_image_file('test_image.jpg')
  58. # Find all the faces in the test image using the default HOG-based model
  59. face_locations = face_recognition.face_locations(test_image)
  60. no = len(face_locations)
  61. print("Number of faces detected: ", no)
  62. # Predict all the faces in the test image using the trained classifier
  63. print("Found:")
  64. for i in range(no):
  65. test_image_enc = face_recognition.face_encodings(test_image)[i]
  66. name = clf.predict([test_image_enc])
  67. print(*name)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...