首页 > 解决方案 > get_boudingbox 未定义错误(opencv2,python)

问题描述

我正在尝试通过创建边界框从图像中提取人脸,但出现如下所示的错误。

第一个代码块:

def get_predicition(image):
    """Expects the image input, this image further cropped to face
    and the cropped face image will be sent for evalution funtion 
    finally 
    returns the annotated reusult with bounding box around the face. 
    """
    height, width = image.shape[:2]
    try: # If in case face is not detected at any frame
        face = face_detector(image, 1)[0]  # Face detection
        x, y, size = get_boundingbox(face=face, width=width, height=height) # Calling to get bound box around the face
    except IndexError:
        pass
    cropped_face = image[y:y+size, x:x+size] # cropping the face 
    output,label = evaluate(cropped_face) # Sending the cropped face to get classifier result 
    font_face = cv2.FONT_HERSHEY_SIMPLEX # font settings
    thickness = 2
    font_scale = 1
    if label=='Real':
        color = (0,255, 0)
    else:
        color = (0, 0, 255)
    x = face.left()    # Setting the bounding box on uncropped image
    y = face.top()
    w = face.right() - x
    h = face.bottom() - y
    cv2.putText(image, label+'_'+str('%.2f'%output)+'%', (x, y+h+30), 
            font_face, font_scale,
            color, thickness, 2) # Putting the label and confidence values

    return cv2.rectangle(image, (x, y), (x + w, y + h), color, 2)# draw box over face

第二个代码块:

cropped_face =[]
for image in images:
    faces = face_detector(image[0], 1)
    height, width = image[0].shape[:2]
    try: # If in case face is not detected at any frame   
        x, y, size = get_boundingbox(face=faces[0], width=width, height=height)
    except IndexError:
        continue
cropped_face.append([image[0][y:y+size, x:x+size],image[1]])

第一个代码块运行没有任何错误,但是当我尝试运行第二个代码块时。我收到如下所示的错误:

NameError                                 Traceback (most recent call last)
<ipython-input-48-52e274eb31f6> in <module>
      5     try: # If in case face is not detected at any frame
      6 
----> 7         x, y, size = get_boundingbox(face=faces[0], width=width, height=height)
      8     except IndexError:
      9         continue

NameError: name 'get_boundingbox' is not defined

标签: pythonopencvface-detection

解决方案


推荐阅读