2

i have installed python3-picamera on raspberry pi and i want to record a video and store it in .avi format untill i press "Esc" or 'q'. I found below program but i don't want to use camera.wait_recording() in my program. I have tried using key = cv2.waitkey(1) & 0XFF, but didn't work. It is throwing

syntax error: 'break' outside loop.

import time
import cv2   
import picamera

with picamera.PiCamera() as camera:
    camera.resolution = (640, 360)
    camera.framerate = 24
    camera.start_recording('one_day.h264', quality=20, bitrate=750000)
    #camera.wait_recording(24 * 60 * 60)
    key = cv2.waitkey(1) & 0XFF
    if key == ord("q")
          break

    camera.stop_recording()
  • 1
    You're missing a colon at the end of the if key == ord("q") line - all blocks are introduced by a colon in Python. If you want to record in .avi format you'll need to transcode the output (preferably on a faster machine); the Pi's camera firmware doesn't support it (probably because .avi doesn't officially support MPEG4 compression, not to mention it's an ancient format largely superseded at this point). – Dave Jones Dec 3 '15 at 16:55
  • 1
    You might want to have a look at the script in this answer as well which doesn't rely on OpenCV for keyboard handling; just uses curses (which is generally built into Python on Unix). – Dave Jones Dec 3 '15 at 17:03
1

Briefly looking into the PiCamera documentation (And with 0% knowledge in Python) I think you just may have a syntax error?

syntax error: 'break' outside loop.

Basically you are trying to break (a loop) from a non looped code. Causing a syntax error. Or break is not allowed in that context.

I suggest the following psuedo code changes.

import time
import cv2   
import picamera

with picamera.PiCamera() as camera:
    camera.resolution = (640, 360)
    camera.framerate = 24
    camera.start_recording('one_day.h264', quality=20, bitrate=750000)
    ///Above line will start recording. I dont think you explicilty need to use wait recording as its already recording on another thread.
    #camera.wait_recording(24 * 60 * 60)

    //Psuedo code here- I dont know Python
    ///start  a never ending loop, like while stop!= true?
    ///  use pause(50) to allow recording to progress
    ///  see if the user pressed a key in the buffer
    ///  Not sure if this is correct?    key = cv2.waitkey(1) & 0XFF
    ///    if key == ord("q")
    ///      exit your loop by setting stop = true (or break, since you are in a loop)
    ///  no key pressed, start the loop again until broken or another condition is met

    /// Now only call stop
    camera.stop_recording()

In the document it says using pause() will not raise errors. Only once you call stop recording. So it may result in half your video not being recorded. Maybe instead of pause() , use the wait_recording with a small time period (like 5 seconds?) If errors it should raise an error and tell you, instead of pretending to carry on and you only realising long time later.

I hope this helps, from a psuedo logical point of view.

| improve this answer | |
1

If its all just a syntax error, it seems like you have break() outside of your if statement. It looks like you used 6 spaces while you should have used four. You are also missing a colon at the end of the statement, making it an incomplete block.

So instead of this:

    if key == ord("q")
          break

Indent it with four spaces and use a colon.

    if key == ord("q"):
        break
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.