summaryrefslogtreecommitdiff
path: root/megapixels/commands/demo/face_search.py
blob: ca0b801669de0f47baeb058f913cdf6b3f29f11c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import click

from app.settings import types
from app.models.dataset import Dataset
from app.utils import click_utils
from app.settings import app_cfg as cfg
from app.utils.logger_utils import Logger

log = Logger.getLogger()

@click.command()
@click.option('-i', '--input', 'opt_fp_in', required=True,
  help='File to lookup')
@click.option('--data_store', 'opt_data_store',
  type=cfg.DataStoreVar,
  default=click_utils.get_default(types.DataStore.SSD),
  show_default=True,
  help=click_utils.show_help(types.DataStore))
@click.option('--dataset', 'opt_dataset',
  type=cfg.DatasetVar,
  required=True,
  show_default=True,
  help=click_utils.show_help(types.Dataset))
@click.option('--results', 'opt_results', default=5, 
  help='Number of match results to display')
@click.option('--gpu', 'opt_gpu', default=0, 
  help='GPU index (use -1 for CPU')
@click.pass_context
def cli(ctx, opt_fp_in, opt_data_store, opt_dataset, opt_results, opt_gpu):
  """Display image info"""
  
  import sys
  from glob import glob
  from os.path import join
  from pathlib import Path
  import time

  import pandas as pd
  import cv2 as cv
  from tqdm import tqdm
  import imutils

  from app.utils import file_utils, im_utils
  from app.models.data_store import DataStore
  from app.processors import face_detector
  from app.processors import face_recognition

  log = Logger.getLogger()
  # init dataset
  dataset = Dataset(opt_data_store, opt_dataset)
  dataset.load_face_vectors()
  dataset.load_records()
  dataset.load_identities()

  # init face detection
  detector = face_detector.DetectorDLIBHOG()

  # init face recognition
  recognition = face_recognition.RecognitionDLIB(gpu=opt_gpu)
  
  # load query image
  im_query = cv.imread(opt_fp_in)

  # get detection as BBox object
  bboxes = detector.detect(im_query, largest=True)
  bbox = bboxes[0]
  dim = im_query.shape[:2][::-1]
  bbox = bbox.to_dim(dim)  # convert back to real dimensions

  if not bbox:
    log.error('No face detected. Exiting')
    return
  
  # extract the face vectors
  vec_query = recognition.vec(im_query, bbox)

  # find matches
  image_records = dataset.find_matches(vec_query, n_results=opt_results)

  # summary
  ims_match = [im_query]
  for image_record in image_records:
    image_record.summarize()
    log.info(f'{image_record.filepath}')
    im_match = cv.imread(image_record.filepath)
    ims_match.append(im_match)

  montages = imutils.build_montages(ims_match, (256, 256), (3,2))

  for i, montage in enumerate(montages):
    cv.imshow(f'{i}', montage)
  # cv gui
  while True:
    k = cv.waitKey(1) & 0xFF
    if k == 27 or k == ord('q'):  # ESC
      cv.destroyAllWindows()
      sys.exit()
    elif k != 255:
      # any key to continue
      break