Uncategorized

ModuleNotFoundError when importing object from a Custom Python Package in a Nested Project Structure


I’ve been writing an image processing pipeline, and part of it is using yolov7 to get bounding boxes around people in images. I made a custom wrapper (which I can pip install) to have a Detection() object to feed images into, and it has worked perfectly in my tests, but not when I import the object into another file. Here is my current project structure:

src/
├── urlProcessor/
│   ├── models/
│   │   ├── __init__.py
│   │   ├── YOLOWrapper.py
│   │   └── yolov7.pt
│   ├── __init__.py
│   └── runAnalysis.py
└── other_stuff/

and inside YOLOWrapper.py I have this code

from YOLOv7Detector import Detector as det
from PIL import Image


class YOLOInferenceSingleton:
    def __init__(self, weights_path):
        self.detector = det(weights_path)

    def calculateDetections(self, image, view_img=False):
        return self.detector.calculateDetections(image, view_img=view_img)


def main():
    # Initialize the YOLO inference object
    detector = det('yolov7.pt')

    # Load the image
    image = Image.open('../../../data/test_set/test_101.jpg')

    # Run the YOLO detection
    dets = detector.calculateDetections(image, view_img=True)


if __name__ == '__main__':
    main()

which works exactly as expected. However, when I import the YOLOInferenceSingleton object over into the runAnalysis.py:

from models.YOLOWrapper import YOLOInferenceSingleton

I get a ModuleNotFoundError:

Traceback (most recent call last):
  File "...\src\urlProcessor\runAnalysis.py", line 12, in <module>
    from models.YOLOWrapper import YOLOInferenceSingleton
  File "...\src\urlProcessor\models\YOLOWrapper.py", line 1, in <module>
    from YOLOv7Detector import Detector as det
  File "...\venv\lib\site-packages\YOLOv7Detector\__init__.py", line 1, in <module>
    from YOLOv7Detector.model import Detector
  File "...\venv\lib\site-packages\YOLOv7Detector\model.py", line 16, in <module>
    from YOLOv7Detector.yolov7.models.experimental import attempt_load
  File "...\venv\lib\site-packages\YOLOv7Detector\yolov7\models\experimental.py", line 6, in <module>
    from models.common import Conv, DWConv
ModuleNotFoundError: No module named 'models.common'

Process finished with exit code 1

which goes all the way back into the original yolov7 implementation in the wrapper.

Some more info that might be useful:

The file structure in the wrapper I made is like

YOLOv7Detector
├── /yolov7/ <---- This is the original implementation repo, I'm importing from here
├── __init__.py         
├── image_funcs.py     
└── model.py <---- This is where I am importing to 

and I had another import error when importing functions from the yolov7 directory to the model.py, which I fixed by adding it to sys.path. This is the first time I have made a package, so I have no idea whether this is the correct way to do it, so it might be dumb.

I have tried importing things through the __init__.py in the /models/ directory, but this didn’t do anything.
I also tried appending the /models/ path in runAnalysis, but this also didn’t do anything.

If you’ve gotten this far, thanks, I would appreciate any help at all, I’m very confused at the moment.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *