嗨,我想测试一下我的相机的分辨率。通常我手动设置,分辨率(w,h) 320,480…
现在我想在一个循环或更好的函数中设置列表分辨率=[(…)],它自动给我每个输出,所以我不必每手写每个分辨率。我试了很多次,但它还是不懂。
希望有人能帮助我
谢谢!
代码:import sys
sys.path.append("C:\opencv\build\python\2.7")
import cv2
import cv2.cv as cv
import time
capture = cv2.VideoCapture(0)
num_frame = 0
resolution = [(320,480),(640,480),(704,680),(960,680),(1280,720),(1440,720),(1920,1080)]
w = 320
h = 480
size = capture.get(cv.CV_CAP_PROP_FRAME_WIDTH), capture.get(cv.CV_CAP_PROP_FRAME_HEIGHT)
size_new = capture.set(cv.CV_CAP_PROP_FRAME_WIDTH, w),capture.set(cv.CV_CAP_PROP_FRAME_HEIGHT,h)
print size
start = time.time()
while(True):
ret, frame = capture.read()
if num_frame < 60:
num_frame = num_frame + 1
else:
break
total_time = (time.time() - start)
fps = (num_frame / total_time)
print str(num_frame) + ' Frames ' + str(total_time) + ' Sekunden = ' + str(fps) + ' fps'
capture.release()
cv2.destroyAllWindows()
您可以简单地将脚本封装在函数中,并在迭代列表时调用列表中的分辨率。
实际上代码非常快,所以total_time = (time.time() - start)
在某些情况下评估为零,因此给出错误:ZeroDivisionError: float division by zero
在fps = (num_frame / total_time)
行,因为total_time评估为0,所以我添加了time.sleep(0.001)
来消除这个错误。
import cv2
import cv2.cv as cv
import time
resolution = [(320,480),(640,480),(704,680),(960,680),(1280,720),(1440,720),(1920,1080)]
def change_resolution(w, h):
capture = cv2.VideoCapture(0)
num_frame = 0
size = capture.get(cv.CV_CAP_PROP_FRAME_WIDTH), capture.get(cv.CV_CAP_PROP_FRAME_HEIGHT)
size_new = capture.set(cv.CV_CAP_PROP_FRAME_WIDTH, w),capture.set(cv.CV_CAP_PROP_FRAME_HEIGHT,h)
print size
start = time.time()
while(True):
ret, frame = capture.read()
if num_frame < 60:
num_frame = num_frame + 1
time.sleep(0.001)
else:
break
total_time = (time.time() - start)
fps = (num_frame / total_time)
print str(num_frame) + ' Frames ' + str(total_time) + ' Sekunden = ' + str(fps) + ' fps' + ' for width: ' + str(w) + ' height: ' + str(h)
capture.release()
cv2.destroyAllWindows()
for reso in resolution:
change_resolution(reso[0], reso[1])
输出:(0.0, 0.0)
60 Frames 0.0599999427795 Sekunden = 1000.00095368 fps for width: 320 height: 480
(0.0, 0.0)
60 Frames 0.0639998912811 Sekunden = 937.501592564 fps for width: 640 height: 480
(0.0, 0.0)
60 Frames 0.0599999427795 Sekunden = 1000.00095368 fps for width: 704 height: 680
(0.0, 0.0)
60 Frames 0.0599999427795 Sekunden = 1000.00095368 fps for width: 960 height: 680
(0.0, 0.0)
60 Frames 0.0599999427795 Sekunden = 1000.00095368 fps for width: 1280 height: 720
(0.0, 0.0)
60 Frames 0.0599999427795 Sekunden = 1000.00095368 fps for width: 1440 height: 720
(0.0, 0.0)
60 Frames 0.0599999427795 Sekunden = 1000.00095368 fps for width: 1920 height: 1080