Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

demo_darknet2onnx.py 2.1 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  1. import sys
  2. import onnx
  3. import os
  4. import argparse
  5. import numpy as np
  6. import cv2
  7. import onnxruntime
  8. from tool.utils import *
  9. from tool.darknet2onnx import *
  10. def main(cfg_file, namesfile, weight_file, image_path, batch_size):
  11. if batch_size <= 0:
  12. onnx_path_demo = transform_to_onnx(cfg_file, weight_file, batch_size)
  13. else:
  14. # Transform to onnx as specified batch size
  15. transform_to_onnx(cfg_file, weight_file, batch_size)
  16. # Transform to onnx as demo
  17. onnx_path_demo = transform_to_onnx(cfg_file, weight_file, 1)
  18. session = onnxruntime.InferenceSession(onnx_path_demo)
  19. # session = onnx.load(onnx_path)
  20. print("The model expects input shape: ", session.get_inputs()[0].shape)
  21. image_src = cv2.imread(image_path)
  22. detect(session, image_src, namesfile)
  23. def detect(session, image_src, namesfile):
  24. IN_IMAGE_H = session.get_inputs()[0].shape[2]
  25. IN_IMAGE_W = session.get_inputs()[0].shape[3]
  26. # Input
  27. resized = cv2.resize(image_src, (IN_IMAGE_W, IN_IMAGE_H), interpolation=cv2.INTER_LINEAR)
  28. img_in = cv2.cvtColor(resized, cv2.COLOR_BGR2RGB)
  29. img_in = np.transpose(img_in, (2, 0, 1)).astype(np.float32)
  30. img_in = np.expand_dims(img_in, axis=0)
  31. img_in /= 255.0
  32. print("Shape of the network input: ", img_in.shape)
  33. # Compute
  34. input_name = session.get_inputs()[0].name
  35. outputs = session.run(None, {input_name: img_in})
  36. boxes = post_processing(img_in, 0.4, 0.6, outputs)
  37. class_names = load_class_names(namesfile)
  38. plot_boxes_cv2(image_src, boxes[0], savename='predictions_onnx.jpg', class_names=class_names)
  39. if __name__ == '__main__':
  40. print("Converting to onnx and running demo ...")
  41. if len(sys.argv) == 6:
  42. cfg_file = sys.argv[1]
  43. namesfile = sys.argv[2]
  44. weight_file = sys.argv[3]
  45. image_path = sys.argv[4]
  46. batch_size = int(sys.argv[5])
  47. main(cfg_file, namesfile, weight_file, image_path, batch_size)
  48. else:
  49. print('Please run this way:\n')
  50. print(' python demo_onnx.py <cfgFile> <namesFile> <weightFile> <imageFile> <batchSize>')
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...