我的程序花了太长时间才能响应

时间:2018-07-31 12:19:39

标签: python

我有两个视频。一个是20 fps的原始视频,另一个是从10 fps插值到20 fps的视频。为了比较这些视频,我获得了视频的每一帧,然后在每一帧中记录了每个像素的RGB值并进行了比较。当我比较多达400帧时,我的代码运行良好。但是,当我要比较更多帧时,至少需要45分钟,这并不方便。您可以在下面看到我的代码

from PIL import Image
from math import sqrt
video1 = []
video2 = []
r_difference = []
g_difference = []
b_difference = []
total_difference = []
difference_per_frame = []
total = 0
newtotal = 0
avg = 0

for a in range(0,2): 
    for b in range(20,827):
        if a == 0:
            im = Image.open('frame%d.jpg' % b)
            pix = im.load()
            for x in range(0,640):#dimension of my videos are 640x480
                for y in range(0,480):
                    video1.append(pix[x,y])#gets the RGB values for every pixel
        if a == 1:
            im = Image.open('frames%d.jpg' % b)
            pix = im.load()
            for x in range(0,640):
                for y in range(0,480):
                    video2.append(pix[x,y])

for x in range(0,len(video1)):
    for y in range(0,3):
        if y == 0:
            r_difference.append(abs(video1[x][y]-video2[x][y]))#calculates the R difference
        if y == 1:
            g_difference.append(abs(video1[x][y]-video2[x][y]))#calculates the G difference
        if y == 2:
            b_difference.append(abs(video1[x][y]-video2[x][y]))#calculates the B difference

for e in range(0,len(r_difference)):
        difference = sqrt(r_difference[e] + g_difference[e] + b_difference[e])#calculates the difference for one pixel
        total_difference.append(difference)

for i in range(0,len(total_difference)):
    total = total + total_difference[i]
    if i % 307200 == 0 and i != 0:
        newtotal = (total / 307200)
        difference_per_frame.append(newtotal)#there are 307200 pixels in every frame(640x480)
        total = 0
    if i == len(total_difference) - 1:
        newtotal = (total / 307200)
        difference_per_frame.append(newtotal)#the left over pixels

for z in range(0,len(difference_per_frame)):
    avg = avg + difference_per_frame[z]
avg = avg / len(difference_per_frame)
print avg#calculates the average difference

有没有一种方法可以缩短运行时间

1 个答案:

答案 0 :(得分:0)

就像@Kevin所说的那样,您正在使用for-case反模式进行过多的循环。这是删除的前两个循环,应该会稍微减少运行时间:

for b in range(20,827):
    frame_pix = Image.open('frame%d.jpg' % b).load()
    frames_pix = Image.open('frames%d.jpg' % b).load()
    for x in range(0,640):#dimension of my videos are 640x480
        for y in range(0, 480):
            video1.append(frame_pix[x, y])  # gets the RGB values for every pixel
            video2.append(frames_pix[x, y])  # gets the RGB values for every pixel

for x in range(0, len(video1)):
    r_difference.append(abs(video1[x][0] - video2[x][0]))  # calculates the R difference
    g_difference.append(abs(video1[x][1] - video2[x][1]))  # calculates the G difference
    b_difference.append(abs(video1[x][2] - video2[x][2]))  # calculates the B difference

如果要大幅提高性能,则需要并行处理帧。我鼓励您将框架拆分为多个块,将差异代码移至函数中,并查看multiprocessing以在每个进程/线程中独立处理那些框架的块。以下是入门文档:https://docs.python.org/2/library/multiprocessing.html