简体   繁体   English

如何解决ImportError:无法在python中导入名称splitfn

[英]How to solve ImportError: cannot import name splitfn in python

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:我在我的calibrate.py 文件中编写了校准相机的代码,但是当我运行该文件时,它显示错误,例如,ImportError: cannot import name splitfn ..so 如何解决这个问题?...我已经使用这个命令运行了这个文件:

sudo python calibrate.py image4.jpg calibration.yaml --debug-dir out须藤python校准.py image4.jpg校准.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?那么这个执行这个脚本的命令是正确的吗?如果有任何变化,那么请建议我..我捕获的用于校准的图像(我已将其作为输入)和输出(RMS,相机矩阵,失真系数)存储在calibration.yaml文件中?我需要制作这个文件还是它是自动创建的?

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:这一行找不到定义 splitfn 函数的 common.py:

from common import splitfn 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.您应该获取此代码来自的存储库的 Python 目录中的所有文件,或者至少获取 common.py 应该可以解决您提供的示例错误。

https://github.com/Itseez/opencv/tree/master/samples/python 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.此外,OpenCv 示例代码记录了如何从其中调用代码,因此不要认为您在调用中需要 .yaml 参数。

usage: calibrate.py [--debug ] [--square_size] []用法: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 ./ [--debug ] = 您希望将输出图像写入的路径 - 默认为 --debug: ./output/ -- 如果您希望将图像写入具有calibrate.py 的相同路径,您可以尝试-调试。/

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:不确定 --square_size 究竟是做什么的,但它默认为 1 最后一个输入是图像的路径或名称,如果它与校准脚本位于同一目录中 - 如果 image4.jpg 是您的图像并且您想使用默认值写入当前路径我会认为:

sudo python calibrate.py --debug ./ --square_size 1 image4.jpg须藤 python 校准.py --debug ./ --square_size 1 image4.jpg

The splitfn just provides path, file_name(without ext) and ext of the input file. splitfn 只提供输入文件的路径、文件名(不带扩展名)和扩展名。 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]

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.pyhttps://github.com/opencv/opencv/blob/master/samples/python/common.py下载这个文件,或者你可以复制这个代码并粘贴到calibrate.py

  • Here is the code copied from commom.py and paste it in calibrate.py这是从commom.py复制的代码并将其粘贴到calibrate.py
def splitfn(fn):
    path, fn = os.path.split(fn)
    name, ext = os.path.splitext(fn)
    return path, name, ext

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM