How do I retrieve the resultant image as a matrix(numpy array) from results given back by yolov5 in pytoch?
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 .

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))

