我能';在python中,不要从另一个类调用类变量

我能';在python中,不要从另一个类调用类变量,python,class,opencv,variables,tkinter,Python,Class,Opencv,Variables,Tkinter,你好,我正在做一个学校的人脸识别项目 使用opencv和tkinter,我发现调用 来自另一个类的变量。我想得到变量id\u的值,要在tkinter标签Label\u 1中使用它,我花了很多时间试图找出原因,但失败了。我真的需要帮助。多谢各位 导入tkinter 进口cv2 导入PIL.Image,PIL.ImageTk 导入时间 进口泡菜 从tkinter导入ttk face_cascade=cv2.CascadeClassifier(“C://Users/poste/PycharmProje

你好,我正在做一个学校的人脸识别项目 使用opencv和tkinter,我发现调用 来自另一个类的变量。我想得到变量
id\u
的值,要在tkinter标签
Label\u 1
中使用它,我花了很多时间试图找出原因,但失败了。我真的需要帮助。多谢各位

导入tkinter
进口cv2
导入PIL.Image,PIL.ImageTk
导入时间
进口泡菜
从tkinter导入ttk
face_cascade=cv2.CascadeClassifier(“C://Users/poste/PycharmProjects/untitled2/data/haarcascade_frontaface_alt2.xml”)
recognizer=cv2.face.LBPHFaceRecognizer_create()
识别器读取(“trainer.yml”)
标签={“人名”:1}
将open(“labels.pickle”,“rb”)作为f:
og_标签=pickle.load(f)
labels={v:k代表k,在og_labels.items()中为v
类应用程序:
定义初始化(自、窗口、窗口标题、几何体、视频源=0):
self.window=window
self.window.title(窗口标题)
self.video\u source=视频\u source
自几何=几何
#打开视频源(默认情况下,这将尝试打开计算机网络摄像头)
self.vid=MyVideoCapture(self.video\u源)
#创建适合上述视频源大小的画布
self.canvas=tkinter.canvas(窗口,宽度=800,高度=self.vid.height+20,bd=10)
self.canvas.pack()
#用于让用户拍摄快照的按钮
self.btn_snapshot=ttk.按钮(窗口,text=“Prendre-une-Screenshot”,命令=self.snapshot,宽度=40)
self.btn_snapshot.place(relx=0.25,rely=0.935)
self.label_1=tkinter.label(窗口,text=“Prendre une屏幕截图”)
self.label\u 1.config(text=self.vid.get\u frame.id)
自标签位置(relx=0.8,rely=0.1)
#调用一次后,update方法将每隔延迟毫秒自动调用一次
self.delay=15
self.update()
self.window.mainloop()
def快照(自身):
#从视频源获取一帧
ret,frame=self.vid.get\u frame()
如果ret:
cv2.imwrite(“帧-”+时间.strftime(“%d-%m-%Y-%H-%m-%S”)+“.jpg”,cv2.CVT颜色(帧,cv2.COLOR_RGB2BGR))
def更新(自我):
#从视频源获取一帧
ret,frame=self.vid.get\u frame()
如果ret:
self.photo=PIL.ImageTk.PhotoImage(image=PIL.image.fromarray(frame))
self.canvas.create_image(0,0,image=self.photo,anchor=tkinter.NW)
self.window.after(self.delay、self.update)
类MyVideoCapture:
定义初始化(自,视频源=0):
#打开视频源
self.vid=cv2.VideoCapture(视频源,cv2.CAP\u DSHOW)
如果不是self.vid.isOpened():
提升值错误(“无法打开视频源”,视频源)
#获取视频源的宽度和高度
self.width=self.vid.get(cv2.CAP\u PROP\u FRAME\u width)
self.height=self.vid.get(cv2.CAP\u PROP\u FRAME\u height)
def get_帧(自身):
如果self.vid.isOpened():
ret,frame=self.vid.read()
灰色=cv2.CVT颜色(边框,cv2.COLOR\u BGR2GRAY)
faces=face_级联。检测多尺度(灰色,scaleFactor=1.5,minNeighbors=5)
对于面中的(x,y,w,h):
#打印(x、y、w、h)
roi_gray=灰色[y:y+h,x:x+w]
roi_color=帧[y:y+h,x:x+w]
img_item=“C://Users/poste/Music/my_image.png”
cv2.imwrite(img_项目,roi_灰色)
id\u,conf=recognizer.predict(roi\u灰色)

如果conf>=45:#并且conf
id 
是一个变量而不是一个属性。您需要在
\uuuu init\uuuu
中将
id\uuu>定义为类属性。IE:
self.id
<代码>自宽
和自高
缩进不正确。实际上你的契约到处都是。你可能也需要纠正这些。非常感谢你,我解决了它
import tkinter
import cv2
import PIL.Image, PIL.ImageTk
import time
import pickle
from tkinter import ttk

face_cascade = cv2.CascadeClassifier("C://Users/poste/PycharmProjects/untitled2/data/haarcascade_frontalface_alt2.xml")

recognizer = cv2.face.LBPHFaceRecognizer_create()

recognizer.read("trainner.yml")

labels = {"person_name":1}

with open("labels.pickle",'rb') as f:
    og_labels = pickle.load(f)
    labels = {v:k for k,v in og_labels.items()}

class App:
      def __init__(self, window, window_title, geometry, video_source=0):
          self.window = window
          self.window.title(window_title)
          self.video_source = video_source
          self.geometry=geometry

     # open video source (by default this will try to open the computer webcam)
      self.vid = MyVideoCapture(self.video_source)

     # Create a canvas that can fit the above video source size
      self.canvas = tkinter.Canvas(window, width = 800, height = self.vid.height+20, bd=10)
      self.canvas.pack()

     # Button that lets the user take a snapshot
      self.btn_snapshot=ttk.Button(window, text="Prendre une Screenshot", command=self.snapshot, width=40)
      self.btn_snapshot.place(relx=0.25, rely=0.935)

      self.label_1=tkinter.Label(window, text="Prendre une Screenshot")
      self.label_1.config(text=self.vid.get_frame.id_)
      self.label_1.place(relx=0.8, rely=0.1)

     # After it is called once, the update method will be automatically called every delay milliseconds
      self.delay = 15
      self.update()

      self.window.mainloop()

  def snapshot(self):
     # Get a frame from the video source
     ret, frame = self.vid.get_frame()

     if ret:
         cv2.imwrite("frame-" + time.strftime("%d-%m-%Y-%H-%M-%S") + ".jpg", cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))

  def update(self):
     # Get a frame from the video source
     ret, frame = self.vid.get_frame()

     if ret:
         self.photo = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(frame))
         self.canvas.create_image(0, 0, image = self.photo, anchor = tkinter.NW)

     self.window.after(self.delay, self.update)



