-1

I have been learning how to implement pretrained yolo using pytorch, and I want to display the output image using openCV's cv2.imshow() method. The output image can be displayed using .show() function and saved using .save() function, I however want to display it using cv2.imshow(), and for that I would need the image in the form of a numpy array. I'm unaware about how we do that or even if that is at all possible. Here's the code for it.

import torch

model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
imgs = ['img.png']  # batch of images

results = model(imgs)

results.print()
results.show()  # or .save(), shows/saves the same image with bounding boxes around detected objects

# Show 'results' using openCV's cv2.imshow() method?

results.xyxy[0]  # img1 predictions (tensor)
print(results.pandas().xyxy[0])  # img1 predictions (pandas)

A longer way of solving this problem would be to create bounding boxes ourselves over the detected objects in the image and display it, but consider me lazy :p .

Shreyas Shrawage
  • 340
  • 2
  • 10
  • 1
    drawing the boxes is **the only way**. [there might be utility functions in pytorch that do it for you](https://pytorch.org/vision/main/utils.html)... but you'd still have to peel the boxes' coordinates out of the network's output. – Christoph Rackwitz Dec 29 '21 at 22:13
  • @ChristophRackwitz well that's a bummer. Thanks for the information though :), I wish there was a way. – Shreyas Shrawage Dec 30 '21 at 17:09

3 Answers3

1

I am lazy like you :) and you can display the bounding boxes without the need to draw them manually. When you call results.save() it will save a version of the image with the boxes to this folder 'runs/detect/exp/' Then you can display that image using cv2.

results.save()
import cv2
img = cv2.imread("runs/detect/exp/zidane.jpg")

#cv2.imgshow does not work on Google collab so this is a work around. 
# You should get the same results if you use cv2.imgshow 
from google.colab.patches import cv2_imshow 
cv2_imshow(img)

enter image description here

alexheat
  • 479
  • 5
  • 9
  • Hey @alexheat! Thanks for the answer :) . Although the same thought crossed my mind and I am working on frames of images, so saving each frame and loading it would not only consume space but also affect the execution time of the code. The space issue can be dealt by deleting the images but again it would affect the time complexity. – Shreyas Shrawage Dec 30 '21 at 17:06
1

had the same issue, so I wrote a small method to do so quickly draw the image without saving it.

  def drawRectangles(image, dfResults):
    for index, row in dfResults.iterrows():
      print( (row['xmin'], row['ymin']))
      image = cv2.rectangle(image, (row['xmin'], row['ymin']), (row['xmax'], row['ymax']), (255, 0, 0), 2)
    cv2_imshow(image)

_

    results = model(image)
    dfResults = results.pandas().xyxy[0]
    self.drawRectangles(image, dfResults[['xmin', 'ymin', 'xmax','ymax']].astype(int))
Tomk07
  • 113
  • 2
  • 12
0

Just open the image via cv2 and then ad rectangles by drawing a rectangle via the given points from your result arrays?

import cv2

img = cv2.imread("image_path")

link how to draw a rectangle with cv2

@ChristophRackwitz

here u have it

cv2.rectangle(image, start_point, end_point, color, thickness)
  • 1
    if your answer amounts to "draw a rectangle" (which is appropriate for this question), it should include the code to draw a rectangle (which is more important than the code to load an image). an *off-site link* would be insufficient. – Christoph Rackwitz Dec 29 '21 at 22:11
  • @ChristophRackwitz well the "offsite" link explains perfectly how to draw a rectangle using cv2 that's why I linked it lol – gerda die gandalfziege Dec 29 '21 at 22:23
  • answers on SO have a higher standard. "link-only answers" are a particular type that is frequently asked to be elaborated upon. https://stackoverflow.com/help/answering and https://meta.stackoverflow.com/tags/link-only-answers/info – Christoph Rackwitz Dec 29 '21 at 22:24
  • @ChristophRackwitz pleased? – gerda die gandalfziege Dec 29 '21 at 22:44