-
Notifications
You must be signed in to change notification settings - Fork 3
/
facedata.py
72 lines (49 loc) · 1.63 KB
/
facedata.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
61
62
63
64
65
66
67
68
69
70
71
72
from picamera.array import PiRGBArray
from picamera import PiCamera
import cv2
import time
import os
import sys
import RPi.GPIO as GPIO
#Setup Pins
pin = 14
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT, initial=0)
camera = PiCamera()
camera.resolution = (320, 240)
camera.framerate = 30
rawCapture = PiRGBArray(camera, size=(320, 240))
display_window = cv2.namedWindow("Faces")
#face classifier
pathtoface = os.path.join(sys.path[0], 'haarcascade_frontalface_default.xml')
face_cascade = cv2.CascadeClassifier(pathtoface)
face_id = 2
count = 0
#full body classifier
pathtobody = os.path.join(sys.path[0], 'full_body.xml')
body_cascade = cv2.CascadeClassifier(pathtobody)
time.sleep(1)
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
image = frame.array
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
#FACE DETECTION STUFF
faces = face_cascade.detectMultiScale(gray, 1.1, 5)
for (x,y,w,h) in faces:
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
GPIO.output(pin, GPIO.HIGH)
count += 1
# Save the captured image into the datasets folder
cv2.imwrite("d/dataSet/s." + str(face_id) + '.' + str(count) + ".jpg", gray[y:y+h,x:x+w])
#BODY DETECTION STUFF
bodies = body_cascade.detectMultiScale(gray, 1.1, 5)
for (x,y,w,h) in bodies:
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
GPIO.output(pin, GPIO.HIGH)
#DISPLAY TO WINDOW
cv2.imshow("Faces", image)
key = cv2.waitKey(1)
rawCapture.truncate(0)
if count>19 or key == ord("q"):
camera.close()
cv2.destroyAllWindows()
break