diff --git a/database/daniel/Jul-09-2020--16:27:19.jpg b/database/daniel/Jul-09-2020--16:27:19.jpg new file mode 100644 index 0000000..a04c594 Binary files /dev/null and b/database/daniel/Jul-09-2020--16:27:19.jpg differ diff --git a/database/daniel/Jul-09-2020--16:28:49.jpg b/database/daniel/Jul-09-2020--16:28:49.jpg new file mode 100644 index 0000000..cf13aaf Binary files /dev/null and b/database/daniel/Jul-09-2020--16:28:49.jpg differ diff --git a/database/daniel/Jul-09-2020--16:38:37.jpg b/database/daniel/Jul-09-2020--16:38:37.jpg new file mode 100644 index 0000000..bda898f Binary files /dev/null and b/database/daniel/Jul-09-2020--16:38:37.jpg differ diff --git a/database/daniel/Jul-09-2020--16:40:01.jpg b/database/daniel/Jul-09-2020--16:40:01.jpg new file mode 100644 index 0000000..749bc21 Binary files /dev/null and b/database/daniel/Jul-09-2020--16:40:01.jpg differ diff --git a/database/daniel/Jul-09-2020--16:51:02:592.jpg b/database/daniel/Jul-09-2020--16:51:02:592.jpg new file mode 100644 index 0000000..80a3d43 Binary files /dev/null and b/database/daniel/Jul-09-2020--16:51:02:592.jpg differ diff --git a/database/daniel/Jul-09-2020--16:53:27:505.jpg b/database/daniel/Jul-09-2020--16:53:27:505.jpg new file mode 100644 index 0000000..55e54e1 Binary files /dev/null and b/database/daniel/Jul-09-2020--16:53:27:505.jpg differ diff --git a/database/daniel/Jul-09-2020--16:54:05:154.jpg b/database/daniel/Jul-09-2020--16:54:05:154.jpg new file mode 100644 index 0000000..d72b88a Binary files /dev/null and b/database/daniel/Jul-09-2020--16:54:05:154.jpg differ diff --git a/database/daniel/Jul-09-2020--16:54:28:769.jpg b/database/daniel/Jul-09-2020--16:54:28:769.jpg new file mode 100644 index 0000000..3ef54a4 Binary files /dev/null and b/database/daniel/Jul-09-2020--16:54:28:769.jpg differ diff --git a/database/representations.pkl b/database/representations.pkl index 39c3582..1053586 100644 Binary files a/database/representations.pkl and b/database/representations.pkl differ diff --git a/register.py b/register.py index fa717d8..f9df981 100644 --- a/register.py +++ b/register.py @@ -5,11 +5,10 @@ from datetime import datetime import os -now = datetime.now() -this_time = now.strftime("%b-%d-%Y--%H:%M:%S") - - def register(database='./database', name='temp'): + now = datetime.now() + this_time = now.strftime("%b-%d-%Y--%H:%M:%S:%f")[:-3] + file_path = os.path.join(database,name) r = 0 R = 11 @@ -48,11 +47,13 @@ def register(database='./database', name='temp'): if isRegister: cv2.destroyWindow("Register Face") cv2.imshow("Register Success!", frame) + cv2.waitKey(3000) break else: cv2.imshow("Register Face", frame) - if cv2.waitKey(1) & 0xFF == ord('q'): + key = cv2.waitKey(1) + if key == ord('q'): break video.release() diff --git a/rekog.py b/rekog.py index f2b9534..7db2837 100644 --- a/rekog.py +++ b/rekog.py @@ -6,25 +6,16 @@ from model import get_input_shape import helper import time +from register import register -# n -> freeze frames for n seconds -# F -> camera fps -# f -> current frame -# R -> wait time -# r -> radius -F = 30 -n = F -f = 0 -R = 0 -r = 180 -isFreeze = False +FPS = 30 def gstreamer_pipeline( capture_width=640, capture_height=480, display_width=640, display_height=480, - framerate=F, + framerate=FPS, flip_method=0, ): return ( @@ -46,90 +37,108 @@ def gstreamer_pipeline( ) ) -# detector = MTCNN() -FR = FaceRecognition() +def rekog(): + # n -> freeze frames for n seconds + # f -> current frame + # R -> wait time + # r -> radius + n = 30 + f = 0 + R = 0 + r = 180 + isFreeze = False + + + # detector = MTCNN() + FR = FaceRecognition() -# Using webcam -video = cv2.VideoCapture(0) -# Using gstreamer -# video = cv2.VideoCapture(gstreamer_pipeline(), cv2.CAP_GSTREAMER) + # Using webcam + video = cv2.VideoCapture(0) + # Using gstreamer + # video = cv2.VideoCapture(gstreamer_pipeline(), cv2.CAP_GSTREAMER) -if not video.isOpened(): - raise Exception('Error opening the camera') + if not video.isOpened(): + raise Exception('Error opening the camera') -# Open the camera and get the camera size -ret, temp = video.read() + # Open the camera and get the camera size + ret, temp = video.read() -# convert image to grayscale image -gray_image = cv2.cvtColor(temp, cv2.COLOR_BGR2GRAY) -# convert the grayscale image to binary image -ret,thresh = cv2.threshold(gray_image,127,255,0) -# calculate moments of binary image -M = cv2.moments(thresh) -# calculate x,y coordinate of center -CX = int(M["m10"] / M["m00"]) -CY = int(M["m01"] / M["m00"]) + # convert image to grayscale image + gray_image = cv2.cvtColor(temp, cv2.COLOR_BGR2GRAY) + # convert the grayscale image to binary image + ret,thresh = cv2.threshold(gray_image,127,255,0) + # calculate moments of binary image + M = cv2.moments(thresh) + # calculate x,y coordinate of center + CX = int(M["m10"] / M["m00"]) + CY = int(M["m01"] / M["m00"]) -while True: - # Open camera for recognizing - ret, frame = video.read() - rekog_frame = frame[CY-r:CY+r, CX-r:CX+r] - - # Draw rekog circle - cv2.circle(frame, center=(CX,CY), radius=r, color=(243,144,29), thickness=3) + while True: + # Open camera for recognizing + ret, frame = video.read() + rekog_frame = frame[CY-r:CY+r, CX-r:CX+r] + + # Draw rekog circle + cv2.circle(frame, center=(CX,CY), radius=r, color=(243,144,29), thickness=3) - # When freeze don't analyze faces for 11 frames - if isFreeze == True and 0 <= R <= 10: - faces = [] - R += 1 - print(f"R = {R}") - else: - faces = helper.detectFacesLive(rekog_frame) - R = 0 - isFreeze = False + # When freeze don't analyze faces for 11 frames + if isFreeze == True and 0 <= R <= 10: + faces = [] + R += 1 + print(f"R = {R}") + else: + faces = helper.detectFacesLive(rekog_frame) + R = 0 + isFreeze = False - if len(faces) != 0: - x, y, w, h = faces[0] - - # Draw rectangle in face - cv2.rectangle(rekog_frame, - (x, y), - (x+w, y+h), - (0,155,255), - 2) - - # Predict - img = helper.detectFace(rekog_frame[y:y+h, x:x+w], get_input_shape(), stream=True) - if img.shape[1:3] == get_input_shape(): - pred, score = FR.predict(img) - - # Count until freeze - f += 1 - print(f"frame {f}") - - # Freeze to show the predicted face - if f == n or score != "": - isFreeze = True - f = 0 - freeze_img = rekog_frame.copy() - # Draw label class prediction - cv2.rectangle(freeze_img, (x, y+h + 65), (x+w, y+h), (0, 0, 0), cv2.FILLED) - if pred == "": - cv2.rectangle(freeze_img, (x, y), (x+w, y+h), (0,155,255), cv2.FILLED) - cv2.putText(freeze_img, "Not Employee!", (x + 3, y+h + 25), cv2.FONT_HERSHEY_DUPLEX, 0.8, (255, 255, 255), 1) - elif pred != "": - cv2.putText(freeze_img, f"{pred}", (x + 3, y+h + 25), cv2.FONT_HERSHEY_DUPLEX, 0.8, (255, 255, 255), 1) - cv2.putText(freeze_img, f"{score:.2f}", (x + 3, y+h + 55), cv2.FONT_HERSHEY_DUPLEX, 0.6, (255, 255, 255), 1) - else: - None - cv2.imshow(f"Hello {pred}", freeze_img) - cv2.waitKey(3000) - cv2.destroyWindow(f"Hello {pred}") + if len(faces) != 0: + x, y, w, h = faces[0] + + # Draw rectangle in face + cv2.rectangle(rekog_frame, + (x, y), + (x+w, y+h), + (0,155,255), + 2) + + # Predict + img = helper.detectFace(rekog_frame[y:y+h, x:x+w], get_input_shape(), stream=True) + if img.shape[1:3] == get_input_shape(): + pred, score = FR.predict(img) + + # Count until freeze + f += 1 + print(f"frame {f}") + + # Freeze to show the predicted face + if f == n or score != "": + isFreeze = True + f = 0 + freeze_img = rekog_frame.copy() + # Draw label class prediction + cv2.rectangle(freeze_img, (x, y+h + 65), (x+w, y+h), (0, 0, 0), cv2.FILLED) + if pred == "": + cv2.rectangle(freeze_img, (x, y), (x+w, y+h), (0,155,255), cv2.FILLED) + cv2.putText(freeze_img, "Not Employee!", (x + 3, y+h + 25), cv2.FONT_HERSHEY_DUPLEX, 0.8, (255, 255, 255), 1) + elif pred != "": + cv2.putText(freeze_img, f"{pred}", (x + 3, y+h + 25), cv2.FONT_HERSHEY_DUPLEX, 0.8, (255, 255, 255), 1) + cv2.putText(freeze_img, f"{score:.2f}", (x + 3, y+h + 55), cv2.FONT_HERSHEY_DUPLEX, 0.6, (255, 255, 255), 1) + else: + None + cv2.imshow(f"Hello {pred}", freeze_img) + cv2.waitKey(3000) + cv2.destroyWindow(f"Hello {pred}") - cv2.imshow('Hello Welcome to iNews Tower', frame) + cv2.imshow('Hello Welcome to iNews Tower', frame) - if cv2.waitKey(1) & 0xFF == ord('q'): - break + key = cv2.waitKey(1) + if key == ord('q'): + break + elif key == ord('r'): + video.release() + cv2.destroyAllWindows() + register(name='daniel') + rekog() -video.release() -cv2.destroyAllWindows() + video.release() + cv2.destroyAllWindows() diff --git a/run.py b/run.py new file mode 100644 index 0000000..2be60be --- /dev/null +++ b/run.py @@ -0,0 +1,3 @@ +from rekog import rekog + +rekog() \ No newline at end of file