Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello
Before I write the question I'd like to thank you for helping me understand and solve the issue.

Here is the problem;
I have a python code to detect any object (which most of them don't have pre trained models) on an image regardless of their type. (this task itself is my objective, I haven't found any source code that does it)

Here is my code:
Python
import torch, torchvision
print(torch.__version__, torch.cuda.is_available())


import detectron2
from detectron2.utils.logger import setup_logger
setup_logger()

import numpy as np
import cv2
import random
from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog
from PIL import Image

im = Image.open("input.jpg")
im.show()

cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
predictor = DefaultPredictor(cfg)
outputs = predictor(im)

v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
v = v.draw_instance_predictions(outputs["instances"].to("cpu"))
im = v.get_image()[:, :, ::-1]
im.show()


the line below keeps prompting errors;

Python
outputs = predictor(im)


the error is this;
Terminal
File "/home/reza/Code/test1/test1.py", line 28, in <module>
    outputs = predictor(im)
  File "/home/reza/Code/test1/detectron2/detectron2/engine/defaults.py", line 320, in __call__
    image = self.aug.get_transform(original_image).apply_image(original_image)
  File "/home/reza/Code/test1/detectron2/detectron2/data/transforms/transform.py", line 113, in apply_image
    assert img.shape[:2] == (self.h, self.w)
  File "/home/reza/Code/test1/venv/lib/python3.10/site-packages/PIL/Image.py", line 529, in __getattr__
    raise AttributeError(name)
AttributeError: shape. Did you mean: 'save'?


What I have tried:

I solved this issue by replacing all
Python
image.shape
calls with
Python
image.size
calls as it returns the height and weight of the image.

It keeps prompting similar errors.
the next error is
Terminal
image = self.aug.get_transform(original_image).apply_image(original_image)
  File "/home/reza/Code/test1/detectron2/detectron2/data/transforms/transform.py", line 117, in apply_image
    if img.dtype == np.uint8:

saying the object doesn't have an img.dtype

it seems like that these error are not going to end.
the last attribute error I encountered was the strides attribute. which I cannot replace this attribute.

I'm assuming the problem is some place else and I shouldn't keep editing the detectron source code.
Posted
Updated 3-Apr-23 2:06am
Comments
Richard MacCutchan 3-Apr-23 8:17am    
All of these errors are related to the detectron package. You should check the documentation to see what information is required by the DefaultPredictor.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900