自学历程15-YOLOv8使用ZED相机检测目标深度信息

前言:

        ZED相机拍照保存至指定文件夹同时提取图片对应的点云信息,YOLOv8识别目标,获取目标中心点的像素位置,对应到点云信息中获取目标的三维信息。

        能够实现YOLOv8检测现实中目标信息,并返回该目标的三维位置信息。

一、整体文件夹布局

自学历程15-YOLOv8使用ZED相机检测目标深度信息_第1张图片

三、photograph.py文件

import time
import os
import glob

import cv2
import pyzed.sl as sl

def get_next_image_name(folder_path):
    # 获取文件夹中已有图片的数字名字
    image_files = glob.glob(os.path.join(folder_path, '*.jpg'))  # 假设图片格式为jpg,可以根据实际情况修改
    existing_names = [os.path.splitext(os.path.basename(file))[0] for file in image_files]
    existing_numbers = [int(name) for name in existing_names if name.isdigit()]
    if existing_numbers:
        next_number = max(existing_numbers) + 1
    else:
        next_number = 1
    return str(next_number)

def capture(FileDirectory_ZedImage):
    # Create a Camera object
    zed = sl.Camera()

    # Create a InitParameters object and set configuration parameters
    init_params = sl.InitParameters()
    init_params.camera_resolution = sl.RESOLUTION.HD1080  # Use HD1080 video mode
    init_params.camera_fps = 30  # Set fps at 60
    init_params.depth_mode = sl.DEPTH_MODE.ULTRA
    init_params.depth_minimum_distance = 0.4
    init_params.coordinate_units = sl.UNIT.METER

    # Open the camera
    err = zed.open(init_params)
    if err != sl.ERROR_CODE.SUCCESS:
        exit(1)

    # camera settings
    zed.set_camera_settings(sl.VIDEO_SETTINGS.BRIGHTNESS, 4)
    zed.set_camera_settings(sl.VIDEO_SETTINGS.CONTRAST, 4)
    zed.set_camera_settings(sl.VIDEO_SETTINGS.HUE, 0)
    zed.set_camera_settings(sl.VIDEO_SETTINGS.SATURATION, 4)
    zed.set_camera_settings(sl.VIDEO_SETTINGS.SHARPNESS, 4)
    zed.set_camera_settings(sl.VIDEO_SETTINGS.GAMMA, 5)
    zed.set_camera_settings(sl.VIDEO_SETTINGS.AEC_AGC, 1)
    zed.set_camera_settings(sl.VIDEO_SETTINGS.WHITEBALANCE_AUTO, 1)
    #zed.set_camera_settings(sl.VIDEO_SETTINGS.EXPOSURE, 50)
    #zed.set_camera_settings(sl.VIDEO_SETTINGS.WHITE_BALANCE, 4600)

    # zed_info = zed.get_camera_information()
    # print('相机旋转矩阵R:%s' % zed_info.calibration_parameters.R)
    # print('相机变换矩阵T:%s' % zed_info.calibration_parameters.T)

    # Capture 1 frames and stop
    image = sl.Mat()
    point_cloud_mat = sl.Mat()
    # depth_image = sl.Mat()
    runtime_parameters = sl.RuntimeParameters()
    runtime_parameters.sensing_mode = sl.SENSING_MODE.FILL
    # Grab an image, a RuntimeParameters object must be given to grab()
    if zed.grab(runtime_parameters) == sl.ERROR_CODE.SUCCESS:
        # A new image is available if grab() returns SUCCESS
        zed.retrieve_image(image, sl.VIEW.LEFT)
        image_data = image.get_data()
        image_np = image_data[..., :3]  # Remove alpha channel
        cv2.imwrite(FileDirectory_ZedImage, image_np)

        # img_path = './inference/image'
        # if not os.path.exists(img_path):
        #     os.mkdir(img_path)
        # img_name = get_next_image_name(img_path)
        # file_path = os.path.join(img_path, f'{img_name}.jpg')
        # image.write(file_path)

        zed.retrieve_measure(point_cloud_mat, sl.MEASURE.XYZRGBA)
        # zed.retrieve_measure(depth_image, sl.MEASURE.DEPTH)
        # Close the camera
        zed.close()
    return point_cloud_mat

if __name__ == "__main__":
    start = time.time()
    pointCloudMat = capture()
    end = time.time()
    print("拍照耗时:" + str(end - start) + "s.")

四、yolov8_detece.py文件

from ultralytics import YOLO

def run_detection(model_path, images_folder, confidence=0.5, save_results=True):
    # 创建YOLO模型实例
    model = YOLO(model_path)
    # 运行检测
    results = model(images_folder, conf=confidence, save=save_results)
    # # 打印结果(或者您可以根据需要返回这些结果)
    # print(results)
    # return results

总结:

        实际操作可行,  能够实现YOLOv8检测现实中目标信息,并返回该目标的三维位置信息。

你可能感兴趣的:(数码相机,YOLO)