Exemplo n.º 1
0
def main():

    path, img = None, None

    mess = '- To index your images: ' \
              'python app.py -path <path-to-your-images-directory>' \
           '- To search on a certain image: ' \
              'python app.py -image <path-to-your-image>'
    try:
        opts, _ = getopt.getopt(sys.argv[1:], 'p:i:h',
                                ['path=', 'image=', 'help'])
    except getopt.GetoptError:
        print mess
        sys.exit(2)

    for opt, arg in opts:
        if opt in ('-p', '--path'):
            if os.path.isdir(arg):
                path = arg
            else:
                print 'Path must link to a directory'
                sys.exit(1)

        if opt in ('-i', '--image'):
            if os.path.isfile(arg):
                img = arg
            else:
                print 'Image path must link to a image'
                sys.exit(1)

        if opt in ('-h', '--help'):
            print mess
            sys.exit(2)

    if path:
        print '--SOURCE PATH:' + path

        prepare_db()

        # Create a temporary directory to store cropped face images
        faces_path = '../images/faces'
        generate_faces(path, faces_path)

        # Generate vectors from images and store them in database
        insert_deep_features(faces_path, 'images')

    if img:
        print '--IMAGE PATH:' + img

        # Detect the face from the image
        face_img = detect_face(img)

        # Get the vector from that face
        vector = get_single_predictions(face_img)

        # Show results
        person, time = search(vector)
        print '--PERSON: ' + str(person)
        print '--SEARCH TIME: ' + str(time)
def frame_face_blur(image, kernel_size=20):
    for face in detect_face(image):
        image[face.top() - 10:face.bottom() + 10,
              face.left() - 10:face.right() + 10] = pixelate(
                  image[face.top() - 10:face.bottom() + 10,
                        face.left() - 10:face.right() + 10],
                  kernel_size=kernel_size)
    return image
Exemplo n.º 3
0
def img_url_to_array_with_face_detect(url):
    junk, face = face_detect.detect_face(url)
    """takes a filename and turns it into a numpy array of RGB pixels"""
    img = open_image_from_url(url)
    if face is not None:
        dress = face_detect.dress_box(face)
        img = img.crop(dress)
    bbox = img.getbbox()
    if bbox[2] != STANDARD_SIZE[0] or bbox[3] != STANDARD_SIZE[1]:
        img = img.resize(STANDARD_SIZE)
    img = list(img.getdata())
    img = map(list, img)
    img = np.array(img)
    s = img.shape[0] * img.shape[1]
    img_wide = img.reshape(1, s)
    return img_wide[0]
Exemplo n.º 4
0
def img_url_to_array_with_face_detect(url):
    junk, face = face_detect.detect_face(url)
    """takes a filename and turns it into a numpy array of RGB pixels"""
    img = open_image_from_url(url)
    if face is not None:
        dress = face_detect.dress_box(face)
        img = img.crop(dress)
    bbox = img.getbbox()
    if bbox[2] != STANDARD_SIZE[0] or bbox[3] != STANDARD_SIZE[1]:
        img = img.resize(STANDARD_SIZE)
    img = list(img.getdata())
    img = map(list, img)
    img = np.array(img)
    s = img.shape[0] * img.shape[1]
    img_wide = img.reshape(1, s)
    return img_wide[0]
def run_ref_image(image_path,
                  uv_kpt_ind,
                  face_ind,
                  triangles,
                  pnet,
                  rnet,
                  onet,
                  x,
                  y,
                  Tsess,
                  minsize=30,
                  threshold=[0.6, 0.7, 0.7],
                  factor=0.709,
                  best_score=0.7,
                  uv_h=256,
                  uv_w=256,
                  image_h=256,
                  image_w=256):

    input_image = cv2.imread(image_path, 1)
    boxes, pnts = face_detect.detect_face(input_image, minsize, pnet, rnet,
                                          onet, threshold, factor)
    faces = process_bbox(boxes, input_image.shape)

    for idx, (x0, y1, x1, y0, conf_score) in enumerate(faces):

        if conf_score > best_score:

            det_face = input_image[int(x0):int(x1), int(y0):int(y1), :]
            det_face = cv2.resize(det_face, (256, 256)) / 255.

            ref_pos = Tsess.run(y,
                                feed_dict={x: det_face[np.newaxis, :, :, :]})
            ref_pos = np.squeeze(ref_pos)
            max_pos = image_h
            ref_pos = ref_pos * max_pos

            ref_texture = cv2.remap(det_face,
                                    ref_pos[:, :, :2].astype(np.float32),
                                    None,
                                    interpolation=cv2.INTER_NEAREST,
                                    borderMode=cv2.BORDER_CONSTANT,
                                    borderValue=(0))

            break

    return ref_texture
Exemplo n.º 6
0
def find_character(query):
    """Download full size images from Google image search.
    Don't print or republish images without permission.
    I used this to train a learning algorithm.
    """
    query = query.replace('/', ' ')
    path = 'tmp/characters'
    BASE_PATH = path
    keywords_i = 0

    if os.path.exists(BASE_PATH + '/' + query + '.jpg'):
        print "Reusing cached image..."
        img = cv2.imread(BASE_PATH + '/' + query + '.jpg')
        results = False
        tries = 0
        while not results and tries < 20:
            print "Trying to find face again"
            results = fd.detect_face(img)
            if float(img.shape[1]) / float(img.shape[0]) > 1.5:
                print "Need to crop %s" % query
                fx, fy, fw, fh = results['face']
                img = img[max(0, fy - 10):min(fy + fh + 10, img.shape[0]),
                          max(0, fx - 10):min(fx + fw + 10, img.shape[1])]
                results = fd.detect_face(img)
                if not results:
                    continue

            tries += 1
        return (results, img)
    if not os.path.exists(BASE_PATH):
        os.makedirs(BASE_PATH)

    start = 0  # Google's start query string parameter for pagination.
    while True:
        if start > 3:
            keywords_i += 1
            start = 0
        keyword = ""
        character_name = ""
        if keywords_i < len(character_lookup_keywords):
            keyword = character_lookup_keywords[keywords_i]
            character_name = query
        elif (keywords_i < 2 *
              len(character_lookup_keywords)) and query.lower().endswith('s'):
            keyword = character_lookup_keywords[keywords_i -
                                                len(character_lookup_keywords)]
            character_name = query[:-1]
        else:
            character_name = 'person'
        print "Searching for " + character_name + " " + keyword + " " + str(
            start)
        BASE_URL = 'https://ajax.googleapis.com/ajax/services/search/images?'\
                 'v=1.0&q=' + character_name + '+' + keyword + '&start=%d'
        r = requests.get(BASE_URL % start)
        for image_info in json.loads(r.text)['responseData']['results']:
            url = image_info['unescapedUrl']
            try:
                image_r = requests.get(url)
            except ConnectionError, e:
                print 'could not download %s' % url
                continue

            # Remove file-system path characters from name.
            title = image_info['titleNoFormatting'].replace('/', '').replace(
                '\\', '')

            file = open(os.path.join(BASE_PATH, '%s.jpg') % query, 'w')
            try:
                Image.open(StringIO(image_r.content)).save(file)
                file.close()
                img = cv2.imread(BASE_PATH + '/' + query + '.jpg')
                # save a copy of the image
                results = fd.detect_face(img)
                if not results:
                    continue
                if float(img.shape[1]) / float(img.shape[0]) > 1.5:
                    print "Need to crop %s" % query
                    fx, fy, fw, fh = results['face']
                    img = img[max(0, fy - 10):min(fy + fh + 10, img.shape[0]),
                              max(0, fx - 10):min(fx + fw + 10, img.shape[1])]
                    results = fd.detect_face(img)
                    if not results:
                        continue
                return (results, img)
            except:
                # Throw away some gifs...blegh.
                print 'could not save %s' % url
                continue
            finally:
                file.close()
        start += 4
def run_one_image(input_image,
                  uv_kpt_ind,
                  face_ind,
                  triangles,
                  uv_coords,
                  pnet,
                  rnet,
                  onet,
                  x,
                  y,
                  Tsess,
                  ref_texture,
                  uv_whole_face,
                  blend_factor=0.35,
                  minsize=30,
                  threshold=[0.6, 0.7, 0.7],
                  factor=0.709,
                  best_score=0.7,
                  uv_h=256,
                  uv_w=256,
                  image_h=256,
                  image_w=256):

    output_image = input_image.copy()
    boxes, pnts = face_detect.detect_face(input_image, minsize, pnet, rnet,
                                          onet, threshold, factor)
    faces = process_bbox(boxes, input_image.shape)
    is_face = False
    for idx, (x0, y1, x1, y0, conf_score) in enumerate(faces):

        if conf_score > best_score:

            is_face = True

            det_face = input_image[int(x0):int(x1), int(y0):int(y1), :]
            template_face = det_face.copy()
            face_shape = (int(y1) - int(y0), int(x1) - int(x0))
            det_face = cv2.resize(det_face, (256, 256)) / 255.

            pos = Tsess.run(y, feed_dict={x: det_face[np.newaxis, :, :, :]})
            pos = np.squeeze(pos)
            max_pos = image_h * 1.1
            pos = pos * max_pos

            vertices = get_vertices(pos, face_ind, uv_h)

            vis_colors = np.ones((vertices.shape[0], 1))
            face_mask = cmesh.render.render_texture(vertices.T,
                                                    vis_colors.T,
                                                    triangles.T,
                                                    image_h,
                                                    image_w,
                                                    c=1)
            face_mask = np.squeeze(face_mask > 0).astype(np.float32)
            '''
            texture = cv2.remap(det_face, pos[:,:,:2].astype(np.float32), 
                        None, interpolation=cv2.INTER_NEAREST, 
                        borderMode=cv2.BORDER_CONSTANT,borderValue=(0))

            new_texture = texture*(1. - uv_whole_face[:,:,np.newaxis]) + ref_texture*uv_whole_face[:,:,np.newaxis]
            '''
            new_texture = ref_texture
            new_colors = get_colors_from_texture(new_texture, face_ind, uv_h)
            new_image = cmesh.render.render_texture(vertices.T,
                                                    new_colors.T,
                                                    triangles.T,
                                                    image_h,
                                                    image_w,
                                                    c=3)

            new_image = blend_factor * det_face * face_mask[:, :, np.newaxis] + (
                1 - blend_factor) * new_image * face_mask[:, :, np.newaxis]
            # new_image = color_hist_match(new_image, det_face*face_mask[:,:,np.newaxis])

            new_image = det_face * (1. -
                                    face_mask[:, :, np.newaxis]) + new_image

            vis_ind = np.argwhere(face_mask > 0)
            vis_min = np.min(vis_ind, 0)
            vis_max = np.max(vis_ind, 0)
            center = (int((vis_min[1] + vis_max[1]) / 2 + 0.5),
                      int((vis_min[0] + vis_max[0]) / 2 + 0.5))

            output = cv2.seamlessClone((new_image * 255.).astype(np.uint8),
                                       (det_face * 255.).astype(np.uint8),
                                       (face_mask * 255.).astype(np.uint8),
                                       center, cv2.NORMAL_CLONE)

            temp_ver_image = cv2.resize(output, face_shape)
            last_ver_image = image_filter(temp_ver_image, template_face)
            output_image[int(x0):int(x1), int(y0):int(y1)] = last_ver_image

    return is_face, output_image
