如何解决Tkinter 内存错误:打开简历视频几秒后停止
我对编码非常陌生,在尝试使用 Open CV 和 Tkinter 可视化视频时遇到了问题。 我正在构建一个 GUI,其中可以可视化 esp32cam 捕获的帧,通过网络服务器访问它们,然后可以拍摄照片。此外,还有一些按钮可以通过向 Arduino 发送字符来控制机器人的运动。
问题是,大约 40 秒后图像冻结,我得到:
Exception in Tkinter callback
MemoryError
涉及opencv和视频的部分代码是:
def visualize_video():
def take_picture(event=""):
Now = time.strftime("%Y%m%d-%H%M%s")
image_name = f"img{Now}.jpg"
cv2.imwrite(image_name,img)
imgResponse = urllib.request.urlopen (url)
imgNp = np.array(bytearray(imgResponse.read()),dtype=np.uint8)
img = cv2.imdecode (imgNp,1)
img = cv2.rotate(img,cv2.ROTATE_90_Clockwise)
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
vid = Image.fromarray(img)
video = ImageTk.PhotoImage(image=vid)
video_label = Label(window)
video_label.grid(row=1,column=0,rowspan=5,columnspan=2,padx=10)
video_label.configure(image=video)
video_label.image = video
video_label.after(10,visualize_video)
bpicture = tk.Button(window,text="Take picture",command=take_picture,bg='red3',fg='gray7',font=('Comic Sans MS',14))
bpicture.grid(row=0,padx=5,pady=10,columnspan=2)
window.bind("<space>",take_picture)
整个程序如下:
import serial
import time
import tkinter as tk
from tkinter import *
from PIL import Image
from PIL import ImageTk
import cv2
import numpy as np
import imutils
import urllib.request
window = tk.Tk()
window.configure(background='ivory3')
window.geometry('1080x720')
window.title('Robot Explorador - PYTHON GUI')
url = 'http://192.168.1.53/cam-res.jpg'
ArduinoUNO = serial.Serial(port='COM4',baudrate=9600,bytesize=8)
def visualize_video():
def take_picture(event=""):
Now = time.strftime("%Y%m%d-%H%M%s")
image_name = f"img{Now}.jpg"
cv2.imwrite(image_name,take_picture)
def robot_control():
showMenu = ''' ****************************************
------------CONTROL DEL ROBOT-------------
****************************************
------------------- CAMara -------------------
barra espaciadora -> tomar foto
i -> girar 15 grados a la IZQUIERDA
o -> CENTRAR camara
p -> girar 15 grados a la DERECHA
****************************************
----------------- DIRECCION ------------------
d -> girar a la DERECHA
a -> girar a la IZQUIERDA
w -> hacia ADELANTE
c -> DETENERSE
s -> REVERSA
****************************************
esc -> SALIR
****************************************\n'''
def c_izquierda(event=""):
ArduinoUNO.write('i'.encode('ascii'))
label_orden.configure(text='camara hacia la IZQUIERDA')
def c_centrar(event=""):
ArduinoUNO.write('o'.encode('ascii'))
label_orden.configure(text='CENTRAR camara')
def c_derecha(event=""):
ArduinoUNO.write('p'.encode('ascii'))
label_orden.configure(text='camara hacia la DERECHA')
def m_derecha(event=""):
ArduinoUNO.write('d'.encode('ascii'))
label_orden.configure(text='girar a la DERECHA')
def m_izquierda(event=""):
ArduinoUNO.write('a'.encode('ascii'))
label_orden.configure(text='girar a la IZQUIERDA')
def m_adelante(event=""):
ArduinoUNO.write('w'.encode('ascii'))
label_orden.configure(text='hacia ADELANTE')
def m_reversa(event=""):
ArduinoUNO.write('s'.encode('ascii'))
label_orden.configure(text='REVERSA')
def m_stop(event=""):
ArduinoUNO.write('c'.encode('ascii'))
label_orden.configure(text='DETENERSE')
def salir(event=""):
ArduinoUNO.write('x'.encode('ascii'))
window.destroy()
window.destroy()
ArduinoUNO.close()
label_orden.configure(text='saliendo...')
image_save = Label(window,text = 'imagen guardada en:',font=('Calibri',11),justify=LEFT)
image_save.grid(row=7,pady=5,sticky='NE')
image_path = Label(window,text='D:\Coding',justify=LEFT)
image_path.grid(row=7,column=1,sticky='NW')
label_instrucciones = Label(window,text = showMenu,13),justify=LEFT)
label_instrucciones.grid(row=1,column=2,padx=10)
label_orden = Label(window,text = 'esperando instrucciones...',font=('Courier New',justify=LEFT)
label_orden.grid(row=7,sticky=N)
b1 = tk.Button(window,text="IZQUIERDA",command=c_izquierda,bg='cadet blue',14))
b2 = tk.Button(window,text="CENTRAR",command=c_centrar,14))
b3 = tk.Button(window,text="DERECHA",command=c_derecha,14))
b4 = tk.Button(window,command=m_derecha,bg='SpringGreen3',14))
b5 = tk.Button(window,text="ADELANTE",command=m_adelante,14))
b6 = tk.Button(window,command=m_izquierda,14))
b7 = tk.Button(window,text="REVERSA",command=m_reversa,14))
b8 = tk.Button(window,text="STOP",command=m_stop,14))
b9 = tk.Button(window,text="SALIR",command=salir,bg='firebrick3',14))
b1.grid(row=1,column=3,pady=10)
b2.grid(row=1,column=4,pady=10)
b3.grid(row=1,column=5,pady=10)
b4.grid(row=3,sticky=W)
b5.grid(row=2,sticky=S)
b6.grid(row=3,sticky=E)
b7.grid(row=4,sticky=N)
b8.grid(row=5,sticky=N)
b9.grid(row=6,sticky=N)
window.bind("e",c_izquierda)
window.bind("r",c_centrar)
window.bind("f",c_derecha)
window.bind("d",m_derecha)
window.bind("w",m_adelante)
window.bind("a",m_izquierda)
window.bind("s",m_reversa)
window.bind("c",m_stop)
window.bind("<Escape>",salir)
window.mainloop()
time.sleep(.1)
visualize_video()
robot_control()
这是我在学校的毕业设计,任何帮助将不胜感激和参考。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。