YOLO 模型本身不直接支持 GIF 文件 ,因为 cv2.VideoCapture 和大多数深度学习模型只接受视频(MP4、AVI 等)或者图片序列。GIF 是多帧动画,但它不是标准的视频容器,所以不能直接用 VideoCapture 读。
方案 1:把 GIF 转成视频,用 moviepy 或 imageio 转成 MP4
from moviepy.editor import VideoFileClip
gif_path = "fight.gif"
video_path = "fight_from_gif.mp4"
clip = VideoFileClip(gif_path)
clip.write_videofile(video_path, fps=25)
方案 2:把 GIF 拆成帧
如果你只想处理帧,也可以拆帧(这种方式不生成视频,只能在内存中处理。):
from PIL import Image
import cv2
import numpy as np
gif_path = "fight.gif"
im = Image.open(gif_path)
frames = []
try:
while True:
frame = np.array(im.convert("RGB"))
frames.append(frame)
im.seek(im.tell() + 1)
except EOFError:
pass
# frames 是一个列表,每个元素是 numpy 数组
# 可以用你的 fight_model 逐帧检测
for frame in frames:
results = fight_model(frame, conf=0.6, iou=0.2)
# ...同之前处理
- GIF → 视频(推荐) → YOLO 处理
- 或 GIF → 拆帧 → YOLO 逐帧处理