Detect left right blink

This commit is contained in:
esimov
2019-09-09 12:56:02 +03:00
parent b292ff777e
commit cbf5c8084f
2 changed files with 25 additions and 11 deletions

View File

@@ -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,

View File

@@ -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)),
@@ -85,22 +90,31 @@ while(True):
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)
# 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)