Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.1 kB
1
Indexable
Never
def resize(info):
    im = cv2.imread(info[0])
    box = info[1]
    cname = info[0]
    angle = info[2]
    gender = info[3]    
    x1 = box[0]
    y1 = box[1]
    x2 = box[2]
    y2 = box[3]
    center = [(x2+x1)/2, (y2+y1)/2]
    w = x2 - x1
    h = y2 - y1
    d = max(w,h)
    delta_d = 1.5
    
    if angle[1] < -50:
        x1 = max(center[0] - d*0.55, 0)
        delta_d = 1.8
    elif angle[1] > 50:
        x1 = max(center[0] - d*1.25, 0)
        delta_d = 1.8
    else:
        x1 = max(center[0] - d*0.75, 0)
        
    x2 = x1 + d*delta_d

    if delta_d == 1.5:
        y1 = max(center[1] - d*0.875, 0)
    else:
        y1 = max(center[1] - d*1.05, 0)
        
    y2 = y1 + d*delta_d
    small_im = im[int(y1):int(y2), int(x1):int(x2), :]
    faces = app.get(small_im)
    
    if max(small_im.shape[0], small_im.shape[1]) > 512 and y1 > 0 and len(faces) == 1:
        cname = 'face_crops/model_{}_{}_{}_{}_'.format(angle[0], angle[1], angle[2], gender) + cname.split('/')[-1]
        small_im = cv2.resize(small_im, (512, 512))
        cv2.imwrite(cname, small_im)