4

New to both python and machine learning. I'm trying to draw bounding boxes on my mss screen capture. This is the part of the code where I believe I should be receiving the coordinates to draw the rectangle.

    while True:

            img = screnshot.grab(bounding_box)

            frame = np.array(img)

            cv2.imshow('Testing', frame)

            results = model(frame)

            results.print()  
            pandasbox=results.pandas().xyxy[0]
            print(pandasbox)

The coordinates and classes are printed just fine using pandas, for example:

image 1/1: 400x350 1 person, 1 truck
Speed: 0.0ms pre-process, 14.4ms inference, 0.0ms NMS per image at shape (1, 3, 640, 576)
         xmin        ymin        xmax      ymax  confidence  class    name
0   61.171875  134.765625  133.046875  334.0625    0.810547      0  person
1  181.562500    0.273438  347.500000   86.2500    0.768555      7   truck

But... I have no idea how to form a rectangle using these coordinates. Adding "(xmin, xmax),(ymin, ymax)" to the cv2.rectangle function was unsuccessful as well as many other attempts, including this attempt, which never drew bounding boxes in the mss screen despite printing detections:

for box in results.xyxy[0]: 
                if box[5]==0:
        
                    xB = int(box[2])
                    xA = int(box[0])
                    yB = int(box[3])
                    yA = int(box[1])
                    
                    cv2.rectangle(frame, (xA, yA), (xB, yB), (0, 255, 0), 2)

If anyone could show me an example of using the coordinates from "results.pandas().xyxy[0]" to draw a bounding box with cv2.rectangle that would be great! As well as any other pointers or insight that someone new to this would be unaware of...

3 Answers3

3

Your code:

for box in results.xyxy[0]: 
            if box[5]==0:
    
                xB = int(box[2])
                xA = int(box[0])
                yB = int(box[3])
                yA = int(box[1])
                
                cv2.rectangle(frame, (xA, yA), (xB, yB), (0, 255, 0), 2)

You have called rectangle function of OpenCV, but you have not call imshow function of OpenCV for visualization.

Modified Code:

 for box in results.xyxy[0]: 
            if box[5]==0:
                xB = int(box[2])
                xA = int(box[0])
                yB = int(box[3])
                yA = int(box[1])
                cv2.rectangle(frame, (xA, yA), (xB, yB), (0, 255, 0), 2)
                cv2.imshow("OutputWindow",frame)
2

try:

results.render()

or

results.show()
0

Use:

image = results.render()
cv2.imshow('image",image)

It will draw the image with the Boundin boxes and the label and the score.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31