1

我正在查看人脸检测,在使用以下代码处理此程序时使用 Kairos API

def Test():
    image = cap.read()[1]
    cv2.imwrite("opencv_frame.png",image)
    recognized_faces = kairos_face.verify_face(file="filepath/opencv_frame.png", subject_id='David',gallery_name='Test')
    print(recognized_faces)
    if recognized_faces.get('images')[0].get('transaction').get('status') !='success':
        print('No')
    else:
        print('Hello', recognized_faces.get('images')[0].get('transaction').get('subject_id'))

如果我直视相机,这很好用,但如果我转过头,它会因以下响应而中断。

kairos_face.exceptions.ServiceRequestError: {'Errors': [{'Message': 'no faces found in the image', 'ErrCode': 5002}]}

我如何处理异常错误,并强制测试功能继续运行,直到检测到人脸。

4

1 回答 1

1

你不能抓住异常再试一次吗?

def Test():
    captured = False
    while not captured:
        try:
            image = cap.read()[1]
            cv2.imwrite("opencv_frame.png",image)
            recognized_faces = kairos_face.verify_face(file="filepath/opencv_frame.png", subject_id='David',gallery_name='Test')
            captured = True
        except kairos_face.exceptions.ServiceRequestError:
            pass # optionally wait
    print(recognized_faces)
    if recognized_faces.get('images')[0].get('transaction').get('status') !='success':
        print('No')
    else:
        print('Hello', recognized_faces.get('images')[0].get('transaction').get('subject_id'))
于 2018-03-18T16:14:35.710 回答