当前位置:网站首页>Deploy yolov7 target detection using OpenCV and onnxruntime - record post

Deploy yolov7 target detection using OpenCV and onnxruntime - record post

2022-07-19 05:46:00 Just do it! ට⋆*

I want to realize these two days yolov5 Of tensort Speed up , A little white , Very shallow understanding , Only record , Prevent forgetting .

Take a note of it yolov7:

yolov7 Of OpenCV、ONNXRuntime Deploy

Share the open source works of the boss yolov7 Of OpenCV、ONNXRuntime Deploy
The boss not only realized yolov7 Of OpenCV、ONNXRuntime Deploy , And selflessly contributed to the transformation model , Thank you very much .

ONNXRuntime Deploy

be based on gpu function , Pretty fast , It can be done in real time .
Running steps : Download the model file and put it in yolov7-opencv-onnxrun-cpp-py-main Under the table of contents

cd yolov7-opencv-onnxrun-cpp-py-main
mikdir simples # Put in a few pictures and videos 
# function 
python onnxruntime/main.py --imgpath simples/1.jpg --modelpath models/yolov7-tiny_384x640.onnx

result  Insert picture description here

Add video stream reasoning

On the basis of the source code, the reasoning of video stream and reasoning for a certain class are added , The code is as follows :

VID_FORMATS = ['asf', 'avi', 'gif', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'wmv']  # include video suffixes
imgpath = args.imgpath
    print(imgpath.split('.')[-1])
    if imgpath.split('.')[-1] in VID_FORMATS:
        cap =cv2.VideoCapture(imgpath)
        while True:
            success, srcimg = cap.read()
            srcimg = imutils.resize(srcimg, width=640)
            t1 = time.time()
            boxes, scores, class_ids = yolov7_detector.detect(srcimg)
            print(time.time() - t1)
            # Draw detections
            dstimg = yolov7_detector.draw_detections(srcimg, boxes, scores, class_ids)
            print(time.time() - t1)
            winName = 'Deep learning object detection in OpenCV'
            cv2.namedWindow(winName, 0)
            cv2.imshow(winName, dstimg)

            cv2.waitKey(1)
        cv2.destroyAllWindows()
    else:
        srcimg = cv2.imread(args.imgpath)
        # Detect Objects
        t1 = time.time()
        boxes, scores, class_ids = yolov7_detector.detect(srcimg)
        print(time.time() - t1)
        # Draw detections
        dstimg = yolov7_detector.draw_detections(srcimg, boxes, scores, class_ids)
        print(time.time() - t1)
        winName = 'Deep learning object detection in OpenCV'
        cv2.namedWindow(winName, 0)
        cv2.imshow(winName, dstimg)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

Modify the reading method of the input source , Add to mp4 Input support for , Add the detection of specified classes in the code

 def draw_detections(self, image, boxes, scores, class_ids):
        for box, score, class_id in zip(boxes, scores, class_ids):
            x1, y1, x2, y2 = box.astype(int)
            
            # Draw rectangle
            #id Corresponding coconame Sort files by category , Specify identifying pedestrians 
			if class_id==0:
	            cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0, 255), thickness=2)
	
	            label = self.class_names[class_id]
	            label = f'{
      label} {
      int(score * 100)}%'
	            labelSize, baseLine = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
	            # top = max(y1, labelSize[1])
	            # cv.rectangle(frame, (left, top - round(1.5 * labelSize[1])), (left + round(1.5 * labelSize[0]), top + baseLine), (255,255,255), cv.FILLED)
	            cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), thickness=2)
        return image

give the result as follows
asdf

Running speed comparison

All adopt yolov7-tiny_384x640.onnx Model
opencv-cpu inference time:1.04 s
ONNXRuntime-gpu inference time:0.17 s


yolov5

原网站

版权声明
本文为[Just do it! ට⋆*]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207170508291146.html