在jetson上测试TensorRT yolov4遇到的问题(tensorrtx)

文 | MESeraph

00 | 序言

  1. 在jetson NX上测试。

01 | 源码

  1. 下载源码库tensorrtx,里面包含yolov4的TensorRT Engine生成代码。

  2. 按yolov4给的操作执行即可。
    期间遇到的问题如下:操作之前请看下面内容,以确有序解决测试过程中遇到的问题。

02 | 安装依赖包

  1. Pytorch
    Nvidia官方给出了相应的Arm包,请根据自己jetpack版本下载和安装。
    参考pytorch-for-jeston

如果安装torchvision遇到找不到#include <libavutil/pixfmt.h>
要安装ffmepg相关软件,请安装以下软件:

sudo apt install libavformat-dev
sudo apt install libavcodec-dev
sudo apt install libswresample-dev
sudo apt install libswscale-dev
sudo apt install libavutil-dev
sudo apt install libsdl1.2-dev

不要安装的话,则修改setup.py

if has ffmpeg 改为if False
  1. 安装matplotlib,tqdm(python3 gen_wts.py yolov4.weight步骤需要)
pip3 install matplotlib
pip3 install tqdm
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
检测教程 1. 安装依赖库 我们需要在Jetson Nano上安装一些必要的依赖库,包括OpenCV、numpy和TensorRT。可以使用以下命令进行安装: ``` sudo apt-get update sudo apt-get install libopencv-dev python3-opencv python3-numpy ``` 安装TensorRT的过程较为复杂,可以参考NVIDIA的官方文档进行安装:https://docs.nvidia.com/deeplearning/tensorrt/install-guide/index.html 2. 下载模型 我们需要下载YoloV7模型并将其转换为TensorRT模型。模型可以从GitHub上的以下链接中下载: https://github.com/WongKinYiu/yolov7 我们可以使用以下命令将模型转换为TensorRT模型: ``` python3 yolov7_to_onnx.py --model yolov7.pt --output yolov7.onnx --input-size 608 608 python3 onnx_to_tensorrt.py --model yolov7.onnx --output yolov7.engine --input-shape 3 3 608 608 --max-batch-size 1 --fp16 ``` 以上命令将模型从PyTorch模型转换为ONNX模型,然后将ONNX模型转换为TensorRT模型。在转换过程中,我们可以指定输入图像的大小、最大批量大小和精度等参数。 3. 编写代码 我们可以使用以下代码来加载TensorRT模型并使用摄像头进行实时检测: ```python import cv2 import numpy as np import time import tensorrt as trt import pycuda.driver as cuda import pycuda.autoinit # 加载TensorRT模型 engine_file_path = 'yolov7.engine' with open(engine_file_path, 'rb') as f: engine_data = f.read() runtime = trt.Runtime(trt.Logger(trt.Logger.WARNING)) engine = runtime.deserialize_cuda_engine(engine_data) context = engine.create_execution_context() # 定义输入和输出张量 input_binding = [] output_binding = [] for i in range(engine.num_bindings): if engine.binding_is_input(i): input_shape = engine.get_binding_shape(i) input_dtype = engine.get_binding_dtype(i) input_size = trt.volume(input_shape) * engine.max_batch_size * np.dtype(input_dtype).itemsize input_array = cuda.mem_alloc(input_size) input_binding.append({'engine_idx': i, 'array': input_array, 'shape': input_shape, 'dtype': input_dtype}) else: output_shape = engine.get_binding_shape(i) output_dtype = engine.get_binding_dtype(i) output_size = trt.volume(output_shape) * engine.max_batch_size * np.dtype(output_dtype).itemsize output_array = cuda.mem_alloc(output_size) output_binding.append({'engine_idx': i, 'array': output_array, 'shape': output_shape, 'dtype': output_dtype}) # 定义预处理和后处理函数 def preprocess(image): image = cv2.resize(image, (input_shape[3], input_shape[2])) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = image.transpose((2, 0, 1)).astype(np.float32) image /= 255.0 return image def postprocess(outputs, conf_th=0.5, iou_th=0.5): class_ids = [] confidences = [] boxes = [] for output in outputs: output = output.reshape((-1, 85)) for o in output: class_id = np.argmax(o[5:]) confidence = o[4] * o[class_id + 5] if confidence > conf_th: cx, cy, w, h = o[:4] x1 = int((cx - w / 2) * input_shape[3]) y1 = int((cy - h / 2) * input_shape[2]) x2 = int((cx + w / 2) * input_shape[3]) y2 = int((cy + h / 2) * input_shape[2]) class_ids.append(class_id) confidences.append(confidence) boxes.append([x1, y1, x2, y2]) indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_th, iou_th) results = [] for i in indices: i = i[0] class_id = class_ids[i] confidence = confidences[i] box = boxes[i] results.append((class_id, confidence, box)) return results # 打开摄像头并进行检测 cap = cv2.VideoCapture(0) while True: ret, image = cap.read() image = preprocess(image) cuda.memcpy_htod_async(input_binding[0]['array'], image, cuda.Stream()) context.execute_async_v2(bindings=[int(i['array']) for i in input_binding] + [int(i['array']) for i in output_binding], stream_handle=cuda.Stream()) outputs = [cuda.memcpy_dtoh_async(output['array'], cuda.Stream()) for output in output_binding] results = postprocess(outputs) for result in results: class_id, confidence, box = result x1, y1, x2, y2 = box cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2) cv2.putText(image, f'{class_id}: {confidence:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) cv2.imshow('image', image) if cv2.waitKey(1) == ord('q'): break cap.release() cv2.destroyAllWindows() ``` 在代码中,我们首先使用TensorRT API加载了TensorRT模型,并定义了输入和输出张量。然后,我们定义了预处理和后处理函数,用于将输入图像转换为模型需要的格式,并将模型输出转换为物体检测结果。最后,我们使用OpenCV打开摄像头并进行实时检测,将检测结果显示在屏幕上。 4. 运行程序 保存代码文件并在Jetson Nano上运行它。程序将打开摄像头并实时检测摄像头中的物体。可以使用以下命令运行程序: ``` python3 detect.py ``` 注意,在首次运行程序时,由于需要编译TensorRT模型,程序可能会需要一些时间来启动。运行程序后,可以通过按下“q”键来退出程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值