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

数字博物馆网站建设内容e点互动网站

数字博物馆网站建设内容,e点互动网站,物流网站建设目标,网站改版索引量下降大家好,YOLOv9 与其前身v8一样,专注于识别和精确定位图像和视频中的对象。本文将介绍如何使用YOLOv9进行图像与视频检测,自动驾驶汽车、安全系统和高级图像搜索等应用在很大程度上依赖于此功能,YOLOv9 引入了比 YOLOv8 更令人印象…

大家好,YOLOv9 与其前身v8一样,专注于识别和精确定位图像和视频中的对象。本文将介绍如何使用YOLOv9进行图像与视频检测,自动驾驶汽车、安全系统和高级图像搜索等应用在很大程度上依赖于此功能,YOLOv9 引入了比 YOLOv8 更令人印象深刻的创新点。

1.安装必要的库

pip install opencv-python ultralytics

2.导入库

import cv2
from ultralytics import YOLO

3.选择模型型号尺寸

model = YOLO("yolov9c.pt")

这里我们选择yolov9c.pt,大家可以选择不同的模型尺寸进行检测,并比较不同的型号并权衡它们各自的优缺点。

4.编写函数预测和检测图像和视频中的对象

def 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

predict() 这个函数采用三个参数:

  • chosen_model :用于预测的训练模型

  • img :要进行预测的图像

  • classes :(可选)要将预测筛选到的类名列表

  • conf :(可选)要考虑的预测的最小置信度阈值

函数首先检查是否提供classes参数。如果是,则使用classes参数调用该chosen_model.predict() 方法,该参数仅将预测筛选为这些类。否则,将调用该 chosen_model.predict() 方法时不带 classes 参数,该参数将返回所有预测。

conf 参数用于筛选出置信度分数低于指定阈值的预测。这对于消除误报很有用。

该函数返回预测结果列表,其中每个结果都包含以下信息:

  • name :预测类的名称

  • conf :预测的置信度分数

  • box :预测对象的边界框

predict_and_detect() 函数采用与 predict() 函数相同的参数,但除了预测结果外,它还返回带注释的图像。

该函数首先调用该 predict() 函数以获取预测结果。然后,它循环访问预测结果,并在每个预测对象周围绘制一个边界框。预测类的名称也写在边界框上方。

该函数返回一个包含带注释的图像和预测结果的元组。

以下是这两个函数之间差异的摘要:

  • predict() 函数仅返回预测结果,而该 predict_and_detect() 函数还返回带注释的图像。

  • predict_and_detect() 函数是 predict() 函数的包装器,这意味着它在内部调用函数 predict()

5.使用 YOLOv9 检测图像

# read the image
image = cv2.imread("YourImagePath")
result_img, _ = predict_and_detect(model, image, classes=[], conf=0.5)

如果要检测特定类,只需在类列表classes中输入对象的 ID 号即可。

6.保存并绘制结果图像

cv2.imshow("Image", result_img)
cv2.imwrite("YourSavePath", result_img)
cv2.waitKey(0)

7.使用 YOLOv9 检测视频

video_path = r"YourVideoPath"
cap = cv2.VideoCapture(video_path)
while True:success, img = cap.read()if not success:breakresult_img, _ = predict_and_detect(model, img, classes=[], conf=0.5)cv2.imshow("Image", result_img)cv2.waitKey(1)

8.保存结果视频

# 定义保存函数
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))#初始化fourcc = cv2.VideoWriter_fourcc(*'MP4V')writer = cv2.VideoWriter(output_filename, fourcc, fps,(frame_width, frame_height))return writer

只需使用上面的函数和代码即可:

output_filename = "YourFilename"
writer = create_video_writer(cap, output_filename)video_path = r"YourVideoPath"
cap = cv2.VideoCapture(video_path)
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()
http://www.yayakq.cn/news/532197/

相关文章:

  • 网站建设的重点是什么天猫网站设计特点
  • 清远网站建设公司有没有外包活的网站
  • 电商网站建设目的wordpress数据库替换
  • 邢台网站建设最新报价php网站建设的公司
  • 智慧团建网站什么时候维护好网络推广seo怎么做
  • 高端品牌网站建设九五网络外国网站界面
  • 专门做鞋子的网站吗中国建设银行网站会员可以改名
  • 免费个人建站系统网络营销的特点主要包括
  • 电子商务网站推广实训心得北京百度关键词优化
  • 网站开发容易做吗建设网站的产品规划
  • 深圳哪里有网站建设如何查看网站的关键词
  • wordpress 点评类网站成都定制公交app
  • 广西网站建设专门做销售招聘网站
  • 北京手机网站北京建网站找哪个公司
  • 网站开发 前端 后端 如何结合网站建设分期收费
  • 渭南网站开发开发板组装实验报告
  • 如何选择网站营销公司建网站 温州
  • 南京网站建设公司排名德州中文网站建设
  • 网站设计论文框架修改wordpress后台登录
  • 影响网站速度的因素桐乡市住房建设局网站公示
  • 番禺网站制作技术做网站西美花街
  • 工信和信息化部网站保定网站建设技术支持
  • 个人博客网站开发历程小影wordpress主题
  • 夏天做哪个网站能致富精美个人网站
  • 域名对网站有什么影响吗做设计什么兼职网站
  • 青海网站 建设网站空间登陆
  • 如何给网站做快速排名素材下载网站模板
  • 03340网站建设与管理wordpress 缩略图 剪裁 位置
  • 网站基本代码建设网站元素搜索引擎
  • 关于建设工程的网站做网站在图片里加文字