1

This is the code i'm trying to run on an virtual environment in python3.6. I use ubuntu newest version 17.10, I run the code as 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

I have this Folder named '2' where I have all the scripts and other folder named selectors where there is 2 scripts init and box_selector

  • 2(folder)

  • ----selectors/

  • ------------_ init _.py
  • ------------box_selector.py
  • ----detector.py
  • ----gather_annotations.py
  • ----test.py
  • ----train.py

How can I fix that, in the post where I got the code from says something about 'relative imports' but i couln't fix it, thank you.

papisilv
  • 41
  • 1
  • 7
  • How do we reproduce the error? Also, what is your working directory and where is it relative to the module you'd import? – ytu Apr 15 '18 at 03:04
  • i run the code in terminal as $ python3 gather_annotations.py, and get the error in the picture, the module im trying to run is box_selector from the script gather_annotations – papisilv Apr 15 '18 at 03:24
  • Welcome to SO. I recommend you edit your question to put the error in the question as text rather that a screen grab image. An image makes it a pain to read making it less likely you'll get help. – James Jones Apr 15 '18 at 03:32
  • I think the title can be misleading. Clearly the error has nothing to do with `from imutils.paths import list_images` but with `from selectors import BoxSelector`. Besides it is a `ModuleNotFoundError` according to your attached photo, not "Imutils error". – ytu Apr 15 '18 at 03:32
  • There's no `BoxSelector` but only `box_selector` under `selectors` directory. I think that's why your are getting the error. – ytu Apr 15 '18 at 03:35
  • I had some trouble adding the error as text so I just took a screenshot(sorry) and ytu yes you are right I didnt know to post the error i'll change it right know – papisilv Apr 15 '18 at 03:37
  • there is a class named BoxSelector in the box_selector.py, my guess is that it calls the function _ _ init _ _ which only have this code ''from box_selector import BoxSelector'' – papisilv Apr 15 '18 at 03:42
  • 1
    I fix it !!!! i literally just added a "." to the _ _ init _ _ .py so instead of havin --from box_selector import BoxSelector-- I did this --from .box_selector import BoxSelector-- and it worked !! python 3 module is very different than python 2 thank you all :) – papisilv Apr 15 '18 at 03:54

2 Answers2

1

You need to use . notation to access a file inside a folder..

so

from folder.python_file import ClassOrMethod

in your case

from selectors.box_selector import BoxSelector

Having__init__.py in the selectors folder is crucial to making this work.

You can as many folders as you like and can access as follows but each folder has to contain an __init__.py to work

from folder.folder1.folder2.python_file import ClassOrMethod
johnashu
  • 2,167
  • 4
  • 19
  • 44
  • 1
    thank you i saw this post https://stackoverflow.com/questions/4142151/how-to-import-the-class-within-the-same-directory-or-sub-directory and i fix it right away XD – papisilv Apr 15 '18 at 03:56
  • Glad to hear it.. I can remember having the same problem!! – johnashu Apr 15 '18 at 03:57
0

One possible area of confusion is that there is a different python library called "selectors" which is DIFFERENT from the selectors of this code example.

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

I ended up rename "selectors" (including the directory) of this example to "boxselectors"

This example is from http://www.hackevolve.com/create-your-own-object-detector/

Clem Wang
  • 689
  • 8
  • 14