How to do continuous firing in python?

Asked

Viewed 136 times

0

Good afternoon. I am developing a project where I need to make two cameras to take sequences of photos and stop only after a pre-defined command.

In my current code you need to decide the number of photos you want to be taken, but I wanted him to continue taking photos endlessly until I told him to stop.

thank you and thank you in advance

my cogido acts:

import cv2
import sys

camera1 = 0
camera2 = 1
foto1 = cv2.VideoCapture(camera1)
foto2 = cv2.VideoCapture(camera2)
n = int(input("Quantas fotos deseja tirar?"))
file1 = ("C:/Users/Desktop/software/DISPARO/2CAMERA/Image1_%03i.jpg" %camera)
file2 = ("C:/Users/Desktop/software/DISPARO/2CAMERA/Image2_%03i.jpg" %camera)



while n > camera1 and n>camera2:
    ret, frame1 = foto1.read()
    ret, frame2 = foto2.read()
    cv2.imshow("Camera 1", frame1)
    cv2.imshow("camera 2", frame2)
    cv2.imwrite(file1,frame1)
    cv2.imwrite(file2,frame2)
    camera1 += 1
    camera2 += 1

print("FINALIZADO!")
  • you have to rename the files?

  • A doubt. Which key would stop? Which action will stop? Because if it gets infinite loop, the performance will get bad.

  • would stop with the command Ctrl+C

  • @Edvaldoteomar Do you want this? https://stackoverflow.com/questions/1364173/stopping-python-using-ctrlc/21460045

  • No, I’d just like to know how to take pictures in an infinite loop

2 answers

0

With the library Matplotlib (plt) this can be achieved using Funcanimation

In that key of event to stop image playback is in event.key with if event.key == 'ctrl+c':.

The conversion of BGR, Opencv standard, to RGB, Matplotlib standard, is performed with cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)

Code

import cv2
import sys
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation


#https://stackoverflow.com/a/44604435/7690982
def grab_frame(cap):
    ret,frame = cap.read()
    return cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)

def atualizar(i):
    imagem1 = grab_frame(foto1)
    imagem2 = grab_frame(foto2)
    im1.set_data(imagem1)
    im2.set_data(imagem2)
    # cv2.imwrite(file1,imagem1)
    # cv2.imwrite(file2,imagem2)

def close(event):
    # Tecla para fechar
    if event.key == 'ctrl+c':
        plt.close(event.canvas.figure)


#Inicialização
camera1 = 0
camera2 = 1

# file1 = ("C:/Users/Desktop/software/DISPARO/2CAMERA/Image1_%03i.jpg" %camera)
# file2 = ("C:/Users/Desktop/software/DISPARO/2CAMERA/Image2_%03i.jpg" %camera)

foto1 = cv2.VideoCapture(camera1)
foto2 = cv2.VideoCapture(camera2)
imagem1 = grab_frame(foto1)
imagem2 = grab_frame(foto2)

#Cria os dois subplots
ax1 = plt.subplot(1,2,1)
ax2 = plt.subplot(1,2,2)

#Cria duas imagens nos subplots
im1 = ax1.imshow(imagem1)
im2 = ax2.imshow(imagem2)

# Escreve o arquivo
# cv2.imwrite(file1,imagem1)
# cv2.imwrite(file2,imagem2)

#Animação e atualização
ani = FuncAnimation(plt.gcf(), atualizar, interval=50)

#Fechar
cid = plt.gcf().canvas.mpl_connect("key_press_event", close)
#Mostrar o gráfico
plt.show()

print("FINALIZADO!")

Note: Why use Matplotlib and not Opencv? Because it is a graphics library and is much more complete than Opencv. For example: Varied color spaces, different subplots and multiple graph generation for image analysis.

0

There is a very simple way to solve this problem. Use the function of cv2 calling for waitKey(delay). What it does is basically wait for a time in milliseconds for the user to press a key. What it returns is a ord of input.

Example:

import cv2

camera = 0
camera = cv2.VideoCapture(camera)

while True:
    ret, frame = camera.read()
    cv2.imshow("Camera", frame)

    #Sai do while caso a tecla apertada for "q" ou "Q"
    if cv2.waitKey(1) in (ord('q'),ord("Q")): 
        break

camera.release()
cv2.destroyAllWindows()
print("FINALIZADO!")
  • waitKey only works with the cameras window appearing, is it possible to finish by CMD? So you don’t necessarily have the windows of the cameras open to finish the program.

  • Unfortunately not ;-;

Browser other questions tagged

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