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 Answers 3

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()
2
  • I am sure it would help the community if you explained to us why and how you code would solve the OP's problem Jan 12, 2022 at 11:24
  • 1
    @SimasJoneliunas this solution uses the yolov5 and yolo5 packages from PyPI as the reference. Sep 18, 2022 at 18:20
0

Use:

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.