12

144.人工智能-YOLOv7(ONNX模型)的体验使用

YOLOv7在 5 FPS 到 160 FPS 范围内的速度和准确度都超过了所有已知的目标检测器(YOLOv5、YOLOX、PPYOLOE、YOLOR等),并且在 GPU V100 上 30 FPS或 更高的所有已知实时目标检测器中具有最高的准确度 56.8% AP。

本文主要检测体验使用一下YOLOv7,模型部署使用的是飞桨的FastDeploy,模型格式:ONNX。有关FastDeploy介绍可以参看:141.人工智能——FastDeploy:PPYOLOE模型部署,实现目标检测

开放神经网络交换(Open Neural Network Exchange)简称ONNX是微软和Facebook提出用来表示深度学习模型的开放格式。所谓开放就是ONNX定义了一组和环境,平台均无关的标准格式,来增强各种AI模型的可交互性

下载地址:https://bj.bcebos.com/paddlehub/fastdeploy/yolov7.onnx

模型

大小

精度

YOLOv7

141MB

51.4%

测试视频:

测试视频

实现代码:

import cv2
import fastdeploy as fd

model_file="yolo7/yolov7.onnx"
params_file=""
config_file="yolo7/infer_cfg.yml"
model=fd.vision.detection.YOLOv7(model_file,params_file="",runtime_option=None,model_format=fd.Frontend.ONNX)

#读取infer_cfg.yml文件,获取目标对象名称
def get_model_config(file_path):
    class_names=[]
    with open(file_path, 'r') as f:
        for line in f:
            if line.strip()=="label_list:":
                continue
            class_names.append(line[1:].strip())
    return class_names[-80:]

class_names=get_model_config(config_file) #获取目标名称
#print(class_names)

#分析结果,返回预测结果图像
def get_result(result,img):
    reslst=str(result).split("
")
    for res in reslst[1:-1]:
        r=res.split(",")        
        if float(r[4].strip())>=0.5:
            x0=int(float(r[0].strip()))
            y0=int(float(r[1].strip()))
            x1=int(float(r[2].strip()))
            y1=int(float(r[3].strip()))
            id=int(r[5].strip())
            idcolor=((id*50)%255,(id*100)%255,(id*200)%255) #id颜色区分
            cv2.rectangle(img,(x0,y0),(x1,y1),idcolor,2) #画框
            cv2.putText(img,class_names[id]+":"+str(round(float(r[4].strip()),2)),(x0,y0),cv2.FONT_HERSHEY_SIMPLEX,0.6,idcolor,2) #目标名称
    return img

videofile="img/c003.mp4"
cap=cv2.VideoCapture(videofile)
fps=int(cap.get(cv2.CAP_PROP_FPS)) #帧率
ftotal=int(cap.get(cv2.CAP_PROP_FRAME_COUNT))#总帧数
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) #宽度
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) #高度
while cap.isOpened():
    ret,frame=cap.read()
    frame=cv2.resize(frame,(w//2,h//2))
    if ret:
        result=model.predict(frame)
        frame=get_result(result,frame) #预测结果,有目标名称
        #frame= fd.vision.vis_detection(frame,result,score_threshold=0.5) #预测结果,只有目标ID
        
    cv2.imshow("video",frame)
    if cv2.waitKey(1)==ord("q"):
        break
    
cap.release()
cv2.destroyAllWindows()  


画面1


测试的运行环境:CPU(没有GPU,能预测,但预测速度很慢)

设备名称
处理器	Intel(R) Core(TM) i5-10210U CPU @ 1.60GHz   2.11 GHz
机带 RAM	8.00 GB (7.81 GB 可用)
设备 ID
产品 ID	
系统类型	64 位操作系统, 基于 x64 的处理器
笔和触控	没有可用于此显示器的笔或触控输入


人工智能=算法+算力+大数据

13
发表评论
留言与评论(共有 0 条评论) “”
昵称:
匿名发表 登录账号
         
   
验证码:

相关文章

推荐文章

10
11