How to save a video captured by webcam without showing it using OpenCV in python?

4

I'm starting to learn Computer Vision. I'm using the OpenCV module for Python 3. Reading tutorials, I discovered the following script:

import cv2

cap = cv2.VideoCapture(0)

fourcc = cv2.VideoWriter_fourcc(*'DIVX')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        out.write(frame)

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

cap.release()
out.release()
cv2.destroyAllWindows()

It shows images captured by the webcam and saves them in a video. The script exits by pressing the "q" key. However, I would like a script that would save the video but not show it. I tried to remove the following line from the script:

cv2.imshow('frame',frame)

It worked, but the script did not stop when you pressed the "q" key, because the key must be pressed in the window that opens with the part of the script that I removed. So how can I create a stop condition without creating the window that shows the video?

    
asked by anonymous 15.01.2017 / 17:24

1 answer

7

The problem is that the OpenCV waitKey function depends on the existence of a window to capture a key pressed (that is, the key is actually pressed in the context of the window which therefore needs not only to exist as well as being focused ).

If you do not create a window and use this function with a timeout (you used 1 millisecond), the function only waits for the time and returns -1 because no key will ever be pressed.

If you want to quit in an "elegant" way but without using a window, one suggestion is to simply capture the process termination signal on the terminal (ie capture the CTRL Here is an example (which contains other small suggestions for improvement as well):

import sys
import signal
import cv2

end = False

# --------------------------------------
def signal_handler(signal, frame):
    global end
    end = True

# --------------------------------------
cap = cv2.VideoCapture(0)

# Tenta abrir a webcam, e já falha se não conseguir
if not cap.isOpened():
    print('Não foi possível abrir a web cam.')
    sys.exit(-1)

# Cria o arquivo de video de saída
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

# Captura o sinal de CTRL+C no terminal
signal.signal(signal.SIGINT, signal_handler)
print('Capturando o vídeo da webcam -- pressione Ctrl+C para encerrar...')

# Processa enquanto o usuário não encerrar (com CTRL+C)
while(not end):
    ret, frame = cap.read()
    if ret:
        out.write(frame)
    else:
        print('Oops! A captura falhou.')
        break

print('Captura encerrada.')

# Encerra tudo
cap.release()
out.release()

If you wait for CTRL + C is not good enough for you, you'll have to use another alternative depending on your need. You can implement a timer and quit after a pre-scheduled interval (using scheduling tasks ) or even terminate from referral by another program (using interprocess communication ).

    
15.01.2017 / 20:24