- import numpy as np
- import cv2
- import time
- from multiprocessing import Process
- from rknn.api import RKNN
- def get_time_stamp():
- ct = time.time()
- local_time = time.localtime(ct)
- data_head = time.strftime("%Y-%m-%d %H:%M:%S", local_time)
- data_secs = (ct - int(ct)) * 1000
- time_stamp = "%s.%03d" % (data_head, data_secs)
- return time_stamp
- def show_outputs(outputs):
- output = outputs[0][0]
- output_sorted = sorted(output, reverse=True)
- top5_str = 'mobilenet_v1\n-----TOP 5-----\n'
- for i in range(5):
- value = output_sorted[i]
- index = np.where(output == value)
- for j in range(len(index)):
- if (i + j) >= 5:
- break
- if value > 0:
- topi = '{}: {}\n'.format(index[j], value)
- else:
- topi = '-1: 0.0\n'
- top5_str += topi
- print(top5_str)
- def show_perfs(perfs):
- perfs = 'perfs: {}\n'.format(outputs)
- print(perfs)
- def infer_rknn(img_name, device_id):
- rknn = RKNN()
-
- # pre-process config
- print('--> config model')
- rknn.config(channel_mean_value='103.94 116.78 123.68 58.82', reorder_channel='0 1 2')
- print('done')
- rknn.load_rknn('./mobilenet_v1.rknn')
- ret = rknn.init_runtime(target='rk1808', device_id = device_id )
- if ret != 0:
- print('Init runtime environment failed')
- exit(ret)
-
- img = cv2.imread(img_name)
- img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
-
- print(device_id + '--> Running model')
- smp = get_time_stamp()
- print(device_id +':'+ smp)
-
- outputs = rknn.inference(inputs=[img])
- show_outputs(outputs)
-
- smp = get_time_stamp()
- print(device_id +':'+ smp)
-
- if __name__ == '__main__':
- p_infer1 = Process(target=infer_rknn, args=('dog1.jpg', 'TS018080190600023'))
- p_infer2 = Process(target=infer_rknn, args=('dog2.jpg', 'TS018080190600068'))
-
- p_infer1.start()
- p_infer2.start()
复制代码
结果显示:bobby_jiang 发表于 2019-8-15 09:32
两根计算棒,模型加载和推理都是多线程进行,没有所谓的先后顺序。
你看到的log先后打印是由于RKNN-toolkit ...
bobby_jiang 发表于 2019-8-15 09:32
两根计算棒,模型加载和推理都是多线程进行,没有所谓的先后顺序。
你看到的log先后打印是由于RKNN-toolkit ...
欢迎光临 Toybrick (https://t.rock-chips.com/) | Powered by Discuz! X3.3 |