mtcnn utils


```python
import numpy as np
from PIL import Image
# 将边框转换为方形窗体
def convert_to_square(bboxes):

    bboxes_like = np.zeros_like(bboxes)
    x1,y1,x2,y2 = [bboxes[:,i] for i in range(4)]
    h = y2-y1+1.0
    w = x2-x1+1.0
    max_side = np.maximum(h,w)
    bboxes_like[:,0] = x1+w*0.5 -max_side*0.5
    bboxes_like[:,1] = y1+h*0.5 -max_side*0.5
    bboxes_like[:,2] =bboxes_like[:,0]+max_side-1.0
    bboxes_like[:,3]=bboxes_like[:,1]+max_side-1.0
    return bboxes_like

#将边界框转换为更像真正的边界框
# 偏移(offsets)是网络的输出之一
def calibraate_box(bboxes,offsers):
    x1,y1,x2,y2 = [bboxes[:,i] for i in range(4)]
    w = x2-x1+1.0
    h = y2-y1+1.0
    w = np.expand_dims(w,1)
    h = np.expand_dims(h,1)
    translation = np.hstack([w,h,w,h])*offsers
    bboxes[:,0:4] = bboxes[:,0:4]+translation
    return bboxes

#解决边界问题
def correct_boxes(bboxes,width,height):
    x1,y1,x2,y2 = [bboxes[:,i] for i in range(4)]
    w = x2-x1+1.0
    h = y2-y1+1.0
    num_boxes = bboxes.shape[0]

    x,y,ex,ey = x1,y1,x2,y2
    dx = np.zeros((num_boxes,))
    dy = np.zeros((num_boxes,))

    edx = w.copy()-1.0
    edy = h.copy()-1.0

    ind = np.where(ex>width-1.0)[0]
    edx[ind] = w[ind]+width-2.0-ex[ind]
    ex[ind] = width-1.0

    ind = np.where(ey > height - 1.0)[0]
    edy[ind] = h[ind] + height - 2.0 - ey[ind]
    ey[ind] = height - 1.0

    ind = np.where(x < 0.0)[0]
    dx[ind] = 0.0 - x[ind]
    x[ind] = 0.0

    ind = np.where(y < 0.0)[0]
    dy[ind] = 0.0 - y[ind]
    y[ind] = 0.0

    return_list = [dy, edy, dx, edx, y, ey, x, ex, w, h]
    return_list = [i.astype('int32') for i in return_list]

    return return_list


#从图像中剪切框。
def get_image_boxes(boxes,img,size=24):
    num_boxes = len(boxes)
    width,height = img.size
    [dy, edy, dx, edx, y, ey, x, ex, w, h] = correct_boxes(
        boxes, width, height)
    img_boxes = np.zeros((num_boxes,3,size,size),'uint8')

    for i in range(num_boxes):
        img_box = np.zeros((h[i],w[i],3),'unit8')

        img_array = np.asarray(img,'uint8')

        img_box[dy[i]:(edy[i]+1),dx[i]:(edx[i]+1),:] = img_array[
            y[i]:(ey[i]+1),x[i]:(ex[i]+1),:
        ]
        img_box = Image.fromarray(img_box)
        img_box = img_box.resize((size,size),Image.BILINEAR)
        img_boxes[i, :, :, :] = _preprocess(img_box)
    return img_boxes

#调整图片符合网络输入要求
def _preprocess(img):
    img = img.transpose((2, 0, 1))
    img = np.expand_dims(img, 0)
    img = (img - 127.5) * 0.0078125
    return img

你可能感兴趣的:(人脸识别)