Bootstrap SomeTools Icon
返回
"""
@description:在一段视频上标注后,保存为新文件
@class:videoDataClean
"""


import cv2
import numpy as np
from videoDataClean import VideoDataClean

# 打开视频文件或摄像头 (0 表示默认摄像头)
cap = cv2.VideoCapture("video/test.mp4")  # 或 cv2.VideoCapture(0)
paused = False  # 视频是否暂停 true--暂停状态  false--播放状态
fps_count = 0  # 某一帧操作计数
fps_index = 0  # 新视频当前帧序号  1为第一帧
# 获取视频宽高和帧率
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out_path = "video/output.avi"
out = cv2.VideoWriter(out_path, fourcc, fps, (frame_width, frame_height))
fps_list = []


# 鼠标回调函数
def draw_circle(event, x, y, flags, param):
    global frame, fps_count  # 使用当前帧
    if event == cv2.EVENT_LBUTTONDOWN:  # 左键单击绘制圆
        fps_count += 1
        cv2.circle(frame, (x, y), 30, (0, 0, 255), -1)


# 循环读取视频帧
while True:
    if not paused:  # 播放状态读取下一帧并显示,暂停状态就一直显示上一帧画面
        ret, frame = cap.read()
        fps_count = 0
        # print(cap.get(cv2.CAP_PROP_POS_FRAMES))
        if not ret:
            print("视频没有了~")
            break
    fps_index += 1
    # 原视频帧数,新视频帧数,操作计数
    fps_list.append((int(cap.get(cv2.CAP_PROP_POS_FRAMES)), fps_index, fps_count))
    cv2.imshow("show", frame)
    cv2.setMouseCallback("show", draw_circle)
    out.write(frame)
    # 键盘控制
    key = cv2.waitKey(20) & 0xFF
    if key == ord(' '):  # 空格键暂停/播放切换
        paused = not paused
    elif key == 27:  # ESC 键退出
        break

# 释放资源
cap.release()
out.release()
cv2.destroyAllWindows()

video = VideoDataClean(fps_list, out_path)
video.video_to_new()


"""
@description:接video_labeled.py
"""

import pandas as pd
import cv2
import os


class VideoDataClean:

    def __init__(self, input_data, video_path):
        self.data = input_data
        self.video_path = video_path

    def video_to_new(self):
        data = pd.DataFrame(self.data, columns=["A", "B", "C"])
        # 获取每组最大帧号
        clean_data = data.groupby("A")["B"].max()

        # 创建输出目录
        # output_dir = "video/clean"
        # os.makedirs(output_dir, exist_ok=True)

        # 处理视频帧
        cap = cv2.VideoCapture(self.video_path)
        if not cap.isOpened():
            print(f"无法打开视频文件: {self.video_path}")
            return 0

        # 获取视频属性
        frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        fps = int(cap.get(cv2.CAP_PROP_FPS))
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        new_out_path = "video/clean/output.avi"
        out = cv2.VideoWriter(new_out_path, fourcc, fps, (frame_width, frame_height))

        # 遍历帧并保存
        for i, frame_idx in enumerate(clean_data - 1):
            cap.set(cv2.CAP_PROP_POS_FRAMES, frame_idx)
            ret, frame = cap.read()
            if ret:
                print(f"处理原始帧{i},实际帧 {frame_idx}中")
                out.write(frame)
            else:
                print(f"原始帧{i},实际帧 {frame_idx}读取失败!")

        # 释放资源
        cap.release()
        out.release()
        print(f"视频已保存至:{new_out_path}")


# # 输入数据
# a = [(1, 1, 0), (2, 2, 0), (3, 3, 0), (4, 4, 0), (5, 5, 0), (6, 6, 0), (7, 7, 0), (8, 8, 0), (9, 9, 0), (10, 10, 0), (11, 11, 0), (12, 12, 0), (12, 13, 0), (12, 14, 0), (12, 15, 0), (12, 16, 0), (12, 17, 0), (12, 18, 0), (12, 19, 0), (12, 20, 0), (12, 21, 0), (12, 22, 0), (12, 23, 0), (12, 24, 1), (12, 25, 1), (12, 26, 1), (12, 27, 1), (12, 28, 1), (12, 29, 1), (12, 30, 1), (12, 31, 1), (12, 32, 1), (12, 33, 1), (12, 34, 2), (12, 35, 2), (12, 36, 2), (12, 37, 2), (12, 38, 2), (12, 39, 2), (12, 40, 2), (12, 41, 2), (12, 42, 2), (12, 43, 2), (12, 44, 2), (12, 45, 2), (12, 46, 3), (12, 47, 3), (12, 48, 3), (12, 49, 3), (12, 50, 3), (12, 51, 3), (12, 52, 3), (12, 53, 3), (12, 54, 3), (12, 55, 3), (12, 56, 3), (12, 57, 4), (12, 58, 4), (12, 59, 4), (12, 60, 4), (12, 61, 4), (12, 62, 4), (12, 63, 4), (12, 64, 4), (12, 65, 4), (12, 66, 4), (12, 67, 4), (12, 68, 4), (12, 69, 4), (12, 70, 4), (12, 71, 4), (12, 72, 4), (12, 73, 4), (12, 74, 4), (12, 75, 4), (12, 76, 4), (12, 77, 4), (12, 78, 4), (12, 79, 4), (12, 80, 4), (12, 81, 4), (12, 82, 4), (12, 83, 4), (12, 84, 4), (12, 85, 4), (12, 86, 4), (12, 87, 4), (12, 88, 4), (12, 89, 4), (12, 90, 4), (12, 91, 4), (12, 92, 4), (12, 93, 4), (12, 94, 4), (12, 95, 4), (12, 96, 4), (12, 97, 4), (12, 98, 4), (12, 99, 4), (12, 100, 4), (12, 101, 4), (12, 102, 4), (12, 103, 4), (12, 104, 4), (12, 105, 4), (12, 106, 5), (12, 107, 5), (12, 108, 5), (12, 109, 5), (12, 110, 5), (12, 111, 5), (12, 112, 5), (12, 113, 5), (12, 114, 5), (12, 115, 5), (12, 116, 5), (12, 117, 5), (12, 118, 5), (12, 119, 5), (12, 120, 5), (12, 121, 5), (12, 122, 5), (12, 123, 5), (12, 124, 5), (12, 125, 5), (12, 126, 5), (13, 127, 0), (14, 128, 0), (15, 129, 0), (16, 130, 0), (17, 131, 0), (18, 132, 0), (19, 133, 0), (20, 134, 0), (21, 135, 0), (22, 136, 0), (23, 137, 0), (24, 138, 0), (25, 139, 0), (26, 140, 0), (27, 141, 0), (28, 142, 0), (29, 143, 0), (30, 144, 0), (31, 145, 0), (32, 146, 0), (33, 147, 0), (34, 148, 0), (35, 149, 0), (36, 150, 0), (37, 151, 0)]
#
# video = VideoDataClean(a, "video/output.avi")
# video.video_to_new()