拍一张照片opencv + python 3.3

时间:2013-02-05 15:48:08

标签: python opencv

我想在用户按's'时拍照。这是我的代码,我不知道为什么这是错误的。

import cv
import sys

win = 'Camera'
cv.NamedWindow(win)
cap = cv.CreateCameraCapture(0)


while cv.WaitKey(1) != 27:
img = cv.QueryFrame(cap)

cv.ShowImage(win, img)
if cv.WaitKey(10) == 115:
cv.SaveImage('test1.jpg', img)

错误说:

文件“dos.py”,第14行     cv.SaveImage('test1.jpg',img)      ^ IndentationError:预期缩进块

1 个答案:

答案 0 :(得分:1)

由于您没有告诉我们问题是什么,我猜测相机无法初始化。

obvious problem是......你需要安全地开始编码 !!!尽可能测试呼叫的返回:

cap = cv.CreateCameraCapture(0)
if not cap:
    print("!!! Failed CreateCameraCapture: invalid parameter!")

修改

既然您已经分享了问题所在,我建议您从代码的Python uses the indentation开始缩进代码,以确定代码块的开始和结束位置。

您还可以使用双引号指定文件名:

if cv.WaitKey(10) == 115:
    cv.SaveImage("test1.jpg", img)