Beispiel #1
0
def main(args):
    image_paths = read_list(args.image_list)
    for path in image_paths:
        im = open_image(path)
        # resize for memory
        width, height = im.size
        if height > 800:
            im = im.resize((int(800 * width / height), 800))
            width, height = im.size

        # use 2D-FAN detect landmarks
        fa = FaceAlignment(LandmarksType._2D,
                           enable_cuda=True,
                           flip_input=False,
                           use_cnn_face_detector=True)
        try:
            landmarks = fa.get_landmarks(np.array(im))[-1]
            landmarks[:, 1] = height - landmarks[:, 1]
        except:
            continue

        # generate a contour curve with contour points
        hull = ConvexHull(landmarks)
        # draw landmarks
        lm = np.array(im)
        for i in range(landmarks.shape[0]):
            rr, cc = draw.circle(height - landmarks[i, 1].astype('int32'),
                                 landmarks[i, 0].astype('int32'), 5)
            lm[rr, cc, :] = np.array((255, 0, 0))
        # create mask
        mask = np.zeros((height, width))
        rr, cc = draw.polygon(height - landmarks[hull.vertices, 1],
                              landmarks[hull.vertices, 0], mask.shape)
        mask[rr, cc] = 1

        save = True if args.save == 'True' else False
        path = path[:-1] if path[:-1] == '/' else path
        image_name = path[path.rindex('/') + 1:-4] + '_contour_nocrf.png'
        show_result(lm,
                    mask,
                    np.tile((mask != 0)[:, :, np.newaxis], (1, 1, 3)) * im,
                    save=save,
                    filename='images/' + image_name)

        # add CRF
        #prob = np.concatenate(((1-mask)[np.newaxis,:,:]*0.9 + mask[np.newaxis, :, :]*0.1, mask[np.newaxis, :, :]*0.9 + (1-mask)[np.newaxis, :, :]*0.1), axis=0)
        prob = ndimage.gaussian_filter(mask * 1.0, sigma=5)
        prob = np.concatenate(
            ((1 - prob)[np.newaxis, :, :], prob[np.newaxis, :, :]), axis=0)

        map = CRF(prob, np.array(im))
        image_name = path[path.rindex('/') + 1:-4] + '_contour_crf.png'
        show_result(im,
                    map,
                    np.tile((map != 0)[:, :, np.newaxis], (1, 1, 3)) * im,
                    save=save,
                    filename='images/' + image_name)
image_paths = []
for folder, subfolders, filenames in os.walk(source_dataset_path):
    for filename in filenames:
        if filename.endswith('.jpg'):
            image_paths.append(os.path.join(folder, filename))

num = len(image_paths)
more_face = 0
non_face = 0

for image_arg, image_path in enumerate(image_paths):
    print('Processing : {}/{}'.format(image_arg+1, num))
    image = cv2.imread(image_path)
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # gray_image = HistEqualize(gray_image)
    landmarks = Coor_2D.get_landmarks(gray_image)
    if landmarks is not None:
        landmarks = np.asarray(landmarks)
        if landmarks.shape == (1, 68, 2):
            landmarks = np.squeeze(landmarks)
            landmarks = landmarks.astype(int)
            x_min, y_min, x_max, y_max = cut_range(landmarks)
            out_face = crop_by_landmls(image, landmarks)
            out_face = out_face[y_min:y_max, x_min:x_max]
            out_face = cv2.resize(out_face, (48, 48))
            # faceAligned = Rotate.align(out_face, landmarks)
            source_path, filename = os.path.split(image_path)
            save_path = source_path.replace('fer2013', 'fer2013_HE_crop')
            if not os.path.exists(save_path):
                os.makedirs(save_path)
            cv2.imwrite(os.path.join(save_path, filename), out_face)
Beispiel #3
0
def main(args):
    caffe.set_mode_gpu()
    caffe.set_device(0)

    # Load both networks
    #net1 = caffe.Net('model/net_landmarks.prototxt', \
    #                 'model/params_landmarks.caffemodel', caffe.TEST)
    net2 = caffe.Net('model/net_segmentation.prototxt', \
                     'model/params_segmentation.caffemodel', caffe.TEST)

    palette = get_palette()

    # We have a Gaussian to recover the output slightly - better results
    f = scipy.io.loadmat('gaus.mat')['f']

    # load image names
    image_paths = read_list(args.image_list)

    # segment and measure performance
    for path in image_paths:
        if path[-3:] == 'jpg' or path[-3:] == 'png':
            imi = open_image(path)
            # resize for memory
            width, height = imi.size
            if height > 800:
                imi = imi.resize((int(800*width/height), 800))
        else:
            continue

        # use 2D-FAN detect landmarks
        fa = FaceAlignment(LandmarksType._2D, enable_cuda=True,
                           flip_input=False, use_cnn_face_detector=True)
        try:
            landmarks = fa.get_landmarks(np.array(imi))[-1]
            landmarks = landmarks.astype('uint16')
        except:
            continue

        if args.crop == 'middle':
            imi, landmarks = crop_image_middle(landmarks, imi)
        elif args.crop == 'min':
            imi, landmarks = crop_image_min(landmarks, imi)

        landmarks[:,0], landmarks[:,1] = landmarks[:,1].copy(), landmarks[:,0].copy()

        # prepare the image, limit image size for memory
        width, height = imi.size
        if width > height:
            if width > 450:
                imi = imi.resize((450, int(450 * height/width)))
                landmarks[:,0] = landmarks[:,0] * 450.0 / width
                landmarks[:,1] = landmarks[:,1] * 450.0 / width
            #elif height < 300:
            #    imi = imi.resize((int(300 * width/height), 300))
        else:
            if height > 450:
                imi = imi.resize((int(450 * width/height), 450))
                landmarks[:,0] = landmarks[:,0] * 450.0 / height
                landmarks[:,1] = landmarks[:,1] * 450.0 / height
            #elif width < 300:
            #    imi = imi.resize((300, int(300 * height/width)))
        width, height = imi.size
        im = np.array(imi, dtype=np.float32)
        if len(im.shape) == 2:
            im = np.reshape(im, im.shape+(1,))
            im = np.concatenate((im,im,im), axis=2)
        im = im[:,:,::-1] # RGB to BGR

        # trained with different means (accidently)
        segIm = im - np.array((87.86,101.92,133.01))
        segIm = segIm.transpose((2,0,1))

        # Do some recovery of the points
        C = np.zeros((landmarks.shape[0], height, width), 'uint8') # cleaned up heatmaps
        C = np.pad(C, ((0,0), (120,120), (120,120)), 'constant')

        for k in range(0,68):
            C[k,landmarks[k,0]+120-100:landmarks[k,0]+120+101,landmarks[k,1]+120-100:landmarks[k,1]+120+101] = f
        C = C[:,120:-120,120:-120] * 0.5

        # Forward through the segmentation network
        D = np.concatenate((segIm, C))
        net2.blobs['data'].reshape(1, *D.shape)
        net2.blobs['data'].data[0,:,:,:] = D
        net2.forward()
        mask = net2.blobs['score'].data[0].argmax(axis=0)
        S = Image.fromarray(mask.astype(np.uint8))
        S.putpalette(palette)

        print 'close figure to process next image'

        # transfer score to probability with softmax for later unary term
        score = net2.blobs['score'].data[0]
        prob = np.exp(score) / np.sum(np.exp(score), 0) # (nlabels, height, width)
        #prob_max = np.max(prob, 0) # (0.28, 1)

        # CRF
        map = CRF(prob, im) # final label

        # show result
        save = True if args.save == 'True' else False
        path = path[:-1] if path[-1] == '/' else path
        image_name = path[path.rindex('/')+1:-4] + '_part_nocrf_' + args.crop + '.png'
        show_result(imi, mask, np.tile((mask!=0)[:,:,np.newaxis], (1,1,3)) * imi,
                    save=save, filename='images/'+image_name)
        image_name = path[path.rindex('/')+1:-4] + '_part_crf_' + args.crop + '.png'
        show_result(imi, map, np.tile((map!=0)[:,:,np.newaxis], (1,1,3)) * imi,
                    save=save, filename='images/'+image_name)
Beispiel #4
0
def main(args):
    image_paths = read_list(args.image_list)

    # init
    caffe.set_device(0)
    caffe.set_mode_gpu()

    # load net
    net = caffe.Net(args.prototxt, args.caffemodel, caffe.TEST)

    for path in image_paths:
        # load image, switch to BGR, subtract mean, and make dims C x H x W for Caffe

        if path[-3:] == 'jpg' or path[-3:] == 'png':
            imi = open_image(path)
            # resize for memory
            width, height = imi.size
            if height > 800:
                imi = imi.resize((int(800 * width / height), 800))
        else:
            continue

        # use 2D-FAN detect landmarks
        fa = FaceAlignment(LandmarksType._2D,
                           enable_cuda=True,
                           flip_input=False,
                           use_cnn_face_detector=True)
        try:
            landmarks = fa.get_landmarks(np.array(imi))[-1]
            landmarks = landmarks.astype('uint16')
        except:
            continue

        if args.crop == 'middle':
            imi, landmarks = crop_image_middle(landmarks, imi)
        elif args.crop == 'min':
            imi, landmarks = crop_image_min(landmarks, imi)

        if '300' in args.prototxt:
            imi = imi.resize((300, 300))
        else:
            imi = imi.resize((500, 500))
        im = np.array(imi, dtype=np.float32)
        im = im[:, :, ::-1]
        im -= np.array((104.00698793, 116.66876762, 122.67891434))
        im = im.transpose((2, 0, 1))

        # shape for input (data blob is N x C x H x W), set data
        net.blobs['data'].reshape(1, *im.shape)
        net.blobs['data'].data[...] = im

        # run net and take argmax for prediction
        net.forward()
        mask = net.blobs['score'].data[0].argmax(axis=0)
        im_seg = imi * np.tile((mask != 0)[:, :, np.newaxis], (1, 1, 3))

        save = True if args.save == 'True' else False
        path = path[:-1] if path[-1] == '/' else path
        if '300' in args.prototxt:
            image_name = path[path.rindex('/') +
                              1:-4] + '_yuval_300_nocrf_' + args.crop + '.png'
        else:
            image_name = path[path.rindex('/') +
                              1:-4] + '_yuval_nocrf_' + args.crop + '.png'

        show_result(imi,
                    mask,
                    im_seg,
                    save=save,
                    filename='images/' + image_name)

        # generate prob
        #prob = np.concatenate(((1-mask)[np.newaxis,:,:]*0.9 + mask[np.newaxis,:,:]*0.1, mask[np.newaxis,:,:]*0.9+(1-mask)[np.newaxis,:,:]*0.1), axis=0)
        prob = ndimage.gaussian_filter(mask * 1.0, sigma=5)
        prob = np.concatenate(
            ((1 - prob)[np.newaxis, :, :], prob[np.newaxis, :, :]), axis=0)

        # add CRF
        map = CRF(prob, np.array(imi))
        if '300' in args.prototxt:
            image_name = path[path.rindex('/') +
                              1:-4] + '_yuval_300_crf_' + args.crop + '.png'
        else:
            image_name = path[path.rindex('/') +
                              1:-4] + '_yuval_crf_' + args.crop + '.png'
        show_result(imi,
                    map,
                    np.tile((map != 0)[:, :, np.newaxis], (1, 1, 3)) * imi,
                    save=save,
                    filename='images/' + image_name)
Beispiel #5
0
import sys

from face_detection.api import S3FD
from face_detection.bbox import nms
from face_alignment.api import FaceAlignment
from face_alignment.api import LandmarksType

s3fd = S3FD("models/s3fd_convert.pth")
face_alignment = FaceAlignment(LandmarksType._3D)

score_threshold = 0.5
nms_threshold = 0.3

image = cv2.imread(sys.argv[1])

boxes = s3fd.detect(image, score_threshold)
boxes = boxes[nms(boxes, nms_threshold)]

figure = image.copy()

for box in boxes:
    box = box[0:4]
    landmarks = face_alignment.get_landmarks(image, box)

    for pt in landmarks:
        pt = tuple(pt[0:2].astype(int))
        cv2.circle(figure, pt, 3, (0, 255, 0))

