-1

I developed a model1 to detect an object by using webcam, and then use it to generate some of the bounding box areas as a detection area for my model2, such as the picture I provided. But I don't know how to code. Can anyone give me some idea or sample code?

enter image description here

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
andy wong
  • 61
  • 5

2 Answers2

0

Yes, you can do this. For example, the model1 and model2 return bounding box values as x,y,w,h and you have an original image, say image you can use something like this:

# extracting the bounding box using model1
x, y, w, h = model1(image)

# extracting cropped image
cropped_image = image[y:y+h, x:x+w]

# now getting the inner bounding box using model2
x2, y2, w2, h2 = model2(cropped_image)

# final image
final_iamge = cropped_image[y2:y2+h2, x2:x2+w2]

This is just a pseudo code you have to see the code where you will get the bounding boxes as some of the models return bounding box values as xmin, ymin, xmax, ymax. Here xmax=xmin+w and ymax=ymin+h if you get the bounding boxes in the form of x, y, w, h.

Sample Code

import torch
# Model
model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5/yolov5s.pt', force_reload=True)

# Images (pass as a list)
image = ['test.png']
results = model(image)

# Show image
results.show()

# results in list format
print(results.xyxy[0].tolist()[0])
Asadullah Naeem
  • 347
  • 2
  • 11
  • thanks for your answer! I got a error message when I trying to load my model1 to get the box values ( TypeError: cannot unpacked non-iterable Detection object) Here is my code – andy wong Jun 24 '22 at 08:10
  • from PIL import Image # Model model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5/yolov5s.pt', force_reload=True) # Images x, y, w, h = model('test.jpeg') # PIL image cropped_image = image[y:y+h, x:x+w] – andy wong Jun 24 '22 at 08:11
  • You need pass the names as a list. Please check the updated sample code – Asadullah Naeem Jun 24 '22 at 13:44
  • currently, I got all the coordinates by using results.pandas().xyxy[0]), but how can I use these coordinates to lead my model2 to detect inside the area without cropping method? since I want to apply this model into webcam. Many thanks – andy wong Jun 24 '22 at 17:46
  • I think, I find all the coordinates in model1, and then use model2 to detect the area and return all the coordinates, finally use these values to run cv2 rectangle function for labelling. – andy wong Jun 24 '22 at 17:59
0

I also encounter same problem. Here is my code to get coordinates in detect.py.

if save_img or save_crop or view_img


x1 = int(xyxy[0].item())
y1 = int(xyxy[1].item())
x2 = int(xyxy[2].item())
y2 = int(xyxy[3].item())
confidence_score = conf
class_index = cls
object_name = names[int(cls)]
MHW
  • 21
  • 2