pythonopencvgoogle-vision

How to draw bounding boxes using normalized bounding polygon vertices?


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?


Solution

  • 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.