-1

I am using the localization module of Google Vision API and I am getting normalized vertices as the response. I want to draw bounding boxes over these objects but I am not able to reach a solution. Response returned:

Top (confidence: 0.8741532564163208)
Normalized bounding polygon vertices: 
 - (0.3563929498195648, 0.36136594414711)
 - (0.6341143250465393, 0.36136594414711)
 - (0.6341143250465393, 0.6402543783187866)
 - (0.3563929498195648, 0.6402543783187866)

Luggage & bags (confidence: 0.8460243940353394)
Normalized bounding polygon vertices: 
 - (0.5353205800056458, 0.6736522316932678)
 - (0.6584492921829224, 0.6736522316932678)
 - (0.6584492921829224, 0.7805569767951965)
 - (0.5353205800056458, 0.7805569767951965)

Shoe (confidence: 0.6873495578765869)
Normalized bounding polygon vertices: 
 - (0.001777957659214735, 0.8177978992462158)
 - (0.10019400715827942, 0.8177978992462158)
 - (0.10019400715827942, 0.9128244519233704)
 - (0.001777957659214735, 0.9128244519233704)


Code:

import cv2
import numpy as np

# Load the image
img = cv2.imread('path to image')

# Define the polygon vertices
vertices = np.array([(0.08251162618398666, 0.7436794638633728), (0.18944908678531647, 0.7436794638633728),
                     (0.18944908678531647, 0.8542687892913818),(0.08251162618398666, 0.8542687892913818)])

# Convert the normalized vertices to pixel coordinates
height, width = img.shape[:2]
pixels = np.array([(int(vertex[0] * width), int(vertex[1] * height)) for vertex in vertices])

# Draw the polygon on the image
cv2.polylines(img, [pixels], True, (0, 255, 0), 2)

# Display the result
cv2.imshow('Image with polygon', img)
cv2.waitKey(0)

Can anyone help me with this?

NevinTroy
  • 13
  • 5
  • What have you tried so far? How did it not work? It is more likely that you will get help if you give us something to start from, rather than asking to solve the problem from scratch :) – Florent Monin Mar 01 '23 at 11:57
  • I've tried your code and it seems to work. What is the problem that you encounter? – Florent Monin Mar 01 '23 at 12:10
  • It does not create a bounding box over the object that it claims to have detected. It just gives the image back again. – NevinTroy Mar 01 '23 at 12:27
  • 1
    Try increasing the thickness of the polygon to something like 50, when I tested it, it did create a bounding box on the images! if that doesn't work, maybe try `img = cv2.polylines(img, [pixels], True, (0, 255, 0), 50)` (though, for some reason I can't explain, it wasn't needed when I tried) – Florent Monin Mar 01 '23 at 12:31
  • Actually, I had one more doubt, I was trying to use Vision API to detect products but for some images, it just returns basic labels such as 'dress' but for some, it names the brand and model. Is there any reason this happens? – NevinTroy Mar 01 '23 at 13:04
  • 1
    I don't know how Vision API works, so not sure. But I suggest you open a new question with those details, since it's a different problem! – Florent Monin Mar 01 '23 at 13:08
  • I suspect that your bounding boxes represent the 4 corners and are each in the range 0 to 1. They just need to be multiplied by the width and height of your image -- width for the x coordinate and height for the y coordinate. Then compute the width and height of the bounding box (w, h) as the difference in diagonal opposite x values and difference in diagonal opposite y values from the 4 corners. The OpenCV bounding box has the form (x,y,w,h) where x,y is the top left corner. However to draw it, you just need to put in two diagonal opposite (x,y) pairs in cv2.rectangle(). – fmw42 Mar 02 '23 at 03:03

1 Answers1

-1
img = cv2.polylines(img, [pixels], True, (0, 255, 0), 50)

This might be an issue from my cv2 module, but previously when this command was executed without the thickness parameter, nothing was displayed but now when I added the thickness as 50, the prolem resolved.

NevinTroy
  • 13
  • 5
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Mar 03 '23 at 10:53