埋在番里的死宅 2023-04-03 16:24 采纳率: 100%
浏览 30
已结题

在生成YML文件时,时不时出现Empty trainin g data was given.

在生成yml文件时,时不时会出现以下报错

D:\miniconda\envs\py37\python.exe "C:/Users/38273/Desktop/code/hope/whole test/facetest/13.py"
images/cap20230403161937
id: 1
face: ()
Traceback (most recent call last):
  File "C:/Users/38273/Desktop/code/hope/whole test/facetest/13.py", line 125, in train_face
    recognizer.train(faces, np.array(ids))
cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv_contrib\modules\face\src\lbph_faces.cpp:362: error: (-210:Unsupported format or combination of formats) Empty trainin
g data was given. You'll need more than one sample to learn a model. in function 'cv::face::LBPH::train'

进程已结束,退出代码为 -1073740791 (0xC0000409)




以下是代码

    def save_face(self):
        self.open_camera()
        path = './photo'
        path1 = './face_id'
        exist = os.path.exists(path)
        exist1 = os.path.exists(path1)
        if not exist:
            os.mkdir(path)
        if not exist1:
            os.mkdir(path1)

        id = '1'
        name = '张三'
        classroom = '通信1902'
        name = self.pinyin(name)
        classroom = self.pinyin(classroom)
        # cap = cv2.VideoCapture(0)
        # ret, frame = cap.read()
        if self.cap.isOpened():
            FName = fr"images/cap{time.strftime('%Y%m%d%H%M%S', time.localtime())}"
            print(FName)
            self.showImage.save('./photo/' + id + '.' + name + '.' + classroom + '.jpg')
            # cv2.imwrite(path + str(id) + '.' + str(classroom) + str(name) + ".jpg", frame)
        else:
            QMessageBox.critical(self, '错误', '摄像头未打开!')
            return None

    def get_face_id(self):
        self.save_face()
        path = './photo'
        picture = []
        ids = []
        imagepaths = [os.path.join(path, f) for f in os.listdir(path)]
        detector = cv2.CascadeClassifier('D:/opencv/opencv/sources/data/haarcascades/haarcascade_frontalface_alt2.xml')

        for imagepath in imagepaths:
            img = Image.open(imagepath).convert('L')
            img_np = np.array(img, 'uint8')
            faces = detector.detectMultiScale(img_np)
            id = int(os.path.split(imagepath)[1].split('.')[0])
            for x, y, w, h in faces:
                ids.append(id)
                picture.append(img_np[y:y + h, x:x + w])
        print('id:', id)
        print('face:', faces)
        return picture, ids
        #
        # recognizer = cv2.face.LBPHFaceRecognizer_create()
        # recognizer.train(picture, np.array(ids))
        # recognizer.write('./face_id/face_id.yml')

    def train_face(self):
        # self.open_camera
        faces, ids = self.get_face_id()
        recognizer = cv2.face.LBPHFaceRecognizer_create()
        recognizer.train(faces, np.array(ids))
        recognizer.write('./face_id/face_id.yml')
        return QMessageBox.information(
            self,
            '成功',
            '保存成功'
        )

  • 写回答

1条回答 默认 最新

  • 特别周冠宇 2023-04-04 14:31
    关注

    "Empty training data was given. You'll need more than one sample to learn a model." 表示训练数据为空
    可能原因:
    训练数据集太小。确保你有足够数量的图像来训练模型。
    数据集中没有正确的标签。确保所有人脸图像都有正确的标签和ID,以便模型能够识别它们。
    训练数据集的路径有误。确保你提供了正确的图像路径,并且程序可以正确地读取它们。
    图像文件格式有误。确保图像文件格式是支持的,并且它们可以被读取和解码。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月13日
  • 已采纳回答 4月5日
  • 创建了问题 4月3日

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算