4

I'm using the Microsoft Custom Vision service for object detection with the Python SDK. I'm able to make predictions and I'm trying to use the bounding box information that comes back from the prediction to overlay a rectangle on the image using OpenCV.

However, I'm not sure how to exactly calculate from the normalized coordinates that come back from the Custom Vision service to the point vertexes that the OpenCV rectangle function takes in.

Here's an example of what comes back from the service as bounding box:

{'left': 0.146396145,
 'top': 0.0305180848,
 'width': 0.373975337,
 'height': 0.570280433}

Currently, I'm doing these calculations below. The x and y values look like they're being calculated correctly, but I'm not sure how to calculate the second vertex. The image shape was resized to (400, 400).

for pred in predictions:
    x = int(pred.bounding_box.left * img.shape[0])
    y = int(pred.bounding_box.top * img.shape[1])

    width = int(pred.bounding_box.width * img.shape[0])
    height = int(pred.bounding_box.height * img.shape[1])

    img = cv2.rectangle(img, (x,y), (width,height), (0,0,255), 2)

And here is the resulting image from the above code: enter image description here

The first box looks like it's not going far enough, whereas the second box looks like it produced a rectangle going the opposite way of where it should.

Does anyone know how to calculate these correctly from normalized coordinates?

Jon
  • 2,644
  • 1
  • 22
  • 31

1 Answers1

5

Arguments for rectangle in opencv-python are point_1 and point_2. Like that:

for pred in predictions:
    x = int(pred.bounding_box.left * img.shape[0])
    y = int(pred.bounding_box.top * img.shape[1])

    x2 = x + int(pred.bounding_box.width * img.shape[0])
    y2 = y + int(pred.bounding_box.height * img.shape[1])

    img = cv2.rectangle(img, (x,y), (x2,y2), (0,0,255), 2) 
Sane_or_not
  • 146
  • 8