0

so I've been attempting to use the code located here to take and record videos in python for a raspberry pie project. I want to use python because all of the other hardware I'm interacting with is written in Python.

I don't really understand how to stop this script. No amount of typing any combination of characters has ever stopped this script for me. Any advice?

Final code after all suggested corrections (works when the input is typed into the screen showing camera picture):

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    #capture frame-by frame
    ret,frame = cap.read()

    #operations on the frame
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

    #display
    cv2.imshow('frame', gray)
    key =  cv2.waitKey(1000) & 0xFF
    print key
    if key == 27:
        break

cap.release()
cv2.destroyAllWindows()
K. Shores
  • 875
  • 1
  • 18
  • 46

2 Answers2

0

If you have pressed everything and the camera doesn't stop capturing, it just means it never meets the conditions in the if statement, and therefore it doesn't break out of the loop.

What you can do is to assign a particular key to stop the capturing. If you look at the code in this question, you can see Mike has assigned the ESCAPE key (ASCII code 27) to be the key which takes him into out of the loop.

Community
  • 1
  • 1
Shahzad
  • 2,033
  • 1
  • 16
  • 23
  • I just tried that, and it still doesn't stop. I am running this through terminal on mac, could that have something to do with this? – K. Shores Oct 10 '15 at 17:58
  • @K.Shores Hmm... how about trying to print the value of key after it gets assigned, this should tell you if the key presses are even getting detected or not. – Shahzad Oct 10 '15 at 18:02
  • It constantly prints -1. After I enter in a value, it does not print that value. – K. Shores Oct 10 '15 at 19:24
0

Try this:

key = cv2.waitKey(1000) & 0xFF

See the "warning" here.

Jason
  • 2,725
  • 2
  • 14
  • 22
  • That still isn't working for me. I'm using one of the late 2013 Macbook pros. So the 64-bit warning applies to me. By the way, when I enter in the escape key, the "^[" symbol appears on the screen. – K. Shores Oct 11 '15 at 14:34
  • Try pressing a key when the image is the active window, not the terminal. – Jason Oct 11 '15 at 14:38
  • Ohhhhh... Wow... That is the answer. My typing was all occurring inside of terminal, not the actual display window showing the picture. Your suggestion of typing while in the display window is correct, and way more sensible to me. Thanks. – K. Shores Oct 11 '15 at 15:51
  • Side question, is it possible to accept input both from the terminal and the display window? – K. Shores Oct 11 '15 at 15:52