1. 从文件夹中读取图片文件,这步可以结合ffmpeg来完成,将视频流切图命令为(但需要再写个脚本定期清理旧图片,比如10分钟前):
ffmpeg -i myfile.avi -r 1000 -f image2 image-%07d.png
2.读取图片并圈出识别出的物体:
import os
import glob
import dlib
# Path to the video frames
video_folder = os.path.join("./video_frames")
# Create the correlation tracker - the object needs to be initialized
# before it can be used
tracker = dlib.correlation_tracker()
win = dlib.image_window()
# We will track the frames as we load them off of disk
for k, f in enumerate(sorted(glob.glob(os.path.join(video_folder, "*.jpg")))):
print("Processing Frame {}".format(k))
img = dlib.load_rgb_image(f)
# We need to initialize the tracker on the first frame
if k == 0:
# Start a track on the juice box. If you look at the first frame you
# will see that the juice box is contained within the bounding
# box (74, 67, 112, 153).
tracker.start_track(img, dlib.rectangle(74, 67, 112, 153))
else:
# Else we just attempt to track from the previous frame
tracker.update(img)
win.clear_overlay()
win.set_image(img)
win.add_overlay(tracker.get_position())
dlib.hit_enter_to_continue()