OpenCV 中的冻结相机
Frozen cameras in OpenCV
我尝试放置两个摄像头以便能够在两个摄像头中拍摄快照,我什至放了一个按钮来激活一个然后另一个,但是每当我同时激活两个或停用一个并激活下一个 window 冻结。如果您对此事有任何建议,我将附上我正在构建的代码,我将不胜感激。我对 Python 了解不多,所以保持友好一点。
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()
我 运行 编码并且它永远不会冻结 window。
我发现的唯一错误是:你在函数 visualizar1
的行 lblVideo1.after(20, visualizar)
中的单词 visualizar1
中忘记了 1
- 所以当你启动第二个相机时 after
尝试 运行 为第一个摄像头编码,但它无法为第二个摄像头更新图像,它可能看起来像冻结了。
我尝试放置两个摄像头以便能够在两个摄像头中拍摄快照,我什至放了一个按钮来激活一个然后另一个,但是每当我同时激活两个或停用一个并激活下一个 window 冻结。如果您对此事有任何建议,我将附上我正在构建的代码,我将不胜感激。我对 Python 了解不多,所以保持友好一点。
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()
我 运行 编码并且它永远不会冻结 window。
我发现的唯一错误是:你在函数 visualizar1
的行 lblVideo1.after(20, visualizar)
中的单词 visualizar1
中忘记了 1
- 所以当你启动第二个相机时 after
尝试 运行 为第一个摄像头编码,但它无法为第二个摄像头更新图像,它可能看起来像冻结了。