Exemplo n.º 8
0
 def CheckImageFace(self, request, context):
     img_str = base64.b64decode(request.imageBuffer)
     buffer = BytesIO(img_str)
     image = Image.open(buffer)
     checked = detect_face(numpy.array(image))
     return data_pb2.CheckReply(checkResult=str(checked))
Exemplo n.º 9
0

    for k,v in faces.items():
        if faces[k] == None:
            frame = cv2.putText(frame,
                                "Press {} to load up {} face".format(k[0], k.upper()),
                                (0,450), cv2.FONT_HERSHEY_COMPLEX,
                                 1, random_color, thickness=2)
            break

    if not len(preds):
        pane = frame[:90, -90:]
        pane[:,:] = [0,0,0]
        frame[:90, -90:] = pane
    else:
        face_info = detect_face(preds[0], frame)
        #print(face_info)

        # DISPLAYTING THE FACE BEING READ
        pane = frame[:90, -90:]
        pane[:,:] = [0,0,0]

        for i in range(3):
            for j in range(3):
                if (2-j):
                    pane[i*30 : (i+1)*30, -(3-j)*30:-(2-j)*30] = colors[face_info[i][j]]
                else:
                    pane[i*30 : (i+1)*30, -(3-j)*30:] = colors[face_info[i][j]]

        frame[:90, -90:] = pane
Exemplo n.º 10
0
def run_one_image(image_path,
                  uv_kpt_ind,
                  face_ind,
                  triangles,
                  s_uv_coords,
                  pnet,
                  rnet,
                  onet,
                  x,
                  y,
                  Tsess,
                  minsize=30,
                  threshold=[0.6, 0.7, 0.7],
                  factor=0.709,
                  best_score=0.7,
                  uv_h=256,
                  uv_w=256,
                  image_h=256,
                  image_w=256):

    input_image = cv2.imread(image_path, 1)
    output_image = input_image.copy()
    boxes, pnts = face_detect.detect_face(input_image, minsize, pnet, rnet,
                                          onet, threshold, factor)
    faces = process_bbox(boxes, input_image.shape)

    for idx, (x0, y1, x1, y0, conf_score) in enumerate(faces):

        if conf_score > best_score:

            det_face = input_image[int(x0):int(x1), int(y0):int(y1), :]

            face_shape = (int(y1) - int(y0), int(x1) - int(x0))
            det_face = cv2.resize(det_face, (256, 256)) / 255.

            pos = Tsess.run(y, feed_dict={x: det_face[np.newaxis, :, :, :]})
            pos = np.squeeze(pos)
            max_pos = image_h
            pos = pos * max_pos

            vertices = get_vertices(pos, face_ind, uv_h)

            from utils.write import write_obj_with_colors
            save_vertices = vertices.copy()
            save_vertices[:, 1] = image_h - 1 - save_vertices[:, 1]
            colors = get_colors(det_face, vertices)
            write_obj_with_colors(os.path.join('images', 'test' + '_c.obj'),
                                  save_vertices, triangles, colors)

            t_image = (det_face * 255.).astype(np.uint8)
            kpt = get_landmarks(pos, uv_kpt_ind)
            kpt_origin = plot_kpt(det_face, kpt).astype(np.uint8)
            kpt_gray = cv2.cvtColor(kpt_origin, cv2.COLOR_RGB2GRAY)
            ret, kpt_mask = cv2.threshold(kpt_gray, 127, 255,
                                          cv2.THRESH_BINARY)
            kpt_mask = cv2.bitwise_not(kpt_mask)
            kpt_and = cv2.bitwise_and(t_image, t_image, mask=kpt_mask)
            kpt_image = cv2.add(kpt_and, kpt_origin)
            imsave(os.path.join('images', 'test' + '_kpt.jpg'),
                   kpt_image / 255.)

            t_image = (det_face * 255.).astype(np.uint8)
            ver_origin = plot_vertices(det_face, vertices).astype(np.uint8)
            ver_gray = cv2.cvtColor(ver_origin, cv2.COLOR_RGB2GRAY)
            ret, ver_mask = cv2.threshold(ver_gray, 127, 255,
                                          cv2.THRESH_BINARY)
            ver_mask = cv2.bitwise_not(ver_mask)
            ver_and = cv2.bitwise_and(t_image, t_image, mask=ver_mask)
            ver_image = cv2.add(ver_and, ver_origin)
            imsave(os.path.join('images', 'test' + '_ver.jpg'),
                   ver_image / 255.)

            resize_ver_image = cv2.resize(ver_image, face_shape)

            output_image[int(x0):int(x1), int(y0):int(y1)] = resize_ver_image

    return output_image / 255.
