网站访问速度跟服务器cpu和内存和带宽哪个重要,h5自适应网站源码,小程序注册公司,wordpress编辑器自动标签大家好#xff0c;YOLOv9 与其前身v8一样#xff0c;专注于识别和精确定位图像和视频中的对象。本文将介绍如何使用YOLOv9进行图像与视频检测#xff0c;自动驾驶汽车、安全系统和高级图像搜索等应用在很大程度上依赖于此功能#xff0c;YOLOv9 引入了比 YOLOv8 更令人印象…大家好YOLOv9 与其前身v8一样专注于识别和精确定位图像和视频中的对象。本文将介绍如何使用YOLOv9进行图像与视频检测自动驾驶汽车、安全系统和高级图像搜索等应用在很大程度上依赖于此功能YOLOv9 引入了比 YOLOv8 更令人印象深刻的创新点。
1.安装必要的库
pip install opencv-python ultralytics2.导入库
import cv2
from ultralytics import YOLO3.选择模型型号尺寸
model YOLO(yolov9c.pt)这里我们选择yolov9c.pt大家可以选择不同的模型尺寸进行检测并比较不同的型号并权衡它们各自的优缺点。
4.编写函数预测和检测图像和视频中的对象
def predict(chosen_model, img, classes[], conf0.5):if classes:results chosen_model.predict(img, classesclasses, confconf)else:results chosen_model.predict(img, confconf)return resultsdef predict_and_detect(chosen_model, img, classes[], conf0.5, rectangle_thickness2, text_thickness1):results predict(chosen_model, img, classes, confconf)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, resultspredict() 这个函数采用三个参数 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[], conf0.5)如果要检测特定类只需在类列表classes中输入对象的 ID 号即可。
6.保存并绘制结果图像
cv2.imshow(Image, result_img)
cv2.imwrite(YourSavePath, result_img)
cv2.waitKey(0)7.使用 YOLOv9 检测视频
video_path rYourVideoPath
cap cv2.VideoCapture(video_path)
while True:success, img cap.read()if not success:breakresult_img, _ predict_and_detect(model, img, classes[], conf0.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 rYourVideoPath
cap cv2.VideoCapture(video_path)
while True:success, img cap.read()if not success:breakresult_img, _ predict_and_detect(model, img, classes[], conf0.5)writer.write(result_img)cv2.imshow(Image, result_img)cv2.waitKey(1)
writer.release()