将视频转换为 3 值数组

Converting Video into 3 Value Array

我需要编写一个 Python 脚本,将视频转换为 json 数组。最终产品将如下所示:

// first frame:
arr1 = [(251, 135, 0), (127, 5, 860), etc]
arr2 = [(251, 135, 0), (127, 5, 860), etc]
arr3 = [(251, 135, 0), (127, 5, 860), etc]

每个数组代表视频的一帧,而每个子数组代表视频的一个像素,以RGB值存储。有谁知道允许您这样做的图书馆吗?任何帮助将不胜感激!

您应该能够使用 OpenCV 来处理该文件。例如,这段代码处理我的一个 320x180 视频,并以您想要的大致格式打印每帧中的前几个像素:

# Open up and get first frame (if any).

import cv2
cap = cv2.VideoCapture("/home/pax/test320x180.mp4")
okay, frame = cap.read()
fnum = 0
print(f'# Video appears to be {len(frame[0])} x {len(frame)}')

# Process each frame.

while okay:
    fnum += 1
    sep = ' ['
    print(f'arr{fnum} =', end='')
    limit = 3
    for row in frame:
        for pixel in row:
            print(f'{sep}({pixel[0]}, {pixel[1]}, {pixel[2]})', end='')
            sep = ', '
            limit -= 1
            if limit == 0: break
        if limit == 0: break
    if limit == 0: print(', ...', end='')
    print(']')
    okay, frame = cap.read()

其输出为:

# Video appears to be 320 x 180
arr1 = [(0, 0, 0), (0, 0, 0), (0, 0, 0), ...]
arr2 = [(157, 130, 73), (194, 167, 110), (192, 170, 100), ...]
arr3 = [(157, 130, 73), (194, 167, 110), (192, 170, 100), ...]
arr4 = [(157, 130, 73), (194, 167, 110), (192, 170, 100), ...]
arr5 = [(157, 130, 73), (194, 167, 110), (192, 170, 100), ...]
arr6 = [(157, 130, 73), (194, 167, 110), (192, 170, 100), ...]
arr7 = [(159, 129, 73), (196, 166, 110), (194, 169, 100), ...]
arr8 = [(159, 129, 73), (196, 166, 110), (194, 169, 100), ...]
arr9 = [(159, 129, 73), (196, 166, 110), (194, 169, 100), ...]
arr10 = [(159, 129, 73), (196, 166, 110), (194, 169, 100), ...]
:
arr887 = [(227, 199, 179), (230, 202, 182), (228, 204, 186), ...]
arr888 = [(227, 199, 179), (230, 202, 182), (228, 204, 186), ...]
arr889 = [(227, 201, 181), (229, 203, 183), (229, 205, 187), ...]
arr890 = [(225, 199, 179), (228, 202, 182), (228, 204, 186), ...]
arr891 = [(225, 199, 179), (228, 202, 182), (228, 204, 186), ...]

如果您愿意检查 整个 文件(或者一旦您对像素正确感到满意),只需删除所有包含 limit变量。

您可以使用 OpenCV 将每个视频帧转换为一维数组并根据您的喜好格式化并进一步处理。

import cv2

cap = cv2.VideoCapture('numbers.mp4')

while (cap.isOpened()):
    # Get a video frame
    hasFrame, frame = cap.read()


    if hasFrame == True:
        ## Convert BGR to RGB
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        print(frame.ravel())

        #print(frame.shape)
        #print(len(frame.ravel()))
        #print(frame[0][1])

    else:
        break

cap.release()

此代码将以 [R G B R G B ...] 格式为每个帧打印一个数组。首先,R G B对应第一个像素等等。