嗨我想测试相机的分辨率。通常我手动设置,分辨率(w,h)320,480 ......
现在我希望在函数中循环或更好地设置列表分辨率= [(...)],它会自动给出每个输出,所以我不必写每手每个分辨率。我尝试了很多,但它没有得到它。
希望有人可以帮助我THX!
代码:
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()
答案 0 :(得分:1)
您可以通过将脚本封装在函数中并在迭代列表时调用列表中的分辨率来完成此操作。
实际上代码非常快,因此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