【发布时间】: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