Home > front end >  Frozen cameras in OpenCV
Frozen cameras in OpenCV

Time:05-19

I have tried to place two cameras to be able to take snapshots in both, I have even put a button to activate one and then the other but one way or another whenever I activate both together or deactivate one and activate the next one the window freezes. I attach the code that I was building if you have any suggestions on the matter I would greatly appreciate it. I have little knowledge of Python in general so keep it a little friendly.

from tkinter import *
from tkinter import Tk, Label, Frame, Entry, Button
from tkinter import ttk
from PIL import Image
from PIL import ImageTk
import cv2
import threading
import imutils 



window0= Tk() #Pantalla inicial
window0.title('Detección del punto optimo de vacunación') #Titulo de la ventana
window0.geometry('1280x720') #Dimesiones de la ventana
window0.configure(bg='#35455d') #Color de fondo 

tabControl = ttk.Notebook(window0)
tab1 = ttk.Frame(tabControl)
tab2 = ttk.Frame(tabControl)

tabControl.add(tab1, text ='Formulario')
tabControl.add(tab2, text ='Captura de webcam')
tabControl.pack(expand = 1, fill ="both")

#Pestaña 1 
label_0 = Label(tab1, text="Formulario de Registro",width=20,font=("bold", 20))
label_0.place(x=90,y=53)

label_1 = Label(tab1, text="Edad",width=20,font=("bold", 10))
label_1.place(x=70,y=130)

entry_1 = Entry(tab1)
entry_1.place(x=240,y=130)

label_2 = Label(tab1, text="Genero",width=20,font=("bold", 10))
label_2.place(x=70,y=180)
var = IntVar()
Radiobutton(tab1, text="M",padx = 5, variable=var, value=1).place(x=235,y=180)
Radiobutton(tab1, text="F",padx = 20, variable=var, value=2).place(x=290,y=180)

def IMC():
    try:
        peso = int(entrada_texto.get())
        estatura = int(entrada2_texto.get())
        imc = peso/estatura**2
        etiqueta5.config(text=imc)
    except ValueError:
        etiqueta5.config(text="Introduce un numero")
        
label_3 = Label(tab1, text="Estatura",width=20,font=("bold", 10))
label_3.place(x=70,y=230)
estatura= ""
entry_3 = Entry(tab1, textvariable=estatura)
entry_3.place(x=240,y=230)

label_4 = Label(tab1,text="Peso",width=20,font=("bold", 10))
label_4.place(x=70,y=280)
peso= ""
entry_4 =Entry(tab1, textvariable=peso)
entry_4.place(x=240,y=280)


# print('Su IMC es:¨{}'.format(indice))

label_5 = Label(tab1,text="IMC:",width=20,font=("bold", 10))
label_5.place(x=70,y=330)

entry_5 =Entry(tab1)
entry_5.place(x=240,y=330)

Button(tab1, text='Capturar Imagen',width=20,bg='brown',fg='white').place(x=580,y=560)
# it is use for display the registration form on the window

def iniciarcam1():
    global cap
    cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
    visualizar()
   
def iniciarcam2():
    global cap1
    cap1 = cv2.VideoCapture(0,cv2.CAP_DSHOW)
    visualizar1()
    
def visualizar():
    global cap
    if cap is not None:
        ret, frame = cap.read()
        if ret == True:
            frame = imutils.resize(frame, width=360)
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

            im = Image.fromarray(frame)
            img = ImageTk.PhotoImage(image=im)

            lblVideo.configure(image=img)
            lblVideo.image = img
            lblVideo.after(20, visualizar)
        else:
            lblVideo.image = ""
            cap.release()

def visualizar1():
    global cap1
    if cap1 is not None:
        ret, frame = cap1.read()
        if ret == True:
            frame = imutils.resize(frame, width=360)
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

            im = Image.fromarray(frame)
            img = ImageTk.PhotoImage(image=im)

            lblVideo1.configure(image=img)
            lblVideo1.image = img
            lblVideo1.after(20, visualizar)
        else:
            lblVideo1.image = ""
            cap1.release()
            
def finalizarcam1():
    global cap
    if cap.isOpened():cap.release()
    print("Camara 1 release")
    
def finalizarcam2():
    global cap1
    if cap1.isOpened():cap1.release() 
    print("Camara 2 release")
    
cap = None
cap1 = None 

btnIniciar = Button(tab1, text="Iniciar CAM1", width=20, command=iniciarcam1)
# btnIniciar.grid(column=0, row=0, padx=5, pady=5)
btnIniciar.place(x=800,y=53)

btnFinalizar = Button(tab1, text="Finalizar CAM1", width=20, command=finalizarcam1)
# btnFinalizar.grid(column=1, row=0, padx=5, pady=5)
btnFinalizar.place(x=800,y=103)

btnIniciar = Button(tab1, text="Iniciar CAM2", width=20, command=iniciarcam2)
# btnIniciar.grid(column=0, row=0, padx=5, pady=5)
btnIniciar.place(x=1000,y=53)

btnFinalizar = Button(tab1, text="Finalizar CAM2", width=20, command=finalizarcam2)
# btnFinalizar.grid(column=1, row=0, padx=5, pady=5)
btnFinalizar.place(x=1000,y=103)



lblVideo = Label(window0)
lblVideo1 = Label(window0)
# lblVideo.grid(column=0, row=1, columnspan=2)
lblVideo.place(x=500,y=200)
lblVideo1.place(x=900,y=200)


#Pestaña 2
label2_0 = Label(tab2, text="Procesado",width=20,font=("bold", 20))
label2_0.place(x=90,y=20)

window0.mainloop() 

CodePudding user response:

I run code and it never freeze window.

The only mistake which I found is: you fogot 1 in word visualizar1 in line lblVideo1.after(20, visualizar) in function visualizar1 - so when you start second camera then after tries to run code for first camera and it can't update image for second camera and it may look like it freeze.

  • Related