我想用 python 构建人脸识别,我按照 youtube 上的教程进行操作,然后当我尝试运行该应用程序时出现此错误。
我完全按照教程进行操作,但我不知道我是否错过了一些库安装。
这就是我写的代码:
import tkinter as tk
import cv2
from PIL import Image, ImageTk
import util
class App:
def __init__(self):
self.main_window = tk.Tk()
self.main_window.geometry("1000x500+350+100")
self.login_button_main_window = util.get_button(self.main_window, "Login", "green", self.login)
self.login_button_main_window.place(x=640, y=300)
self.register_new_user_button_main_window = util.get_button(self.main_window, "Register New User", "gray", self.register_new_user, fg='black')
self.register_new_user_button_main_window.place(x=640, y=400)
self.webcam_label = util.get_img_label(self.main_window)
self.webcam_label.place(x=10, y=0, width=700, height=500)
self.add_webcam(self.webcam_label)
def add_webcam(self, label):
**if 'cap' not in self.__dict__:
self.cap = cv2.VideoCapture(0)**
self._label = label
self.process_webcam()
def process_webcam(self):
ret, frame = self.cap.read()
self.most_recent_capture_arr = frame
img_ = cv2.cvtColor(self.most_recent_capture_arr, cv2.COLOR_BGR2RGB)
self.most_recent_capture_pil Image.fromarray(img_)
imgtk = ImageTk.PhotoImage(image=self.most_recent_capture_pil)
self._label.imgtk = imgtk
self._label.configure(image=imgtk)
self._label.after(20, self.process_webcam)
def login(self):
pass
def register_new_user(self):
pass
def start(self):
self.main_window.mainloop()
if __name__ == "__main__":
app = App()
app.start()
错误是:
self.cap = cv2.VideoCapture(0)
^
IndentationError: expected an indented block after 'if' statement on line 23
有人可以帮我解决这个问题吗?因为我搜索了一个小时但什么也没找到。
修复我的人脸识别应用程序
如果“if”语句为 True,则您想要在“if”语句上处理的任何进程都应该缩进。
def add_webcam(self, label):
if 'cap' not in self.__dict__:
self.cap = cv2.VideoCapture(0)
self._label = label
self.process_webcam()
上面的代码在“if”语句之后没有任何缩进行,这会导致错误。