summaryrefslogtreecommitdiff
path: root/megapixels/commands/cv/face_roi.py
blob: d7248aeee6d96aab9df704ae53c9c63e85e049b6 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""
Crop images to prepare for training
"""

import click
# from PIL import Image, ImageOps, ImageFilter, ImageDraw

from app.settings import types
from app.utils import click_utils
from app.settings import app_cfg as cfg

color_filters = {'color': 1, 'gray': 2, 'all': 3}

@click.command()
@click.option('-i', '--input', 'opt_fp_in', default=None,
  help='Override enum input filename CSV')
@click.option('-o', '--output', 'opt_fp_out', default=None,
  help='Override enum output filename CSV')
@click.option('-m', '--media', 'opt_dir_media', default=None,
  help='Override enum media directory')
@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.Dataset))
@click.option('--dataset', 'opt_dataset',
  type=cfg.DatasetVar,
  required=True,
  show_default=True,
  help=click_utils.show_help(types.Dataset))
@click.option('--size', 'opt_size', 
  type=(int, int), default=(300, 300),
  help='Output image size')
@click.option('-t', '--detector-type', 'opt_detector_type',
  type=cfg.FaceDetectNetVar,
  default=click_utils.get_default(types.FaceDetectNet.DLIB_CNN),
  help=click_utils.show_help(types.FaceDetectNet))
@click.option('-g', '--gpu', 'opt_gpu', default=0,
  help='GPU index')
@click.option('--conf', 'opt_conf_thresh', default=0.85, type=click.FloatRange(0,1),
  help='Confidence minimum threshold')
@click.option('-p', '--pyramids', 'opt_pyramids', default=0, type=click.IntRange(0,4),
  help='Number pyramids to upscale for DLIB detectors')
@click.option('--slice', 'opt_slice', type=(int, int), default=(None, None),
  help='Slice list of files')
@click.option('--display/--no-display', 'opt_display', is_flag=True, default=False,
  help='Display detections to debug')
@click.option('-f', '--force', 'opt_force', is_flag=True,
  help='Force overwrite file')
@click.option('--color', 'opt_color_filter', 
  type=click.Choice(color_filters.keys()), default='all',
  help='Filter to keep color or grayscale images (color = keep color')
@click.option('--largest/--all-faces', 'opt_largest', is_flag=True, default=True,
  help='Only keep largest face')
@click.pass_context
def cli(ctx, opt_fp_in, opt_dir_media, opt_fp_out, opt_data_store, opt_dataset, opt_size, opt_detector_type, 
  opt_gpu, opt_conf_thresh, opt_pyramids, opt_slice, opt_display, opt_force, opt_color_filter,
  opt_largest):
  """Converts frames with faces to CSV of ROIs"""
  
  import sys
  import os
  from os.path import join
  from pathlib import Path
  from glob import glob
  
  from tqdm import tqdm
  import numpy as np
  import dlib  # must keep a local reference for dlib
  import cv2 as cv
  import pandas as pd

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

  # -------------------------------------------------
  # init here

  log = logger_utils.Logger.getLogger()

  # set data_store
  data_store = DataStore(opt_data_store, opt_dataset)

  # get filepath out
  fp_out = data_store.metadata(types.Metadata.FACE_ROI) if opt_fp_out is None else opt_fp_out
  if not opt_force and Path(fp_out).exists():
    log.error('File exists. Use "-f / --force" to overwite')
    return
  
  # set detector
  if opt_detector_type == types.FaceDetectNet.CVDNN:
    detector = face_detector.DetectorCVDNN()
  elif opt_detector_type == types.FaceDetectNet.DLIB_CNN:
    detector = face_detector.DetectorDLIBCNN(opt_gpu)
  elif opt_detector_type == types.FaceDetectNet.DLIB_HOG:
    detector = face_detector.DetectorDLIBHOG()
  elif opt_detector_type == types.FaceDetectNet.MTCNN:
    detector = face_detector.DetectorMTCNN()
  elif opt_detector_type == types.FaceDetectNet.HAAR:
    log.error('{} not yet implemented'.format(opt_detector_type.name))
    return

  
  # get list of files to process
  fp_in = data_store.metadata(types.Metadata.FILE_RECORD) if opt_fp_in is None else opt_fp_in
  df_records = pd.read_csv(fp_in).set_index('index')
  if opt_slice:
    df_records = df_records[opt_slice[0]:opt_slice[1]]
  log.debug('processing {:,} files'.format(len(df_records)))

  # filter out grayscale
  color_filter = color_filters[opt_color_filter]

  data = []

  for df_record in tqdm(df_records.itertuples(), total=len(df_records)):
    fp_im = data_store.face_image(str(df_record.subdir), str(df_record.fn), str(df_record.ext))
    im = cv.imread(fp_im)

    # filter out color or grayscale iamges
    if color_filter != color_filters['all']:
      try:
        is_gray = im_utils.is_grayscale(im)
        if is_gray and color_filter != color_filters['gray']:
          log.debug('Skipping grayscale image: {}'.format(fp_im))
          continue
      except Exception as e:
        log.error('Could not check grayscale: {}'.format(fp_im))
        continue
        
    try:
      bboxes = detector.detect(im, size=opt_size, pyramids=opt_pyramids, largest=opt_largest)
    except Exception as e:
      log.error('could not detect: {}'.format(fp_im))
      log.error('{}'.format(e))
      continue

    for bbox in bboxes:
      roi = {
        'record_index': int(df_record.Index),
        'x': bbox.x, 
        'y': bbox.y, 
        'w': bbox.w, 
        'h': bbox.h,
        'image_width': im.shape[1],
        'image_height': im.shape[0]}
      data.append(roi)
    
    # debug display
    if opt_display and len(bboxes):
      bbox_dim = bbox.to_dim(im.shape[:2][::-1])  # w,h
      im_md = im_utils.resize(im, width=min(1200, opt_size[0]))
      for bbox in bboxes:
        bbox_dim = bbox.to_dim(im_md.shape[:2][::-1])
        cv.rectangle(im_md, bbox_dim.pt_tl, bbox_dim.pt_br, (0,255,0), 3)
      cv.imshow('', im_md)
      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

  # save date
  file_utils.mkdirs(fp_out)
  df = pd.DataFrame.from_dict(data)
  df.index.name = 'index'
  df.to_csv(fp_out)