-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
74 lines (62 loc) · 2.33 KB
/
main.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
import cv2
import compare
import os
import time
import winsound
if __name__ == '__main__':
# 定数定義
ESC_KEY = 27 # Escキー
INTERVAL= 33 # 待ち時間
FRAME_RATE = 30 # fps
ORG_WINDOW_NAME = "org"
GAUSSIAN_WINDOW_NAME = "gaussian"
DEVICE_ID = 0
# 分類器の指定
cascade_file = "haarcascade_frontalface_alt2.xml"
cascade = cv2.CascadeClassifier(cascade_file)
# カメラ映像取得
cap = cv2.VideoCapture(DEVICE_ID)
# 初期フレームの読込
end_flag, c_frame = cap.read()
height, width, channels = c_frame.shape
# ウィンドウの準備
cv2.namedWindow(ORG_WINDOW_NAME)
cv2.namedWindow(GAUSSIAN_WINDOW_NAME)
# 変換処理ループ
while end_flag == True:
# 画像の取得と顔の検出
img = c_frame
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_list = cascade.detectMultiScale(img_gray, minSize=(100, 100))
# 検出した顔に印を付ける
for (x, y, w, h) in face_list:
color = (0, 0, 225)
pen_w = 3
cv2.rectangle(img_gray, (x, y), (x+w, y+h), color, thickness = pen_w)
try:
sampleimg = img[y-10:y+h+10, x-10:x+w+10]
cv2.imwrite("out.jpg", sampleimg)
got=compare.compare("out.jpg")
print ("類似度:"+ str(got))
time.sleep(0.0001)
if got<=180:
print ("許可された人物ではありません")
winsound.Beep(2000,250)
except cv2.error:
print("err:CV2.error=画像が読み込めません。対象が視程外に移動したか高速で移動中です")
continue
except IndexError:
print("err:IndexError=タプルの値が不正です。対象は視程の境界上に存在しますが、許可された人物か判定できません")
continue
# フレーム表示
cv2.imshow(ORG_WINDOW_NAME, c_frame)
cv2.imshow(GAUSSIAN_WINDOW_NAME, img_gray)
# Escキーで終了
key = cv2.waitKey(INTERVAL)
if key == ESC_KEY:
break
# 次のフレーム読み込み
end_flag, c_frame = cap.read()
# 終了処理
cv2.destroyAllWindows()
cap.release()