-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
camera_reader.py
51 lines (41 loc) · 1.68 KB
/
camera_reader.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
# -*- coding:utf-8 -*-
import cv2
from boss_train import Model
from image_show import show_image
if __name__ == '__main__':
cap = cv2.VideoCapture(0)
cascade_path = "/usr/local/opt/opencv/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml"
model = Model()
model.load()
while True:
_, frame = cap.read()
# グレースケール変換
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# カスケード分類器の特徴量を取得する
cascade = cv2.CascadeClassifier(cascade_path)
# 物体認識(顔認識)の実行
facerect = cascade.detectMultiScale(frame_gray, scaleFactor=1.2, minNeighbors=3, minSize=(10, 10))
#facerect = cascade.detectMultiScale(frame_gray, scaleFactor=1.01, minNeighbors=3, minSize=(3, 3))
if len(facerect) > 0:
print('face detected')
color = (255, 255, 255) # 白
for rect in facerect:
# 検出した顔を囲む矩形の作成
#cv2.rectangle(frame, tuple(rect[0:2]), tuple(rect[0:2] + rect[2:4]), color, thickness=2)
x, y = rect[0:2]
width, height = rect[2:4]
image = frame[y - 10: y + height, x: x + width]
result = model.predict(image)
if result == 0: # boss
print('Boss is approaching')
show_image()
else:
print('Not boss')
#10msecキー入力待ち
k = cv2.waitKey(100)
#Escキーを押されたら終了
if k == 27:
break
#キャプチャを終了
cap.release()
cv2.destroyAllWindows()