简体   繁体   中英

Take a photo opencv + python 3.3

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)

The mistake said:

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!")

EDIT:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM