import cv2 import tkinter as tk from tkinter import * from PIL import Image, ImageTk#图像控件 from Layout_Settings.one import top1 class Two_Layout(): def __init__(self, top): self.top = top self.cap = cv2.VideoCapture(0) self.canvas2 = Canvas(self.top, bg='LightSkyBlue', width=1100, height=650, highlightthickness=2, highlightbackground='Black') self.canvas2.place(x=0, y=20) self.windows_display() def Camera(self): ref, frame = self.cap.read() if ref is True: frame = cv2.flip(frame, 1) # 摄像头翻转 cvimage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA) pilImage = Image.fromarray(cvimage) pilImage = pilImage.resize((1100, 650), Image.ANTIALIAS) tkImage = ImageTk.PhotoImage(image=pilImage) return tkImage if ref is False: return False def windows_display(self): ref, frame = self.cap.read() if ref is False: label = tk.Label(self.canvas2, text='未接入摄像头!', font=("黑体", 25), width=15, height=1).place(x=400, y=300, anchor='nw') cap = cv2.VideoCapture(0) elif ref is not False: self.photo = ImageTk.PhotoImage(image=Image.fromarray(frame)) self.canvas2.create_image(0, 0, image=self.photo, anchor=tk.NW) self.top.after(15, self.windows_display()) top = tk.Tk() Two_Layout(top)为什么不显示
时间: 2023-06-15 16:03:05 浏览: 123
可能是因为缺少必要的库或者代码逻辑有误。可以尝试检查一下以下几点:
1. 确定你已经安装了必要的库,比如 cv2,tkinter 和 PIL。
2. 确定你在代码中正确导入了库和模块,比如 from PIL import Image, ImageTk 以及 import tkinter as tk。
3. 检查代码逻辑是否有误,比如是否正确调用了 Camera 和 windows_display 函数。
如果以上都没问题,可以尝试添加一些 print 语句来调试程序,找到哪里出了问题。
相关问题
import cv2 import tkinter as tk from tkinter import * from PIL import Image, ImageTk#图像控件 from Layout_Settings.one import top1 class Two_Layout(): def __init__(self, top): self.top = top self.cap = cv2.VideoCapture(0) self.canvas2 = Canvas(self.top, bg='LightSkyBlue', width=1100, height=650, highlightthickness=2, highlightbackground='Black') self.canvas2.place(x=0, y=20) self.windows_display() def Camera(self): ref, frame = self.cap.read() if ref is True: frame = cv2.flip(frame, 1) # 摄像头翻转 cvimage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA) pilImage = Image.fromarray(cvimage) pilImage = pilImage.resize((1100, 650), Image.ANTIALIAS) tkImage = ImageTk.PhotoImage(image=pilImage) return tkImage if ref is False: return False def windows_display(self): ref, frame = self.cap.read() if ref is False: label = tk.Label(self.canvas2, text='未接入摄像头!', font=("黑体", 25), width=15, height=1).place(x=400, y=300, anchor='nw') cap = cv2.VideoCapture(0) elif ref is not False: self.photo = ImageTk.PhotoImage(image=Image.fromarray(frame)) self.canvas2.create_image(0, 0, image=self.photo, anchor=tk.NW) self.top.after(15, self.windows_display()) top = tk.Tk() Two_Layout(top)
这是一个基于 tkinter 和 OpenCV 的摄像头界面程序,其中主要包含一个名为 Two_Layout 的类,该类实现了图形界面的布局和摄像头的显示。具体来说,该类中包含了以下方法:
- `__init__(self, top)`:构造函数,初始化界面和摄像头对象;
- `Camera(self)`:获取摄像头捕获的图像,将其转换为 tkinter 控件中可用的格式;
- `windows_display(self)`:在 tkinter 窗口中显示摄像头捕获的图像。
在程序运行时,会创建一个 tkinter 窗口,并在其中显示摄像头捕获的图像。如果未能成功接入摄像头,则会在界面中显示“未接入摄像头!”的提示信息。
import threading import cv2 import tkinter as tk from tkinter import * from PIL import Image, ImageTk#图像控件 from Background.Thread_Demo import Thread_data from Data_demo import data_demo class Two_Layout(): def __init__(self, top): self.top = top self.canvas2 = Canvas(self.top, bg='LightSkyBlue', width=1100, height=610, highlightthickness=2, highlightbackground='Black') self.canvas2.place(x=0, y=28) data_demo.display_id = self.canvas2 Thread_data(self.Thread_()) def Thread_(self): self.cap = cv2.VideoCapture(0) t = threading.Thread(target=self.windows_display()) t.start() def windows_display(self): ref, frame = self.cap.read() data_demo.display_id.delete('all') label = tk.Label(data_demo.display_id, text='未接入摄像头!', font=("黑体", 25), width=15, height=1) if ref is False: label.place(x=400, y=300, anchor='nw') self.cap = cv2.VideoCapture(0) else: for widget in data_demo.display_id.winfo_children(): widget.destroy() image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image1 = Image.fromarray(image) image2 = image1.resize((1100, 645), Image.ANTIALIAS) self.photo = ImageTk.PhotoImage(image=image2) data_demo.display_id.create_image(0, 0, image=self.photo, anchor=tk.NW) data_demo.display_id.after(15, self.windows_display)为什么还是与filedialog.askopenfilename()起了冲突
你的代码中没有引入 `filedialog`,所以很难确定具体原因。但是,在你的代码中,没有直接调用 `filedialog.askopenfilename()`,所以很可能是其他部分的代码和 `filedialog` 产生了冲突。请检查你的代码并确定是否有其他部分调用了 `filedialog.askopenfilename()`。另外,检查你的代码是否正确导入了 `filedialog`。如果你仍然无法解决问题,请提供更多的细节和代码,以便我能够更好地帮助你。
阅读全文