当前位置: 首页 > news >正文

使用YOLOv11进行视频目标检测

使用YOLOv11进行视频目标检测
完整代码

import cv2
from ultralytics import YOLOdef predict(chosen_model, img, classes=[], conf=0.5):if classes:results = chosen_model.predict(img, classes=classes, conf=conf)else:results = chosen_model.predict(img, conf=conf)return resultsdef predict_and_detect(chosen_model, img, classes=[], conf=0.5, rectangle_thickness=2, text_thickness=1):results = predict(chosen_model, img, classes, conf=conf)for result in results:for box in result.boxes:cv2.rectangle(img, (int(box.xyxy[0][0]), int(box.xyxy[0][1])),(int(box.xyxy[0][2]), int(box.xyxy[0][3])), (255, 0, 0), rectangle_thickness)cv2.putText(img, f"{result.names[int(box.cls[0])]}",(int(box.xyxy[0][0]), int(box.xyxy[0][1]) - 10),cv2.FONT_HERSHEY_PLAIN, 1, (255, 0, 0), text_thickness)return img, results# defining function for creating a writer (for mp4 videos)
def create_video_writer(video_cap, output_filename):# grab the width, height, and fps of the frames in the video stream.frame_width = int(video_cap.get(cv2.CAP_PROP_FRAME_WIDTH))frame_height = int(video_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))fps = int(video_cap.get(cv2.CAP_PROP_FPS))# initialize the FourCC and a video writer objectfourcc = cv2.VideoWriter_fourcc(*'MP4V')writer = cv2.VideoWriter(output_filename, fourcc, fps,(frame_width, frame_height))return writermodel = YOLO("yolo11x.pt")output_filename = "YourFilename.mp4"video_path = r"YourVideoPath.mp4"
cap = cv2.VideoCapture(video_path)
writer = create_video_writer(cap, output_filename)
while True:success, img = cap.read()if not success:breakresult_img, _ = predict_and_detect(model, img, classes=[], conf=0.5)writer.write(result_img)cv2.imshow("Image", result_img)cv2.waitKey(1)
writer.release()

参考资料:
1.https://blog.csdn.net/qq_42589613/article/details/142729428
2.https://blog.csdn.net/java1314777/article/details/142665078

http://www.lryc.cn/news/457060.html

相关文章:

  • DEEP和DeepBook V3将于10月14日推出
  • 学习之高阶编程列表推导式,字典推导式
  • QT实现QInputDialog中文按钮
  • Redis 常用指令技术解读
  • Web前端入门
  • 贝塞尔曲线详细讲解,如何用 Canvas 绘制三阶贝塞尔曲线?
  • Ubuntu20.04卸载ros2 foxy版本安装ros1 noetic版本
  • PicGo+Gitee搭建Typora图床
  • MySQL 脱敏函数使用详解:保护数据隐私的关键手段
  • nginx之virtual host
  • Windows 下纯手工打造 QT 开发环境
  • k8s的安装和部署
  • 第十八篇:一文说清楚ICMP的底层原理
  • 【优选算法】(第三十二篇)
  • 线程(四)线程的同步——条件变量
  • 二维数组的旋转与翻转(C++)(上(这只是简单讲解))
  • 【在Linux世界中追寻伟大的One Piece】System V共享内存
  • 【大数据】Spark弹性分布式数据集RDD详细说明
  • 人参玉桂膏简介
  • 消费者Rebalance机制
  • 消息队列介绍
  • 告别@Value,Spring Boot 3.3更优雅的配置注入方案
  • 甲虫身体图像分割系统源码&数据集分享
  • Qt - QMenu
  • 舵机驱动详解(模拟/数字 STM32)
  • dvwa:文件包含、文件上传
  • 基于 C# .NET Framework 4.0 开发实现 WCF 服务实例详解(二)——实现Windows服务内嵌WCF服务
  • 【ArcGIS/C#】调用控制台处理代码
  • 06_23 种设计模式之《适配器模式》
  • Go语言--快速入门