Пример #1
0
 def get_aligned_face(self, img, force=False):
     bounding_boxes, points = detect_face.detect_face(
         img, self.det_minsize, self.pnet, self.rnet, self.onet,
         self.det_threshold, self.det_factor)
     if bounding_boxes.shape[0] == 0 and force:
         bounding_boxes, points = detect_face.detect_face_force(
             img, None, self.pnet, self.rnet, self.onet)
     if bounding_boxes.shape[0] == 0:
         return None
     bindex = 0
     nrof_faces = bounding_boxes.shape[0]
     det = bounding_boxes[:, 0:4]
     img_size = np.asarray(img.shape)[0:2]
     if nrof_faces > 1:
         bounding_box_size = (det[:, 2] - det[:, 0]) * (det[:, 3] -
                                                        det[:, 1])
         img_center = img_size / 2
         offsets = np.vstack([(det[:, 0] + det[:, 2]) / 2 - img_center[1],
                              (det[:, 1] + det[:, 3]) / 2 - img_center[0]])
         offset_dist_squared = np.sum(np.power(offsets, 2.0), 0)
         bindex = np.argmax(bounding_box_size - offset_dist_squared *
                            2.0)  # some extra weight on the centering
     det = bounding_boxes[:, 0:4]
     det = det[bindex, :]
     points = points[:, bindex]
     landmark = points.reshape((2, 5)).T
     #points need to be transpose, points = points.reshape( (5,2) ).transpose()
     det = np.squeeze(det)
     bb = det
     points = list(points.flatten())
     assert (len(points) == 10)
     str_image_size = "%d,%d" % (self.image_size[0], self.image_size[1])
     warped = face_preprocess.preprocess(img,
                                         bbox=bb,
                                         landmark=landmark,
                                         image_size=str_image_size)
     warped = np.transpose(warped, (2, 0, 1))
     print(warped.shape)
     return warped
Пример #2
0
def main(args):
    output_dir = os.path.expanduser(args.output_dir)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    datamap = {}
    pp = 0
    datasize = 0
    verr = 0
    for line in open(args.input_dir + "_clean_list.txt", 'r'):
        pp += 1
        if pp % 10000 == 0:
            print('loading list', pp)
        line = line.strip()[2:]
        if not line.startswith('m.'):
            continue
        vec = line.split('/')
        assert len(vec) == 2
        # print(line)
        person = vec[0]
        img = vec[1]
        try:
            img_id = int(img.split('.')[0])
        except ValueError:
            # print('value error', line)
            verr += 1
            continue
        if not person in datamap:
            labelid = len(datamap)
            datamap[person] = [labelid, {img_id: 1}]
        else:
            datamap[person][1][img_id] = 1
        datasize += 1

    print('dataset size', args.name, datasize)
    print('dataset err', verr)

    print('Creating networks and loading parameters')

    with tf.Graph().as_default():
        # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        # sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        sess = tf.Session()
        with sess.as_default():
            pnet, rnet, onet = detect_face.create_mtcnn(sess, None)

    minsize = 100  # minimum size of face
    # threshold = [ 0.6, 0.7, 0.7 ]  # three steps's threshold
    threshold = [0.6, 0.6, 0.3]  # three steps's threshold
    factor = 0.709  # scale factor

    print(minsize)
    print(threshold)
    print(factor)

    # Add a random key to the filename to allow alignment using multiple processes
    # random_key = np.random.randint(0, high=99999)
    # bounding_boxes_filename = os.path.join(output_dir, 'bounding_boxes_%05d.txt' % random_key)
    output_filename = os.path.join(output_dir,
                                   'faceinsight_align_%s.lst' % args.name)

    with open(output_filename, "w") as text_file:
        nrof_images_total = 0
        nrof_successfully_aligned = 0
        nrof_changed = 0
        nrof_iou3 = 0
        nrof_force = 0
        for line in open(args.input_dir, 'r'):
            vec = line.strip().split()
            person = vec[0]
            img_id = int(vec[1])
            v = datamap.get(person, None)
            if v is None:
                continue
            # TODO
            # if not img_id in v[1]:
            #  continue
            labelid = v[0]
            img_str = base64.b64decode(vec[-1])
            nparr = np.fromstring(img_str, np.uint8)
            img = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR)
            img = img[..., ::-1]  # to rgb
            if nrof_images_total % 100 == 0:
                print("Processing %d, (%d)" %
                      (nrof_images_total, nrof_successfully_aligned))
            nrof_images_total += 1
            target_dir = os.path.join(output_dir, person)
            if not os.path.exists(target_dir):
                os.makedirs(target_dir)
            target_path = os.path.join(target_dir, "%d.jpg" % img_id)
            _minsize = minsize
            fimage = edict()
            fimage.bbox = None
            fimage.image_path = target_path
            fimage.classname = str(labelid)
            if fimage.bbox is not None:
                _bb = fimage.bbox
                _minsize = min([
                    _bb[2] - _bb[0], _bb[3] - _bb[1], img.shape[0] // 2,
                    img.shape[1] // 2
                ])
            else:
                _minsize = min(img.shape[0] // 5, img.shape[1] // 5)
            bounding_boxes, points = detect_face.detect_face(
                img, _minsize, pnet, rnet, onet, threshold, factor)
            bindex = -1
            nrof_faces = bounding_boxes.shape[0]
            if fimage.bbox is None and nrof_faces > 0:
                det = bounding_boxes[:, 0:4]
                img_size = np.asarray(img.shape)[0:2]
                bindex = 0
                if nrof_faces > 1:
                    bounding_box_size = (det[:, 2] - det[:, 0]) * (det[:, 3] -
                                                                   det[:, 1])
                    img_center = img_size / 2
                    offsets = np.vstack([
                        (det[:, 0] + det[:, 2]) / 2 - img_center[1],
                        (det[:, 1] + det[:, 3]) / 2 - img_center[0]
                    ])
                    offset_dist_squared = np.sum(np.power(offsets, 2.0), 0)
                    bindex = np.argmax(
                        bounding_box_size - offset_dist_squared *
                        2.0)  # some extra weight on the centering
            if fimage.bbox is not None:
                if nrof_faces > 0:
                    assert (bounding_boxes.shape[0] == points.shape[1])
                    det = bounding_boxes[:, 0:4]
                    img_size = np.asarray(img.shape)[0:2]
                    index2 = [0.0, 0]
                    for i in xrange(det.shape[0]):
                        _det = det[i]
                        iou = IOU(fimage.bbox, _det)
                        if iou > index2[0]:
                            index2[0] = iou
                            index2[1] = i
                    if index2[0] > -0.3:
                        bindex = index2[1]
                        nrof_iou3 += 1
                if bindex < 0:
                    bounding_boxes, points = detect_face.detect_face_force(
                        img, fimage.bbox, pnet, rnet, onet)
                    bindex = 0
                    nrof_force += 1

            if bindex >= 0:
                det = bounding_boxes[:, 0:4]
                det = det[bindex, :]
                points = points[:, bindex]
                landmark = points.reshape((2, 5)).T
                # points need to be transpose, points = points.reshape( (5,2) ).transpose()
                det = np.squeeze(det)
                bb = det
                points = list(points.flatten())
                assert (len(points) == 10)
                warped = face_preprocess.preprocess(img,
                                                    bbox=bb,
                                                    landmark=landmark,
                                                    image_size=args.image_size)
                misc.imsave(target_path, warped)
                nrof_successfully_aligned += 1
                oline = '%d\t%s\t%d' % (1, fimage.image_path,
                                        int(fimage.classname))
                # oline = '%d\t%s\t%d\t%d\t%d\t%d\t%d\t' % (0,fimage.image_path, int(fimage.classname), bb[0], bb[1], bb[2], bb[3])
                # oline += '\t'.join([str(x) for x in points])
                text_file.write("%s\n" % oline)

    print('Total number of images: %d' % nrof_images_total)
    print('Number of successfully aligned images: %d' %
          nrof_successfully_aligned)
    print('Number of changed: %d' % nrof_changed)
    print('Number of iou3: %d' % nrof_iou3)
    print('Number of force: %d' % nrof_force)
Пример #3
0
def main(args):
    output_dir = os.path.expanduser(args.output_dir)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    # Store some git revision info in a text file in the log directory
    #facenet.store_revision_info(src_path, output_dir, ' '.join(sys.argv))
    image_dir = os.path.join(args.input_dir, 'facescrub')
    dataset = face_image.get_dataset('facescrub', image_dir)
    print('dataset size', len(dataset))
    bbox = {}
    for label_file in ['facescrub_actors.txt', 'facescrub_actresses.txt']:
        label_file = os.path.join(args.input_dir, label_file)
        pp = 0
        for line in open(label_file, 'r'):
            pp += 1
            if pp == 1:
                continue
            vec = line.split("\t")
            key = (vec[0], int(vec[2]))
            value = [int(x) for x in vec[4].split(',')]
            bbox[key] = value
    print('bbox size', len(bbox))

    valid_key = {}
    json_data = open(
        os.path.join(args.input_dir,
                     'facescrub_uncropped_features_list.json')).read()
    json_data = json.loads(json_data)['path']
    for _data in json_data:
        key = _data.split('/')[-1]
        pos = key.rfind('.')
        if pos < 0:
            print(_data)
        else:
            key = key[0:pos]
        keys = key.split('_')
        #print(key)
        if len(keys) != 2:
            print('err', key, _data)
            continue
        #assert len(keys)==2
        key = (keys[0], int(keys[1]))
        valid_key[key] = 1
        #print(key)
    print('valid keys', len(valid_key))

    print('Creating networks and loading parameters')

    with tf.Graph().as_default():
        #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        #sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        sess = tf.Session()
        with sess.as_default():
            pnet, rnet, onet = detect_face.create_mtcnn(sess, None)

    minsize = 100  # minimum size of face
    threshold = [0.6, 0.7, 0.7]  # three steps's threshold
    factor = 0.709  # scale factor
    image_size = [112, 96]
    image_size = [112, 112]
    src = np.array([[30.2946, 51.6963], [65.5318, 51.5014], [48.0252, 71.7366],
                    [33.5493, 92.3655], [62.7299, 92.2041]],
                   dtype=np.float32)
    if image_size[1] == 112:
        src[:, 0] += 8.0

    # Add a random key to the filename to allow alignment using multiple processes
    #random_key = np.random.randint(0, high=99999)
    #bounding_boxes_filename = os.path.join(output_dir, 'bounding_boxes_%05d.txt' % random_key)
    #output_filename = os.path.join(output_dir, 'faceinsight_align_%s.lst' % args.name)
    if not os.path.exists(args.output_dir):
        os.makedirs(args.output_dir)

    output_filename = os.path.join(args.output_dir, 'lst')

    with open(output_filename, "w") as text_file:
        nrof_images_total = 0
        nrof = np.zeros((5, ), dtype=np.int32)
        for fimage in dataset:
            if nrof_images_total % 100 == 0:
                print("Processing %d, (%s)" % (nrof_images_total, nrof))
            nrof_images_total += 1
            #if nrof_images_total<950000:
            #  continue
            image_path = fimage.image_path
            if not os.path.exists(image_path):
                print('image not found (%s)' % image_path)
                continue
            #print(image_path)
            filename = os.path.splitext(os.path.split(image_path)[1])[0]
            _paths = fimage.image_path.split('/')
            print(fimage.image_path)
            a, b = _paths[-2], _paths[-1]
            pb = b.rfind('.')
            bname = b[0:pb]
            pb = bname.rfind('_')
            body = bname[(pb + 1):]
            img_id = int(body)
            key = (a, img_id)
            if not key in valid_key:
                continue
            #print(b, img_id)
            assert key in bbox
            fimage.bbox = bbox[key]
            try:
                img = misc.imread(image_path)
            except (IOError, ValueError, IndexError) as e:
                errorMessage = '{}: {}'.format(image_path, e)
                print(errorMessage)
            else:
                if img.ndim < 2:
                    print('Unable to align "%s", img dim error' % image_path)
                    #text_file.write('%s\n' % (output_filename))
                    continue
                if img.ndim == 2:
                    img = to_rgb(img)
                img = img[:, :, 0:3]
                tb = bname.replace(' ', '_') + ".png"
                ta = a.replace(' ', '_')
                target_dir = os.path.join(args.output_dir, ta)
                if not os.path.exists(target_dir):
                    os.makedirs(target_dir)

                target_file = os.path.join(target_dir, tb)
                warped = None
                if fimage.landmark is not None:
                    dst = fimage.landmark.astype(np.float32)

                    tform = trans.SimilarityTransform()
                    tform.estimate(dst,
                                   src[0:3, :] * 1.5 + image_size[0] * 0.25)
                    M = tform.params[0:2, :]
                    warped0 = cv2.warpAffine(
                        img,
                        M, (image_size[1] * 2, image_size[0] * 2),
                        borderValue=0.0)
                    _minsize = image_size[0]
                    bounding_boxes, points = detect_face.detect_face(
                        warped0, _minsize, pnet, rnet, onet, threshold, factor)
                    if bounding_boxes.shape[0] > 0:
                        bindex = 0
                        det = bounding_boxes[bindex, 0:4]
                        #points need to be transpose, points = points.reshape( (5,2) ).transpose()
                        dst = points[:, bindex].reshape((2, 5)).T
                        tform = trans.SimilarityTransform()
                        tform.estimate(dst, src)
                        M = tform.params[0:2, :]
                        warped = cv2.warpAffine(warped0,
                                                M,
                                                (image_size[1], image_size[0]),
                                                borderValue=0.0)
                        nrof[0] += 1
                #assert fimage.bbox is not None
                if warped is None and fimage.bbox is not None:
                    _minsize = img.shape[0] // 4
                    bounding_boxes, points = detect_face.detect_face(
                        img, _minsize, pnet, rnet, onet, threshold, factor)
                    if bounding_boxes.shape[0] > 0:
                        det = bounding_boxes[:, 0:4]
                        bindex = -1
                        index2 = [0.0, 0]
                        for i in range(det.shape[0]):
                            _det = det[i]
                            iou = IOU(fimage.bbox, _det)
                            if iou > index2[0]:
                                index2[0] = iou
                                index2[1] = i
                        if index2[0] > 0.3:
                            bindex = index2[1]
                        if bindex >= 0:
                            dst = points[:, bindex].reshape((2, 5)).T
                            tform = trans.SimilarityTransform()
                            tform.estimate(dst, src)
                            M = tform.params[0:2, :]
                            warped = cv2.warpAffine(
                                img,
                                M, (image_size[1], image_size[0]),
                                borderValue=0.0)
                            nrof[1] += 1
                            #print('1',target_file,index2[0])
                if warped is None and fimage.bbox is not None:
                    bb = fimage.bbox
                    #croped = img[bb[1]:bb[3],bb[0]:bb[2],:]
                    bounding_boxes, points = detect_face.detect_face_force(
                        img, bb, pnet, rnet, onet)
                    assert bounding_boxes.shape[0] == 1
                    _box = bounding_boxes[0]
                    if _box[4] >= 0.3:
                        dst = points[:, 0].reshape((2, 5)).T
                        tform = trans.SimilarityTransform()
                        tform.estimate(dst, src)
                        M = tform.params[0:2, :]
                        warped = cv2.warpAffine(img,
                                                M,
                                                (image_size[1], image_size[0]),
                                                borderValue=0.0)
                        nrof[2] += 1
                        #print('2',target_file)

                if warped is None:
                    roi = np.zeros((4, ), dtype=np.int32)
                    roi[0] = int(img.shape[1] * 0.06)
                    roi[1] = int(img.shape[0] * 0.06)
                    roi[2] = img.shape[1] - roi[0]
                    roi[3] = img.shape[0] - roi[1]
                    if fimage.bbox is not None:
                        bb = fimage.bbox
                        h = bb[3] - bb[1]
                        w = bb[2] - bb[0]
                        x = bb[0]
                        y = bb[1]
                        #roi = np.copy(bb)
                        _w = int((float(h) / image_size[0]) * image_size[1])
                        x += (w - _w) // 2
                        #x = min( max(0,x), img.shape[1] )
                        x = max(0, x)
                        xw = x + _w
                        xw = min(xw, img.shape[1])
                        roi = np.array((x, y, xw, y + h), dtype=np.int32)
                        nrof[3] += 1
                    else:
                        nrof[4] += 1
                    #print('3',bb,roi,img.shape)
                    #print('3',target_file)
                    warped = img[roi[1]:roi[3], roi[0]:roi[2], :]
                    #print(warped.shape)
                    warped = cv2.resize(warped, (image_size[1], image_size[0]))
                bgr = warped[..., ::-1]
                cv2.imwrite(target_file, bgr)
                oline = '%d\t%s\t%d\n' % (1, target_file, int(
                    fimage.classname))
                text_file.write(oline)
def main(args):
    output_dir = os.path.expanduser(args.output_dir)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    # Store some git revision info in a text file in the log directory
    #facenet.store_revision_info(src_path, output_dir, ' '.join(sys.argv))
    image_dir = os.path.join(args.input_dir, 'facescrub')
    dataset = face_image.get_dataset('facescrub', image_dir)
    print('dataset size', len(dataset))
    bbox = {}
    for label_file in ['facescrub_actors.txt',  'facescrub_actresses.txt']:
      label_file = os.path.join(args.input_dir, label_file)
      pp = 0
      for line in open(label_file, 'r'):
        pp+=1
        if pp==1:
          continue
        vec = line.split("\t")
        key = (vec[0], int(vec[2]))
        value = [int(x) for x in vec[4].split(',')]
        bbox[key] = value
    print('bbox size', len(bbox))

    valid_key = {}
    json_data = open(os.path.join(args.input_dir, 'facescrub_uncropped_features_list.json')).read()
    json_data = json.loads(json_data)['path']
    for _data in json_data:
      key = _data.split('/')[-1]
      pos = key.rfind('.')
      if pos<0:
        print(_data)
      else:
        key = key[0:pos]
      keys = key.split('_')
      #print(key)
      if len(keys)!=2:
        print('err', key, _data)
        continue
      #assert len(keys)==2
      key = (keys[0], int(keys[1]))
      valid_key[key] = 1
      #print(key)
    print('valid keys', len(valid_key))
    
    print('Creating networks and loading parameters')
    
    with tf.Graph().as_default():
        #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        #sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        sess = tf.Session()
        with sess.as_default():
            pnet, rnet, onet = detect_face.create_mtcnn(sess, None)
    
    minsize = 100 # minimum size of face
    threshold = [ 0.6, 0.7, 0.7 ]  # three steps's threshold
    factor = 0.709 # scale factor
    image_size = [112,96]
    image_size = [112,112]
    src = np.array([
      [30.2946, 51.6963],
      [65.5318, 51.5014],
      [48.0252, 71.7366],
      [33.5493, 92.3655],
      [62.7299, 92.2041] ], dtype=np.float32 )
    if image_size[1]==112:
      src[:,0] += 8.0

    # Add a random key to the filename to allow alignment using multiple processes
    #random_key = np.random.randint(0, high=99999)
    #bounding_boxes_filename = os.path.join(output_dir, 'bounding_boxes_%05d.txt' % random_key)
    #output_filename = os.path.join(output_dir, 'faceinsight_align_%s.lst' % args.name)
    if not os.path.exists(args.output_dir):
      os.makedirs(args.output_dir)

    output_filename = os.path.join(args.output_dir, 'lst')
    
    
    with open(output_filename, "w") as text_file:
        nrof_images_total = 0
        nrof = np.zeros( (5,), dtype=np.int32)
        for fimage in dataset:
            if nrof_images_total%100==0:
              print("Processing %d, (%s)" % (nrof_images_total, nrof))
            nrof_images_total += 1
            #if nrof_images_total<950000:
            #  continue
            image_path = fimage.image_path
            if not os.path.exists(image_path):
              print('image not found (%s)'%image_path)
              continue
            #print(image_path)
            filename = os.path.splitext(os.path.split(image_path)[1])[0]
            _paths = fimage.image_path.split('/')
            print(fimage.image_path)
            a,b = _paths[-2], _paths[-1]
            pb = b.rfind('.')
            bname = b[0:pb]
            pb = bname.rfind('_')
            body = bname[(pb+1):]
            img_id = int(body)
            key = (a, img_id)
            if not key in valid_key:
              continue
            #print(b, img_id)
            assert key in bbox
            fimage.bbox = bbox[key]
            try:
                img = misc.imread(image_path)
            except (IOError, ValueError, IndexError) as e:
                errorMessage = '{}: {}'.format(image_path, e)
                print(errorMessage)
            else:
                if img.ndim<2:
                    print('Unable to align "%s", img dim error' % image_path)
                    #text_file.write('%s\n' % (output_filename))
                    continue
                if img.ndim == 2:
                    img = to_rgb(img)
                img = img[:,:,0:3]
                tb = bname.replace(' ','_')+".png"
                ta = a.replace(' ','_')
                target_dir = os.path.join(args.output_dir, ta)
                if not os.path.exists(target_dir):
                  os.makedirs(target_dir)

                target_file = os.path.join(target_dir, tb)
                warped = None
                if fimage.landmark is not None:
                  dst = fimage.landmark.astype(np.float32)

                  tform = trans.SimilarityTransform()
                  tform.estimate(dst, src[0:3,:]*1.5+image_size[0]*0.25)
                  M = tform.params[0:2,:]
                  warped0 = cv2.warpAffine(img,M,(image_size[1]*2,image_size[0]*2), borderValue = 0.0)
                  _minsize = image_size[0]
                  bounding_boxes, points = detect_face.detect_face(warped0, _minsize, pnet, rnet, onet, threshold, factor)
                  if bounding_boxes.shape[0]>0:
                    bindex = 0
                    det = bounding_boxes[bindex,0:4]
                    #points need to be transpose, points = points.reshape( (5,2) ).transpose()
                    dst = points[:, bindex].reshape( (2,5) ).T
                    tform = trans.SimilarityTransform()
                    tform.estimate(dst, src)
                    M = tform.params[0:2,:]
                    warped = cv2.warpAffine(warped0,M,(image_size[1],image_size[0]), borderValue = 0.0)
                    nrof[0]+=1
                #assert fimage.bbox is not None
                if warped is None and fimage.bbox is not None:
                  _minsize = img.shape[0]//4
                  bounding_boxes, points = detect_face.detect_face(img, _minsize, pnet, rnet, onet, threshold, factor)
                  if bounding_boxes.shape[0]>0:
                    det = bounding_boxes[:,0:4]
                    bindex = -1
                    index2 = [0.0, 0]
                    for i in xrange(det.shape[0]):
                      _det = det[i]
                      iou = IOU(fimage.bbox, _det)
                      if iou>index2[0]:
                        index2[0] = iou
                        index2[1] = i
                    if index2[0]>0.3:
                      bindex = index2[1]
                    if bindex>=0:
                      dst = points[:, bindex].reshape( (2,5) ).T
                      tform = trans.SimilarityTransform()
                      tform.estimate(dst, src)
                      M = tform.params[0:2,:]
                      warped = cv2.warpAffine(img,M,(image_size[1],image_size[0]), borderValue = 0.0)
                      nrof[1]+=1
                      #print('1',target_file,index2[0])
                if warped is None and fimage.bbox is not None:
                  bb = fimage.bbox
                  #croped = img[bb[1]:bb[3],bb[0]:bb[2],:]
                  bounding_boxes, points = detect_face.detect_face_force(img, bb, pnet, rnet, onet)
                  assert bounding_boxes.shape[0]==1
                  _box = bounding_boxes[0]
                  if _box[4]>=0.3:
                    dst = points[:, 0].reshape( (2,5) ).T
                    tform = trans.SimilarityTransform()
                    tform.estimate(dst, src)
                    M = tform.params[0:2,:]
                    warped = cv2.warpAffine(img,M,(image_size[1],image_size[0]), borderValue = 0.0)
                    nrof[2]+=1
                    #print('2',target_file)

                if warped is None:
                  roi = np.zeros( (4,), dtype=np.int32)
                  roi[0] = int(img.shape[1]*0.06)
                  roi[1] = int(img.shape[0]*0.06)
                  roi[2] = img.shape[1]-roi[0]
                  roi[3] = img.shape[0]-roi[1]
                  if fimage.bbox is not None:
                    bb = fimage.bbox
                    h = bb[3]-bb[1]
                    w = bb[2]-bb[0]
                    x = bb[0]
                    y = bb[1]
                    #roi = np.copy(bb)
                    _w = int( (float(h)/image_size[0])*image_size[1] )
                    x += (w-_w)//2
                    #x = min( max(0,x), img.shape[1] )
                    x = max(0,x)
                    xw = x+_w
                    xw = min(xw, img.shape[1])
                    roi = np.array( (x, y, xw, y+h), dtype=np.int32)
                    nrof[3]+=1
                  else:
                    nrof[4]+=1
                  #print('3',bb,roi,img.shape)
                  #print('3',target_file)
                  warped = img[roi[1]:roi[3],roi[0]:roi[2],:]
                  #print(warped.shape)
                  warped = cv2.resize(warped, (image_size[1], image_size[0]))
                bgr = warped[...,::-1]
                cv2.imwrite(target_file, bgr)
                oline = '%d\t%s\t%d\n' % (1,target_file, int(fimage.classname))
                text_file.write(oline)
Пример #5
0
def main(args):
    output_dir = os.path.expanduser(args.output_dir)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    # Store some git revision info in a text file in the log directory
    src_path, _ = os.path.split(os.path.realpath(__file__))
    #facenet.store_revision_info(src_path, output_dir, ' '.join(sys.argv))
    dataset = face_image.get_dataset(args.name, args.input_dir)
    print('dataset size', args.name, len(dataset))

    print('Creating networks and loading parameters')

    with tf.Graph().as_default():
        #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        #sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        sess = tf.Session()
        with sess.as_default():
            pnet, rnet, onet = detect_face.create_mtcnn(sess, None)

    minsize = 100  # minimum size of face
    threshold = [0.6, 0.7, 0.7]  # three steps's threshold
    factor = 0.709  # scale factor
    if args.name == 'lfw' or args.name == 'webface' or args.name == 'vgg':
        minsize = 20
        threshold = [0.6, 0.7, 0.9]
        factor = 0.85
    if args.name == 'ytf':
        minsize = 20
        threshold = [0.6, 0.7, 0.4]
        factor = 0.85

    print(minsize)
    print(threshold)
    print(factor)

    # Add a random key to the filename to allow alignment using multiple processes
    #random_key = np.random.randint(0, high=99999)
    #bounding_boxes_filename = os.path.join(output_dir, 'bounding_boxes_%05d.txt' % random_key)
    output_filename = os.path.join(output_dir,
                                   'faceinsight_align_%s.lst' % args.name)

    with open(output_filename, "w") as text_file:
        nrof_images_total = 0
        nrof_successfully_aligned = 0
        nrof_changed = 0
        nrof_iou3 = 0
        nrof_force = 0
        for fimage in dataset:
            if nrof_images_total % 100 == 0:
                print("Processing %d, (%d)" %
                      (nrof_images_total, nrof_successfully_aligned))
            nrof_images_total += 1
            image_path = fimage.image_path
            if not os.path.exists(image_path):
                print('image not found (%s)' % image_path)
                continue
            filename = os.path.splitext(os.path.split(image_path)[1])[0]
            #print(image_path)
            try:
                img = misc.imread(image_path)
            except (IOError, ValueError, IndexError) as e:
                errorMessage = '{}: {}'.format(image_path, e)
                print(errorMessage)
            else:
                if img.ndim < 2:
                    print('Unable to align "%s", img dim error' % image_path)
                    #text_file.write('%s\n' % (output_filename))
                    continue
                if img.ndim == 2:
                    img = to_rgb(img)
                img = img[:, :, 0:3]
                _minsize = minsize
                if fimage.bbox is not None:
                    _bb = fimage.bbox
                    _minsize = min([
                        _bb[2] - _bb[0], _bb[3] - _bb[1], img.shape[0] // 2,
                        img.shape[1] // 2
                    ])

                bounding_boxes, points = detect_face.detect_face(
                    img, _minsize, pnet, rnet, onet, threshold, factor)
                bindex = -1
                nrof_faces = bounding_boxes.shape[0]
                if fimage.bbox is None and nrof_faces > 0:
                    det = bounding_boxes[:, 0:4]
                    img_size = np.asarray(img.shape)[0:2]
                    bindex = 0
                    if nrof_faces > 1:
                        bounding_box_size = (det[:, 2] - det[:, 0]) * (
                            det[:, 3] - det[:, 1])
                        img_center = img_size / 2
                        offsets = np.vstack([
                            (det[:, 0] + det[:, 2]) / 2 - img_center[1],
                            (det[:, 1] + det[:, 3]) / 2 - img_center[0]
                        ])
                        offset_dist_squared = np.sum(np.power(offsets, 2.0), 0)
                        bindex = np.argmax(
                            bounding_box_size - offset_dist_squared *
                            2.0)  # some extra weight on the centering
                if fimage.bbox is not None:
                    if nrof_faces > 0:
                        assert (bounding_boxes.shape[0] == points.shape[1])
                        det = bounding_boxes[:, 0:4]
                        img_size = np.asarray(img.shape)[0:2]
                        index2 = [0.0, 0]
                        for i in xrange(det.shape[0]):
                            _det = det[i]
                            iou = IOU(fimage.bbox, _det)
                            if iou > index2[0]:
                                index2[0] = iou
                                index2[1] = i
                        if index2[0] > -0.3:
                            bindex = index2[1]
                            nrof_iou3 += 1
                    if bindex < 0:
                        bounding_boxes, points = detect_face.detect_face_force(
                            img, fimage.bbox, pnet, rnet, onet)
                        bindex = 0
                        nrof_force += 1
                    #if bindex<0:
                    #  _img = img[fimage.bbox[1]:fimage.bbox[3], fimage.bbox[0]:fimage.bbox[2],:]
                    #  woffset = fimage.bbox[0]
                    #  hoffset = fimage.bbox[1]
                    #  _minsize = min( [_img.shape[0]//3, _img.shape[1]//3] )
                    #  bounding_boxes, points = detect_face.detect_face(_img, _minsize, pnet, rnet, onet, [0.6,0.7,0.01], factor)
                    #  nrof_faces = bounding_boxes.shape[0]
                    #  print(nrof_faces)
                    #  if nrof_faces>0:
                    #    #print(points.shape)
                    #    #assert(nrof_faces>0)
                    #    bounding_boxes[:,0]+=woffset
                    #    bounding_boxes[:,2]+=woffset
                    #    bounding_boxes[:,1]+=hoffset
                    #    bounding_boxes[:,3]+=hoffset
                    #    points[0:5,:] += woffset
                    #    points[5:10,:] += hoffset
                    #    bindex = 0
                    #    score = bounding_boxes[bindex,4]
                    #    print(score)
                    #    if score<=0.0:
                    #      bindex = -1
                    #    else:
                    #      nrof_force+=1
                    #if bindex<0:
                    #  _bb = fimage.bbox
                    #  _minsize = min( [_bb[2]-_bb[0], _bb[3]-_bb[1], img.shape[0]//2, img.shape[1]//2] )
                    #  bounding_boxes, points = detect_face.detect_face(img, _minsize, pnet, rnet, onet, [0.6,0.7,0.1], factor)
                    #  nrof_faces = bounding_boxes.shape[0]
                    #  print(nrof_faces)
                    #  if nrof_faces>0:
                    #    bindex = 0
                #if fimage.bbox is not None and bounding_boxes.shape[0]==0:
                #  bounding_boxes, points = detect_face.detect_face(img, _minsize, pnet, rnet, onet, [0.6,0.7,0.3], factor)

                #print(bounding_boxes.shape, points.shape)
                #print(nrof_faces, points.shape)

                if bindex >= 0:

                    det = bounding_boxes[:, 0:4]
                    det = det[bindex, :]
                    points = points[:, bindex]
                    #points need to be transpose, points = points.reshape( (5,2) ).transpose()
                    det = np.squeeze(det)
                    #bb = np.zeros(4, dtype=np.int32)
                    #bb[0] = np.maximum(det[0]-args.margin/2, 0)
                    #bb[1] = np.maximum(det[1]-args.margin/2, 0)
                    #bb[2] = np.minimum(det[2]+args.margin/2, img_size[1])
                    #bb[3] = np.minimum(det[3]+args.margin/2, img_size[0])
                    bb = det
                    #print(points.shape)
                    points = list(points.flatten())
                    assert (len(points) == 10)
                    #cropped = img[bb[1]:bb[3],bb[0]:bb[2],:]
                    #scaled = misc.imresize(cropped, (args.image_size, args.image_size), interp='bilinear')
                    #misc.imsave(output_filename, scaled)
                    nrof_successfully_aligned += 1
                    oline = '%d\t%s\t%d\t%d\t%d\t%d\t%d\t' % (
                        0, fimage.image_path, int(
                            fimage.classname), bb[0], bb[1], bb[2], bb[3])
                    oline += '\t'.join([str(x) for x in points])
                    text_file.write("%s\n" % oline)
                else:
                    print('Unable to align "%s", no face detected' %
                          image_path)
                    if args.force > 0:
                        if fimage.bbox is None:
                            oline = '%d\t%s\t%d\n' % (0, fimage.image_path,
                                                      int(fimage.classname))
                        else:
                            bb = fimage.bbox
                            oline = '%d\t%s\t%d\t%d\t%d\t%d\t%d\n' % (
                                0, fimage.image_path, int(fimage.classname),
                                bb[0], bb[1], bb[2], bb[3])
                        text_file.write(oline)
                        #text_file.write('%s\n' % (output_filename))

    print('Total number of images: %d' % nrof_images_total)
    print('Number of successfully aligned images: %d' %
          nrof_successfully_aligned)
    print('Number of changed: %d' % nrof_changed)
    print('Number of iou3: %d' % nrof_iou3)
    print('Number of force: %d' % nrof_force)
Пример #6
0
def main(args):
    output_dir = os.path.expanduser(args.output_dir)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    # Store some git revision info in a text file in the log directory
    src_path, _ = os.path.split(os.path.realpath(__file__))
    #facenet.store_revision_info(src_path, output_dir, ' '.join(sys.argv))
    dataset = face_image.get_dataset(args.name, args.input_dir)
    print('dataset size', args.name, len(dataset))

    print('Creating networks and loading parameters')

    with tf.Graph().as_default():
        #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        #sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        sess = tf.Session()
        with sess.as_default():
            pnet, rnet, onet = detect_face.create_mtcnn(sess, None)

    minsize = 100  # minimum size of face
    threshold = [0.6, 0.7, 0.7]  # three steps's threshold
    factor = 0.709  # scale factor
    #image_size = [112,96]
    image_size = [112, 112]
    src = np.array([[30.2946, 51.6963], [65.5318, 51.5014], [48.0252, 71.7366],
                    [33.5493, 92.3655], [62.7299, 92.2041]],
                   dtype=np.float32)

    if image_size[1] == 112:
        src[:, 0] += 8.0

    # Add a random key to the filename to allow alignment using multiple processes
    #random_key = np.random.randint(0, high=99999)
    #bounding_boxes_filename = os.path.join(output_dir, 'bounding_boxes_%05d.txt' % random_key)
    #output_filename = os.path.join(output_dir, 'faceinsight_align_%s.lst' % args.name)
    if not os.path.exists(args.output_dir):
        os.makedirs(args.output_dir)

    output_filename = os.path.join(args.output_dir, 'lst')

    with open(output_filename, "w") as text_file:
        nrof_images_total = 0
        nrof = np.zeros((5, ), dtype=np.int32)
        for fimage in dataset:
            if nrof_images_total % 100 == 0:
                print("Processing %d, (%s)" % (nrof_images_total, nrof))
            nrof_images_total += 1
            #if nrof_images_total<950000:
            #  continue
            image_path = fimage.image_path
            if not os.path.exists(image_path):
                print('image not found (%s)' % image_path)
                continue
            filename = os.path.splitext(os.path.split(image_path)[1])[0]
            #print(image_path)
            try:
                img = misc.imread(image_path)
            except (IOError, ValueError, IndexError) as e:
                errorMessage = '{}: {}'.format(image_path, e)
                print(errorMessage)
            else:
                if img.ndim < 2:
                    print('Unable to align "%s", img dim error' % image_path)
                    #text_file.write('%s\n' % (output_filename))
                    continue
                if img.ndim == 2:
                    img = to_rgb(img)
                img = img[:, :, 0:3]
                _paths = fimage.image_path.split('/')
                a, b, c = _paths[-3], _paths[-2], _paths[-1]
                target_dir = os.path.join(args.output_dir, a, b)
                if not os.path.exists(target_dir):
                    os.makedirs(target_dir)
                target_file = os.path.join(target_dir, c)
                warped = None
                if fimage.landmark is not None:
                    dst = fimage.landmark.astype(np.float32)

                    tform = trans.SimilarityTransform()
                    tform.estimate(dst,
                                   src[0:3, :] * 1.5 + image_size[0] * 0.25)
                    M = tform.params[0:2, :]
                    warped0 = cv2.warpAffine(
                        img,
                        M, (image_size[1] * 2, image_size[0] * 2),
                        borderValue=0.0)
                    _minsize = image_size[0]
                    bounding_boxes, points = detect_face.detect_face(
                        warped0, _minsize, pnet, rnet, onet, threshold, factor)
                    if bounding_boxes.shape[0] > 0:
                        bindex = 0
                        det = bounding_boxes[bindex, 0:4]
                        #points need to be transpose, points = points.reshape( (5,2) ).transpose()
                        dst = points[:, bindex].reshape((2, 5)).T
                        tform = trans.SimilarityTransform()
                        tform.estimate(dst, src)
                        M = tform.params[0:2, :]
                        warped = cv2.warpAffine(warped0,
                                                M,
                                                (image_size[1], image_size[0]),
                                                borderValue=0.0)
                        nrof[0] += 1  # ! tight bbox
                #assert fimage.bbox is not None
                if warped is None and fimage.bbox is not None:
                    _minsize = img.shape[0] // 4
                    bounding_boxes, points = detect_face.detect_face(
                        img, _minsize, pnet, rnet, onet, threshold, factor)
                    if bounding_boxes.shape[0] > 0:
                        det = bounding_boxes[:, 0:4]
                        bindex = -1
                        index2 = [0.0, 0]
                        for i in xrange(det.shape[0]):
                            _det = det[i]
                            iou = IOU(fimage.bbox, _det)
                            if iou > index2[0]:
                                index2[0] = iou
                                index2[1] = i
                        if index2[0] > 0.3:
                            bindex = index2[1]
                        if bindex >= 0:
                            dst = points[:, bindex].reshape((2, 5)).T
                            tform = trans.SimilarityTransform()
                            tform.estimate(dst, src)
                            M = tform.params[0:2, :]
                            warped = cv2.warpAffine(
                                img,
                                M, (image_size[1], image_size[0]),
                                borderValue=0.0)
                            nrof[1] += 1  # ! small loose bbox
                            #print('1',target_file,index2[0])
                if warped is None and fimage.bbox is not None:
                    bb = fimage.bbox
                    #croped = img[bb[1]:bb[3],bb[0]:bb[2],:]
                    bounding_boxes, points = detect_face.detect_face_force(
                        img, bb, pnet, rnet, onet)
                    assert bounding_boxes.shape[0] == 1
                    _box = bounding_boxes[0]
                    if _box[4] >= 0.3:
                        dst = points[:, 0].reshape((2, 5)).T
                        tform = trans.SimilarityTransform()
                        tform.estimate(dst, src)
                        M = tform.params[0:2, :]
                        warped = cv2.warpAffine(img,
                                                M,
                                                (image_size[1], image_size[0]),
                                                borderValue=0.0)
                        nrof[2] += 1  # adjust label bbox
                        #print('2',target_file)

                if warped is None:
                    roi = np.zeros((4, ), dtype=np.int32)
                    roi[0] = int(img.shape[1] * 0.06)
                    roi[1] = int(img.shape[0] * 0.06)
                    roi[2] = img.shape[1] - roi[0]
                    roi[3] = img.shape[0] - roi[1]
                    if fimage.bbox is not None:
                        bb = fimage.bbox
                        h = bb[3] - bb[1]
                        w = bb[2] - bb[0]
                        x = bb[0]
                        y = bb[1]
                        #roi = np.copy(bb)
                        _w = int((float(h) / image_size[0]) * image_size[1])
                        x += (w - _w) // 2
                        #x = min( max(0,x), img.shape[1] )
                        x = max(0, x)
                        xw = x + _w
                        xw = min(xw, img.shape[1])
                        roi = np.array((x, y, xw, y + h), dtype=np.int32)
                        nrof[3] += 1  # label bbox
                    else:
                        nrof[4] += 1  # tight inside bbox
                    #print('3',bb,roi,img.shape)
                    #print('3',target_file)
                    warped = img[roi[1]:roi[3], roi[0]:roi[2], :]
                    #print(warped.shape)
                    warped = cv2.resize(warped, (image_size[1], image_size[0]))
                bgr = warped[..., ::-1]
                cv2.imwrite(target_file, bgr)
                oline = '%d\t%s\t%d\n' % (1, target_file, int(
                    fimage.classname))
                text_file.write(oline)
Пример #7
0
 landmark = []
 for line in open(txt_path, 'r'):
     vec = line.strip().split(',')
     x = np.array([float(x) for x in vec], dtype=np.float32)
     landmark.append(x)
 landmark = np.array(landmark, dtype=np.float32)
 # print(landmark.shape)
 img = misc.imread(img_path)
 _bbox = None
 _landmark = None
 bounding_boxes, points = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
 nrof_faces = bounding_boxes.shape[0]
 if nrof_faces > 0:
     nrof[0] += 1
 else:
     bounding_boxes, points = detect_face.detect_face_force(img, minsize, pnet, rnet, onet)
     nrof_faces = bounding_boxes.shape[0]
     if nrof_faces > 0:
         nrof[1] += 1
     else:
         nrof[2] += 1
 if nrof_faces > 0:
     det = bounding_boxes[:, 0:4]
     img_size = np.asarray(img.shape)[0:2]
     bindex = 0
     if nrof_faces > 1:
         bounding_box_size = (det[:, 2] - det[:, 0]) * (det[:, 3] - det[:, 1])
         img_center = img_size / 2
         offsets = np.vstack(
             [(det[:, 0] + det[:, 2]) / 2 - img_center[1], (det[:, 1] + det[:, 3]) / 2 - img_center[0]])
         offset_dist_squared = np.sum(np.power(offsets, 2.0), 0)
Пример #8
0
 landmark = []
 for line in open(txt_path, 'r'):
   vec = line.strip().split(',')
   x = np.array( [float(x) for x in vec], dtype=np.float32)
   landmark.append(x)
 landmark = np.array(landmark, dtype=np.float32)
 #print(landmark.shape)
 img = misc.imread(img_path)
 _bbox = None
 _landmark = None
 bounding_boxes, points = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
 nrof_faces = bounding_boxes.shape[0]
 if nrof_faces>0:
   nrof[0]+=1
 else:
   bounding_boxes, points = detect_face.detect_face_force(img, minsize, pnet, rnet, onet)
   nrof_faces = bounding_boxes.shape[0]
   if nrof_faces>0:
     nrof[1]+=1
   else:
     nrof[2]+=1
 if nrof_faces>0:
   det = bounding_boxes[:,0:4]
   img_size = np.asarray(img.shape)[0:2]
   bindex = 0
   if nrof_faces>1:
       bounding_box_size = (det[:,2]-det[:,0])*(det[:,3]-det[:,1])
       img_center = img_size / 2
       offsets = np.vstack([ (det[:,0]+det[:,2])/2-img_center[1], (det[:,1]+det[:,3])/2-img_center[0] ])
       offset_dist_squared = np.sum(np.power(offsets,2.0),0)
       bindex = np.argmax(bounding_box_size-offset_dist_squared*2.0) # some extra weight on the centering
def main(args):
    output_dir = os.path.expanduser(args.output_dir)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    # Store some git revision info in a text file in the log directory
    src_path,_ = os.path.split(os.path.realpath(__file__))
    #facenet.store_revision_info(src_path, output_dir, ' '.join(sys.argv))
    dataset = face_image.get_dataset(args.name, args.input_dir)
    print('dataset size', args.name, len(dataset))
    
    print('Creating networks and loading parameters')
    
    with tf.Graph().as_default():
        #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        #sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        sess = tf.Session()
        with sess.as_default():
            pnet, rnet, onet = detect_face.create_mtcnn(sess, None)
    
    minsize = 100 # minimum size of face
    threshold = [ 0.6, 0.7, 0.7 ]  # three steps's threshold
    factor = 0.709 # scale factor
    if args.name=='lfw' or args.name=='webface' or args.name=='vgg':
      minsize = 20
      threshold = [0.6,0.7,0.9]
      factor = 0.85

    print(minsize)
    print(threshold)
    print(factor)

    # Add a random key to the filename to allow alignment using multiple processes
    #random_key = np.random.randint(0, high=99999)
    #bounding_boxes_filename = os.path.join(output_dir, 'bounding_boxes_%05d.txt' % random_key)
    output_filename = os.path.join(output_dir, 'faceinsight_align_%s.lst' % args.name)
    
    with open(output_filename, "w") as text_file:
        nrof_images_total = 0
        nrof_successfully_aligned = 0
        nrof_changed = 0
        nrof_iou3 = 0
        nrof_force = 0
        for fimage in dataset:
            if nrof_images_total%100==0:
              print("Processing %d, (%d)" % (nrof_images_total, nrof_successfully_aligned))
            nrof_images_total += 1
            image_path = fimage.image_path
            if not os.path.exists(image_path):
              print('image not found (%s)'%image_path)
              continue
            filename = os.path.splitext(os.path.split(image_path)[1])[0]
            #print(image_path)
            try:
                img = misc.imread(image_path)
            except (IOError, ValueError, IndexError) as e:
                errorMessage = '{}: {}'.format(image_path, e)
                print(errorMessage)
            else:
                if img.ndim<2:
                    print('Unable to align "%s", img dim error' % image_path)
                    #text_file.write('%s\n' % (output_filename))
                    continue
                if img.ndim == 2:
                    img = to_rgb(img)
                img = img[:,:,0:3]
                _minsize = minsize
                if fimage.bbox is not None:
                  _bb = fimage.bbox
                  _minsize = min( [_bb[2]-_bb[0], _bb[3]-_bb[1], img.shape[0]//2, img.shape[1]//2] )

                bounding_boxes, points = detect_face.detect_face(img, _minsize, pnet, rnet, onet, threshold, factor)
                bindex = -1
                nrof_faces = bounding_boxes.shape[0]
                if fimage.bbox is None and nrof_faces>0:
                  det = bounding_boxes[:,0:4]
                  img_size = np.asarray(img.shape)[0:2]
                  bindex = 0
                  if nrof_faces>1:
                    bounding_box_size = (det[:,2]-det[:,0])*(det[:,3]-det[:,1])
                    img_center = img_size / 2
                    offsets = np.vstack([ (det[:,0]+det[:,2])/2-img_center[1], (det[:,1]+det[:,3])/2-img_center[0] ])
                    offset_dist_squared = np.sum(np.power(offsets,2.0),0)
                    bindex = np.argmax(bounding_box_size-offset_dist_squared*2.0) # some extra weight on the centering
                if fimage.bbox is not None:
                  if nrof_faces>0:
                    assert(bounding_boxes.shape[0]==points.shape[1])
                    det = bounding_boxes[:,0:4]
                    img_size = np.asarray(img.shape)[0:2]
                    index2 = [0.0, 0]
                    for i in xrange(det.shape[0]):
                      _det = det[i]
                      iou = IOU(fimage.bbox, _det)
                      if iou>index2[0]:
                        index2[0] = iou
                        index2[1] = i
                    if index2[0]>-0.3:
                      bindex = index2[1]
                      nrof_iou3+=1
                  if bindex<0:
                    bounding_boxes, points = detect_face.detect_face_force(img, fimage.bbox, pnet, rnet, onet)
                    bindex = 0
                    nrof_force+=1
                  #if bindex<0:
                  #  _img = img[fimage.bbox[1]:fimage.bbox[3], fimage.bbox[0]:fimage.bbox[2],:]
                  #  woffset = fimage.bbox[0]
                  #  hoffset = fimage.bbox[1]
                  #  _minsize = min( [_img.shape[0]//3, _img.shape[1]//3] )
                  #  bounding_boxes, points = detect_face.detect_face(_img, _minsize, pnet, rnet, onet, [0.6,0.7,0.01], factor)
                  #  nrof_faces = bounding_boxes.shape[0]
                  #  print(nrof_faces)
                  #  if nrof_faces>0:
                  #    #print(points.shape)
                  #    #assert(nrof_faces>0)
                  #    bounding_boxes[:,0]+=woffset
                  #    bounding_boxes[:,2]+=woffset
                  #    bounding_boxes[:,1]+=hoffset
                  #    bounding_boxes[:,3]+=hoffset
                  #    points[0:5,:] += woffset
                  #    points[5:10,:] += hoffset
                  #    bindex = 0
                  #    score = bounding_boxes[bindex,4]
                  #    print(score)
                  #    if score<=0.0:
                  #      bindex = -1
                  #    else:
                  #      nrof_force+=1
                  #if bindex<0:
                  #  _bb = fimage.bbox
                  #  _minsize = min( [_bb[2]-_bb[0], _bb[3]-_bb[1], img.shape[0]//2, img.shape[1]//2] )
                  #  bounding_boxes, points = detect_face.detect_face(img, _minsize, pnet, rnet, onet, [0.6,0.7,0.1], factor)
                  #  nrof_faces = bounding_boxes.shape[0]
                  #  print(nrof_faces)
                  #  if nrof_faces>0:
                  #    bindex = 0
                #if fimage.bbox is not None and bounding_boxes.shape[0]==0:
                #  bounding_boxes, points = detect_face.detect_face(img, _minsize, pnet, rnet, onet, [0.6,0.7,0.3], factor)


                #print(bounding_boxes.shape, points.shape)
                #print(nrof_faces, points.shape)
                        
                if bindex>=0:

                    det = bounding_boxes[:,0:4]
                    det = det[bindex,:]
                    points = points[:, bindex]
                    #points need to be transpose, points = points.reshape( (5,2) ).transpose()
                    det = np.squeeze(det)
                    #bb = np.zeros(4, dtype=np.int32)
                    #bb[0] = np.maximum(det[0]-args.margin/2, 0)
                    #bb[1] = np.maximum(det[1]-args.margin/2, 0)
                    #bb[2] = np.minimum(det[2]+args.margin/2, img_size[1])
                    #bb[3] = np.minimum(det[3]+args.margin/2, img_size[0])
                    bb = det
                    #print(points.shape)
                    points = list(points.flatten())
                    assert(len(points)==10)
                    #cropped = img[bb[1]:bb[3],bb[0]:bb[2],:]
                    #scaled = misc.imresize(cropped, (args.image_size, args.image_size), interp='bilinear')
                    #misc.imsave(output_filename, scaled)
                    nrof_successfully_aligned += 1
                    oline = '%d\t%s\t%d\t%d\t%d\t%d\t%d\t' % (0,fimage.image_path, int(fimage.classname), bb[0], bb[1], bb[2], bb[3])
                    oline += '\t'.join([str(x) for x in points])
                    text_file.write("%s\n"%oline)
                else:
                    print('Unable to align "%s", no face detected' % image_path)
                    if args.force>0:
                      if fimage.bbox is None:
                        oline = '%d\t%s\t%d\n' % (0,fimage.image_path, int(fimage.classname))
                      else:
                        bb = fimage.bbox
                        oline = '%d\t%s\t%d\t%d\t%d\t%d\t%d\n' % (0,fimage.image_path, int(fimage.classname), bb[0], bb[1], bb[2], bb[3])
                      text_file.write(oline)
                      #text_file.write('%s\n' % (output_filename))
                            
    print('Total number of images: %d' % nrof_images_total)
    print('Number of successfully aligned images: %d' % nrof_successfully_aligned)
    print('Number of changed: %d' % nrof_changed)
    print('Number of iou3: %d' % nrof_iou3)
    print('Number of force: %d' % nrof_force)
Пример #10
0
def main(args):
    output_dir = os.path.expanduser(args.output_dir)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    datamap = {}
    pp = 0
    datasize = 0
    verr = 0
    for line in open(args.input_dir+"_clean_list.txt", 'r'):
      pp+=1
      if pp%10000==0:
        print('loading list', pp)
      line = line.strip()[2:]
      if not line.startswith('m.'):
        continue
      vec = line.split('/')
      assert len(vec)==2
      #print(line)
      person = vec[0]
      img = vec[1]
      try:
        img_id = int(img.split('.')[0])
      except ValueError:
        #print('value error', line)
        verr+=1
        continue
      if not person in datamap:
        labelid = len(datamap)
        datamap[person] = [labelid, {img_id : 1}]
      else:
        datamap[person][1][img_id] = 1
      datasize+=1

    print('dataset size', args.name, datasize)
    print('dataset err', verr)
    
    print('Creating networks and loading parameters')
    
    with tf.Graph().as_default():
        #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        #sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        sess = tf.Session()
        with sess.as_default():
            pnet, rnet, onet = detect_face.create_mtcnn(sess, None)
    
    minsize = 100 # minimum size of face
    threshold = [ 0.6, 0.7, 0.7 ]  # three steps's threshold
    factor = 0.709 # scale factor

    print(minsize)
    print(threshold)
    print(factor)

    # Add a random key to the filename to allow alignment using multiple processes
    #random_key = np.random.randint(0, high=99999)
    #bounding_boxes_filename = os.path.join(output_dir, 'bounding_boxes_%05d.txt' % random_key)
    output_filename = os.path.join(output_dir, 'faceinsight_align_%s.lst' % args.name)
    
    with open(output_filename, "w") as text_file:
        nrof_images_total = 0
        nrof_successfully_aligned = 0
        nrof_changed = 0
        nrof_iou3 = 0
        nrof_force = 0
        for line in open(args.input_dir, 'r'):
            vec = line.strip().split()
            person = vec[0]
            img_id = int(vec[1])
            v = datamap.get(person, None)
            if v is None:
              continue
            if not img_id in v[1]:
              continue
            labelid = v[0]
            img_str = base64.b64decode(vec[-1])
            nparr = np.fromstring(img_str, np.uint8)
            img = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR)
            img = img[...,::-1] #to rgb
            if nrof_images_total%100==0:
              print("Processing %d, (%d)" % (nrof_images_total, nrof_successfully_aligned))
            nrof_images_total += 1
            target_dir = os.path.join(output_dir, person)
            if not os.path.exists(target_dir):
              os.makedirs(target_dir)
            target_path = os.path.join(target_dir, "%d.jpg"%img_id)
            _minsize = minsize
            fimage = edict()
            fimage.bbox = None
            fimage.image_path = target_path
            fimage.classname = str(labelid)
            if fimage.bbox is not None:
              _bb = fimage.bbox
              _minsize = min( [_bb[2]-_bb[0], _bb[3]-_bb[1], img.shape[0]//2, img.shape[1]//2] )

            bounding_boxes, points = detect_face.detect_face(img, _minsize, pnet, rnet, onet, threshold, factor)
            bindex = -1
            nrof_faces = bounding_boxes.shape[0]
            if fimage.bbox is None and nrof_faces>0:
              det = bounding_boxes[:,0:4]
              img_size = np.asarray(img.shape)[0:2]
              bindex = 0
              if nrof_faces>1:
                bounding_box_size = (det[:,2]-det[:,0])*(det[:,3]-det[:,1])
                img_center = img_size / 2
                offsets = np.vstack([ (det[:,0]+det[:,2])/2-img_center[1], (det[:,1]+det[:,3])/2-img_center[0] ])
                offset_dist_squared = np.sum(np.power(offsets,2.0),0)
                bindex = np.argmax(bounding_box_size-offset_dist_squared*2.0) # some extra weight on the centering
            if fimage.bbox is not None:
              if nrof_faces>0:
                assert(bounding_boxes.shape[0]==points.shape[1])
                det = bounding_boxes[:,0:4]
                img_size = np.asarray(img.shape)[0:2]
                index2 = [0.0, 0]
                for i in xrange(det.shape[0]):
                  _det = det[i]
                  iou = IOU(fimage.bbox, _det)
                  if iou>index2[0]:
                    index2[0] = iou
                    index2[1] = i
                if index2[0]>-0.3:
                  bindex = index2[1]
                  nrof_iou3+=1
              if bindex<0:
                bounding_boxes, points = detect_face.detect_face_force(img, fimage.bbox, pnet, rnet, onet)
                bindex = 0
                nrof_force+=1
                    
            if bindex>=0:

                det = bounding_boxes[:,0:4]
                det = det[bindex,:]
                points = points[:, bindex]
                landmark = points.reshape((2,5)).T
                #points need to be transpose, points = points.reshape( (5,2) ).transpose()
                det = np.squeeze(det)
                bb = det
                points = list(points.flatten())
                assert(len(points)==10)
                warped = face_preprocess.preprocess(img, bbox=bb, landmark = landmark, image_size=args.image_size)
                misc.imsave(target_path, warped)
                nrof_successfully_aligned += 1
                oline = '%d\t%s\t%d' % (1,fimage.image_path, int(fimage.classname))
                #oline = '%d\t%s\t%d\t%d\t%d\t%d\t%d\t' % (0,fimage.image_path, int(fimage.classname), bb[0], bb[1], bb[2], bb[3])
                #oline += '\t'.join([str(x) for x in points])
                text_file.write("%s\n"%oline)
                            
    print('Total number of images: %d' % nrof_images_total)
    print('Number of successfully aligned images: %d' % nrof_successfully_aligned)
    print('Number of changed: %d' % nrof_changed)
    print('Number of iou3: %d' % nrof_iou3)
    print('Number of force: %d' % nrof_force)