-
Notifications
You must be signed in to change notification settings - Fork 1
/
DetectFace_cascade_v1.0.py
79 lines (63 loc) · 2.17 KB
/
DetectFace_cascade_v1.0.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
73
74
75
76
77
78
79
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# import
import cv2
from datetime import datetime
from httprequest import HttpRequest, StatusReq
import hue
# read data file
face_cascade_path = '/usr/share/opencv/haarcascades/'\
'haarcascade_frontalface_default.xml'
eye_cascade_path = '/usr/share/opencv/haarcascades/'\
'haarcascade_eye.xml'
face_cascade = cv2.CascadeClassifier(face_cascade_path)
eye_cascade = cv2.CascadeClassifier(eye_cascade_path)
# open VideoCapture
cap = cv2.VideoCapture(0)
# definition
path_w = '/home/pi/Desktop/cloud/log/log.json'
room_Name = '101'
def main():
req = HttpRequest()
req.start()
hthread = hue.HueThread(ip="192.168.10.2")
hthread.start()
while True:
# get image
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# face detect
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)
human_num = len(faces)
# if face detect
if(human_num > 0):
hthread.changeState(hue.MeetingStart())
#time = datetime.now().strftime("%Y/%m/%d %H:%M:%S")
time = datetime.now().isoformat()
#time.microsecond = 0
#time = time.isoformat()
print (time , ' human_num : ', human_num)
req.add(StatusReq(room=room_Name, timestamp=time, occupied = human_num))
else:
hthread.changeState(hue.MeetingEnd())
# draw rect
for x, y, w, h in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
#face = img[y: y + h, x: x + w]
#face_gray = gray[y: y + h, x: x + w]
#eyes = eye_cascade.detectMultiScale(face_gray)
#for (ex, ey, ew, eh) in eyes:
# cv2.rectangle(face, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
# show img
cv2.imshow('video image', img)
# wait key
key = cv2.waitKey(10)
# quit
if key == 27: # ESCキーで終了
break
cap.release()
cv2.destroyAllWindows()
req.stop()
req.join()
if __name__ == '__main__':
main()