diff --git a/examples/blinkdet/blinkdet.go b/examples/blinkdet/blinkdet.go index 53244fd..ee90aab 100644 --- a/examples/blinkdet/blinkdet.go +++ b/examples/blinkdet/blinkdet.go @@ -92,7 +92,7 @@ func clusterDetection(pixels []uint8, rows, cols int) []pigo.Detection { Dim: cols, } cParams := pigo.CascadeParams{ - MinSize: 200, + MinSize: 260, MaxSize: 640, ShiftFactor: 0.1, ScaleFactor: 1.1, diff --git a/examples/blinkdet/blinkdet.py b/examples/blinkdet/blinkdet.py index 4532214..4a3f8c2 100644 --- a/examples/blinkdet/blinkdet.py +++ b/examples/blinkdet/blinkdet.py @@ -12,6 +12,8 @@ os.system('rm blinkdet.so') MAX_NDETS = 2024 ARRAY_DIM = 6 +# Number of consecutive frames the eye must be below the threshold +EYE_CLOSED_CONSEC_FRAMES = 8 # define class GoPixelSlice to map to: # C type struct { void *data; GoInt len; GoInt cap; } @@ -20,7 +22,7 @@ class GoPixelSlice(Structure): ("pixels", POINTER(c_ubyte)), ("len", c_longlong), ("cap", c_longlong), ] -# Obtain the camera pixels and transfer them to Go trough Ctypes. +# Obtain the camera pixels and transfer them to Go trough C types. def process_frame(pixs): dets = np.zeros(ARRAY_DIM * MAX_NDETS, dtype=np.float32) pixels = cast((c_ubyte * len(pixs))(*pixs), POINTER(c_ubyte)) @@ -58,6 +60,8 @@ time.sleep(0.4) showPupil = True showEyes = False +counter = 0 +posFaceY = 0 while(True): ret, frame = cap.read() @@ -72,6 +76,7 @@ while(True): # We know that the detected faces are taking place in the first positions of the multidimensional array. for det in dets: if det[4] == 1: # 1 == face; 0 == pupil + posFaceY = det[1] cv2.rectangle(frame, (int(det[1])-int(det[2]/2), int(det[0])-int(det[2]/2)), (int(det[1])+int(det[2]/2), int(det[0])+int(det[2]/2)), @@ -81,27 +86,36 @@ while(True): if showPupil: x1, x2 = int(det[0])-int(det[2]*1.2), int(det[0])+int(det[2]*1.2) y1, y2 = int(det[1])-int(det[2]*1.2), int(det[1])+int(det[2]*1.2) - subimg = frame[x1:x2, y1:y2] - + subimg = frame[x1:x2, y1:y2] + if subimg is not None: gray = cv2.cvtColor(subimg, cv2.COLOR_BGR2GRAY) - img_blur = cv2.medianBlur(gray, 3) + img_blur = cv2.medianBlur(gray, 1) if img_blur is not None: max_radius = int(det[2]*0.45) circles = cv2.HoughCircles(img_blur, cv2.HOUGH_GRADIENT, 1, int(det[2]*0.3), - param1=60, param2=18, minRadius=4, maxRadius=max_radius) + param1=60, param2=21, minRadius=4, maxRadius=max_radius) if circles is not None: circles = np.uint16(np.around(circles)) for i in circles[0, :]: - if i[2] < max_radius and i[2] > 0: - # Draw outer circle - print(i) - cv2.circle(frame, (int(det[1]), int(det[0])), i[2], (0, 255, 0), 2) + if i[2] < max_radius and i[2] > 0: + # Draw outer circle + cv2.circle(frame, (int(det[1]), int(det[0])), i[2], (0, 255, 0), 2) # Draw inner circle cv2.circle(frame, (int(det[1]), int(det[0])), 2, (255, 0, 255), 3) - + else: + counter += 1 + if counter > EYE_CLOSED_CONSEC_FRAMES: + if posFaceY < y1: + cv2.putText(frame, "Left blink!", (10, 30), + cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) + else: + cv2.putText(frame, "Right blink!", (frame.shape[1]-150, 30), + cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) + counter = 0 + cv2.circle(frame, (int(det[1]), int(det[0])), 4, (0, 0, 255), -1, 8, 0) if showEyes: