Skip to content
Advertisements

PYTHON OpenCV Eye Tracking

import cv2
import numpy as np

cap = cv2.VideoCapture('eye_recording.flv')

while True:
    ret,frame = cap.read()
    roi = frame[269: 795, 537: 1416]
    rows,cols,_ = roi.shape # width,height,color_pix
    gray_roi = cv2.cvtColor(roi,cv2.COLOR_BGR2GRAY)
    
    # yumuşatma
    gray_roi = cv2.GaussianBlur(gray_roi, (7, 7), 0)
    # GaussianBlur(kaynak,Gauss standart sapması,filtre katasyısı)
    
    _,threshold = cv2.threshold(gray_roi,3,255,cv2.THRESH_BINARY_INV) # return dark-white so 1-0
    # THRESH_BINARY: 1-0
    # THRESH_BINARY_INV: 0-1
    
    contours,_ = cv2.findContours(threshold,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
    for cnt in contours:
        (x,y,w,h) = cv2.boundingRect(cnt)
        # cv2.drawContours(roi,[cnt],-1,(0,0,255),3)
        cv2.rectangle(roi,(x,y),(x+w,y+h),(255,0,0),2)
        cv2.line(roi,(x+int(w/2),0),(x+int(w/2),rows),(0,255,0),2)
        cv2.line(roi,(0,y+int(h/2)),(cols,y+int(h/2)),(0,255,0),2)
        break
    
    cv2.imshow('roi',roi)
    cv2.imshow('gray_roi',gray_roi)
    cv2.imshow('threshold',threshold)
    key = cv2.waitKey(30)
    if key == 27:
        break

cv2.destroyAllWindows()

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: