我正在使用OpenCV和Python从5100个全尺寸图像(每个约5000 x 3000像素)创建一个视频。然而,在图像3000周围(AVI文件大约为160 MB),write()方法显着减慢效果很好。
我正在裁剪图像,然后我在写作之前正在调整大小。我在下面写了很多时序代码。
CPU不是问题......大量的磁盘空间......有没有人见过这个? (我搜索过高和低)我一直在寻找内存泄漏,但代码太简单了。
import numpy as np
import cv2, glob, time
files = glob.glob("./pics/*.JPG")
video = cv2.VideoWriter('test.avi',cv2.cv.CV_FOURCC('m', 'p', '4', 'v'),120,(1920,1080))
print "INDEX, IMREAD, CROP_IMG, RESIZE, WRITE, TOTAL"
for idx, file in enumerate(files):
offset=idx/500 #just an offset to make sure that it reflects my real world.
start = time.time()
img = cv2.imread(file)
imread_dur = time.time() - start
start = time.time()
crop_img = img[1000+offset:2050+offset, 500-offset:2370-offset]
crop_dur = time.time() - start
start = time.time()
resized_image = cv2.resize(crop_img, (1920, 1080))
resize_dur = time.time() - start
start = time.time()
video.write(resized_image)
write_dur = time.time() - start
print "%5d, %3.6f, %3.6f, %3.6f, %3.6f, %3.6f " % (idx,imread_dur, crop_dur, resize_dur, write_dur, imread_dur+crop_dur+resize_dur+write_dur)
video.release()
在第2900张图像附近,它从大约200ms到video.write()到每张图像大约2000ms或更多。这感觉就像输出视频文件上的缓冲区问题,但似乎没有什么可配置的。 (当写入()减慢时,CPU几乎从一个完整的核心变为~3%。)在3000,写入时需要13秒()
INDEX, IMREAD, CROP_IMG, RESIZE, WRITE, TOTAL
...
2847, 0.232032, 0.004127, 0.015886, 0.214999, 0.467044
2848, 0.233260, 0.003745, 0.013973, 0.214703, 0.465681
2849, 0.228818, 0.003882, 0.016408, 0.251602, 0.500710
...
3096, 0.238894, 0.003780, 0.013936, 13.183812, 13.440422
3097, 0.253249, 0.004052, 0.015534, 13.668831, 13.941666
我已经看了一眼,没有任何东西出现。有谁见过这个?