forked from reikdas/Pidetect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect.py
71 lines (53 loc) · 1.6 KB
/
detect.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 10 19:13:37 2018
@author: pratyush
"""
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 = 18
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], 'face_classifier.xml')
face_cascade = cv2.CascadeClassifier(pathtoface)
#full body classifier
pathtobody = os.path.join(sys.path[0], 'full_body.xml')
body_cascade = cv2.CascadeClassifier(pathtobody)
#eyebrow classifier
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)
#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)
if faces is None or bodies is None:
GPIO.output(pin, GPIO.LOW)
#DISPLAY TO WINDOW
cv2.imshow("Faces", image)
key = cv2.waitKey(1)
rawCapture.truncate(0)
if key == ord("q"):
camera.close()
cv2.destroyAllWindows()
break