-1

i have write code to calibrate camera in my calibrate.py file,but when i run that file it shows error like, ImportError: cannot import name splitfn ..so how to solve this?...i have run this file using this command:

sudo python calibrate.py image4.jpg calibration.yaml --debug-dir out

so is this right command to execute this script?if any changes then plz suggest me.. my captured image for calibration(which i have gave as input) and output(RMS,camera matrix,distortion coefficient)is stored in calibration.yaml file?i need to make this file or it is automatically created?

snehal desai
  • 119
  • 5
  • 23

3 Answers3

1

Looks like you did not get the rest of the code repository. This line cannot find common.py which is where the splitfn function is defined:

from common import splitfn

You should get all files in Python directory of the repository that this code came from or at least getting common.py should resolve the example error you provided.

https://github.com/Itseez/opencv/tree/master/samples/python

Also, the OpenCv example code documents how to call the code from within it so don't think you will need the .yaml parameter in your call.

usage: calibrate.py [--debug ] [--square_size] []

[--debug ] = path where you want the output image to be written to - defaults to --debug: ./output/ -- if you want image to be written to same path that has calibrate.py you could try --debug ./

Not sure exactly what --square_size does but it defaults to 1 Last input is path or name of image if it is in same directory as the calibrate script - if image4.jpg is your image and you want to write to current path using defaults it would be I think:

sudo python calibrate.py --debug ./ --square_size 1 image4.jpg

bbergvt
  • 493
  • 3
  • 8
  • Thank u bbergvt,one more question here what is [--debug] and [--square_size] and []? – snehal desai Apr 07 '16 at 05:35
  • 1
    updated answer to give more details on the parameters and possible working command - your welcome! – bbergvt Apr 07 '16 at 05:45
  • one more qustion plz..i got common.py file and add in my current directory and run using above command so above error of splitfn is goes out but now it shows error like : – snehal desai Apr 07 '16 at 06:09
  • processing image4.jpg... chessboard not found OpenCV Error: Assertion failed (nimages > 0) in calibrateCamera, file /root/opencv-2.4.10/modules/calib3d/src/calibration.cpp, line 3415 Traceback (most recent call last): File "calibrate.py", line 87, in rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, (w, h), None, None) cv2.error: /root/opencv-2.4.10/modules/calib3d/src/calibration.cpp:3415: error: (-215) nimages > 0 in function calibrateCamera so how to solve this? – snehal desai Apr 07 '16 at 06:09
  • From sounds of it the code did not recognize the chessboard from your single image. Based on this - http://stackoverflow.com/questions/20706719/calibrate-camera-with-opencv-how-does-it-work-and-how-do-i-have-to-move-my-ches it sounds like you will need more than 1 image and need give a mask as a parameter to allow software to find them - if you named your images image4.jpg image5.jpg image6.jpg etc. the command would be sudo python calibrate.py --debug ./ --square_size 1 image*.jpg Sounds like you may need 10-20 images of the chessboard to get good result – bbergvt Apr 07 '16 at 06:20
  • i captured more than one image and run using sudo python calibrate.py --debug ./ --square_size 1 image*.jpg but still got same error as above...is i have to capture image at different angle or capture by setting camera position same(at only one place)?i have captured by setting camera at a place only...so is this the reason for this error that all images are same?the images shall be captured at different angle? – snehal desai Apr 07 '16 at 07:13
  • Yes sounds like images should be different angles and distances from chessboard – bbergvt Apr 07 '16 at 07:14
  • i captured image1 to image20(total 20 images)at different angle and distance, and run above command sudo python calibrate.py --debug ./ --square_size 1 image*.jpg ....still get error like: – snehal desai Apr 07 '16 at 10:28
  • processing img10.jpg... chessboard not found OpenCV Error: Assertion failed (nimages > 0) in calibrateCamera, file /root/opencv-2.4.10/modules/calib3d/src/calibration.cpp, line 3415 Traceback (most recent call last): File "calibrate.py", line 87, in rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, (w, h), None, None) cv2.error: /root/opencv-2.4.10/modules/calib3d/src/calibration.cpp:3415: error: (-215) nimages > 0 in function calibrateCamera ...plz tell me how to solve this? – snehal desai Apr 07 '16 at 10:28
1

The splitfn just provides path, file_name(without ext) and ext of the input file. One can use below implementation for the same.

#from common import splitfn
def splitfn(file_path):

    file_path_parts = file_path.split(sep=os.sep)
    _path = os.path.join(*file_path_parts[:-1])
    file_name = file_path_parts[-1]
    file_name_parts = file_name.split(sep='.')
    return _path, file_name_parts[0], file_name_parts[1]
Deepak Sharma
  • 582
  • 8
  • 10
0

Go to https://github.com/opencv/opencv/blob/master/samples/python/common.py download this file or you can just copy this code and paste it into calibrate.py

  • Here is the code copied from commom.py and paste it in calibrate.py
def splitfn(fn):
    path, fn = os.path.split(fn)
    name, ext = os.path.splitext(fn)
    return path, name, ext