Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No reaction after running #8

Open
emrecolak55 opened this issue Dec 15, 2023 · 5 comments
Open

No reaction after running #8

emrecolak55 opened this issue Dec 15, 2023 · 5 comments

Comments

@emrecolak55
Copy link

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
Copy link

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

@emrecolak55
Copy link
Author

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
Copy link
Author

emrecolak55 commented Feb 25, 2024

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
Copy link

thank you <3

@ahmed-rebai
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants