import cv2
from rockx import RockX
import numpy as np
BODY_DETECT_MIN_SCORE = 0.4
#存放了模型和数据
ROCKX_DATA_DIR = "/home/coolpi/RockX_SDK_V1.6.0_20220913/sdk/rockx-data-rk3588"
#实例化RockX类
handle = RockX(RockX.ROCKX_MODULE_PERSON_DETECTION, rockx_data_dir=ROCKX_DATA_DIR)
process_map = {
RockX.ROCKX_MODULE_PERSON_DETECTION : ("body", "rockx_person_detect", BODY_DETECT_MIN_SCORE),
}
#调用RockX类的rockx_person_detect方法
process = getattr(handle, "rockx_person_detect")
dataset = 'datatest/run.mp4'
cap = cv2.VideoCapture(dataset)
flag, frame = cap.read()
i = 0
det_results = []
while flag:
# 读取每一帧,flag表示是否读取成功,frame为图片内容。
flag, frame = cap.read()
# frame = cv2.pyrDown(frame)
if frame is None:
# print("No video captured")
continue
else:
width = frame.shape[1]
height = frame.shape[0]
ret, results = process(frame, width, height,RockX.ROCKX_PIXEL_FORMAT_BGR888)
converted_results = []
for result in results:
if result.score > BODY_DETECT_MIN_SCORE:
b = [result.box.top, result.box.right, result.box.bottom, result.box.left,result.score]
converted_results.append(b)
det_results.append(converted_results)
i=i+1
print(i)
save_det_results = np.array(det_results)
np.save('det_results.npy',save_det_results)
det_results=np.load('det_results.npy',allow_pickle=True)
det_results=det_results.tolist()#全是list
RockX.py的原方法如下
def rockx_person_detect(self, in_img, width, height, pixel_fmt):
"""
body detect
:param in_img: Input image(numpy ndarray)
:param width: Image width
:param height: Image height
:param pixel_fmt: Image pixel format
:return: (ret, obj_results) ret: return code; obj_results: list of RockX.Object
"""
ret, results = self.rockx_base.rockx_person_detect(in_img, width, height, pixel_fmt)
obj_results = list()
if results is not None:
for i in range(results.count):
orig_obj = results.object[i]
new_obj = RockX.Object(id=orig_obj.id,
cls_idx=orig_obj.cls_idx,
box=RockX.Rect(orig_obj.box.left, orig_obj.box.top,
orig_obj.box.right, orig_obj.box.bottom),
score=orig_obj.score)
obj_results.append(new_obj)
return ret, obj_results