【问题标题】:How to draw bounding box with xmin,xmax,ymin,ymax如何用 xmin,xmax,ymin,ymax 绘制边界框
【发布时间】:2021-09-13 14:19:00
【问题描述】:

我使用 Yolov5 作为我的检测模块,输入基于屏幕抓取。屏幕大小为bounding_box = {'top': 340, 'left': 650, 'width': 350, 'height': 400}。我成功地从对象检测中获得了 xmin、ymin、xmax 和 ymax 值。但是如何使用这些值在检测对象上绘制边界框?

样本 xmin、ymin、xmax 和 ymax 值

         xmin       ymin        xmax        ymax  confidence  class    name
0  205.366241  68.419243  279.225586  272.266388    0.808757      0  person
1  134.863235  45.008553  208.445160  268.448029    0.777079      0  person

当前屏幕

预期输出:

我的完整代码:

# PyTorch Hub
import torch
import cv2 as cv
import numpy as np
import threading
import time

import numpy as np
import cv2
from PIL.ImageDraw import ImageDraw
from mss import mss
from PIL import Image
from tensorboard.summary.v1 import image
from torch import nn
from torch.utils.checkpoint import checkpoint

bounding_box = {'top': 340, 'left': 650, 'width': 350, 'height': 400}
# Model

#model = torch.hub.load('ultralytics/yolov5', 'custom', path='runs/train/exp/weights/last.pt')
#model = model.fuse().autoshape()
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True).autoshape()

# Images
dir = 'https://ultralytics.com/images/'
#imgs = [dir + f for f in ('zidane.jpg', 'bus.jpg')]  # batch of images

sct = mss()

class ScreenGrab(threading.Thread):
    def __init__(self, id_manager):
        threading.Thread.__init__(self)
        self.id_manager = int(id_manager)

    def run(self):
        while True:
            global scr_img

            sct_img = sct.grab(bounding_box)
            scr_img = np.array(sct_img)
            cv.imshow('Testing', scr_img)

            if (cv2.waitKey(1) & 0xFF) == ord('q'):
                cv2.destroyAllWindows()
                break


class DetectionModule(threading.Thread):
    def __init__(self, id_manager):
        threading.Thread.__init__(self)
        self.id_manager = int(id_manager)

    def run(self):
        while True:
            global x, y, w, h
        # Inference
            results = model(scr_img)
            results.print()  # or .show(), .save()
            boxes = results.pandas().xyxy[0]

        #GET ALL COLUMN WITH ONLY CLASS == PERSON
            person = boxes.loc[boxes['class'] == 0]
            print(person)


def ScreenGrabMain():
    thread_id = ("0")
    led_index = 0
    thread_list = list()
    for objs in thread_id:
        thread = ScreenGrab(led_index)
        thread_list.append(thread)
        led_index += 1
    for thread in thread_list:
        thread.start()
        time.sleep(1)

def DetectionMain2():
    thread_id = ("0")
    led_index = 0
    thread_list = list()
    for objs in thread_id:
        thread = DetectionModule(led_index)
        thread_list.append(thread)
        led_index += 1
    for thread in thread_list:
        thread.start()
        time.sleep(1)

if __name__ == "__main__":
    ScreenGrabMain()
    DetectionMain2()

【问题讨论】:

    标签: python


    【解决方案1】:

    您可以使用cv2.rectangle in 来绘制边界框。下面给出一个例子:

    
    # cv2
    
    image = cv2.rectangle(image, (xmin,ymin), (xmax,ymax), (0,255,0), 2)
    
    
    

    【讨论】:

      【解决方案2】:

      #just 将每个转换为 int 类型并裁剪给出原始图像。

      for index, row in results.pandas().xyxy[0].iterrows():
          print(row['xmin'], row['ymin'], row['xmax'], row['ymax'], row['confidence'])
          x1 = int(row['xmin'])
          y1 = int(row['ymin'])
          x2 = int(row['xmax'])
          y2 = int(row['ymax'])
          cropped_image = imgOrg[y1:y2, x1:x2]
      
          cv2.imwrite(imageName+'_'+str(counter)+'_cropped.png', cropped_image)
          counter = counter + 1 # initialize this to 1 before the for loop.
      

      【讨论】:

      • 请解释您认为这如何回答了这个问题。看起来您只是在裁剪图像,而 OP 希望图像中的人被矩形包围。请参阅How to Answer 了解有关编写好答案的更多信息。
      猜你喜欢
      • 2018-08-07
      • 1970-01-01
      • 1970-01-01
      • 2019-11-20
      • 2021-10-30
      • 1970-01-01
      • 2022-12-14
      • 2017-10-02
      • 2019-07-05
      相关资源
      最近更新 更多