-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonnx_cpu_infer.py
45 lines (43 loc) · 1.61 KB
/
onnx_cpu_infer.py
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
import onnxruntime as ort
import numpy as np
import cv2
from tqdm import tqdm
import openvino.utils as utils
utils.add_openvino_libs_to_path()
#load model
sess = ort.InferenceSession('rvm_mobilenetv3_fp16.onnx',providers=['CPUExecutionProvider'])
#rec:rnn input,downsample_ratio:1280x720=0.375,1920x1080=0.25,4k=0.125
rec = [ np.zeros([1, 1, 1, 1], dtype=np.float16) ] * 4 # Must match dtype of the model.
downsample_ratio = np.array([0.375], dtype=np.float32) # dtype always FP32
cap = cv2.VideoCapture('input.avi',cv2.CAP_FFMPEG) #using ffmpeg to read
out = cv2.VideoWriter('com.avi',cv2.VideoWriter_fourcc(*'XVID'),30,(1280,720),True)
bgr = np.array([.47,.1,.6]) #bgr:background
bgr = np.resize(bgr,[3,1,1]) #resize shape
#progress bar
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
pbar = tqdm(total = frame_count)
i = 1
#inference loop
while(True):
pbar.update(i)
ret, src = cap.read() #ret:True or False,src:frame
if ret == False:
break;
src = src.swapaxes(1,2).swapaxes(0,1) #swap dimension
src = src.astype('float16')/255 #0-255 to 0 -1
src = src.reshape([1,3,720,1280]) #BCHW shape
fgr, pha, *rec = sess.run([], {
'src': src,
'r1i': rec[0],
'r2i': rec[1],
'r3i': rec[2],
'r4i': rec[3],
'downsample_ratio': downsample_ratio
})
#compute fgr and pha to get outcome frame
com = (fgr * pha + bgr * (1 - pha))*255
com = com.reshape([3,720,1280]) #repeat converting process
com = com.astype('int8')
com = com.swapaxes(0,1).swapaxes(1,2)
out.write(com)
cap.release()