一、本文介绍
Hello,各位读者, 最近会给大家发一些进阶实战的讲解 ,如何利用YOLOv11现有的一些功能进行一些实战, 让我们不仅会改进YOLOv11,也能够利用YOLOv11去做一些简单的小工作,后面我也会将这些功能利用 PyQt 或者是pyside2做一些小的界面给大家使用。
在开始之前给大家推荐一下我的专栏,本专栏每周更新3-10篇最新前沿机制 | 包括二次创新全网无重复,以及融合改进(大家拿到之后添加另外一个改进机制在你的 数据集 上实现涨点即可撰写论文),还有各种前沿顶会改进机制 |,更有包含我所有附赠的文件 (文件内集成我所有的改进机制全部注册完毕可以直接运行)和交流群和视频讲解提供给大家。
欢迎大家订阅我的专栏一起学习YOLO!
开始之前先给大家展示一下视频效果图,以下两幅动图片来自于ultralytics官方->
| Logistics | Aquaculture |
|---|---|
|
|
| Conveyor Belt Packets Counting Using Ultralytics YOLOv11 | Fish Counting in Sea using Ultralytics YOLguagua |
二、项目完整代码
帮我们将这个代码,复制粘贴到我们YOLOv11的仓库里然后创建一个py文件存放进去即可。
- import cv2
- from ultralytics import YOLO, solutions
- # Load the pre-trained YOLOv8 model
- model = YOLO("yolo11n.pt")
- # Open the video file
- cap = cv2.VideoCapture("video.mp4")
- assert cap.isOpened(), "Error reading video file"
- # Get video properties: width, height, and frames per second (fps)
- w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
- # Define points for a line or region of interest in the video frame
- line_points = [(20, 400), (1080, 400)] # Line coordinates
- # Specify classes to count, for example: person (0) and car (2)
- classes_to_count = [0, 2] # Class IDs for person and car
- # Initialize the video writer to save the output video
- video_writer = cv2.VideoWriter("object_counting_output.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
- # Initialize the Object Counter with visualization options and other parameters
- counter = solutions.ObjectCounter(
- view_img=True, # Display the image during processing
- reg_pts=line_points, # Region of interest points
- names=model.names, # Class names from the YOLO model
- draw_tracks=True, # Draw tracking lines for objects
- line_thickness=2, # Thickness of the lines drawn
- )
- # Process video frames in a loop
- while cap.isOpened():
- success, im0 = cap.read()
- if not success:
- print("Video frame is empty or video processing has been successfully completed.")
- break
- # Perform object tracking on the current frame, filtering by specified classes
- tracks = model.track(im0, persist=True, show=False, classes=classes_to_count)
- # Use the Object Counter to count objects in the frame and get the annotated image
- im0 = counter.start_counting(im0, tracks)
- # Write the annotated frame to the output video
- video_writer.write(im0)
- # Release the video capture and writer objects
- cap.release()
- video_writer.release()
- # Close all OpenCV windows
- cv2.destroyAllWindows()
三、参数解析
下面上面项目核心代码的参数解析,共有3个,能够起到作用的参数并不多,这里直接截图说明以下即可。
下面红框内的两个参数,一个为权重文件地址(可替换为自己训练的权重文件),第二个就是需要检测的视频地址,这里如果有实时性需求的是可以改成视频流的形式的。
下面红框内的参数为,视频中我们检测线的大小,和宽细已经横线在视频中的横纵坐标。
四、项目的使用教程
4.1 步骤一
我们在Yolo仓库的目录下创建一个py文件将代码存放进去,如下图所示。
4.2 步骤二
我们填好参数之后运行文件即可,此时会弹出视频框,然后就可以看到检测的效果了,我这里的视频找不到静态的没办法大家只能对付看以下。
五、本文总结
到此本文的正式分享内容就结束了,在这里给大家推荐我的YOLOV11改进有效涨点专栏,本专栏目前为新开的平均质量分98分,后期我会根据各种最新的前沿顶会进行论文复现,也会对一些老的改进机制进行补充,如果大家觉得本文帮助到你了,订阅本专栏,关注后续更多的更新~