-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.py
59 lines (45 loc) · 1.46 KB
/
register.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
import cv2
import helper
from model import get_input_shape
from PIL import Image
from datetime import datetime
import os
def register(database="./database", name="temp"):
now = datetime.now()
this_time = now.strftime("%b-%d-%Y--%H:%M:%S:%f")[:-3]
minWidthFace = 200
file_path = os.path.join(database, name)
r = 0
R = 11
isRegister = False
video = cv2.VideoCapture(0)
if not video.isOpened():
raise Exception("Error opening the camera")
while True:
ret, frame = video.read()
faces = helper.detectFacesLive(frame)
if len(faces) != 0 and r != R and faces[0][2] >= minWidthFace:
x, y, w, h = faces[0]
# Draw rectangle in face
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 155, 255), 2)
r += 1
elif len(faces) != 0 and r == R and faces[0][2] >= minWidthFace:
r = 0
if os.path.exists(file_path) == False:
os.makedirs(file_path)
cv2.imwrite(os.path.join(file_path, this_time) + ".jpg", frame)
isRegister = True
else:
r = 0
if isRegister:
cv2.destroyWindow("Register Face")
cv2.imshow("Register Success!", frame)
cv2.waitKey(3000)
break
else:
cv2.imshow("Register Face", frame)
key = cv2.waitKey(1)
if key == ord("q"):
break
video.release()
cv2.destroyAllWindows()