import torch import numpy as np from yolov5.models.experimental import attempt_load from yolov5.utils.datasets import letterbox from yolov5.utils.general import non_max_suppression, scale_coords from yolov5.utils.torch_utils import select_device class Detector: def __init__(self, img_size, view_img_size,weights): self.img_size = img_size self.view_img_size=view_img_size self.threshold = 0.3 self.stride = 1 self.weights =weights self.device = '0' if torch.cuda.is_available() else 'cpu' self.device = select_device(self.device) half = self.device != 'cpu' # half precision only supported on CUDA # Load model model = torch.load(self.weights, map_location=self.device)[ 'model'].float() # load to FP32 model.to(self.device).eval() if half: model.half() # to FP16 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 img def detect(self, im): img = self.preprocess(im) pred = self.m(img, augment=False)[0] # pred = pred.float() # pred = non_max_suppression(pred, self.threshold, 0.4) return img,pred # 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)] # print(lbl,':'+str(conf)) # if float(conf)<0.5: # continue # if lbl not in ['person', 'bicycle', 'car', 'motorcycle', 'bus', 'truck','dog']: # continue # pass # x1, y1 = int(x[0]), int(x[1]) # x2, y2 = int(x[2]), int(x[3]) # boxes.append( # (x1, y1, x2, y2, cls_id, conf,lbl)) # return boxes