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

【Opencv】Pyhton 播放上一帧,下一帧,存video,逐帧分析

文章目录

  • 读取具体哪一帧
  • 等待按钮
  • 写入解码方式与文件格式对应
  • 全部代码

读取具体哪一帧

这个方法可以获取某一帧:


while True:cap.set(cv2.CAP_PROP_POS_FRAMES, current_frame)ret, frame = cap.read()if not ret:break

等待按钮

这个方法可以显示当前帧,然后等待你的按钮:

        # 显示当前帧cv2.imshow('Video Frame', frame)# 等待按键输入key = cv2.waitKey(0)  # 使用较短的等待时间以确保视频正常播放if key == 27:  # ESCbreakelif key == ord('q'):  # Q 键(往回跳一帧)if current_frame > 0:current_frame -= 1elif key == ord('w'):  # W 键(往前播放一帧)if current_frame < len(json_data) - 1:current_frame += 1

这个标志打开可以让你存储一个mp4视频:

FLAG_SAVE_VIDEOS = False

写入解码方式与文件格式对应

不同的视频文件格式通常需要使用不同的编解码器,因此你需要根据你要创建的视频文件格式来选择合适的四字符编码标识。以下是一些常见的视频文件格式和相应的四字符编码标识示例:

  1. H.264 编码(通常用于.mp4文件):

    fourcc = cv2.VideoWriter_fourcc(*'H264')
    
  2. XVID 编码(通常用于.avi文件):

    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    
  3. MJPG 编码(通常用于.avi文件,适用于每帧图像质量高的场景):

    fourcc = cv2.VideoWriter_fourcc(*'MJPG')
    
  4. DIVX 编码(通常用于.avi文件):

    fourcc = cv2.VideoWriter_fourcc(*'DIVX')
    
  5. VP8 编码(通常用于.webm文件):

    fourcc = cv2.VideoWriter_fourcc(*'VP80')
    
  6. VP9 编码(通常用于.webm文件):

    fourcc = cv2.VideoWriter_fourcc(*'VP90')
    

这些是一些常见的视频文件格式和相应的四字符编码标识示例。根据你的需求和所使用的视频文件格式,选择适合的编码标识以确保视频文件可以正确编码和解码。不同的视频编辑软件和播放器也支持不同的编解码器,因此你可能需要根据最终使用情况进行调整。

全部代码

import logging
import timeimport cv2
import json# 读取JSON文件
with open('../inoutdir/long.json', 'r') as f:json_data = json.load(f)# 打开视频文件
cap = cv2.VideoCapture('../inoutdir/long.mp4')current_frame = 0
# 配置日志记录
logging.basicConfig(level=logging.INFO, format='%(asctime)s [Frame %(frame)d / %(frame_all)d] %(message)s')
logger = logging.getLogger()FLAG_SAVE_VIDEOS = False
if FLAG_SAVE_VIDEOS:output_file = '../output/long_draw.mp4'fourcc = cv2.VideoWriter_fourcc(*'mp4v')frame_width = int(cap.get(3))frame_height = int(cap.get(4))out = cv2.VideoWriter(output_file, fourcc, 30, (frame_width, frame_height))# 初始化时间统计
start_time = time.time()
total_frames = len(json_data)while True:cap.set(cv2.CAP_PROP_POS_FRAMES, current_frame)ret, frame = cap.read()if not ret:break# 记录时间戳frame_timestamp = time.time()# 从JSON中获取当前帧的检测结果if current_frame < len(json_data):detections = json_data[current_frame]['dets']# 在每个检测上绘制边界框for det in detections:x1, y1, x2, y2, score, class_id = detcolor = (0, 255, 0)  # 绿色边界框label = f'{int(class_id)},{score:.2f}'cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), color, 2)# 计算文本位置以确保在框内text_x = int(x1)text_y = int(y1) - 10if text_y - 10 < 0:text_y = int(y1) + 20  # 如果文本位置超出了帧的上边界,则将其放在边界框的下方cv2.putText(frame, label, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)if FLAG_SAVE_VIDEOS:out.write(frame)  # 将当前帧写入输出视频current_frame += 1else:# 显示当前帧cv2.imshow('Video Frame', frame)# 等待按键输入key = cv2.waitKey(0)  # 使用较短的等待时间以确保视频正常播放if key == 27:  # ESCbreakelif key == ord('q'):  # Q 键(往回跳一帧)if current_frame > 0:current_frame -= 1elif key == ord('w'):  # W 键(往前播放一帧)if current_frame < len(json_data) - 1:current_frame += 1# 计算每一帧消耗的时间并记录到日志中frame_processing_time = time.time() - frame_timestamplogger.info(f'Frame processed in {frame_processing_time:.4f} seconds',extra={'frame': current_frame, 'frame_all': total_frames})# 计算总共消耗的时间
total_processing_time = time.time() - start_time
average_frame_time = total_processing_time / total_frames if total_frames > 0 else 0print(total_processing_time)
print(average_frame_time)# 释放视频文件、关闭窗口和输出视频文件
cap.release()
if FLAG_SAVE_VIDEOS:out.release()
cv2.destroyAllWindows()
http://www.lryc.cn/news/160813.html

相关文章:

  • 【关于Java:认识异常】
  • 【C++ • STL • 力扣】详解string相关OJ
  • 【Tomcat服务部署及优化】
  • C++之红黑树
  • Go语言网络编程(socket编程)TCP
  • C语言——局部和全局变量
  • 【Java基础篇 | 类和对象】--- 聊聊什么是内部类
  • 合宙Air724UG LuatOS-Air LVGL API控件-页面 (Page)
  • mongodb数据库操作
  • 第 2 章 线性表 ( 双链循环线性表(链式存储结构)实现)
  • redis在日常开发工作中的常见用法
  • 小程序实现下拉刷新
  • Day 36 贪心算法 part05 : 435. 无重叠区间 763.划分字母区间 56. 合并区间
  • 使用Python将网页数据保存到NoSQL数据库的方法和示例
  • 两个路由器如何连接设置的方法攻略
  • 分类任务评价指标
  • c++静态成员
  • go-zero直连与etcd服务注册中心
  • Kotlin File writeText appendText appendBytes readBytes readText
  • 常见缺少msvcp140.dll问题及解决方法,分享多种方法帮你解决
  • 【K210+ESP8266图传上位机开发】TCP server + JPEG图像解析上位机开发
  • Linux查看当前文件夹的大小
  • YOLO目标检测——密集人群人头数据集+已标注yolo格式标签下载分享
  • 论文精读 —— Gradient Surgery for Multi-Task Learning
  • 【VS Code插件开发】常见自定义命令(七)
  • Spring Cloud服务发现与注册的原理与实现
  • FFmpeg入门之简单介绍
  • 新版DBeaver调整编辑窗口字体大小
  • 《vue3实战》运用push()方法实现电影评价系统的添加功能
  • JavaScript学习笔记02