cv2.imshow('', figure)
cv2.waitKey(0)
def main(args):
    image_paths = read_list(args.image_list)

    # init
    caffe.set_device(0)
    caffe.set_mode_gpu()

    # load net
    net = caffe.Net(args.prototxt, args.caffemodel, caffe.TEST)

    for path in image_paths:
        # load image, switch to BGR, subtract mean, and make dims C x H x W for Caffe

        print path

        if path[-3:] == 'jpg' or path[-3:] == 'png':
            imi = open_image(path)
            # resize for memory
            width, height = imi.size
            if height > 800:
                imi = imi.resize((int(800*width/height), 800))
                width, height = imi.size
        else:
            continue

        dir = 'images/' + path[path.rindex('/', 0, path.rindex('/'))+1:path.rindex('/')] + '/'
        if not os.path.exists(dir):
            os.makedirs(dir)
        txtfile = dir+path[path.rindex('/')+1:-3]+'txt'

        # use 2D-FAN detect landmarks
        fa = FaceAlignment(LandmarksType._2D, enable_cuda=True,
                           flip_input=False, use_cnn_face_detector=True)
        try:
            landmarks = fa.get_landmarks(np.array(imi))[-1]
            save_landmarks(txtfile, landmarks)
            landmarks = landmarks.astype('uint16')
        except:
            # no face deteced
            continue

        if args.crop == 'middle':
            imi, landmarks = crop_image_middle(landmarks, imi)
        elif args.crop == 'min':
            imi, landmarks = crop_image_min(landmarks, imi)
        width, height = imi.size

        if '300' in args.prototxt:
            imi = imi.resize((300, 300))
            landmarks[:,0] = landmarks[:,0] * 300 / width
            landmarks[:,1] = landmarks[:,1] * 300 / height
            width = height = 300
        else:
            imi = imi.resize((500, 500))
            landmarks[:,0] = landmarks[:,0] * 500 / width
            landmarks[:,1] = landmarks[:,1] * 500 / height
            width = height = 500
        im = np.array(imi, dtype=np.float32)
        im = im[:,:,::-1]
        im -= np.array((104.00698793,116.66876762,122.67891434))
        im = im.transpose((2,0,1))

        # shape for input (data blob is N x C x H x W), set data
        net.blobs['data'].reshape(1, *im.shape)
        net.blobs['data'].data[...] = im

        # run net and take argmax for prediction
        net.forward()
        mask = net.blobs['score'].data[0].argmax(axis=0)

        save = True if args.save == 'True' else False
        path = path[:-1] if path[-1] == '/' else path
        if '300' in args.prototxt:
            image_name = path[path.rindex('/')+1:-4] + '_yuvalcontour_300_nocrf_' + args.crop + '.png'
        else:
            image_name = path[path.rindex('/')+1:-4] + '_yuvalcontour_nocrf_' + args.crop + '.png'

        # draw landmarks
        lm = np.array(imi)
        for i in range(landmarks.shape[0]):
            rr, cc = draw.circle(landmarks[i,1].astype('int32'), landmarks[i,0].astype('int32'), 1)
            lm[rr, cc, :] = np.array((255, 0, 0))

        # create mask contour
        hull = ConvexHull(landmarks)
        mask_contour = np.zeros((height, width))
        rr, cc = draw.polygon(landmarks[hull.vertices,1], landmarks[hull.vertices,0], mask_contour.shape)
        mask_contour[rr,cc] = 1
        mask = np.clip(mask + mask_contour, 0, 1)

        im_seg = imi * np.tile((mask!=0)[:,:,np.newaxis], (1,1,3))

        show_result(lm, mask, im_seg, save=save, filename=dir+image_name)

        # generate prob
        #prob = np.concatenate(((1-mask)[np.newaxis,:,:]*0.9 + mask[np.newaxis,:,:]*0.1, mask[np.newaxis,:,:]*0.9+(1-mask)[np.newaxis,:,:]*0.1), axis=0)
        prob = ndimage.gaussian_filter(mask*1.0, sigma=5)
        prob = np.concatenate(((1-prob)[np.newaxis,:,:], prob[np.newaxis,:,:]), axis=0)

        # add CRF
        map = CRF(prob, np.array(imi))
        if '300' in args.prototxt:
            image_name = path[path.rindex('/')+1:-4] + '_yuvalcontour_300_crf_' + args.crop + '.png'
        else:
            image_name = path[path.rindex('/')+1:-4] + '_yuvalcontour_crf_' + args.crop + '.png'
        show_result(imi, map, np.tile((map!=0)[:,:,np.newaxis], (1,1,3)) * imi, save=save, filename=dir+image_name)