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

Asked

Viewed 1,728 times

4

I’m starting to learn Computer Vision. I’m using the Opencv module for Python 3. Reading tutorials, discover 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 ends by pressing the "q" key. However, I would like a script that saves the video but does not show it. I tried to remove the following line from the script:

cv2.imshow('frame',frame)

It worked, but the script no longer ended when pressing 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?

  • Comment on the line fourcc = cv2.VideoWriter_fourcc(*'DIVX') then change the line out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480)) for out = cv2.VideoWriter('output.avi', -1, 20.0, (640,480)) by changing the second parameter to -1 and see if it works.

  • When I did this, the difference was: when starting the script, the program asked me to choose a compactor. After I chose it, the program worked the same way as before, @gato

  • See here on documentation this script, leave a comment here if this script solves your problem.

  • The link script you sent me is the same script I want to fix, @cat

  • At the moment I am without webcan to test, in case nobody responds until Monday I put a reward, but I believe the problem is in the same condition. since the command that closes the loop depends on the window. Seeing this made it clearer to understand, then you have to think of another way.

1 answer

7


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

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

If you want to close in a "classy" way but without using a window, a suggestion is to simply capture the end signal of the process in the terminal (i.e., capture the keystroke CTRL+C. 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 the pressure of CTRL+C It’s not good for you either, you’ll have to use another alternative depending on your need. You can implement a time counter and terminate after a pre-programmed interval (using the scheduling of tasks) or even quit from the indication by another program (using inter-process communication).

  • 1

    I was waiting for you to answer ;D+1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.