qfgaohao / pytorch-ssd

MobileNetV1, MobileNetV2, VGG based SSD/SSD-lite implementation in Pytorch 1.0 / Pytorch 0.4. Out-of-box support for retraining on Open Images dataset. ONNX and Caffe2 support. Experiment Ideas like CoordConv.
https://medium.com/@smallfishbigsea/understand-ssd-and-implement-your-own-caa3232cd6ad
MIT License
1.39k stars 529 forks source link

run_ssd_example.py box variable type does not mach opencv rectangle and putText functions #184

Closed yusufcananar closed 1 year ago

yusufcananar commented 1 year ago

box is a tensor type variable so run_ssd_example.py gives an error.

    box = boxes[i, :]
    cv2.rectangle(orig_image, (box[0], box[1]), (box[2], box[3]), (255, 255, 0), 4)

    cv2.putText(orig_image, label,
                (box[0] + 20, box[1] + 40),
                cv2.FONT_HERSHEY_SIMPLEX,
                1,  # font scale
                (255, 0, 255),
                2)  # line type

Solution

Changing the box coordinates into numpy and int will fix the issue.

    bbox1 = int(box[0].numpy())
    bbox2 = int(box[1].numpy())
    bbox3 = int(box[2].numpy())
    bbox4 = int(box[3].numpy())

    cv2.rectangle(orig_image, (bbox1, bbox2), (bbox3, bbox4), (255, 0, 0), 1)

    cv2.putText(orig_image, label,
                (bbox1 - 5 , bbox2 - 5),
                cv2.FONT_HERSHEY_SIMPLEX,
                0.25,  # font scale
                (255, 0, 0),
                1)  # line type