代码拉取完成,页面将自动刷新
同步操作将从 HardSoft2023/unbox_yolov5_deepsort_counting 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
import torch
import numpy as np
from models.experimental import attempt_load
from utils.datasets import letterbox
from utils.general import non_max_suppression, scale_coords
from utils.torch_utils import select_device
class Detector:
def __init__(self, img_size=640, conf=0.01, iou=0.65, model_type='x6', old_init=False):
if old_init:
self.__old_init__()
else:
self.model = torch.hub.load('./weights/', 'yolov5{}'.format(model_type), pretrained=True, source='local')
self.model.conf = conf
self.model.iou = iou
# self.model.classes = [0, 1, 2, 3, 5, 7, 8]
self.img_size = img_size
def predict(self, img):
"""im = cv2.imread(im_path)
"""
results = self.model(img, size=self.img_size)
results.render()
for img in results.imgs:
output_image_frame = img
return output_image_frame, results.pandas().xyxy[0][['xmin', 'ymin', 'xmax', 'ymax', 'name', 'confidence']].values.tolist()
def __old_init__(self):
self.img_size = 640
self.threshold = 0.3
self.stride = 1
self.weights = './weights/yolov5m.pt'
self.device = '0' if torch.cuda.is_available() else 'cpu'
self.device = select_device(self.device)
# model = attempt_load(self.weights, map_location=self.device)
model = torch.hub.load('./weights/', 'yolov5{}'.format(model_type), pretrained=True, source='local')
model.to(self.device).eval()
model.half()
self.m = model
self.names = model.module.names if hasattr(
model, 'module') else model.names
def preprocess(self, img):
img0 = img.copy()
img = letterbox(img, new_shape=self.img_size)[0]
img = img[:, :, ::-1].transpose(2, 0, 1)
img = np.ascontiguousarray(img)
img = torch.from_numpy(img).to(self.device)
img = img.half()
img /= 255.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
return img0, img
def detect(self, im):
im0, img = self.preprocess(im)
pred = self.m(img, augment=False)[0]
pred = pred.float()
pred = non_max_suppression(pred, self.threshold, 0.4)
boxes = []
for det in pred:
if det is not None and len(det):
det[:, :4] = scale_coords(
img.shape[2:], det[:, :4], im0.shape).round()
for *x, conf, cls_id in det:
lbl = self.names[int(cls_id)]
if lbl not in ['person', 'bicycle', 'car', 'motorcycle', 'bus', 'truck']:
continue
pass
x1, y1 = int(x[0]), int(x[1])
x2, y2 = int(x[2]), int(x[3])
boxes.append(
(x1, y1, x2, y2, lbl, conf))
return boxes
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。