pperle / gaze-tracking-pipeline

full camera-to-screen gaze tracking pipeline
72 stars 19 forks source link

No reaction after running #8

Open emrecolak55 opened 6 months ago

emrecolak55 commented 6 months ago

I am running the main.py as it's described. I've created the yaml file and downloaded the trained model. After running the command, there is no reaction. I am debugging to see the issue and it says : Exception has occurred: OSError

ahmed-rebai commented 5 months ago

hello how did you creat the ymal file i'm having a problem in detecting pictures for calibration

emrecolak55 commented 4 months ago

hello how did you creat the ymal file i'm having a problem in detecting pictures for calibration

Hi, I'd done the calibration using another project. I don't have any link for it right now but there are several calibration ways if you search on google.

emrecolak55 commented 4 months ago

Okay here is the code for calibration that I used; First you need to save images by holding a "calibration chessboard" into the images folder, then run this code. It'll give you the .yaml file.

import numpy as np
import cv2 as cv
import glob
import pickle

chessboardSize = (9,7) # Your inner chessboard corners ( If you have 6x6 chessboard, put (5,5) here )
frameSize = (640,480) # Your camera resolution

criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)

objp = np.zeros((chessboardSize[0] * chessboardSize[1], 3), np.float32)
objp[:,:2] = np.mgrid[0:chessboardSize[0],0:chessboardSize[1]].T.reshape(-1,2)

size_of_chessboard_squares_mm = 20
objp = objp * size_of_chessboard_squares_mm

objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.

images = glob.glob('cameraCalibration/images/*.png') # Your path to images folder

for image in images:

    img = cv.imread(image)
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

    ret, corners = cv.findChessboardCorners(gray, chessboardSize, None)

    if ret == True:

        objpoints.append(objp)
        corners2 = cv.cornerSubPix(gray, corners, (11,11), (-1,-1), criteria)
        imgpoints.append(corners)

        cv.drawChessboardCorners(img, chessboardSize, corners2, ret)
        cv.imshow('img', img)
        cv.waitKey(1000)

cv.destroyAllWindows()

ret, cameraMatrix, dist, rvecs, tvecs = cv.calibrateCamera(objpoints, imgpoints, frameSize, None, None)

pickle.dump((cameraMatrix, dist), open( "calibration.pkl", "wb" ))
pickle.dump(cameraMatrix, open( "cameraMatrix.pkl", "wb" ))
pickle.dump(dist, open( "dist.pkl", "wb" ))

img = cv.imread('cali5.png')
h,  w = img.shape[:2]
newCameraMatrix, roi = cv.getOptimalNewCameraMatrix(cameraMatrix, dist, (w,h), 1, (w,h))

dst = cv.undistort(img, cameraMatrix, dist, None, newCameraMatrix)

x, y, w, h = roi
dst = dst[y:y+h, x:x+w]
cv.imwrite('caliResult1.png', dst)

mapx, mapy = cv.initUndistortRectifyMap(cameraMatrix, dist, None, newCameraMatrix, (w,h), 5)
dst = cv.remap(img, mapx, mapy, cv.INTER_LINEAR)

x, y, w, h = roi
dst = dst[y:y+h, x:x+w]
cv.imwrite('caliResult2.png', dst)

mean_error = 0

for i in range(len(objpoints)):
    imgpoints2, _ = cv.projectPoints(objpoints[i], rvecs[i], tvecs[i], cameraMatrix, dist)
    error = cv.norm(imgpoints[i], imgpoints2, cv.NORM_L2)/len(imgpoints2)
    mean_error += error

print( "total error: {}".format(mean_error/len(objpoints)) )

EDIT: The code above generates .pkl file, you can use this for .yaml: import numpy as np import cv2 as cv import glob import yaml

chessboardSize = (9,7)
frameSize = (640,480)

# termination criteria
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)

# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((chessboardSize[0] * chessboardSize[1], 3), np.float32)
objp[:,:2] = np.mgrid[0:chessboardSize[0],0:chessboardSize[1]].T.reshape(-1,2)

size_of_chessboard_squares_mm = 20
objp = objp * size_of_chessboard_squares_mm

# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.

images = glob.glob('images/*.png')

for image in images:
    img = cv.imread(image)
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

    # Find the chess board corners
    ret, corners = cv.findChessboardCorners(gray, chessboardSize, None)

    # If found, add object points, image points (after refining them)
    if ret == True:
        objpoints.append(objp)
        corners2 = cv.cornerSubPix(gray, corners, (11,11), (-1,-1), criteria)
        imgpoints.append(corners)

cv.destroyAllWindows()

############## CALIBRATION #######################################################

ret, cameraMatrix, dist, rvecs, tvecs = cv.calibrateCamera(objpoints, imgpoints, frameSize, None, None)

# Save the camera calibration result as a YAML file
calibration_data = {
    'camera_matrix': cameraMatrix.tolist(),
    'dist_coeff': dist.tolist(),
    'rms': ret
}

with open("calibration.yaml", 'w') as file:
    yaml.dump(calibration_data, file, default_flow_style=False)

print("Calibration parameters saved to calibration.yaml")
ahmed-rebai commented 4 months ago

thank you <3

ahmed-rebai commented 3 months ago

so i was thinking of doing some changement for the original code does any one have a solution on where to train it since it takes decades to train on local XD