paddle_detection推理代码
1.在拉下来的源代码中新建一个py文件
2.首先,完成模型构建
from deploy.python.infer import Detector
import os
########加载模型,传入模型构建过程中重要的参数model_dir以及图片可视化保存路径output_dir##########
model_dir = r".\output_inference\yolox_s_300e_coco"
output_dir = r"C:\Users\da\Desktop\test_detection"
model = Detector(model_dir=model_dir,
device='GPU',
run_mode='paddle',
batch_size=1,
trt_min_shape=1,
trt_max_shape=1280,
trt_opt_shape=640,
trt_calib_mode=False,
cpu_threads=1,
enable_mkldnn=False,
enable_mkldnn_bfloat16=False,
output_dir=output_dir,
threshold=0.5,
delete_shuffle_pass=False)
########加载模型,传入模型构建过程中重要的参数model_dir以及图片可视化保存路径output_dir##########
在model_dir中键入你所使用的模型的地址
将output_dir修改为本地存储识别完成后图片的路径
在device中设置是GPU推理还是CPU推理
至此,模型构建完毕
3.单张图片推理
#推理单张图片
img_path = [r"C:\Users\da\Desktop\template_1.jpg"]
results = model.predict_image(img_path)
img_path是个列表,将要推理的图片的绝对路径放入列表中,如果是多张就用’,'隔开.
img_path设置好后,直接运行.py文件
4.多张图片(多张图片处于同一文件夹下)推理
#推理文件夹下所有图片
img_path = []
root_dir = r'D:\da\PaddleDetection-release-2.4\data'
for i in os.listdir(root_dir):
if i.endswith('.jpg') or i.endswith('.png'): #判断文件后缀为jpg或者png才进行推理
img_path.append(os.path.join(root_dir,i)) #将要推理的图片的绝对路径加入img_path列表中
model.predict_image(img_path)
将root_dir设置为你要推理的文件夹的绝对路径后运行py文件即可