当前位置:   article > 正文

关于逐帧读取视频,并且读取视频帧图片标注

关于逐帧读取视频,并且读取视频帧图片标注
import cv2
import json
from collections import defaultdict
from collections import deque
from pynput import keyboard

frame_objects = defaultdict(list)
frame_buffer = deque(maxlen=300)
queue_index = -1
load_new_frame = True
frame_index = -1

def read_log_file(log_file_path):
    global frame_objects
    with open(log_file_path, 'r') as f:
        for line in f:
            data = json.loads(line)
            frame_index = int(data["imagePath"])
            shapes = data["shapes"]
            frame_objects[frame_index] = shapes

def on_press(key):
    global queue_index, load_new_frame
    if key == keyboard.Key.right or key == keyboard.Key.down or key == keyboard.Key.space:  # Right arrow key, Down arrow key, Space
        if queue_index < len(frame_buffer) - 1:
            queue_index += 1
        else:
            load_new_frame = True
    elif key == keyboard.Key.left or key == keyboard.Key.up:  # Left arrow key, Up arrow key
        if queue_index > 0:
            queue_index -= 1
        load_new_frame = False
    elif str(key) == "'q'":  # Press 'q' to exit
        return False

def play_video_with_annotation(log_file_path, video_file_path):
    global frame_objects, frame_buffer, queue_index, load_new_frame, frame_index
    read_log_file(log_file_path)
    cap = cv2.VideoCapture(video_file_path)

    # 启动键盘监听器
    listener = keyboard.Listener(on_press=on_press)
    listener.start()

    while cap.isOpened():
        if load_new_frame:
            ret, frame = cap.read()
            if not ret:
                print('video end')
                break

            frame_index += 1
            cv2.putText(frame, str(frame_index), (20, 70), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
            frame_buffer.append(frame)
            queue_index = len(frame_buffer) - 1
            load_new_frame = False

        if len(frame_buffer) > 0:
            view_index = frame_index - len(frame_buffer) + queue_index + 1
            imshow = frame_buffer[queue_index]
            if max(imshow.shape[:2]) > 1280:
                imshow = cv2.resize(imshow, (1280, 720))

            if view_index in frame_objects:
                shapes = frame_objects[view_index]
                for shape in shapes:
                    label = shape.get("label", "")
                    points = shape.get("points")
                    if points:
                        cv2.rectangle(imshow, tuple(points[0]), tuple(points[1]), (0, 255, 0), 2)
                        cv2.putText(imshow, label, (points[0][0], points[0][1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8,
                                    (0, 255, 0), 2)

            cv2.imshow('Video', imshow)

        key = cv2.waitKey(1)
        if key == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

def main():
    log_file_path = r'D:\GZ_renti\output.log'
    video_file_path = r'D:\GZ_renti\output.mp4'
    play_video_with_annotation(log_file_path, video_file_path)

if __name__ == "__main__":
    main()
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/547572
推荐阅读
相关标签
  

闽ICP备14008679号