Python+OpenCV读取视频常见坑

# coding=utf-8

import os
from os import path
import cv2

VIDEO_PATH = "video"

for game_name in os.listdir(VIDEO_PATH):
    for video_name in os.listdir(path.join(VIDEO_PATH, game_name)):
        video_path = path.join(VIDEO_PATH, game_name, video_name)
        video_instance = cv2.VideoCapture(video_path)
        if not video_instance.isOpened():
            print(video_path, "读取失败")
        else:
            i_total_frame_num = video_instance.get(cv2.CAP_PROP_FRAME_COUNT)
            i_frame_index = 0
            while i_frame_index < i_total_frame_num:
                #set need capture frame index
                video_instance.set(cv2.CAP_PROP_POS_FRAMES, i_frame_index)
                #get current index frame
                b_is_success, current_frame = video_instance.read()
                assert b_is_success, "读取视频异常!"
                #get the frame width,heigth,and channel num
                (i_frame_height, i_frame_width, i_channel_num) = current_frame.shape

                for i_width in range(0, i_frame_width):
                    for i_height in range(0, i_frame_height):
                        #read color pixel by index [height, width]
                        current_color_list = current_frame[i_height, i_width]
                        print(current_color_list)
发布了92 篇原创文章 · 获赞 23 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/Tokyo_2024/article/details/101052574