Skip to content

Commit

Permalink
feat: Combine rekog and register into one run, so the system can swit…
Browse files Browse the repository at this point in the history
…ch easily between recognizing and registering without exit the program
  • Loading branch information
danieldanuega committed Jul 9, 2020
1 parent 22b7912 commit c0f85e2
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 94 deletions.
Binary file added database/daniel/Jul-09-2020--16:27:19.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/daniel/Jul-09-2020--16:28:49.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/daniel/Jul-09-2020--16:38:37.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/daniel/Jul-09-2020--16:40:01.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/daniel/Jul-09-2020--16:51:02:592.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/daniel/Jul-09-2020--16:53:27:505.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/daniel/Jul-09-2020--16:54:05:154.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/daniel/Jul-09-2020--16:54:28:769.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified database/representations.pkl
Binary file not shown.
11 changes: 6 additions & 5 deletions register.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
187 changes: 98 additions & 89 deletions rekog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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()
3 changes: 3 additions & 0 deletions run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from rekog import rekog

rekog()

0 comments on commit c0f85e2

Please sign in to comment.