ImageAI (三) 使用Python快速简单实现视频中物体检测 Video Object Detection and Tracking

前两篇已经讲解了ImageAI实现图片预测以及图片物体检测的方法,现在再来讲解一下ImageAI的第三个功能视频中的物体检测。
准备工作以及ImageAI的安装可以详见上一篇 Image Prediction: ImageAI (一)
Object Detection:ImageAI (二)

本篇github官方地址:https://github.com/OlafenwaMoses/ImageAI/blob/master/imageai/Detection/VIDEO.md


Video Object Detection and Tracking

这里它提供了三种不同的模型供我们选择:(ImageAI至少需要更新到2.0.2 后两个模型是新加的
RetinaNet (Size = 145 mb, high performance and accuracy, with longer detection time)
YOLOv3 (Size = 237 mb, moderate performance and accuracy, with a moderate detection time)
TinyYOLOv3 (Size = 34 mb, optimized for speed and moderate performance, with fast detection time)

代码如下 很简单

from imageai.Detection import VideoObjectDetection
import os
import time
#计时
start = time.time()
 
 #当前文件目录
execution_path = os.getcwd()
 
detector = VideoObjectDetection()
detector.setModelTypeAsTinyYOLOv3() #设置需要使用的模型
detector.setModelPath( os.path.join(execution_path, "yolo-tiny.h5")) #加载已经训练好的模型数据
detector.loadModel()
 
 #设置输入视频地址 输出地址 每秒帧数等
video_path = detector.detectObjectsFromVideo(input_file_path=os.path.join(execution_path, "traffic.mp4"), output_file_path=os.path.join(execution_path, "traffic_detected"), frames_per_second=20, log_progress=True)
print(video_path)
#结束计时
end = time.time()
print ("\ncost time:",end-start)

最终可以得到检测好的视频文件traffic_detected.mp4

截图如下:
1

2

3


Custom Video Object Detection 自定义视频对象检测
ImageAI 的模型(RetinaNet),可以检测 80 种不同类型的对象。包括:

person, bicycle, car, motorcycle, airplane, bus, train, truck, boat, traffic light, fire hydrant, stop_sign, parking meter, bench, bird, cat, dog, horse, sheep, cow, elephant, bear, zebra, giraffe, backpack, umbrella, handbag, tie, suitcase, frisbee, skis, snowboard, sports ball, kite, baseball bat, baseball glove, skateboard, surfboard, tennis racket, bottle, wine glass, cup, fork, knife, spoon, bowl, banana, apple, sandwich, orange, broccoli, carrot, hot dog, pizza, donot, cake, chair, couch, potted plant, bed, dining table, toilet, tv, laptop, mouse, remote, keyboard, cell phone, microwave, oven, toaster, sink, refrigerator, book, clock, vase, scissors, teddy bear, hair dryer, toothbrush.

部分代码如下:

execution_path = os.getcwd()

detector = VideoObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()

custom_objects = detector.CustomObjects(person=True, bicycle=True, motorcycle=True)

video_path = detector.detectCustomObjectsFromVideo(custom_objects=custom_objects, input_file_path=os.path.join(execution_path, "traffic.mp4"), output_file_path=os.path.join(execution_path, "traffic_custom_detected"), frames_per_second=20, log_progress=True)
print(video_path)

可以看到多了一条custom_objects = detector.CustomObjects(person=True, bicycle=True, motorcycle=True)
这里定义了需要检测的物体,person,bicycle以及motorcycle
然后在detector.detectCustomObjectsFromVideo中添加了一个参数custom_objects=custom_objects

通过这样生成的视频中就只会检测自定义的那些物体了!
截图如下:
4
可以看到上图已经不检测汽车这些物体了 只检测除了person以及motorcycle


Video Detection Speed 视频检测速度

在load的时候添加参数detection_speed

detector.loadModel(detection_speed="fast")

以下是官方提供的数据:
视频长度= 1分钟24秒,检测速度=”normal”,最小百分比概率= 50(默认值),检测时间= 29分钟3秒
视频长度= 1分钟24秒,检测速度=”fast”,最小百分比概率= 40,检测时间= 11分钟6秒
视频长度= 1分钟24秒,检测速度=”faster”,最小百分比概率= 30,检测时间= 7分47秒
视频长度= 1分钟24秒,检测速度=”fastest”,最小百分比概率= 20,检测时间= 6分钟20秒
视频长度= 1分钟24秒,检测速度=”flash”,最小百分比概率= 10,检测时间= 3分55

代码以及模型文件放在了网盘上,有需要可以自行下载
链接: https://pan.baidu.com/s/1kgvu_95U7f6AB1rJkpm9Cg 提取码: r3n6

  • 14
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
您可以使用OpenCV的cv2.Tracker()函数来实现视频物体的跟踪。以下是一个示例代码: ``` import cv2 # 读取视频 cap = cv2.VideoCapture("video.mp4") # 选择跟踪器类型 tracker_type = "CSRT" tracker = None if tracker_type == "BOOSTING": tracker = cv2.TrackerBoosting_create() elif tracker_type == "MIL": tracker = cv2.TrackerMIL_create() elif tracker_type == "KCF": tracker = cv2.TrackerKCF_create() elif tracker_type == "TLD": tracker = cv2.TrackerTLD_create() elif tracker_type == "MEDIANFLOW": tracker = cv2.TrackerMedianFlow_create() elif tracker_type == "GOTURN": tracker = cv2.TrackerGOTURN_create() elif tracker_type == "MOSSE": tracker = cv2.TrackerMOSSE_create() elif tracker_type == "CSRT": tracker = cv2.TrackerCSRT_create() # 读取第一帧并获取初始位置 success, frame = cap.read() bbox = cv2.selectROI("Tracking", frame, False) tracker.init(frame, bbox) # 实时跟踪 while True: # 读取当前帧 success, frame = cap.read() if not success: break # 更新跟踪器并获取新位置 success, bbox = tracker.update(frame) # 绘制跟踪结果 if success: x, y, w, h = [int(i) for i in bbox] cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2, 1) else: cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) # 显示跟踪结果 cv2.imshow("Tracking", frame) # 按q键退出 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放资源 cap.release() cv2.destroyAllWindows() ``` 这段代码,通过选择不同的跟踪器类型来初始化跟踪器,并用cv2.selectROI()函数获取初始位置,之后不断调用tracker.update()函数更新跟踪器并绘制跟踪结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值