找不到模块错误 (imutils.paths)

Module not found error (imutils.paths)

这是我在 python3.6 的虚拟环境中尝试 运行 的代码。我使用 ubuntu 最新版本 17.10,我 运行 代码为 python3 gather_annotations.py

import numpy as np  
import cv2 
import argparse  
from imutils.paths import list_images
from selectors import BoxSelector  
#parse arguments
ap = argparse.ArgumentParser()  
ap.add_argument("-d","--dataset",required=True,help="path to images dataset...")  
ap.add_argument("-a","--annotations",required=True,help="path to save annotations...")  
ap.add_argument("-i","--images",required=True,help="path to save images")  
args = vars(ap.parse_args())  
#annotations and image paths  
annotations = []  
imPaths = []  
#loop through each image and collect annotations  
for imagePath in list_images(args["dataset"]):  
    #load image and create a BoxSelector instance  
    image = cv2.imread(imagePath)  
    bs = BoxSelector(image,"Image")  
    cv2.imshow("Image",image)  
    cv2.waitKey(0)  
    #order the points suitable for the Object detector  
    pt1,pt2 = bs.roiPts  
    (x,y,xb,yb) = [pt1[0],pt1[1],pt2[0],pt2[1]]  
    annotations.append([int(x),int(y),int(xb),int(yb)])  
    imPaths.append(imagePath)  
#save annotations and image paths to disk  
annotations = np.array(annotations)  
imPaths = np.array(imPaths,dtype="unicode")  
np.save(args["annotations"],annotations)  
np.save(args["images"],imPaths)  

And I get the following errors

我有一个名为“2”的文件夹,其中包含所有脚本和其他名为 selectors 的文件夹,其中有 2 个脚本 init 和 box_selector

我该如何解决这个问题,在 post 中,我从中获取代码的地方说了一些关于 'relative imports' 但我无法解决的问题,谢谢。

您需要使用 .访问文件夹内文件的符号..

所以

from folder.python_file import ClassOrMethod

你的情况

from selectors.box_selector import BoxSelector

选择器文件夹中有__init__.py对于完成这项工作至关重要。

您可以根据需要创建任意数量的文件夹,并且可以按如下方式访问,但每个文件夹都必须包含一个 __init__.py 才能工作

from folder.folder1.folder2.python_file import ClassOrMethod

一个可能造成混淆的地方是有一个不同的 python 库称为 "selectors",它与此代码示例的选择器不同。

https://docs.python.org/3/library/selectors.html

我最终将此示例的 "selectors"(包括目录)重命名为 "boxselectors"

这个例子来自http://www.hackevolve.com/create-your-own-object-detector/