实现YOLOv5封装成函数以供其他程序进行调用

import detect
detect.UAPI(source="data/images")

通过在YOLOv5中的detect.py的代码中,对检测函数进行封装,之后其他代码通过已经封装好的函数进行调用,从而实现简单便捷的YOLOv5代码调用。

代码的主要修改部分就是如何detect.py中的配置代码封装到调用的函数中:

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--weights', nargs='+', type=str, default='runs/train/exp/weights/best.pt', help='model.pt path(s)')
    parser.add_argument('--source', type=str, default='data/images', help='source')  # file/folder, 0 for webcam
    parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
    parser.add_argument('--conf-thres', type=float, default=0.25, help='object confidence threshold')
    parser.add_argument('--iou-thres', type=float, default=0.45, help='IOU threshold for NMS')
    parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
    parser.add_argument('--view-img', action='store_true', help='display results')
    parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
    parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
    parser.add_argument('--nosave', action='store_true', help='do not save images/videos')
    parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --class 0, or --class 0 2 3')
    parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
    parser.add_argument('--augment', action='store_true', help='augmented inference')
    parser.add_argument('--update', action='store_true', help='update all models')
    parser.add_argument('--project', default='runs/detect', help='save results to project/name')
    parser.add_argument('--name', default='exp', help='save results to project/name')
    parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
    opt = parser.parse_args()
    print(opt)
    check_requirements(exclude=('pycocotools', 'thop'))

对于这部分我们可以直接进行修改

主要部分就是--source参数


        source=ROOT / 'data/images',  # file/dir/URL/glob/screen/0(webcam)

剩余的代码基本可以使用detect.py中默认的detect中的文件

之后其他代码甚至可以删除

对文件夹中的图片进行检测可实现的结果:

前提是视频文件夹下有图片,或者直接指定某个图片

对文件夹中的视频进行检测

只需要把相应的图片文件夹改为视频文件夹即可

前提是视频文件夹下有视频文件,或者直接指定某个视频

import detect
detect.UAPI(source="data/video")

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在YOLOv5程序调用realsense的接口,您需要使用pyrealsense2库和OpenCV库。以下是一些步骤: 1. 安装pyrealsense2库和OpenCV库。您可以使用pip命令来安装它们: ``` pip install pyrealsense2 opencv-python ``` 2. 初始化RealSense相机,并设置所需的参数: ``` import pyrealsense2 as rs # Initialize RealSense camera pipeline = rs.pipeline() config = rs.config() # Set the resolution and frame rate of the camera config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30) config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30) # Start the camera pipeline.start(config) ``` 3. 在YOLOv5程序中读取相机帧,并进行目标检测: ``` import cv2 # Read frames from RealSense camera while True: # Wait for a new frame frames = pipeline.wait_for_frames() # Get the depth and color frames depth_frame = frames.get_depth_frame() color_frame = frames.get_color_frame() # Convert the color frame to a numpy array color_image = np.asanyarray(color_frame.get_data()) # Run object detection on the color image using YOLOv5 results = detect_objects(color_image) # Draw bounding boxes around the detected objects for result in results: xmin, ymin, xmax, ymax = result['bbox'] cv2.rectangle(color_image, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2) # Display the resulting image cv2.imshow('RealSense', color_image) cv2.waitKey(1) ``` 4. 最后,记得在程序结束时停止相机: ``` # Stop the RealSense camera pipeline.stop() ``` 这样,您就可以功在YOLOv5程序调用realsense的接口了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值