class MyVideoCapture:
     def __init__(self, video_source=0):
         # Open the video source
         self.vid = cv2.VideoCapture(video_source,cv2.CAP_DSHOW)
         if not self.vid.isOpened():
             raise ValueError("Unable to open video source", video_source)

     # Get video source width and height
     self.width = self.vid.get(cv2.CAP_PROP_FRAME_WIDTH)
     self.height = self.vid.get(cv2.CAP_PROP_FRAME_HEIGHT)

 def get_frame(self):


     if self.vid.isOpened():
         ret, frame = self.vid.read()
         gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
         faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5)
         for (x, y, w, h) in faces:
             # print (x,y,w,h)
             roi_gray = gray[y:y + h, x:x + w]
             roi_color = frame[y:y + h, x:x + w]
             img_item = "C://Users/poste/Music/my_image.png"
             cv2.imwrite(img_item, roi_gray)

             id_,conf = recognizer.predict(roi_gray)

             if conf >= 45:  # and conf <= 85:
                 print(id_)
                 print(labels[id_])

             color = (255, 0, 0)
             stroke = 2
             width = x + w
             height = y + h
             cv2.rectangle(frame, (x, y), (width, height), color, stroke)
         if ret:
             # Return a boolean success flag and the current frame converted to BGR
             return (ret, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
         else:
             return (ret, None)
     else:
         return(ret, None)

 # Release the video source when the object is destroyed
 def __del__(self):
     if self.vid.isOpened():
         self.vid.release()



# Create a window and pass it to the Application object
App(tkinter.Tk(), "Tkinter and OpenCV", "800x650")