Guest User

ONNX Object Detector

a guest
Apr 23rd, 2019
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import onnxruntime as rt
  4. from skimage.transform import resize
  5.  
  6. if __name__ == "__main__":
  7.     sess = rt.InferenceSession('model.onnx')
  8.     shape = sess.get_inputs()[0].shape
  9.     output = sess.get_outputs()[0].shape
  10.     shape.pop(0)
  11.     shape = tuple(shape)
  12.     output.pop(0)
  13.  
  14.     img = cv2.imread('bill.jpg')
  15.     X = np.array(img)
  16.     X = resize(X/255, shape)
  17.     X = X[np.newaxis, :, :, :]
  18.     X = X.astype(np.float32)
  19.  
  20.     input_name = sess.get_inputs()[0].name
  21.     label_name = sess.get_outputs()[0].name
  22.     pred_onnx = sess.run([label_name], {input_name: X})
  23.     print(pred_onnx)
  24.    
  25.     while True:
  26.         gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  27.         for x, y, w, h in pred_onnx:
  28.             cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
  29.             roiGray = gray[y:y+h, x:x+w]
  30.             roiColor = img[y:y+h, x:x+w]
  31.  
  32.         cv2.imshow("Detect", cv2.resize(img, (500, 500)))
  33.         cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment