Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Capture images on left and right eye with resize pixel #6

Open
wants to merge 1 commit into
base: tasks/PBL5-36
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 29 additions & 43 deletions drowsiness-detector/detector/blink_detector.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import cv2
import dlib
import numpy as np
from keras.models import load_model
from scipy.spatial import distance as dist
from imutils import face_utils
import matplotlib.pyplot as plt
import os
import time

predictor = dlib.shape_predictor("./shape_predictor_68_face_landmarks.dat")
face_cascade = cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')
Expand All @@ -16,7 +14,7 @@ def detect(img, cascade = face_cascade , minimumFeatureSize=(20, 20)):
rects = cascade.detectMultiScale(img, scaleFactor=1.3, minNeighbors=1, minSize=minimumFeatureSize)

# if it doesn't return rectangle return array
# with zero lenght
# with zero length
if len(rects) == 0:
return []

Expand Down Expand Up @@ -68,7 +66,7 @@ def cropEyes(frame):
# compute the width of the eye
lw = (leftEye[3][0] - leftEye[0][0])

# we want the image for the cnn to be (26,34)
# we want the image for the CNN to be (26,34)
# so we add the half of the difference at x and y
# axis from the width at height respectively left-right
# and up-down
Expand Down Expand Up @@ -113,8 +111,21 @@ def cnnPreprocess(img):
img = np.expand_dims(img, axis=0)
return img

def save_images(left_eye_image, right_eye_image, folder):
if not os.path.exists(folder):
os.makedirs(folder)

# Create unique filenames
timestamp = str(int(time.time()))
left_filename = os.path.join(folder, f"left_eye_{timestamp}.jpg")
right_filename = os.path.join(folder, f"right_eye_{timestamp}.jpg")

# Save images
cv2.imwrite(left_filename, left_eye_image)
cv2.imwrite(right_filename, right_eye_image)

def main():
# open the camera,load the cnn model
# open the camera
camera = cv2.VideoCapture(0)
# model = load_model('/Users/phuc1403/projects/simple-blink-detector/detector/blinkModel.hdf5')

Expand All @@ -124,15 +135,14 @@ def main():
close_counter = blinks = mem_counter= 0
state = ''
while True:

ret, frame = camera.read()

# detect eyes
eyes = cropEyes(frame)
if eyes is None:
continue
else:
left_eye,right_eye, left_eye_rect, right_eye_rect = eyes
left_eye,right_eye, _, _ = eyes

# left_eye_rect[0] -= 20 # Giảm tọa độ x bên trái
# left_eye_rect[1] -= 20 # Giảm tọa độ y bên trên
Expand All @@ -149,45 +159,21 @@ def main():
# Vẽ hộp giới hạn xung quanh mắt phải trên hình ảnh gốc
cv2.rectangle(frame, (right_eye_rect[0], right_eye_rect[1]), (right_eye_rect[2], right_eye_rect[3]), (0, 255, 0), 2)


# average the predictions of the two eyes
# prediction = (model.predict(cnnPreprocess(left_eye)) + model.predict(cnnPreprocess(right_eye)))/2.0

# # blinks
# # if the eyes are open reset the counter for close eyes
# if prediction > 0.5 :
# state = 'open'
# close_counter = 0
# else:
# state = 'close'
# close_counter += 1

# # if the eyes are open and previousle were closed
# # for sufficient number of frames then increcement
# # the total blinks
# if state == 'open' and mem_counter > 1:
# blinks += 1
# # keep the counter for the next loop
# mem_counter = close_counter

# draw the total number of blinks on the frame along with
# the state for the frame
cv2.putText(frame, "Blinks: {}".format(blinks), (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
cv2.putText(frame, "State: {}".format(state), (300, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)

# show the frame
cv2.imshow('blinks counter', frame)
key = cv2.waitKey(1) & 0xFF
key = cv2.waitKey(1)

# if the `q` key was pressed, break from the loop
if key == ord('q'):
break
# do a little clean up

# Delay for 1 second
if close_counter == 10: # 10 frames = approximately 1 second at 10 fps
save_images(left_eye, right_eye, "eye_images")
close_counter = 0 # Reset the counter after saving images

close_counter += 1

cv2.destroyAllWindows()
del(camera)


if __name__ == '__main__':
main()
main()