首页 > 解决方案 > 每当我运行此代码时。摄像头无法打开,并且文字转语音命令显示“未找到出席人​​员”。为什么会这样?

问题描述

此代码用于python中的人脸检测

每当我运行此代码时,cam 都不会打开,并且文本到语音命令会显示“没有找到出席的面孔”。

我在这里使用了 HaarCasecade 算法和 opencv-python 进行人脸识别。这段代码基本上是这样工作的:它将主题名称作为输入,然后填写出勤。如果面部被识别,它会在名为 {subject}.csv 的 csv 文件中填写出勤率

        try:
            recognizer = cv2.face.LBPHFaceRecognizer_create()
            try:
                recognizer.read(trainimagelabel_path)
            except:
                e = "Model not found,please train model"
                Notifica.configure(
                    text=e,
                    bg="black",
                    fg="yellow",
                    width=33,
                    font=("times", 15, "bold"),
                )
                Notifica.place(x=20, y=250)
                text_to_speech(e)
            facecasCade = cv2.CascadeClassifier(haarcasecade_path)
            df = pd.read_csv(studentdetail_path)
            cam = cv2.VideoCapture(0)
            font = cv2.FONT_HERSHEY_SIMPLEX
            col_names = ["Enrollment", "Name"]
            attendance = pd.DataFrame(columns=col_names)
            while True:
                ___, im = cam.read()
                gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
                faces = facecasCade.detectMultiScale(gray, 1.2, 5)
                for (x, y, w, h) in faces:
                    global Id

                    Id, conf = recognizer.predict(gray[y : y + h, x : x + w])
                    if conf < 70:
                        print(conf)
                        global Subject
                        global aa
                        global date
                        global timeStamp
                        Subject = tx.get()
                        ts = time.time()
                        date = datetime.datetime.fromtimestamp(ts).strftime(
                            "%Y-%m-%d"
                        )
                        timeStamp = datetime.datetime.fromtimestamp(ts).strftime(
                            "%H:%M:%S"
                        )
                        aa = df.loc[df["Enrollment"] == Id]["Name"].values
                        global tt
                        tt = str(Id) + "-" + aa
                        # En='1604501160'+str(Id)
                        attendance.loc[len(attendance)] = [
                            Id,
                            aa,
                        ]
                        cv2.rectangle(im, (x, y), (x + w, y + h), (0, 260, 0), 4)
                        cv2.putText(
                            im, str(tt), (x + h, y), font, 1, (255, 255, 0,), 4
                        )
                    else:
                        Id = "Unknown"
                        tt = str(Id)
                        cv2.rectangle(im, (x, y), (x + w, y + h), (0, 25, 255), 7)
                        cv2.putText(
                            im, str(tt), (x + h, y), font, 1, (0, 25, 255), 4
                        )
                if time.time() > future:
                    break

                attendance = attendance.drop_duplicates(
                    ["Enrollment"], keep="first"
                )
                cv2.imshow("Filling Attendance...", im)
                key = cv2.waitKey(30) & 0xFF
                if key == 27:
                    break

            ts = time.time()
            print(aa)
            # attendance["date"] = date
            # attendance["Attendance"] = "P"
            attendance[date] = 1
            date = datetime.datetime.fromtimestamp(ts).strftime("%Y-%m-%d")
            timeStamp = datetime.datetime.fromtimestamp(ts).strftime("%H:%M:%S")
            Hour, Minute, Second = timeStamp.split(":")
            # fileName = "Attendance/" + Subject + ".csv"
            path = os.path.join(attendance_path, Subject)
            fileName = (
                f"{path}/"
                + Subject
                + "_"
                + date
                + "_"
                + Hour
                + "-"
                + Minute
                + "-"
                + Second
                + ".csv"
            )
            attendance = attendance.drop_duplicates(["Enrollment"], keep="first")
            print(attendance)
            attendance.to_csv(fileName, index=False)

            m = "Attendance Filled Successfully of " + Subject
            Notifica.configure(
                text=m,
                bg="black",
                fg="yellow",
                width=33,
                relief=RIDGE,
                bd=5,
                font=("times", 15, "bold"),
            )
            text_to_speech(m)

            Notifica.place(x=20, y=250)

            cam.release()
            cv2.destroyAllWindows()

            import csv
            import tkinter

            root = tkinter.Tk()
            root.title("Attendance of " + Subject)
            root.configure(background="black")
            cs = os.path.join(path, fileName)
            print(cs)
            with open(cs, newline="") as file:
                reader = csv.reader(file)
                r = 0

                for col in reader:
                    c = 0
                    for row in col:

                        label = tkinter.Label(
                            root,
                            width=10,
                            height=1,
                            fg="yellow",
                            font=("times", 15, " bold "),
                            bg="black",
                            text=row,
                            relief=tkinter.RIDGE,
                        )
                        label.grid(row=r, column=c)
                        c += 1
                    r += 1
            root.mainloop()
            print(attendance)
        except:
            f = "No Face found for attendance"
            text_to_speech(f)
            cv2.destroyAllWindows()

标签: pythonface-recognitionopencv-python

解决方案


推荐阅读