学习资源站

YOLOv11改进-进阶实战篇-利用YOLOv11进行过线统计(可用于人,车过线统计)

一、本文介绍

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文件存放进去即可。

  1. import cv2
  2. from ultralytics import YOLO, solutions
  3. # Load the pre-trained YOLOv8 model
  4. model = YOLO("yolo11n.pt")
  5. # Open the video file
  6. cap = cv2.VideoCapture("video.mp4")
  7. assert cap.isOpened(), "Error reading video file"
  8. # Get video properties: width, height, and frames per second (fps)
  9. w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
  10. # Define points for a line or region of interest in the video frame
  11. line_points = [(20, 400), (1080, 400)] # Line coordinates
  12. # Specify classes to count, for example: person (0) and car (2)
  13. classes_to_count = [0, 2] # Class IDs for person and car
  14. # Initialize the video writer to save the output video
  15. video_writer = cv2.VideoWriter("object_counting_output.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
  16. # Initialize the Object Counter with visualization options and other parameters
  17. counter = solutions.ObjectCounter(
  18. view_img=True, # Display the image during processing
  19. reg_pts=line_points, # Region of interest points
  20. names=model.names, # Class names from the YOLO model
  21. draw_tracks=True, # Draw tracking lines for objects
  22. line_thickness=2, # Thickness of the lines drawn
  23. )
  24. # Process video frames in a loop
  25. while cap.isOpened():
  26. success, im0 = cap.read()
  27. if not success:
  28. print("Video frame is empty or video processing has been successfully completed.")
  29. break
  30. # Perform object tracking on the current frame, filtering by specified classes
  31. tracks = model.track(im0, persist=True, show=False, classes=classes_to_count)
  32. # Use the Object Counter to count objects in the frame and get the annotated image
  33. im0 = counter.start_counting(im0, tracks)
  34. # Write the annotated frame to the output video
  35. video_writer.write(im0)
  36. # Release the video capture and writer objects
  37. cap.release()
  38. video_writer.release()
  39. # Close all OpenCV windows
  40. cv2.destroyAllWindows()

三、参数解析

下面上面项目核心代码的参数解析,共有3个,能够起到作用的参数并不多,这里直接截图说明以下即可。

下面红框内的两个参数,一个为权重文件地址(可替换为自己训练的权重文件),第二个就是需要检测的视频地址,这里如果有实时性需求的是可以改成视频流的形式的。

下面红框内的参数为,视频中我们检测线的大小,和宽细已经横线在视频中的横纵坐标。

四、项目的使用教程

4.1 步骤一

我们在Yolo仓库的目录下创建一个py文件将代码存放进去,如下图所示。


4.2 步骤二

我们填好参数之后运行文件即可,此时会弹出视频框,然后就可以看到检测的效果了,我这里的视频找不到静态的没办法大家只能对付看以下。


五、本文总结

到此本文的正式分享内容就结束了,在这里给大家推荐我的YOLOV11改进有效涨点专栏,本专栏目前为新开的平均质量分98分,后期我会根据各种最新的前沿顶会进行论文复现,也会对一些老的改进机制进行补充,如果大家觉得本文帮助到你了,订阅本专栏,关注后续更多的更新~