├── .gitattributes ├── README.md └── Gaze_Estimation.py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python Gaze estimation (Eye tracking) using single low-cost web-cam and visualization of data 2 | 3 | The widespread use of the Internet, where users look on the screen and where they pay more attention has begun to gain importance. 4 | In the project, it has been tried to predict where the user is looking on the screen in the light of this issue. 5 | 6 | Using the Python application, the pupil was determined by the hough circle method and it was tried to predict where the user was looking according to the position of the pupil. 7 | Finally, these data were plotted on the screen where the user was looking. 8 | 9 | 10 | **Dependencies** 11 | 12 | - python=3.8 13 | - numpy 14 | - pygame 15 | - matplotlib 16 | - opencv 17 | 18 | ## Enjoy with Eye Tracking !! 19 | Open a terminal ./Python-Gaze-estimation--Eye-tracking--using-single-low-cost-web-am-and-visualization-of-data in folder. 20 | Now you are ready to test Eye Tracking by the following command: 21 | 22 | ```` 23 | Gaze_Estimation.py 24 | ```` 25 | -------------------------------------------------------------------------------- /Gaze_Estimation.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 3 | import autopy 4 | from matplotlib import pyplot as plt 5 | import pygame 6 | 7 | ESCAPE_KEY = 27 8 | 9 | k=1 10 | sumx=0 11 | sumy=0 12 | orn=7 13 | 14 | cap = cv2.VideoCapture(0) 15 | 16 | screen_resolution=(1280,720) 17 | video_resolution=(1280,720) 18 | 19 | 20 | pygame.init() 21 | screen =pygame.display.set_mode((600,480)) 22 | 23 | screen_resolution = autopy.screen.size() 24 | 25 | eye_x_positions = list() 26 | eye_y_positions = list() 27 | 28 | while 1: 29 | success, image = cap.read() 30 | image=cv2.flip(image,1) 31 | roi = image[150:250 , 230:330] 32 | resized1 = cv2.resize(roi, (200,200), interpolation = cv2.INTER_AREA) 33 | 34 | cv2.circle(resized1, (160, 127), 2, (155, 155, 255), 4) 35 | cv2.circle(resized1, (50, 127), 2, (155, 155, 255), 4) 36 | 37 | cv2.imshow("Parça",resized1) 38 | 39 | resized1=resized1[80:170 , 50:160] 40 | resized = cv2.resize(resized1, (440,360), interpolation = cv2.INTER_AREA) 41 | rows,cols,_ = resized1.shape 42 | gray1 = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY) 43 | eye_blur = cv2.bilateralFilter(gray1, 10, 195,195) 44 | cv2.imshow("eye_blur",eye_blur) 45 | img_blur = cv2.Canny(eye_blur,10,30) 46 | 47 | cv2.imshow("img_blur",img_blur) 48 | #img_blur = cv2.Canny(eye_blur,10,51) 49 | #eye_blur = cv2.bilateralFilter(gray1, 10, 80,95) 50 | #img_blur = cv2.Canny(eye_blur,10,35) 51 | 52 | 53 | 54 | cv2.imshow('Canny', img_blur) 55 | circles = cv2.HoughCircles(img_blur, cv2.HOUGH_GRADIENT, 0.1, 400, param1=200, param2=10, minRadius=76, maxRadius=84) 56 | if circles is not None: 57 | circles = np.uint16(np.around(circles)) 58 | for i in circles[0, :]: 59 | cv2.circle(resized, (i[0], i[1]), i[2], (0, 255, 0), 2) 60 | cv2.circle(resized, (i[0], i[1]), 2, (0, 0, 255), 5) 61 | print(i[0],i[1]) 62 | #pygame.draw.circle(screen, (0,0,255), ((i[0]-64)*17, (i[1]-60)*40), 5) 63 | #cv2.putText(frame,"Left eye x location = " + str(i[0]) , (20,30), cv2.FONT_HERSHEY_SIMPLEX,0.5, (155, 255, 0), 2) 64 | #cv2.putText(frame,"Left eye y location = " + str(i[1]) , (20,50), cv2.FONT_HERSHEY_SIMPLEX,0.5, (155, 255, 0), 2) 65 | if k==orn: 66 | k=1 67 | sumx=sumx/orn 68 | sumx=round(sumx,2) 69 | sumy=sumy/orn 70 | sumy=round(sumy,2) 71 | print("_______\n",sumx,sumy,"\n_______") 72 | eye_x_p=round((sumx-145),2) 73 | #eye_y_p=(sumy-62) 74 | eye_y_p=round((sumy-145),2) 75 | pygame.draw.circle(screen, (0,0,255), (eye_x_p*4, eye_y_p*9), 5) 76 | 77 | 78 | 79 | eye_x_positions.append(eye_x_p) 80 | eye_y_positions.append(eye_y_p) 81 | 82 | 83 | elif k==0: 84 | pygame.display.update() 85 | screen.fill((0,0,0)) 86 | sumx=0 87 | sumy=0 88 | k=k+1 89 | 90 | elif k==1: 91 | pygame.display.update() 92 | screen.fill((0,0,0)) 93 | sumx=sumx+i[0] 94 | sumy=sumy+i[1] 95 | k=k+1 96 | 97 | else: 98 | sumx=sumx+i[0] 99 | sumy=sumy+i[1] 100 | k=k+1 101 | 102 | cv2.putText(image, str(i[0]) , (200,30), cv2.FONT_HERSHEY_SIMPLEX,0.5, (155, 255, 0), 2) 103 | cv2.putText(image, str(i[1]) , (200,50), cv2.FONT_HERSHEY_SIMPLEX,0.5, (155, 255, 0), 2) 104 | 105 | 106 | cv2.putText(image,"Left eye x location = " , (20,30), cv2.FONT_HERSHEY_SIMPLEX,0.5, (155, 255, 0), 2) 107 | cv2.putText(image,"Left eye y location = " , (20,50), cv2.FONT_HERSHEY_SIMPLEX,0.5, (155, 255, 0), 2) 108 | cv2.imshow("Eye", resized) 109 | #cv2.imshow("Roi", resized1) 110 | 111 | cv2.imshow("frame", image) 112 | key_pressed = cv2.waitKey(1) & 0xff 113 | if key_pressed == ESCAPE_KEY: 114 | break 115 | 116 | 117 | data_all = list(zip(eye_x_positions,eye_y_positions )) 118 | print(data_all) 119 | plt.scatter(eye_x_positions, eye_y_positions ,color="blue") 120 | plt.title("Eye position") 121 | plt.xlabel("X position") 122 | plt.ylabel("Y position") 123 | plt.axis([0, 150, 55, 0]) 124 | plt.show() 125 | 126 | 127 | ''' 128 | x_axis_labels = [0,10,20,30,40,50,60,70,80,90,100] 129 | y_axis_labels = [0,2.5,5,7.5,10,12.5,15,17.5,20] 130 | sns.heatmap(data_all, xticklabels=x_axis_labels, yticklabels=y_axis_labels, cbar=False) 131 | plt.xlabel("X position") 132 | plt.ylabel("Y position") 133 | plt.show() 134 | 135 | vid.release() 136 | 137 | ''' 138 | 139 | pygame.display.quit() 140 | cap.release() 141 | cv2.destroyAllWindows() --------------------------------------------------------------------------------