-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfaces.py
More file actions
55 lines (44 loc) · 1.55 KB
/
faces.py
File metadata and controls
55 lines (44 loc) · 1.55 KB
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
import numpy as np
import cv2
import os
#Lest use the camera
cap = cv2.VideoCapture(0)
#import the classifier data from cv2
face_cascade = cv2.CascadeClassifier('cascades/haarcascade_frontalface_alt2.xml')
eye_cascade = cv2.CascadeClassifier('cascades/haarcascade_eye.xml')
smile_cascade = cv2.CascadeClassifier('cascades/haarcascade_smile.xml')
counter = 0
print("What's your ID number? ")
name = input()
directory = "train_img/"+name
if not os.path.exists(directory):
os.makedirs(directory)
#start getting frames from the camera
while(True):
#capture the frame
ret, frame = cap.read()
counter = counter + 1
#convert it to a grayscale frame to work with it
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5)
for (x,y,w,h) in faces:
print(x,y,w,h)
#region of interest = roi (face detected)
#save the roi to a file in grayscale
roi_gray = gray[y:y+h, x:x+w]
#save the roi to a file in color
roi_color = frame[y:y+h, x:x+w]
img_item = name + str(counter) + ".jpg"
cv2.imwrite("train_img/" + name + "/" + img_item, frame)
color = (255,0,0)
stroke = 2
end_cord_x = x + w
end_cord_y = y + h
cv2.rectangle(frame, (x, y), (end_cord_x, end_cord_y), color, stroke)
#show the frame captured
cv2.imshow('frame', frame)
#stop the frame capture with q pressed
if cv2.waitKey(20) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()