-
Notifications
You must be signed in to change notification settings - Fork 1
/
Collecting_Data.py
60 lines (45 loc) · 1.67 KB
/
Collecting_Data.py
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
import cv2
import numpy as np
import requests
# Load HAAR face classifier
face_classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Load functions
def face_extractor(img):
# Function detects faces and returns the cropped face
# If no face detected, it returns the input image
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray, 1.3, 5)
if faces is ():
return None
# Crop all faces found
for (x,y,w,h) in faces:
cropped_face = img[y:y+h, x:x+w]
return cropped_face
# Initialize Webcam
url = "https://192.168.43.1:8080/shot.jpg"
count = 0
# Collect 100 samples of your face from webcam input
while True:
data = requests.get(url)
image = data.content
imageArray = bytearray(image)
imageID = np.array(imageArray)
img = cv2.imdecode(imageID, -1)
face_coord = face_classifier.detectMultiScale(img)
if face_extractor(face_coord) is not None:
count += 1
face = cv2.resize(face_extractor(face_coord), (200, 200))
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
# Save file in specified directory with unique name
file_name_path = '/root/new/face' + str(count) + '.jpg'
cv2.imwrite(file_name_path, face)
# Put count on images and display live count
cv2.putText(face, str(count), (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
cv2.imshow('Face Cropper', face)
else:
print("Face not found")
pass
if cv2.waitKey(1) == 13 or count == 100: #13 is the Enter Key
break
cv2.destroyAllWindows()
print("Collecting Samples Complete")