Skip to content

Instantly share code, notes, and snippets.

@AdroitAnandAI
Last active September 20, 2020 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdroitAnandAI/216370d66da0ac94cc3ce247e7bf32fe to your computer and use it in GitHub Desktop.
Save AdroitAnandAI/216370d66da0ac94cc3ce247e7bf32fe to your computer and use it in GitHub Desktop.
Detect Wink with Peak
# To detect wink from stream of eye images and print the sequence of Event trigger
while(runLoop):
for i in range (1000):
# read the image
image = cv2.imread("eyeImages_day/eye"+str(i) + ".jpg", 1)
# Convert to gray scale as histogram works well on 256 values.
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# calculate frequency of pixels in range 0-255
histg = cv2.calcHist([gray],[0],None,[256],[0,256])
# hack to know whether eye is closed or not.
# more spread of pixels in a histogram signifies an opened eye
activePixels = np.count_nonzero(histg)
pixelCount.append(activePixels)
# check for peak only if at least 50 frames are processed.
if len(pixelCount) > 50:
diff = np.diff(pixelCount[-50:])
peaks = peakutils.peak.indexes(np.array(diff), thres=0.8, min_dist=2)
x = np.array([i * -1 for i in diff])
peaksReflected = peakutils.peak.indexes(np.array(x), thres=0.8, min_dist=2)
# if peak is there on upright and reflected signal then the closed eyes are open soon
# i.e. it denotes a blink and not a gesture. But if peak is found only on the reflected
# signal then eyes are closed for long time to indicate gesture.
if (peaksReflected.size > 0 and x[peaksReflected[0]] > 0 and peaks.size == 0):
print('Event triggered at ' + str(i) + '...')
pixelCount.clear()
# Display the resulting frame
cv2.imshow('frame',image)
if cv2.waitKey(10) & 0xFF == ord('q'):
runLoop = False
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment