14. rk3588自带的RKNNLite检测yolo模型(python)

       首先将文件夹~/rknpu2/runtime/RK3588/Linux/librknn_api/aarch64/下的文件librknnrt.so复制到文件夹/usr/lib/下(该文件夹下原有的文件librknnrt.so是用来测试resnet50模型的,所以要替换成yolo模型的librknnrt.so),如下图所示:

然后在文件夹/home/rpdzkj/rknn-toolkit-lit2-examples/inference_with_lite/下放入以下3个文件:

bus.jpg       best.rknn      des.py

       其中bus.jpg是要检测的图片,best.rknn是我们转换的yolov5的rknn模型,des.py是要运行的py代码,其代码如下:

# -*- coding: utf-8 -*-
# coding:utf-8import os
import urllib
import traceback
import time
import sys
import numpy as np
import cv2
import platform
from rknnlite.api import RKNNLite
from PIL import Image# Model from https://github.com/airockchip/rknn_model_zoo
RKNN_MODEL = 'best.rknn'
IMG_PATH = './bus.jpg'
DATASET = './dataset.txt'QUANTIZE_ON = True
BOX_THRESH = 0.5
OBJ_THRESH = 0.45
NMS_THRESH = 0.65
IMG_SIZE = 640CLASSES = ("car", "moto", "persons")# decice tree for rk356x/rk3588
DEVICE_COMPATIBLE_NODE = '/proc/device-tree/compatible'def get_host():# get platform and device typesystem = platform.system()machine = platform.machine()os_machine = system + '-' + machineif os_machine == 'Linux-aarch64':try:with open(DEVICE_COMPATIBLE_NODE) as f:device_compatible_str = f.read()host = 'RK3588'except IOError:print('Read device node {} failed.'.format(DEVICE_COMPATIBLE_NODE))exit(-1)else:host = os_machinereturn hostINPUT_SIZE = 640RK3588_RKNN_MODEL = 'best.rknn'def sigmoid(x):return 1 / (1 + np.exp(-x))def xywh2xyxy(x):# Convert [x, y, w, h] to [x1, y1, x2, y2]y = np.copy(x)y[:, 0] = x[:, 0] - x[:, 2] / 2  # top left xy[:, 1] = x[:, 1] - x[:, 3] / 2  # top left yy[:, 2] = x[:, 0] + x[:, 2] / 2  # bottom right xy[:, 3] = x[:, 1] + x[:, 3] / 2  # bottom right yreturn ydef process(input, mask, anchors):anchors = [anchors[i] for i in mask]grid_h, grid_w = map(int, input.shape[0:2])box_confidence = sigmoid(input[..., 4])box_confidence = np.expand_dims(box_confidence, axis=-1)box_class_probs = sigmoid(input[..., 5:])box_xy = sigmoid(input[..., :2]) * 2 - 0.5col = np.tile(np.arange(0, grid_w), grid_w).reshape(-1, grid_w)row = np.tile(np.arange(0, grid_h).reshape(-1, 1), grid_h)col = col.reshape(grid_h, grid_w, 1, 1).repeat(3, axis=-2)row = row.reshape(grid_h, grid_w, 1, 1).repeat(3, axis=-2)grid = np.concatenate((col, row), axis=-1)box_xy += gridbox_xy *= int(IMG_SIZE/grid_h)box_wh = pow(sigmoid(input[..., 2:4]) * 2, 2)box_wh = box_wh * anchorsbox = np.concatenate((box_xy, box_wh), axis=-1)return box, box_confidence, box_class_probsdef filter_boxes(boxes, box_confidences, box_class_probs):"""Filter boxes with box threshold. It's a bit different with origin yolov5 post process!# Argumentsboxes: ndarray, boxes of objects.box_confidences: ndarray, confidences of objects.box_class_probs: ndarray, class_probs of objects.# Returnsboxes: ndarray, filtered boxes.classes: ndarray, classes for boxes.scores: ndarray, scores for boxes."""boxes = boxes.reshape(-1, 4)box_confidences = box_confidences.reshape(-1)box_class_probs = box_class_probs.reshape(-1, box_class_probs.shape[-1])_box_pos = np.where(box_confidences >= OBJ_THRESH)boxes = boxes[_box_pos]box_confidences = box_confidences[_box_pos]box_class_probs = box_class_probs[_box_pos]class_max_score = np.max(box_class_probs, axis=-1)classes = np.argmax(box_class_probs, axis=-1)_class_pos = np.where(class_max_score >= OBJ_THRESH)boxes = boxes[_class_pos]classes = classes[_class_pos]scores = (class_max_score* box_confidences)[_class_pos]return boxes, classes, scoresdef nms_boxes(boxes, scores):"""Suppress non-maximal boxes.# Argumentsboxes: ndarray, boxes of objects.scores: ndarray, scores of objects.# Returnskeep: ndarray, index of effective boxes."""x = boxes[:, 0]y = boxes[:, 1]w = boxes[:, 2] - boxes[:, 0]h = boxes[:, 3] - boxes[:, 1]areas = w * horder = scores.argsort()[::-1]keep = []while order.size > 0:i = order[0]keep.append(i)xx1 = np.maximum(x[i], x[order[1:]])yy1 = np.maximum(y[i], y[order[1:]])xx2 = np.minimum(x[i] + w[i], x[order[1:]] + w[order[1:]])yy2 = np.minimum(y[i] + h[i], y[order[1:]] + h[order[1:]])w1 = np.maximum(0.0, xx2 - xx1 + 0.00001)h1 = np.maximum(0.0, yy2 - yy1 + 0.00001)inter = w1 * h1ovr = inter / (areas[i] + areas[order[1:]] - inter)inds = np.where(ovr <= NMS_THRESH)[0]order = order[inds + 1]keep = np.array(keep)return keepdef yolov5_post_process(input_data):masks = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]anchors = [[15,20], [20, 75], [28, 25], [31,136], [44,42],[53,215], [75,76], [98,421], [148,226]]#anchors = [[10, 13], [16, 30], [33, 23], [30, 61], [62, 45],#[59, 119], [116, 90], [156, 198], [373, 326]]boxes, classes, scores = [], [], []for input, mask in zip(input_data, masks):b, c, s = process(input, mask, anchors)b, c, s = filter_boxes(b, c, s)boxes.append(b)classes.append(c)scores.append(s)boxes = np.concatenate(boxes)boxes = xywh2xyxy(boxes)classes = np.concatenate(classes)scores = np.concatenate(scores)nboxes, nclasses, nscores = [], [], []for c in set(classes):inds = np.where(classes == c)b = boxes[inds]c = classes[inds]s = scores[inds]keep = nms_boxes(b, s)nboxes.append(b[keep])nclasses.append(c[keep])nscores.append(s[keep])if not nclasses and not nscores:return None, None, Noneboxes = np.concatenate(nboxes)classes = np.concatenate(nclasses)scores = np.concatenate(nscores)return boxes, classes, scoresdef draw(image, boxes, scores, classes):"""Draw the boxes on the image.# Argument:image: original image.boxes: ndarray, boxes of objects.classes: ndarray, classes of objects.scores: ndarray, scores of objects.all_classes: all classes name."""for box, score, cl in zip(boxes, scores, classes):top, left, right, bottom = boxprint('class: {}, score: {}'.format(CLASSES[cl], score))print('box coordinate left,top,right,down: [{}, {}, {}, {}]'.format(top, left, right, bottom))top = int(top)left = int(left)right = int(right)bottom = int(bottom)cv2.rectangle(image, (top, left), (right, bottom), (255, 0, 0), 2)cv2.putText(image, '{0} {1:.2f}'.format(CLASSES[cl], score),(top, left - 6),cv2.FONT_HERSHEY_SIMPLEX,0.6, (0, 0, 255), 2)def letterbox(im, new_shape=(640, 640), color=(0, 0, 0)):# Resize and pad image while meeting stride-multiple constraintsshape = im.shape[:2]  # current shape [height, width]if isinstance(new_shape, int):new_shape = (new_shape, new_shape)# Scale ratio (new / old)r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])# Compute paddingratio = r, r  # width, height ratiosnew_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1]  # wh paddingdw /= 2  # divide padding into 2 sidesdh /= 2if shape[::-1] != new_unpad:  # resizeim = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))left, right = int(round(dw - 0.1)), int(round(dw + 0.1))im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)  # add borderreturn im, ratio, (dw, dh)if __name__ == '__main__':host_name = get_host()rknn_model = RK3588_RKNN_MODELrknn_lite = RKNNLite()# load RKNN modelprint('--> Load RKNN model')ret = rknn_lite.load_rknn(rknn_model)if ret != 0:print('Load RKNN model failed')exit(ret)print('done')#####ori_img = cv2.imread('./bus.jpg')######img = cv2.cvtColor(ori_img, cv2.COLOR_BGR2RGB)# init runtime environmentprint('--> Init runtime environment')# run on RK356x/RK3588 with Debian OS, do not need specify target.ret = rknn_lite.init_runtime(core_mask=RKNNLite.NPU_CORE_0)if ret != 0:print('Init runtime environment failed')exit(ret)print('done')# Set inputsimg = cv2.imread(IMG_PATH)img, ratio, (dw, dh) = letterbox(img, new_shape=(IMG_SIZE, IMG_SIZE))img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))# Inferenceprint('--> Running model')outputs = rknn_lite.inference(inputs=[img])#np.save('./onnx_yolov5_0.npy', outputs[0])#np.save('./onnx_yolov5_1.npy', outputs[1])#np.save('./onnx_yolov5_2.npy', outputs[2])print('done')# post processinput0_data = outputs[0]input1_data = outputs[1]input2_data = outputs[2]input0_data = input0_data.reshape([3, -1]+list(input0_data.shape[-2:]))input1_data = input1_data.reshape([3, -1]+list(input1_data.shape[-2:]))input2_data = input2_data.reshape([3, -1]+list(input2_data.shape[-2:]))input_data = list()input_data.append(np.transpose(input0_data, (2, 3, 0, 1)))input_data.append(np.transpose(input1_data, (2, 3, 0, 1)))input_data.append(np.transpose(input2_data, (2, 3, 0, 1)))boxes, classes, scores = yolov5_post_process(input_data)img_1 = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)if boxes is not None:draw(img_1, boxes, scores, classes)cv2.imwrite('result.jpg', img_1)rknn_lite.release()

代码运行结果如下:

         这里没有出车,是我训练的模型问题。

        大家对以上代码是不是很熟悉,其实这部分代码的很多函数都是将onnx文件转换为rknn文件的代码,只是把它的from rknn.api import RKNN修改为from rknnlite.api import RKNNLite,并且将载入rknn模型的内容修改即可,数据的处理完全没变。

       用这个代码的好处是,我们以后对相机进行检测目标时,只需要下载一次rknn模型,后续只需要处理图片即可。

二、下面再结合lidar数据的获取,我们可以同时获取雷达数据和检测rknn模型,代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# coding:utf-8
import cv2import rospy
from sensor_msgs.msg import PointCloud2
import sensor_msgs.point_cloud2 as pc2
from std_msgs.msg import Header
from visualization_msgs.msg import Marker, MarkerArray
from geometry_msgs.msg import Point#import torch
import numpy as np
import sys
import time,datetime
print(sys.version)
#from recon_barriers_model import recon_barriers
#from pclpy import pcl
from queue import Queue
import open3d as o3dimport matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#%matplotlib#from create_date_file import data_time
import threading
import time,datetime
import os#先初始化ros,再from rknnlite.api import RKNNLite,否则会报错
rospy.init_node('lidar_node')import os
import urllib
import traceback
import sys
import numpy as np
import platform
from rknnlite.api import RKNNLite
from PIL import Image#######################################################################################################
#1.==================================rknn模型检测=====================================================#
#######################################################################################################QUANTIZE_ON = True
BOX_THRESH = 0.5
OBJ_THRESH = 0.45
NMS_THRESH = 0.65
IMG_SIZE = 640CLASSES = ("car", "moto", "persons")# decice tree for rk356x/rk3588
DEVICE_COMPATIBLE_NODE = '/proc/device-tree/compatible'def get_host():# get platform and device typesystem = platform.system()machine = platform.machine()os_machine = system + '-' + machineif os_machine == 'Linux-aarch64':try:with open(DEVICE_COMPATIBLE_NODE) as f:device_compatible_str = f.read()host = 'RK3588'except IOError:print('Read device node {} failed.'.format(DEVICE_COMPATIBLE_NODE))exit(-1)else:host = os_machinereturn hostINPUT_SIZE = 640RK3588_RKNN_MODEL = 'best.rknn'def sigmoid(x):return 1 / (1 + np.exp(-x))def xywh2xyxy(x):# Convert [x, y, w, h] to [x1, y1, x2, y2]y = np.copy(x)y[:, 0] = x[:, 0] - x[:, 2] / 2  # top left xy[:, 1] = x[:, 1] - x[:, 3] / 2  # top left yy[:, 2] = x[:, 0] + x[:, 2] / 2  # bottom right xy[:, 3] = x[:, 1] + x[:, 3] / 2  # bottom right yreturn ydef process(input, mask, anchors):anchors = [anchors[i] for i in mask]grid_h, grid_w = map(int, input.shape[0:2])box_confidence = sigmoid(input[..., 4])box_confidence = np.expand_dims(box_confidence, axis=-1)box_class_probs = sigmoid(input[..., 5:])box_xy = sigmoid(input[..., :2]) * 2 - 0.5col = np.tile(np.arange(0, grid_w), grid_w).reshape(-1, grid_w)row = np.tile(np.arange(0, grid_h).reshape(-1, 1), grid_h)col = col.reshape(grid_h, grid_w, 1, 1).repeat(3, axis=-2)row = row.reshape(grid_h, grid_w, 1, 1).repeat(3, axis=-2)grid = np.concatenate((col, row), axis=-1)box_xy += gridbox_xy *= int(IMG_SIZE/grid_h)box_wh = pow(sigmoid(input[..., 2:4]) * 2, 2)box_wh = box_wh * anchorsbox = np.concatenate((box_xy, box_wh), axis=-1)return box, box_confidence, box_class_probsdef filter_boxes(boxes, box_confidences, box_class_probs):"""Filter boxes with box threshold. It's a bit different with origin yolov5 post process!# Argumentsboxes: ndarray, boxes of objects.box_confidences: ndarray, confidences of objects.box_class_probs: ndarray, class_probs of objects.# Returnsboxes: ndarray, filtered boxes.classes: ndarray, classes for boxes.scores: ndarray, scores for boxes."""boxes = boxes.reshape(-1, 4)box_confidences = box_confidences.reshape(-1)box_class_probs = box_class_probs.reshape(-1, box_class_probs.shape[-1])_box_pos = np.where(box_confidences >= OBJ_THRESH)boxes = boxes[_box_pos]box_confidences = box_confidences[_box_pos]box_class_probs = box_class_probs[_box_pos]class_max_score = np.max(box_class_probs, axis=-1)classes = np.argmax(box_class_probs, axis=-1)_class_pos = np.where(class_max_score >= OBJ_THRESH)boxes = boxes[_class_pos]classes = classes[_class_pos]scores = (class_max_score* box_confidences)[_class_pos]return boxes, classes, scoresdef nms_boxes(boxes, scores):"""Suppress non-maximal boxes.# Argumentsboxes: ndarray, boxes of objects.scores: ndarray, scores of objects.# Returnskeep: ndarray, index of effective boxes."""x = boxes[:, 0]y = boxes[:, 1]w = boxes[:, 2] - boxes[:, 0]h = boxes[:, 3] - boxes[:, 1]areas = w * horder = scores.argsort()[::-1]keep = []while order.size > 0:i = order[0]keep.append(i)xx1 = np.maximum(x[i], x[order[1:]])yy1 = np.maximum(y[i], y[order[1:]])xx2 = np.minimum(x[i] + w[i], x[order[1:]] + w[order[1:]])yy2 = np.minimum(y[i] + h[i], y[order[1:]] + h[order[1:]])w1 = np.maximum(0.0, xx2 - xx1 + 0.00001)h1 = np.maximum(0.0, yy2 - yy1 + 0.00001)inter = w1 * h1ovr = inter / (areas[i] + areas[order[1:]] - inter)inds = np.where(ovr <= NMS_THRESH)[0]order = order[inds + 1]keep = np.array(keep)return keepdef yolov5_post_process(input_data):masks = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]anchors = [[15,20], [20, 75], [28, 25], [31,136], [44,42],[53,215], [75,76], [98,421], [148,226]]#anchors = [[10, 13], [16, 30], [33, 23], [30, 61], [62, 45],#[59, 119], [116, 90], [156, 198], [373, 326]]boxes, classes, scores = [], [], []for input, mask in zip(input_data, masks):b, c, s = process(input, mask, anchors)b, c, s = filter_boxes(b, c, s)boxes.append(b)classes.append(c)scores.append(s)boxes = np.concatenate(boxes)boxes = xywh2xyxy(boxes)classes = np.concatenate(classes)scores = np.concatenate(scores)nboxes, nclasses, nscores = [], [], []for c in set(classes):inds = np.where(classes == c)b = boxes[inds]c = classes[inds]s = scores[inds]keep = nms_boxes(b, s)nboxes.append(b[keep])nclasses.append(c[keep])nscores.append(s[keep])if not nclasses and not nscores:return None, None, Noneboxes = np.concatenate(nboxes)classes = np.concatenate(nclasses)scores = np.concatenate(nscores)return boxes, classes, scoresdef draw(image, boxes, scores, classes):"""Draw the boxes on the image.# Argument:image: original image.boxes: ndarray, boxes of objects.classes: ndarray, classes of objects.scores: ndarray, scores of objects.all_classes: all classes name."""for box, score, cl in zip(boxes, scores, classes):top, left, right, bottom = boxprint('class: {}, score: {}'.format(CLASSES[cl], score))print('box coordinate left,top,right,down: [{}, {}, {}, {}]'.format(top, left, right, bottom))top = int(top)left = int(left)right = int(right)bottom = int(bottom)cv2.rectangle(image, (top, left), (right, bottom), (255, 0, 0), 2)cv2.putText(image, '{0} {1:.2f}'.format(CLASSES[cl], score),(top, left - 6),cv2.FONT_HERSHEY_SIMPLEX,0.6, (0, 0, 255), 2)def letterbox(im, new_shape=(640, 640), color=(0, 0, 0)):# Resize and pad image while meeting stride-multiple constraintsshape = im.shape[:2]  # current shape [height, width]if isinstance(new_shape, int):new_shape = (new_shape, new_shape)# Scale ratio (new / old)r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])# Compute paddingratio = r, r  # width, height ratiosnew_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1]  # wh paddingdw /= 2  # divide padding into 2 sidesdh /= 2if shape[::-1] != new_unpad:  # resizeim = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))left, right = int(round(dw - 0.1)), int(round(dw + 0.1))im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)  # add borderreturn im, ratio, (dw, dh)host_name = get_host()
rknn_model = RK3588_RKNN_MODELrknn_lite = RKNNLite()# load RKNN model
print('--> Load RKNN model')
ret = rknn_lite.load_rknn(rknn_model)
if ret != 0:print('Load RKNN model failed')exit(ret)
print('done')# init runtime environment
print('--> Init runtime environment')# run on RK356x/RK3588 with Debian OS, do not need specify target.
ret = rknn_lite.init_runtime(core_mask=RKNNLite.NPU_CORE_0)
if ret != 0:print('Init runtime environment failed')exit(ret)
print('done')#######################################################################################################
#1.==================================rknn模型检测=====================================================#
##############################################################################################################################################################################################################
#2.==================================雷达数据获取=====================================================#
########################################################################################################2.打开相机
cap = cv2.VideoCapture("/dev/video61")#1.根据时间自动创建文件夹
def data_time(root_path="img_lidar_save/"):# 1.鑾峰彇褰撳墠鏃堕棿瀛楃涓叉垨鏃堕棿鎴筹紙閮藉彲绮剧‘鍒板井绉掞級start_time=datetime.datetime.now().strftime(f'%Y-%m-%d %H:%M:%S{r".%f"}')times=start_time.split(" ")# 2.data_files锛氭牴鎹棩鏈熻幏鍙栬鍒涘缓鐨勬枃浠跺す鍚嶇О锛屾瘮濡備粖澶╂槸2023_12_07data_files=times[0]#3.鑾峰彇鏂囦欢澶硅矾寰勶細img_lidar_save/2023_12_07file_path=root_path+data_filescamera_file = file_path + "/" + "camera_data"lidar_file = file_path + "/" + "lidar_data"#4.濡傛灉浠婂ぉ杩樻病鏈夋枃浠跺す锛屽垯鍒涘缓鏂囦欢澶?鏂囦欢澶瑰悕绉颁负 2023_12_07if not os.path.exists(file_path):os.makedirs(file_path)#5.寤虹珛camera鍜宭idar鏂囦欢澶癸紝瀛樺彇鍚勮嚜鐨勬暟鎹?if not os.path.exists(camera_file):os.makedirs(camera_file)if not os.path.exists(lidar_file):os.makedirs(lidar_file)#6.寤虹珛鍚勮嚜鐨勫瓨鍙栧浘鐗囧拰瑙嗛鐨勬枃浠跺すimg_file=camera_file+ "/" +"image"vedios=camera_file+ "/" +"vedios"lidar_videos=lidar_file +"/" +"vedios"lidar_pcd=lidar_file +"/" +"image"if not os.path.exists(img_file):os.makedirs(img_file)if not os.path.exists(vedios):os.makedirs(vedios)if not os.path.exists(lidar_videos):os.makedirs(lidar_videos)if not os.path.exists(lidar_pcd):os.makedirs(lidar_pcd)return img_file,vedios,lidar_videos,lidar_pcd#1.聚类的数据处理
def cluster(points, radius=0.2):"""points: pointcloudradius: max cluster range"""items = []while len(points)>1:item = np.array([points[0]])base = points[0]points = np.delete(points, 0, 0)distance = (points[:,0]-base[0])**2+(points[:,1]-base[1])**2+(points[:,2]-base[2])**2infected_points = np.where(distance <= radius**2)item = np.append(item, points[infected_points], axis=0)border_points = points[infected_points]points = np.delete(points, infected_points, 0)while len(border_points) > 0:border_base = border_points[0]border_points = np.delete(border_points, 0, 0)border_distance = (points[:,0]-border_base[0])**2+(points[:,1]-border_base[1])**2border_infected_points = np.where(border_distance <= radius**2)item = np.append(item, points[border_infected_points], axis=0)border_points = points[border_infected_points]points = np.delete(points, border_infected_points, 0)items.append(item)return items#2.保存点云
def save_pointcloud(pointcloud_np, file_name="pointcloud.pcd"):point_cloud_o3d = o3d.geometry.PointCloud()point_cloud_o3d.points = o3d.utility.Vector3dVector(pointcloud_np[:, 0:3])o3d.io.write_point_cloud(file_name, point_cloud_o3d, write_ascii=False, compressed=True)#3.点云数据的处理
def lidars(msg,pcd_path):#4.点云数据的获取pcl_msg = pc2.read_points(msg, skip_nans=False, field_names=("x", "y", "z", "intensity","ring"))#5.点云数据的过滤np_p_2 = np.array(list(pcl_msg), dtype=np.float32)#print(np_p_2)#6.将过滤后的点云数据保存为pcd文件#print(pcd_path)save_pointcloud(np_p_2, file_name=pcd_path)#7.根据条件过滤点云数据ss=np.where([s[0]>2 and s[1]<3 and s[-1]>-3 and s[2]>-0.5 for s in np_p_2])ans=np_p_2[ss]#8.点云的聚类算法 item=cluster(ans, radius=0.2)m_item=[]#9.求每个类的均值,之后与目标进行匹配#hh=np.where([s.shape[0]>20 for s in item])#print("//",len(hh),len(item))for items in item:#print("..............",items.shape)#x,y,z=int(items[:,:1].sum().mean())                          x,y,z,r=items[:,:1].mean(),items[:,1:2].mean(),items[:,2:3].mean(),items[:,3:4].mean()m_item.append([x,y,z]) #4.相机图片数据的处理
def images(frame,jpg_path):#1.保存jpg文件cv2.imwrite(jpg_path,frame)#5.对相机图片和点云数据的汇总处理
def velo_callback(msg):#1.自动生成当天保存文件的文件夹及保存文件的路径#img_file:存放图片的路径(jpg或者png文件)#vedios:存放相机视频的路径(mp4文件)#lidar_videos:存放雷达视频的路径(bag文件)#lidar_pcd:存放点云数据的pcd文件的视频(pcd文件)img_file,vedios,lidar_videos,lidar_pcd=data_time(root_path="img_lidar_save/")vedio_time=day_time()vedio_path=vedios+"/"+vedio_time+".avi"lidar_path=lidar_videos+"/"+vedio_time+".bag"ret, frame = cap.read()frame = cv2.rotate(frame, 0, dst=None)  # 视频是倒着的,要对视频进行两次90度的翻转frame = cv2.rotate(frame, 0, dst=None)  # 视频是倒着的,要对视频进行两次90度的翻转########################################################################################################1.==================================rknn模型检测=====================================================########################################################################################################img, ratio, (dw, dh) = letterbox(frame, new_shape=(IMG_SIZE, IMG_SIZE))img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))# Inferenceprint('--> Running model')outputs = rknn_lite.inference(inputs=[img])#np.save('./onnx_yolov5_0.npy', outputs[0])#np.save('./onnx_yolov5_1.npy', outputs[1])#np.save('./onnx_yolov5_2.npy', outputs[2])print('done')# post processinput0_data = outputs[0]input1_data = outputs[1]input2_data = outputs[2]input0_data = input0_data.reshape([3, -1]+list(input0_data.shape[-2:]))input1_data = input1_data.reshape([3, -1]+list(input1_data.shape[-2:]))input2_data = input2_data.reshape([3, -1]+list(input2_data.shape[-2:]))input_data = list()input_data.append(np.transpose(input0_data, (2, 3, 0, 1)))input_data.append(np.transpose(input1_data, (2, 3, 0, 1)))input_data.append(np.transpose(input2_data, (2, 3, 0, 1)))boxes, classes, scores = yolov5_post_process(input_data)img_1 = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)if boxes is not None:draw(img_1, boxes, scores, classes)cv2.imwrite('result.jpg', img_1)cv2.imshow("src_image", img_1)cv2.waitKey(1)########################################################################################################1.==================================rknn模型检测=====================================================#########################################################################################################显示视频#cv2.imshow("src_image", frame)#cv2.waitKey(1)#3.获取实时时间,作为保存jpg和pcd文件的名称now_time=day_time()print("------>",now_time)#4.获得存取pcd文件和jpg文件的路径pcd_path=lidar_pcd+"/"+now_time+".pcd"jpg_path=img_file+"/"+now_time+".jpg"#4.lidar和images数据的处理及保存,两个函数放在两个线程同时运行lidars(msg,pcd_path)#lidar数据的处理及保存images(frame,jpg_path)#images数据的处理及保存#根据时间给jpg和pcd文件命名
def day_time():start_time=datetime.datetime.now().strftime(f'%Y-%m-%d %H:%M:%S{r".%f"}')times=start_time.split(" ")mins=times[1].split(":")day_names=mins[0]+"_"+mins[1]+"_"+mins[2][:2]+"_"+mins[2][3:5]return day_namesif __name__ == '__main__':sub_ = rospy.Subscriber("livox/lidar", PointCloud2,velo_callback)print("ros_node has started!")rospy.spin()rknn_lite.release()

         注意,在该文件的同栏目需要建立一个文件夹 img_lidar_save,这个文件夹下面保存相机的jpg文件和pcd文件。效果如下:

  

以上是根据当天的实际时间自动保存的jig和pcd文件。下面是rknn模型检测的结果。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/693379.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Python爬虫】requests库get和post方法使用

requests库是一个常用于http请求的模块&#xff0c;性质是和urllib&#xff0c;urllib2是一样的&#xff0c;作用就是向指定目标网站的后台服务器发起请求&#xff0c;并接收服务器返回的响应内容。 1. 安装requests库 使用pip install requests安装 如果再使用pip安装python…

kali linux出现添加源无法更新的问题:更新时显示签名无效和没有数字签名

kali linux更新源时显示签名无效和没有数字签名 一、出现显示签名无效和没有数字签名二、 解决办法三、几种开源镜像站 一、出现显示签名无效和没有数字签名 原因&#xff1a;因为没有下载签名&#xff0c;所以显示签名无效和没有数字签名 二、 解决办法 wget archive.kali.o…

OpenCV边缘检测与视频读写

原理 OpenCV中的边缘检测原理主要基于图像梯度的计算&#xff0c;包括一阶梯度和二阶梯度。 一阶梯度&#xff1a;它反映了图像亮度变化的速度。Sobel算法就是一种以一阶梯度为基础的边缘检测算法。它通过计算图像在水平和垂直方向上的梯度来检测边缘。这种方法简单有效&…

命令行窗口文本复制到 Word 格式保持不变

命令行窗口文本复制到 Word 格式保持不变 References 标题栏右键 -> 编辑 -> 标记 / 全选 标题栏右键 -> 编辑 -> 复制 粘贴到 Notepad 中&#xff0c;语言栏设置对应语言&#xff0c;格式可以保持不变 复制文本粘贴到 Excel 中 选中 Excel 中文本复制&#xf…

基于qt的图书管理系统----01数据库设计

参考b站&#xff1a;视频连接 目录 1、数据库设计2、数据库增删改查2.1 book表操作2.2 user表操作2.3 record表的操作&#xff08;重点&#xff09; 3、数据表导出 1、数据库设计 使用sqlite3&#xff0c;新建一个book的表&#xff0c;并且都让主键自增 NmaeValuebookid书本…

GIN框架介绍以及使用

Gin是一个用Go语言编写的web框架。它是一个类似于martini但拥有更好性能的API框架, 由于使用了httprouter&#xff0c;速度提高了近40倍。 如果你是性能和高效的追求者, 你会爱上Gin&#xff0c;而且现在大多数企业都在使用Gin框架&#xff0c;反正学一学总没有错。 1、 GIn框…

赞:java使用easy-excel导入数据的通用模板思路

我们在项目中都会有导入导出的功能&#xff0c;这篇文章主要是讲导出的&#xff0c;导入我会在另外一篇博客文章中讲解。 现在我们开始。 首先&#xff1a;需要在项目中的pom.xml中导入easy-excel的依赖 <!--使用esay-excel进行导入导出 --> <dependency> &…

实习日志26 捷通打印机交接

概要 捷通打印机的研究和使用需要下载一些软件 打印机驱动下载说明 驱动&#xff1a;选择DL-721Z 端口选择&#xff1a;USB002 智能助手&#xff1a;&#xff08;连接&#xff0c;设置打印机&#xff09; 打印机显示 rfid未校准 时可以在这里点RFID标签校验 LabelEditor编译…

unity学习(31)——跳转到角色选择界面(打勾?手滑挂错脚本)

There are 2 audio listeners in the scene. Please ensure there is always exactly one audio listener in the scene. 是因为后来创建了一个camera&#xff0c;因为camera中自带一个组件Audio Listener。所以有两个camera就有两个audio listener导致报错。 一个简单的解决…

超声波清洗机大测评!希亦、洁盟、德国ODI、苏泊尔哪款性价比高?

眼镜逐渐已经成为现在大部分都离不开的一个视线辅助&#xff0c;但是很多朋友对于眼镜的清洗从开始佩戴眼镜时&#xff0c;就没有重视起来。其实清洗眼镜的方法有很多种&#xff0c;手动清洗跟超声波清洗机&#xff0c;后者的清洗相对来说会更加方便快捷一点&#xff0c;且清洗…

计算机网络——15套接字编程

套接字编程 Socket编程 Socket编程&#xff1a;应用进程使用传输层提供的服务才能够交换报文&#xff0c;实现应用协议&#xff0c;实现应用 TCP/IP&#xff1a;应用进程使用Socket API访问传输服务 地点&#xff1a;界面上的SAP 方式&#xff1a;Socket API 目标&#xff1…

强化学习入门(Matlab2021b)-创建环境【2】

目录 1 前言2 利用step和reset函数创建自定义环境2.1 对象描述2.2 reset函数2.3 step函数2.3 构建自定义环境3 使用匿名函数传递额外的参数4 可视化检查自定义函数的输出参考链接1 前言 本文介绍如何基于MATLAB编写step、reset函数,创建自己的强化学习环境(Environment)。 使…

Windows 中文版下 MSVC 对 UTF-8 支持(避免乱码)

原文&#xff1a;https://blog.iyatt.com/?p14017 1 测试环境 我这里在 Windows 11 专业版 23H2 中文版PowerShell 7.4.1 中&#xff0c;默认的字符编码是 936 GB2312 官方的标识码解释&#xff1a;https://learn.microsoft.com/zh-cn/windows/win32/Intl/code-page-iden…

IDEA的版本控制Local Changes和settings按钮显示问题

经常用idea的小伙伴应该对标题的这两个功能不陌生&#xff0c;特别是Local Changes 周日刚开工&#xff0c;我的idea就过期了&#xff0c;索性就下载了一个2023.3.3版本的&#xff0c;安装好打开一看&#xff0c;发现Local Changes 和 settings的按钮消失了&#xff0c;虽然说…

红外光谱法(IR)应用领域宽广 全球市场增长速度加快