Exemplo n.º 11
0
def crop_all(from_root_path, from_dirs, match_names, output_dir):

    if not os.path.isdir(output_dir):
        os.mkdir(output_dir)
        raise RuntimeError('can not find dir %s' % output_dir)

    for from_dir in from_dirs:
        for root, _, filenames in os.walk(os.path.join(from_root_path, from_dir)):

            count = 1
            invalid_pts_count = 0
            inferenced_from_pts_count=0
            total_count = len(filenames) / 2

            for filename in filenames:

                if filename.endswith('.pts'):
                    f = filename.split('.')
                    if not match_names is None and len(match_names)!=0 and not f[0] in match_names:
                        continue

                    jpg = os.path.join(root, f[0] + '.jpg')
                    png = os.path.join(root, f[0] + '.png')
                    if os.path.isfile(jpg):
                        url = jpg
                    elif os.path.isfile(png):
                        url = png
                    else:
                        raise ValueError('unknown file: %s' % os.path.join(root, filename))

                    path = os.path.join(root, filename)
                    pts = list(detect.read_pts(path))

                    def should_reserve(x):
                        return x != 'data' and x != '..' and x != 'image'

                    if count % 10 == 0:
                        print('[%s] %d/%d' % (root, count, total_count))

                    count += 1

                    if [1 for pt in pts if pt[0] < 0 or pt[1] < 0]:
                        # if exists invalid(negative) pt, skip
                        invalid_pts_count += 1
                        continue

                    new_f = '_'.join(filter(should_reserve, os.path.join(root, f[0]).split(os.sep)))
                    meta_path, img_path, pts_path, = os.path.join(output_dir, new_f + '.meta'), os.path.join(output_dir,
                                                                                                             new_f + '.jpg'), os.path.join(
                        output_dir, new_f + '.pts')

                    if not os.path.exists(img_path):
                        # read img file
                        img = cv2.imread(url)

                        # filter face
                        faces = detect.detect_face(img)
                        if len(faces)==0:
                            # can not detect face, use face inerenced from pts
                            inferenced_from_pts_count+=1
                            face=detect.inference_face_from_pts(img_size=img.shape[:2],pts=pts)

                        else:
                            face, pts_num_contained = detect.filter_face(faces, pts)
                            if pts_num_contained < len(pts) / 2:
                                # if the detected face does not include most pts, it is invalid, use face inerenced from pts
                                inferenced_from_pts_count += 1
                                face = detect.inference_face_from_pts(img_size=img.shape[:2], pts=pts)

                        zoom_ratio=1.0
                        while True:
                            face, can_adjust = detect.adjust_face(img.shape[0:2], face, zoom_ratio)

                            if not can_adjust:
                                # adjusted face is out of boundary. use face inerenced from pts
                                inferenced_from_pts_count += 1
                                face = detect.inference_face_from_pts(img_size=img.shape[:2], pts=pts)
                                break

                            _, pts_num_contained = detect.filter_face([face], pts)
                            if len(pts) != pts_num_contained:
                                # some pts is still out of current face, enlarge face
                                zoom_ratio += 0.1
                            else:
                                #current face contains all pts, use it
                                break

                        new_img, new_pts = crop(img, face, pts, size=IMG_SIZE)

                        # write pts file and meta file first
                        with open(pts_path, 'w') as pts_file:
                            pts_file.write(print_pts(new_pts))

                        with open(meta_path, 'w') as meta_file:
                            src_img_path = url
                            if src_img_path.startswith('../'):
                                src_img_path = src_img_path[3:]

                            # meta file contains two lines
                            # line 1 : raw image path
                            # line 2 : face
                            meta_file.write("%s\n%d,%d,%d,%d" % (src_img_path, *face))

                        # then write img, thus if img_path does not exists, it is guaranteed to generate pts file
                        cv2.imwrite(img_path, new_img)                  
    return invalid_pts_count,inferenced_from_pts_count,total_count
