opencv python 多张jpg图片转成avi视频

在进行微表情识别研究时,使用到了山东大学发布的MMEW微表情数据集,这个数据集提供的数据是图片的形式。数据集在采集的时候使用的帧率是90帧,而使用图片进行连续查看的时候,帧率大概能达到10几帧,看到的面部表情的运动特别慢,不容易进行微表情的面部动作分析。

把图片合成视频后,可以通过视频软件的倍速播放功能的0.5倍速可以得到比较好的查看效果。

环境:python 3.7

opencv 4.6.0

下面是图片合成视频的代码:

# coding:utf-8
import numpy as np
import cv2
import time
import os

dir_path = r'E:\datasets\micro-expression\MMEW\Micro_Expression\anger\S17-07-001'

fps = 90.0  # 帧率
fourcc = cv2.VideoWriter_fourcc(*'XVID')  # 视频编码器
size = (231, 231)  # 视频分辨率,与原始图片保持一致,或者将图片皆resize到訪分辨率
output_full_path  = os.path.join(os.path.dirname(dir_path), os.path.basename(dir_path) + '.avi')
out = cv2.VideoWriter(output_full_path, fourcc, fps, size)  # 定义输出文件及其它参数

files = os.listdir(dir_path)
print(files)

for idx in range(1, len(files)+1):

    image_file = os.path.join(dir_path, str(idx) + '.jpg')
    frame = cv2.imread(image_file)
    out.write(frame)
    cv2.imshow('frame', frame)
    cv2.waitKey(100)
    print(image_file)

    if cv2.waitKey(25) & 0xFF == ord('q'):
        break

out.release()
cv2.destroyAllWindows()

批量合成MMEW数据集中微表情部分jpg图片为avi视频:

MMEW_jpg2avi.py

# coding:utf-8
import numpy as np
import cv2
import time
import os

mmew_dir = r'E:\datasets\micro-expression\MMEW'
mmew_mirco_e_dir = os.path.join(mmew_dir, 'Micro_Expression')
fps = 90.0  # 帧率
fourcc = cv2.VideoWriter_fourcc(*'XVID')  # 视频编码器
size = (231, 231)  # 视频分辨率,与原始图片保持一致,或者将图片皆resize到訪分辨率

emotions_dir = os.listdir(mmew_mirco_e_dir)
print(emotions_dir)
for emotion_dir in emotions_dir:
    print(os.path.join(mmew_mirco_e_dir, emotion_dir))
    emotion_dir_path = os.path.join(mmew_mirco_e_dir, emotion_dir)
    emotion_dir_subs = os.listdir(emotion_dir_path)
    for emotion_dir_sub in emotion_dir_subs:
        dir_path = os.path.join(emotion_dir_path, emotion_dir_sub)
        print(dir_path)

        if os.path.isdir(dir_path):
            output_full_path = os.path.join(os.path.dirname(dir_path), os.path.basename(dir_path) + '.avi')
            out = cv2.VideoWriter(output_full_path, fourcc, fps, size)  # 定义输出文件及其它参数
            files = os.listdir(dir_path)
            print(files)

            for idx in range(1, len(files)+1):

                image_file = os.path.join(dir_path, str(idx) + '.jpg')
                frame = cv2.imread(image_file)
                out.write(frame)
                cv2.imshow(emotion_dir, frame)
                cv2.waitKey(1)
                # print(image_file)

                if cv2.waitKey(25) & 0xFF == ord('q'):
                    break

            out.release()

cv2.destroyAllWindows()

你可能感兴趣的:(图像处理,情感计算,opencv,计算机视觉,python)