代码拉取完成,页面将自动刷新
同步操作将从 Tianmo/pytorch-YOLOv4 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
import sys
import onnx
import os
import argparse
import numpy as np
import cv2
import onnxruntime
from tool.utils import *
from tool.darknet2onnx import *
def main(cfg_file, weight_file, image_path, batch_size):
# Transform to onnx as specified batch size
fransform_to_onnx(cfg_file, weight_file, batch_size)
# Transform to onnx for demo
onnx_path_demo = fransform_to_onnx(cfg_file, weight_file, 1)
session = onnxruntime.InferenceSession(onnx_path_demo)
# session = onnx.load(onnx_path)
print("The model expects input shape: ", session.get_inputs()[0].shape)
image_src = cv2.imread(image_path)
detect(session, image_src)
def detect(session, image_src):
IN_IMAGE_H = session.get_inputs()[0].shape[2]
IN_IMAGE_W = session.get_inputs()[0].shape[3]
# Input
resized = cv2.resize(image_src, (IN_IMAGE_W, IN_IMAGE_H), interpolation=cv2.INTER_LINEAR)
img_in = cv2.cvtColor(resized, cv2.COLOR_BGR2RGB)
img_in = np.transpose(img_in, (2, 0, 1)).astype(np.float32)
img_in = np.expand_dims(img_in, axis=0)
img_in /= 255.0
print("Shape of the network input: ", img_in.shape)
# Compute
input_name = session.get_inputs()[0].name
# output, output_exist = session.run(['decoder.output_conv', 'lane_exist.linear2'], {"input.1": image_np})
# print(img_in)
outputs = session.run(None, {input_name: img_in})
print(len(outputs))
print(outputs[0].shape)
print(outputs[1].shape)
print(outputs[2].shape)
# print(outputs[2])
num_classes = 80
boxes = post_processing(img_in, 0.5, num_classes, 0.4, outputs)
if num_classes == 20:
namesfile = 'data/voc.names'
elif num_classes == 80:
namesfile = 'data/coco.names'
else:
namesfile = 'data/names'
class_names = load_class_names(namesfile)
plot_boxes_cv2(image_src, boxes, savename='predictions_onnx.jpg', class_names=class_names)
if __name__ == '__main__':
print("Converting to onnx and running demo ...")
if len(sys.argv) == 5:
cfg_file = sys.argv[1]
weight_file = sys.argv[2]
image_path = sys.argv[3]
batch_size = int(sys.argv[4])
main(cfg_file, weight_file, image_path, batch_size)
else:
print('Please run this way:\n')
print(' python demo_onnx.py <cfgFile> <weightFile> <imageFile> <batchSize>')
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。