Exemplo n.º 12
0
def find_character(query):
    """Download full size images from Google image search.
    Don't print or republish images without permission.
    I used this to train a learning algorithm.
    """
    query = query.replace('/', ' ')
    path = 'tmp/characters'
    BASE_PATH = path
    keywords_i = 0

    if os.path.exists(BASE_PATH + '/' + query + '.jpg'):
        print "Reusing cached image..."
        img = cv2.imread(BASE_PATH + '/' + query + '.jpg')
        results = False
        tries = 0
        while not results and tries < 20:
            print "Trying to find face again"
            results = fd.detect_face(img)
            if float(img.shape[1]) / float(img.shape[0]) > 1.5:
                print "Need to crop %s" % query
                fx, fy, fw, fh = results['face']
                img = img[max(0, fy-10):min(fy+fh+10, img.shape[0]), max(0, fx-10):min(fx+fw+10, img.shape[1])]
                results = fd.detect_face(img)
                if not results:
                  continue 

            tries += 1
        return (results, img)
    if not os.path.exists(BASE_PATH):
        os.makedirs(BASE_PATH)

    start = 0 # Google's start query string parameter for pagination.
    while True:
        if start > 3:
            keywords_i += 1
            start = 0
        keyword = ""
        character_name = ""
        if keywords_i < len(character_lookup_keywords):
            keyword = character_lookup_keywords[keywords_i]
            character_name = query
        elif (keywords_i < 2 * len(character_lookup_keywords)) and query.lower().endswith('s'):
            keyword = character_lookup_keywords[keywords_i - len(character_lookup_keywords)]
            character_name = query[:-1]
        else:
            character_name = 'person'
        print "Searching for " + character_name + " " + keyword + " " + str(start)
        BASE_URL = 'https://ajax.googleapis.com/ajax/services/search/images?'\
                 'v=1.0&q=' + character_name + '+' + keyword + '&start=%d'
        r = requests.get(BASE_URL % start)
        for image_info in json.loads(r.text)['responseData']['results']:
            url = image_info['unescapedUrl']
            try:
                image_r = requests.get(url)
            except ConnectionError, e:
                print 'could not download %s' % url
                continue

            # Remove file-system path characters from name.
            title = image_info['titleNoFormatting'].replace('/', '').replace('\\', '')

            file = open(os.path.join(BASE_PATH, '%s.jpg') % query, 'w')
            try:
                Image.open(StringIO(image_r.content)).save(file)
                file.close()
                img = cv2.imread(BASE_PATH + '/' + query + '.jpg')
                # save a copy of the image
                results = fd.detect_face(img)
                if not results:
                    continue
                if float(img.shape[1]) / float(img.shape[0]) > 1.5:
                    print "Need to crop %s" % query
                    fx, fy, fw, fh = results['face']
                    img = img[max(0, fy-10):min(fy+fh+10, img.shape[0]), max(0, fx-10):min(fx+fw+10, img.shape[1])]
                    results = fd.detect_face(img)
                    if not results:
                        continue 
                return (results, img)
            except:
                # Throw away some gifs...blegh.
                print 'could not save %s' % url
                continue
            finally:
                file.close()
        start += 4