Python 3.x 如何在Python中处理ServiceRequestError

Python 3.x 如何在Python中处理ServiceRequestError,python-3.x,python-requests,kairos-api,Python 3.x,Python Requests,Kairos Api,我正在研究人脸检测,使用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(re

我正在研究人脸检测,使用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}]}

如何处理异常错误,并强制测试函数继续运行,直到检测到一个面。

您不能捕获异常并重试吗

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'))

谢谢你,我确实试过接球,但它仍然在退出程序,似乎这不是在玩把戏。