- import numpy as np
- import re
- import math
- import random
- import cv2
- import copy
- import time
- from rknn.api import RKNN
- INPUT_SIZE = 300
- NUM_RESULTS = 1917
- NUM_CLASSES = 91
- Y_SCALE = 10.0
- X_SCALE = 10.0
- H_SCALE = 5.0
- W_SCALE = 5.0
- def expit(x):
- return 1. / (1. + math.exp(-x))
- def load_box_priors():
- box_priors_ = []
- fp = open('./box_priors.txt', 'r')
- ls = fp.readlines()
- for s in ls:
- aList = re.findall('([-+]?\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?', s)
- for ss in aList:
- aNum = float((ss[0]+ss[2]))
- box_priors_.append(aNum)
- fp.close()
- box_priors = np.array(box_priors_)
- box_priors = box_priors.reshape(4, NUM_RESULTS)
- return box_priors
- if __name__ == '__main__':
- # Create RKNN object
- rknn = RKNN()
- # Config for Model Input PreProcess
- rknn.config(channel_mean_value='128 128 128 128', reorder_channel='0 1 2')
- # Direct Load RKNN Model
- print('--> Loading model')
- rknn.load_rknn('./ssd_mobilenet_v1_coco.rknn')
- print('done')
- # Set inputs
- webcam = cv2.VideoCapture(0)
- # init runtime environment
- print('--> Init runtime environment')
- ret = rknn.init_runtime(host='rk3399pro')
- if ret != 0:
- print('Init runtime environment failed')
- exit(ret)
- print('done')
- while True:
- ref, frame = webcam.read()
- orig_img = frame
- img = copy.deepcopy(orig_img)
- img = cv2.resize(img, (INPUT_SIZE, INPUT_SIZE), interpolation=cv2.INTER_CUBIC)
- # Inference
- print('--> Running model')
- start = time.time()
- outputs = rknn.inference(inputs=[img])
- print("total time: ")
- print(time.time() - start)
- print('done')
- # print('inference result: ', outputs)
- cv2.imshow("test", orig_img)
- c= cv2.waitKey(30) & 0xff
- if c==27:
- webcam.release()
- break
- # Release RKNN Context
- rknn.release()
复制代码
qiaoqiang126 发表于 2019-1-31 14:56
rknn api 手册里有 outputs_release 每次outputs要释放,要不然的话3次之后会内存泄漏 ...
ecjon 发表于 2019-2-1 09:56
我这个是python版本哦是toolkit,c版本的我知道有
troy 发表于 2019-2-1 12:01
把RKNN的转换代码也贴出来,interface循环推理是可以用的,我们有个rknn_camera.py就是实时显示的。从你 ...
troy 发表于 2019-2-1 12:01
把RKNN的转换代码也贴出来,interface循环推理是可以用的,我们有个rknn_camera.py就是实时显示的。从你 ...
ecjon 发表于 2019-2-1 12:11
你实时的demo方便发给我参考下吗?
troy 发表于 2019-2-1 12:01
把RKNN的转换代码也贴出来,interface循环推理是可以用的,我们有个rknn_camera.py就是实时显示的。从你 ...
- import numpy as np
- import re
- import math
- import random
- import cv2
- import time
- from rknn.api import RKNN
- INPUT_SIZE = 300
- NUM_RESULTS = 1917
- NUM_CLASSES = 91
- Y_SCALE = 10.0
- X_SCALE = 10.0
- H_SCALE = 5.0
- W_SCALE = 5.0
- def expit(x):
- return 1. / (1. + math.exp(-x))
- def load_box_priors():
- box_priors_ = []
- fp = open('./box_priors.txt', 'r')
- ls = fp.readlines()
- for s in ls:
- aList = re.findall('([-+]?\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?', s)
- for ss in aList:
- aNum = float((ss[0]+ss[2]))
- box_priors_.append(aNum)
- fp.close()
- box_priors = np.array(box_priors_)
- box_priors = box_priors.reshape(4, NUM_RESULTS)
- return box_priors
- if __name__ == '__main__':
- # Create RKNN object
- rknn = RKNN()
- # Direct Load RKNN Model
- print('--> Loading model')
- rknn.load_rknn('./ssd_mobilenet_v1_coco.rknn')
- # init runtime environment
- print('--> Init runtime environment')
- ret = rknn.init_runtime(host='rk3399pro')
- if ret != 0:
- print('Init runtime environment failed')
- exit(ret)
- print('done')
- print('--> Running model')
-
- webcam = cv2.VideoCapture(0)
- while True:
- startTime = time.time()
- # Set inputs
- ret, orig_img = webcam.read()
- if ret == True:
- img = cv2.cvtColor(orig_img, cv2.COLOR_BGR2RGB)
- img = cv2.resize(img, (INPUT_SIZE, INPUT_SIZE), interpolation=cv2.INTER_CUBIC)
-
- # Inference
- outputs = rknn.inference(inputs=[img])
- print('done')
-
- predictions = outputs[1].reshape((1, NUM_RESULTS, 4))
- outputClasses = outputs[0].reshape((1, NUM_RESULTS, NUM_CLASSES))
- box_priors = load_box_priors()
- # Post Process
- for i in range(0, NUM_RESULTS):
- ycenter = predictions[0][i][0] / Y_SCALE * box_priors[2][i] + box_priors[0][i]
- xcenter = predictions[0][i][1] / X_SCALE * box_priors[3][i] + box_priors[1][i]
- h = math.exp(predictions[0][i][2] / H_SCALE) * box_priors[2][i]
- w = math.exp(predictions[0][i][3] / W_SCALE) * box_priors[3][i]
- ymin = ycenter - h / 2.
- xmin = xcenter - w / 2.
- ymax = ycenter + h / 2.
- xmax = xcenter + w / 2.
- predictions[0][i][0] = ymin
- predictions[0][i][1] = xmin
- predictions[0][i][2] = ymax
- predictions[0][i][3] = xmax
- topClassScore = -1000
- topClassScoreIndex = -1
- # Skip the first catch-all class.
- for j in range(1, NUM_CLASSES):
- score = expit(outputClasses[0][i][j])
- if score > topClassScore:
- topClassScoreIndex = j
- topClassScore = score
- if topClassScore > 0.4:
- detection = (
- predictions[0][i][1] * INPUT_SIZE,
- predictions[0][i][0] * INPUT_SIZE,
- predictions[0][i][3] * INPUT_SIZE,
- predictions[0][i][2] * INPUT_SIZE)
- xmin = detection[0]
- ymin = detection[1]
- xmax = detection[2]
- ymax = detection[3]
- # print("%d @ (%d, %d) (%d, %d) score=%f" % (topClassScoreIndex, xmin, ymin, xmax, ymax, topClassScore))
- cv2.rectangle(orig_img, (int(xmin), int(ymin)), (int(xmax), int(ymax)),
- (random.random()*255, random.random()*255, random.random()*255), 3)
- print(time.time() - startTime)
- cv2.imshow('ssd-mobilenet',orig_img)
-
- if cv2.waitKey(1) == ord('q'):
- webcam.release()
- rknn.release()
- break
复制代码
karbon 发表于 2019-2-9 19:05
楼主后来解决这个问题了吗
yhc 发表于 2019-2-13 15:56
可以更新rknn-toolkit的新版本试试,旧版本好像有这个bug
slim 发表于 2019-2-14 07:47
I have the same problem and I'm running v0.9.7. Where can I find the latest version? Thanks.
欢迎光临 Toybrick (https://t.rock-chips.com/) | Powered by Discuz! X3.3 |