1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
import os import sys from pathlib import Path
import cv2 import torch
import numpy as np FILE = Path(__file__).resolve() ROOT = FILE.parents[0] if str(ROOT) not in sys.path: sys.path.append(str(ROOT)) ROOT = Path(os.path.relpath(ROOT, Path.cwd())) from utils.augmentations import letterbox from models.common import DetectMultiBackend from utils.datasets import IMG_FORMATS, VID_FORMATS from utils.general import (LOGGER, non_max_suppression, scale_coords) from utils.plots import Annotator, colors from utils.torch_utils import select_device, time_sync
class YoloV5: def __init__(self) -> None: self.img_size = 640 self.stride = 32 self.weights = ROOT / 'yolov5s.pt' self.data = ROOT / 'data/coco128.yaml' self.device = 0 self.conf_thres = 0.25 self.iou_thres = 0.45 self.max_det = 1000 self.dnn = False self.imgsz = (640,640) self.augment=False self.visualize=False self.classes=None self.agnostic_nms=False self.line_thickness=3 self.hide_labels=False self.hide_conf=True self.view_img=False
def load_img(self, img0): img = letterbox(img0, self.img_size, stride=self.stride, auto=True)[0] img = img.transpose((2, 0, 1))[::-1] img = np.ascontiguousarray(img) return img
def select_dev(self, dev): self.device = dev
def load_model(self): self.dev = select_device(self.device) self.model = DetectMultiBackend(self.weights, self.dev, self.dnn, self.data)
def detect(self, srcImg):
names = self.model.names
im = self.load_img(srcImg) im = torch.from_numpy(im).to(self.dev) im = im.float() im /= 255 if len(im.shape) == 3: im = im[None] pred = self.model(im, augment=self.augment, visualize=self.visualize) pred = non_max_suppression(pred, self.conf_thres, self.iou_thres, self.classes, self.agnostic_nms, max_det=self.max_det) det = pred[0] im0 = srcImg.copy() im0rect = [0,0,0,0] isexsit = 0 annotator = Annotator(im0, line_width=self.line_thickness, example=str(names)) if len(det): det[:, :4] = scale_coords(im.shape[2:], det[:, :4], im0.shape).round() for *xyxy, conf, cls in reversed(det): c = int(cls) label = None if self.hide_labels else (names[c] if self.hide_conf else f'{names[c]} {conf:.2f}') if label == 'person': isexsit = 1 annotator.box_label(xyxy, label, color=colors(c, True)) im0rect[0] = (int(xyxy[0])) im0rect[1] = (int(xyxy[1])) im0rect[2] = (int(xyxy[2])) im0rect[3] = (int(xyxy[3]))
im0 = annotator.result()
if self.view_img: cv2.imshow("img", im0) cv2.waitKey(0)
return im0.copy(),isexsit,im0rect[0],im0rect[1],im0rect[2],im0rect[3]
if __name__ == '__main__': yolo = YoloV5() yolo.load_model() img = cv2.imread(str(ROOT / 'data/images/bus.jpg')) im0, nd = yolo.detect(img) cv2.imshow("img", im0) key = cv2.waitKey(0) if key == ord('q'): cv2.destroyAllWindows()
|