拍张照片opencv + python 3.3 [英] Take a photo opencv + python 3.3

查看:96
本文介绍了拍张照片opencv + python 3.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户按"s"时,我想拍照.这是我的代码,我不知道为什么这是错误的.

I want to take a photo when the user press 's'. This is my code and I don't find why it's wrong.

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:预期出现缩进的块

File "dos.py", line 14 cv.SaveImage('test1.jpg', img) ^ IndentationError: expected an indented block

推荐答案

由于您没有告诉我们问题出在哪里,所以我猜想相机初始化失败.

Since you did not tell us what the problem is, I'm going to guess that the camera failed to initialize.

明显的问题是...您需要安全地开始编码 !!!!尽可能测试返回的呼叫:

The obvious problem is... you need to start coding safely!!! Test the return of the calls whenever possible:

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

现在,您分享了问题所在,我建议您缩进代码,因为 Python使用了代码缩进,以找出代码块的开始和结束位置.

Now that you shared what the problem is, I suggest you indent the code since Python uses the indentation of the code to figure out where a block of code begins and ends.

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

You can also specify the filename with double quotes:

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

这篇关于拍张照片opencv + python 3.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