我正试着写一个脚本,以便每秒钟从一部电影中得到一个画面的平均颜色。目前,我正在使用枕头调整大小()函数来完成这个任务,但是它非常慢(每秒5-10帧),这意味着分析整个电影需要几个小时。这是我写的代码:
def analyse_frames(movie_path):
try:
with open(MOVIE_TITLE, "w") as file:
counter = 0
video = cv2.VideoCapture(movie_path)
total_seconds = int(video.get(
cv2.CAP_PROP_FRAME_COUNT)/video.get(cv2.CAP_PROP_FPS))
succeeded, frame = video.read()
while succeeded:
video.set(cv2.CAP_PROP_POS_MSEC, (counter*1000))
succeeded, frame = video.read()
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img_pil = Image.fromarray(img).resize((1, 1))
color = str(img_pil.getpixel((0, 0)))[1:-1]
file.write((str(color) + "\n"))
counter += 1
succeeded = counter <= total_seconds
print(f"Frames analyzed: {counter} / {total_seconds}")
except:
print("Couldn't read movie file.")有人知道一个更快的方法来计算这个平均值吗?任何帮助都将不胜感激!
发布于 2021-01-17 13:25:19
frame.mean(axis=(0,1))
给出该帧的平均BGR (或RGB)值,假设它是一个3通道的numpy数组。
https://stackoverflow.com/questions/65759840
复制相似问题