红外光谱法&#xff08;IR&#xff09;应用领域宽广 全球市场增长速度加快 红外光谱法&#xff08;IR&#xff09;&#xff0c;也称为红外分光光度法&#xff0c;分子吸收红外线&#xff0c;引起分子振动能级、转动能级跃迁&#xff0c;获得物质红外吸收光谱&#xff0c;测定物…

专业定制线缆厂家推荐:精工电联-小批量、多品类集成线缆定制的领航者

高品质定制线缆厂家推荐&#xff1a;精工电联-小批量、多品类集成线缆定制的领航者 在当今这个多元化、个性化的时代&#xff0c;定制化产品和服务越来越受到市场的青睐。精工电联作为高科技智能化产品及自动化设备专用连接线束和连接器配套服务商&#xff0c;致力于为高科技行…

怎么选择通配符证书?

通配符SSL证书又叫泛域名SSL证书&#xff08;Wildcard Certficates&#xff09;&#xff0c;通配符SSL证书可以保护一个域名下同级子域名&#xff0c;不限制该级子域名的数量&#xff0c;且添加新的该级子域名无需重新审核和另外付费&#xff0c;可以节省大量的时间和成本。 一…

QFormLayout 背景色设置不成功

&#xff08;图中的colour 拼错了&#xff09; layout 一定要放在QWidget 里面&#xff0c;然后设置QWidget 的背景色就好了

【C++】C++中的继承

目录 介绍&#xff1a; 一&#xff0c;继承的访问权限 二&#xff0c;基类和派生类对象赋值转换 三&#xff0c;继承中的作用域 四&#xff0c;派生类的默认成员函数 1&#xff0c;构造函数 2&#xff0c;析构函数 3&#xff0c;拷贝构造和赋值运算符 五&#xff0c;继…

GitCode配置ssh

下载SSH windows设置里选“应用” 选“可选功能” 添加功能 安装这个 坐等安装&#xff0c;安装好后可以关闭设置。 运行 打开cmd 执行如下指令&#xff0c;启动SSH服务。 net start sshd设置开机自启动 把OpenSSH服务添加到Windows自启动服务中&#xff0c;可避免每…