extract_eyes_center.py 26.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
#!/usr/bin/env python
# encoding: utf-8
# Guillaume HEUSCH <guillaume.heusch@idiap.ch>
# Mon 21 Nov 08:25:54 CET 2016

"""Eyes center extractor for the FARGO images (%(version)s)

Usage:
  %(prog)s [--dbdir=<path>] [--eyesdir=<path>] 
10
           [--verbose ...] [--plot] [--log=<string>]
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

Options:
  -h, --help                Show this screen.
  -V, --version             Show version.
  -d, --dbdir=<path>        The path to the database on your disk.
  -i, --eyesdir=<path>      Where to store saved images.
  -l, --log=<string>        Log filename [default: logs.txt]
  -v, --verbose             Increase the verbosity (may appear multiple times).
  -P, --plot                Show some stuff

Example:

  To get the eyes center, do

    $ %(prog)s --dbdir path/to/database

See '%(prog)s --help' for more information.

"""

import os
import sys
import pkg_resources

import logging
__logging_format__='[%(levelname)s] %(message)s'
logging.basicConfig(format=__logging_format__)
logger = logging.getLogger("extract_log")

from docopt import docopt

version = pkg_resources.require('bob.db.fargo')[0].version

import numpy

import bob.io.base
import bob.io.image
import bob.io.video

import bob.ip.draw
51
import bob.ip.color
52

53
54
55
56
57
58
from ..camera import IntrinsicParameters 
from ..camera import ExtrinsicParameters 
from ..camera import get_UV_map
from ..camera import get_correspondences


59
def get_color_frame(annotation_dir):
60
  """ get_color_frame(annotation_dir) -> frame
61
  
62
63
64
65
66
67
68
69
70
71
72
73
  This function gets the color frame that corresponds to the first annotations.

  **Parameters**

    ``annotation_dir`` (path):
      The dir with the annotations, and the images that were annotated

  **Returns**

    ``frame`` (numpy 3d array):
      The frame where the annotation have been made
  """
74
75
76
77
78
79
80
  annotated_file = os.path.join(annotation_dir, '0.264')
  if os.path.isfile(annotated_file): 
    annotated_stream = bob.io.video.reader(annotated_file)
    for i, frame in enumerate(annotated_stream):
      return frame


81
def get_data_frame(annotation_dir):
82
83
84
  """ get_data_frame(annotation_dir) -> frame

  This function gets the IR frame that corresponds to the first annotations.
85

86
87
88
89
90
91
92
93
94
95
  **Parameters**

    ``annotation_dir`` (path):
      The dir with the annotations, and the images that were annotated

  **Returns**

    ``image`` (numpy 3d array):
      The IR image (rescaled) where the annotation have been made
  """
96
97
98
  bin_file = os.path.join(annotation_dir, '0.bin')
  if os.path.isfile(bin_file): 
    bin_data = numpy.fromfile(bin_file, dtype=numpy.int16).reshape(-1, 640)
99
100
101
102
103
    bin_image = bin_data / 4.0
    bin_image = bin_image.astype('uint8')
    image = numpy.zeros((3, bin_image.shape[0], bin_image.shape[1]), 'uint8')
    image[0] = image[1] = image[2] = bin_image
  return image 
104
105


106
107
108
109
110
111
112
def is_annotation_complete(landmarks):
  """ is_annotation_complete(landmarks) -> True or False
 
  This function checks if the landmarks read from the provided 
  file contain what we need to infer eyes center (i.e. eyes corner)
  
  **Parameters**
113

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    ``landmarks`` (dict):
     The landmarks, read from a txt file and stored as a dict 

  **Returns**

    ``bool`` (boolean):
      True if all eyes corners are present, False otherwise.
  """
  if '1' not in landmarks.keys() or '2' not in landmarks.keys() or '3' not in landmarks.keys() or '4' not in landmarks.keys():
    return False
  return True


def get_eyes_center(landmarks):
  """ get_eyes_center(landmarks) -> eyes_center

  This function computes the position of the eyes center,
  based on eyes corners.

  Note that left and right are defined wrt the imaged subject.

  **Parameters**
136
    
137
138
139
140
141
142
143
144
145
146
147
148
149
    ``landmarks`` (dict):
     The landmarks, read from a txt file and stored as a dict 

  **Returns**

    ``eyes_center`` (tuple):
     Tuple containing the (x, y) position of the right and left eye. 
  """
  reye_x = int(0.5 * (landmarks['1'][1] + landmarks['2'][1]))
  reye_y = int(0.5 * (landmarks['1'][0] + landmarks['2'][0]))
  leye_x = int(0.5 * (landmarks['3'][1] + landmarks['4'][1]))
  leye_y = int(0.5 * (landmarks['3'][0] + landmarks['4'][0]))
  return (reye_x, reye_y, leye_x, leye_y)
150
151
152


def plot_eyes_center(frame, positions):
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
  """ plot_eyes_center(frame, positions)

  This function plots the computed eyes center on the provided image.
  
  **Parameters**
    
    ``frame`` (numpy array):
    The frame on which to draw eyes center. 

    ``positions`` (tuple):
    The position of the center of both eyes.
  """
  display = draw_eyes_center(frame, positions)
  from matplotlib import pyplot
  pyplot.imshow(numpy.rollaxis(numpy.rollaxis(display, 2),2))
  pyplot.title('Retrieved eyes center')
  pyplot.show()

def draw_eyes_center(frame, positions):
  """ draw_eyes_center(frame, positions) -> frame

  This function draws the computed eyes center on the provided image.
  
  **Parameters**
    
    ``frame`` (numpy array):
    The frame on which to draw eyes center. 

    ``positions`` (tuple):
    The position of the center of both eyes.
  """
184
185
186
187
188
  reye_x = positions[0]
  reye_y = positions[1]
  leye_x = positions[2]
  leye_y = positions[3]
  bob.ip.draw.cross(frame, (reye_y, reye_x), 4, (255,0,0)) 
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
  bob.ip.draw.cross(frame, (leye_y, leye_x), 4, (255,0,0)) 
  return frame

def plot_landmarks(frame, annotation_file, retrieved_landmarks):
  """ plot_landmarks(frame, annotation_file, retrieved_landmarks)
  
  This function draws both the original landmarks (provided in the file)
  and the projected ones.
   
  **Parameters**
    
    ``frame`` (numpy array):
      The frame on which to draw eyes center. 

    ``annotation_file`` (path):
      The original annotation file. 

    ``retrieved_landmarks`` (dict):
      The retrieved landmarks (i.e. they may have been obtained
      after reprojection).
  """
  display = draw_landmarks(frame, annotated_file, retrieved_landmarks)
211
  from matplotlib import pyplot
212
213
  pyplot.imshow(numpy.rollaxis(numpy.rollaxis(display, 2),2))
  pyplot.title('Original (red) and projected (green) landmarks')
214
  pyplot.show()
215
216
217

def draw_landmarks(frame, annotation_file, retrieved_landmarks):
  """ draw_landmarks(frame, annotation_file, retrieved_landmarks) -> frame
218
  
219
220
221
222
223
224
225
  This function draws both the original landmarks (provided in the file)
  and the projected ones.
   
  **Parameters**
    
    ``frame`` (numpy array):
      The frame on which to draw eyes center. 
226

227
228
    ``annotation_file`` (path):
      The original annotation file. 
229

230
231
232
    ``retrieved_landmarks`` (dict):
      The retrieved landmarks (i.e. they may have been obtained
      after reprojection).
233
  """
234
235
236
  original_landmarks = get_landmarks(annotation_file)
  for i in original_landmarks.keys():
    bob.ip.draw.plus(frame, (original_landmarks[i][0], original_landmarks[i][1]), 4, (255,0,0))
237
238
  #for i in retrieved_landmarks.keys():  
    #bob.ip.draw.cross(frame, (retrieved_landmarks[i][0], retrieved_landmarks[i][1]), 4, (0,255,0))
239
240
  return frame 

241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
def draw_eyes_corner(frame, annotation_file, draw_landmarks=True):
  """ draw_eyes_corner(frame, annotation_file) -> frame
  
  This function draws the eyes corner (and the landmarks) 
   
  **Parameters**
    
    ``frame`` (numpy array):
      The frame on which to draw eyes center. 

    ``annotation_file`` (path):
      The original annotation file. 

    ``draw_landmarks`` (boolean):
      Also draws the original landmarks.
  """
  original_landmarks = get_landmarks(annotation_file)
  for i in original_landmarks.keys():
    if i == '1': 
      bob.ip.draw.plus(frame, (original_landmarks[i][0], original_landmarks[i][1]), 4, (255,255,255))
    if i == '2': 
      bob.ip.draw.plus(frame, (original_landmarks[i][0], original_landmarks[i][1]), 4, (255,0,0))
    if i == '3': 
      bob.ip.draw.plus(frame, (original_landmarks[i][0], original_landmarks[i][1]), 4, (0,255,0))
    if i == '4': 
      bob.ip.draw.plus(frame, (original_landmarks[i][0], original_landmarks[i][1]), 4, (0,0,255))
    if draw_landmarks:
      bob.ip.draw.cross(frame, (retrieved_landmarks[i][0], retrieved_landmarks[i][1]), 4, (0,0,0))
  return frame 

271
272
273
274
275
276

def read_landmarks(pos_filename):
  """ read_landmarks(pos_filename) -> landmarks

  This function read landmarks from file.
  It is used to be compliant with camera utilities.
277
  
278
279
280
281
282
283
284
285
286
  **Parameters**
    
    ``pos_filename`` (path):
      The original annotation file. 
   
   **Returns**

    ``landmarks`` (numpy array (16x2)):
      Array continaing the landmarks 
287
288

  """
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
  landmarks = numpy.zeros((16, 2), dtype=numpy.int32)
  if not os.path.exists(pos_filename):
    logging.warning('{} does not exist'.format(pos_filename))
    return []
  with open(pos_filename, 'r') as f:
    for line in f:
      line_split = line.split()
      if len(line_split) != 3:
        continue
      idx = int(line_split[0])
      landmarks[idx - 1, :] = (int(line_split[1]), int(line_split[2]))
  return landmarks


def load_calibration(recording_dir):
  """ load_calibration(recording_dir) -> depth_intrinsics, color_intrinsics, depth2color 
 
  This function loads camera related parameters from a file.

  **Parameters**
    
    ``recording_dir`` (path):
      The path to the directory corresponding to the recording. 
   
   **Returns**

    ``depth_intrinsics`` (:class: ..camera.IntrinsicParameters):
      Intrinsic parameters of the depth camera 
    
    ``color_intrinsics`` (:class: ..camera.IntrinsicParameters):
      Intrinsic parameters of the color camera 

    ``depth2color`` (:class: ..camera.ExtrinsicParameters):
      Extrinsic parameters relating the depth to the color 
  """
  device_info_filepath = os.path.join(recording_dir, 'device_info.json')
  if not os.path.exists(device_info_filepath):
    logging.error('{} does not exist'.format(device_info_filepath))
  depth_intrinsics = IntrinsicParameters()
  depth_intrinsics.read_json(device_info_filepath, 'depth_intrinsics')
  color_intrinsics = IntrinsicParameters()
  color_intrinsics.read_json(device_info_filepath, 'color_intrinsics')
  depth2color = ExtrinsicParameters()
  depth2color.read_json(device_info_filepath, 'extrinsics')
  return depth_intrinsics, color_intrinsics, depth2color


def project_ir_to_color(ir_file, recording_dir):
  """ project_ir_to_color(ir_file, recording_dir) -> color_landmarks

  This function projects landmarks from the IR image to the
  color image referential.

  **Parameters**
    
    ``ir_file`` (path):
      The path to the file containing the IR landmarks.
   
    ``recording_dir`` (path):
      The path to the directory corresponding to the recording. 
   
   **Returns**

    ``color_landmarks`` (dict):
      The projected landmarks.
  """
  min_depth = 200
  max_pixel_distance = 100
  ir_landmarks = read_landmarks(ir_file)
  depth_file = os.path.join(recording_dir, 'annotations/depth/0.bin') 
  depth_frame = numpy.fromfile(depth_file, dtype=numpy.int16).reshape(-1, 640)
  depth_intrinsics, color_intrinsics, depth2color = load_calibration(recording_dir)
  UV_map = get_UV_map(depth_frame, min_depth, depth_intrinsics, 1e-3, color_intrinsics, depth2color)

  c = 1
  color_landmarks = {}
  for idx, l in enumerate(ir_landmarks):
    min_dist2 = numpy.inf
    min_color = (-1, -1)
    
    for row in range(l[1] - max_pixel_distance, l[1] + max_pixel_distance):
      for col in range(l[0] - max_pixel_distance, l[0] + max_pixel_distance):
        
        if row < 0 or col < 0 or row >= depth_intrinsics.height or col >= depth_intrinsics.width:
          continue
        
        color_col = UV_map[0][row, col] * color_intrinsics.width
        color_row = UV_map[1][row, col] * color_intrinsics.height
        
        if not numpy.isnan(color_col) and not numpy.isnan(color_row):
          dist2 = (l[1] - row) * (l[1] - row) + (l[0] - col) * (l[0] - col)
          if dist2 < min_dist2:
            min_dist2 = dist2
            min_color = (color_row, color_col)
    
    if not numpy.isinf(min_dist2):
      color_col = min_color[1]
      color_row = min_color[0]
      color_landmarks[str(c)] = (int(color_row), int(color_col))
    c += 1

  return color_landmarks


def project_color_to_ir(color_file, recording_dir):
  """ project_color_to_ir(color_file, recording_dir) -> ir_landmarks
  
  This function projects landmarks from the color image to the
  IR image referential.

  **Parameters**
    
    ``color_file`` (path):
      The path to the file containing the color landmarks.
   
    ``recording_dir`` (path):
      The path to the directory corresponding to the recording. 
   
   **Returns**

    ``ir_landmarks`` (dict):
      The projected landmarks.
  """
  min_depth = 200
  max_pixel_distance = 100

  color_landmarks = read_landmarks(color_file)
  depth_file = os.path.join(recording_dir, 'annotations/depth/0.bin') 
  depth_frame = numpy.fromfile(depth_file, dtype=numpy.int16).reshape(-1, 640)
  depth_intrinsics, color_intrinsics, depth2color = load_calibration(recording_dir)
  UV_map = get_UV_map(depth_frame, min_depth, depth_intrinsics, 1e-3, color_intrinsics, depth2color)
  min_dist2, min_idx = get_correspondences(color_landmarks, UV_map, 
                                           color_intrinsics.width, 
                                           color_intrinsics.height,
                                           depth_intrinsics.width,
                                           depth_intrinsics.height)

  ir_landmarks = {}
  c = 1
  for idx in range(min_idx.shape[0]):
    ir_landmarks[str(c)] = (int(min_idx[idx] / depth_intrinsics.width), int(min_idx[idx] % depth_intrinsics.width))
    c += 1

  return ir_landmarks


def get_landmarks(annotation_file):
  """ get_landmarks(annotation_file) -> landmarks
  
  This function reads landmarks from a file and load
  them into a dictionary.

441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460

  Note: left and right are defined in terms of subject
  Landmarks:
  1 right corner of right eye 
  2 left corner of right eye 
  3 right corner of left eye 
  4 left corner of left eye 
  5
  6
  7
  8
  9
  10
  11
  12
  13
  14
  15
  16

461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
  **Parameters**
    
    ``annotated_file`` (path):
      The path to the file containing the landmarks.
   
   **Returns**

    ``landmarks`` (dict):
      The landmarks.
  """
  with open(annotation_file, "r") as c:
    landmarks = {}
    for line in c:
      line = line.rstrip()
      ints = line.split()
      landmarks[ints[0]] = ((int(ints[2]), int(ints[1])))

    return landmarks

def write_eyes_pos(eyes, eyes_dir):
  """ write_eyes_pos(eyes, eyes_dir)

  This function write the eyes center in text file(s).

  **Parameters**
    
    ``eyes`` (tuple):
      tuple containing the (x,y) coordinates of the eyes center.
   
    ``eyes_dir`` (path):
      The path to the dir where the file(s) are written.
  """ 
  if not os.path.isdir(eyes_dir):
    os.makedirs(eyes_dir)
  for i in range(0, 10):
    eyes_filename = os.path.join(eyes_dir, '{:0>2d}.pos'.format(i))
    eyes_file = open(eyes_filename, 'w')
    eyes_file.write('{0} {1} {2} {3}'.format(eyes[0], eyes[1], eyes[2], eyes[3]))
    eyes_file.close()

501
502
503
504
505
506
507
508
509
def annotate(image_file, eyes, annotation_dir):

  # create examplar pos file
  f = open('00.pos', 'w')
  f.write('InR InL\n')
  f.write('0 {0} {1} {2} {3}'.format(eyes[0], eyes[1], eyes[2], eyes[3]))
  f.close()

  os.system('./bin/annotate.py ' + image_file + ' 00.pos --output temp.txt')
510
  input("Press Enter to terminate.")
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530

  # read eyes center from temp annotation file
  f1 = open('temp.txt', 'r')
  for line in f1:
    line = line.rstrip()
    data = line.split()
    if data[0] == '0':
      eyes = (int(data[1]), int(data[2]), int(data[3]), int(data[4]))
  f1.close()
  return eyes


def annotations_exist(annotation_dir):

  for i in range(0,10):
    annotation_file = os.path.join(annotation_dir, '{:0>2d}.pos'.format(i))
    if not os.path.isfile(annotation_file):
      return False
  return True
  
531
532
533
534
535

def main(user_input=None):
  """
  Main function to extract eyes center from existing annotations.
  """
536
537
538
539
540
541
542
543

  # Parse the command-line arguments
  if user_input is not None:
      arguments = user_input
  else:
      arguments = sys.argv[1:]

  prog = os.path.basename(sys.argv[0])
544
545
546
  completions = dict(prog=prog,version=version,)
  args = docopt(__doc__ % completions,argv=arguments,
                version='Eyes position extractor (%s)' % version,)
547
548
549
550
551
552
553
554
555
556
557
558
559

  # if the user wants more verbosity, lowers the logging level
  if args['--verbose'] == 1: logging.getLogger("extract_log").setLevel(logging.INFO)
  elif args['--verbose'] >= 2: logging.getLogger("extract_log").setLevel(logging.DEBUG)

  if args['--dbdir'] is None:
    logger.warning("You should provide a valid path to the data")
    sys.exit()

  base_dir = args['--dbdir']
  if not os.path.isdir(args['--eyesdir']):
    os.mkdir(args['--eyesdir'])

560
  logfile = open(args['--log'], 'w')
561
562
563
564
 
  # python 2 / 3 compatibility
  from builtins import input

565
566
567
568
569
570
  # to compute various stats
  total_counter = 0
  inexisting_counter = 0
  incomplete_counter = 0
  projection_counter = 0
  heuristic_counter = 0
571

572
573
574
575
576
577
578
579
580
581
582
583
584
585
  # go through the subjects 
  for subject in os.listdir(base_dir):

    sessions = ['controlled', 'dark', 'outdoor']
    # small hack to process FdV subjects ...
    if int(subject) >= 129:
      sessions = ['fdv']

    for session in sessions: 
      session_dir = os.path.abspath(os.path.join(base_dir, subject, session))
      
      for condition in ['SR300-laptop', 'SR300-mobile']:
        
        for recording in ['0', '1']:
586
          logger.debug("===== Subject {0}, session {1}, device {2}, recording {3} ...".format(subject, session, condition, recording))
587
588
589

          # create directories to save the extracted annotations 
          if not os.path.isdir(os.path.join(args['--eyesdir'], subject, session, condition, recording)):
590
            os.makedirs(os.path.join(args['--eyesdir'], subject, session, condition, recording))
591
          if not os.path.isdir(os.path.join(args['--eyesdir'], subject, session, condition, recording, 'color')):
592
            os.makedirs(os.path.join(args['--eyesdir'], subject, session, condition, recording, 'color'))
593
          if not os.path.isdir(os.path.join(args['--eyesdir'], subject, session, condition, recording, 'ir')):
594
            os.makedirs(os.path.join(args['--eyesdir'], subject, session, condition, recording, 'ir'))
595
          if not os.path.isdir(os.path.join(args['--eyesdir'], subject, session, condition, recording, 'depth')):
596
            os.makedirs(os.path.join(args['--eyesdir'], subject, session, condition, recording, 'depth'))
597

598

599
600
          # the directories - input
          recording_dir = os.path.join(session_dir, condition, recording)
601
602
603
604
          color_dir = os.path.join(session_dir, condition, recording, 'annotations', 'color')
          ir_dir = os.path.join(session_dir, condition, recording, 'annotations', 'ir')
          depth_dir = os.path.join(session_dir, condition, recording, 'annotations', 'depth')

605
606
607
608
609
          # the directories - output
          color_eyes_dir = os.path.join(args['--eyesdir'], subject, session, condition, recording, 'color')
          ir_eyes_dir = os.path.join(args['--eyesdir'], subject, session, condition, recording, 'ir')
          depth_eyes_dir = os.path.join(args['--eyesdir'], subject, session, condition, recording, 'depth')
          
610
611
612
613
614
615
          # check if annotations for this recording already exists
          if annotations_exist(color_eyes_dir) and annotations_exist(ir_eyes_dir) and annotations_exist(depth_eyes_dir):
            logger.warn('Existing annotations for {0}'.format(recording_dir))
            continue
          
          # read the original landmarks - color
616
          color_file = os.path.join(color_dir, '0.pos')
617
          try:
618
            color_landmarks = get_landmarks(color_file)
619
          except IOError:
620
            logger.warn("No color annotations for recording {0}".format(recording_dir))
621
            logfile.write('[NO ANNOTATIONS] ' + color_dir + '\n')
622
            inexisting_counter += 1
623

624
          # read the original landmarks - ir
625
          ir_file  = os.path.join(ir_dir, '0.pos')
626
          try:
627
            ir_landmarks = get_landmarks(ir_file)
628
          except IOError:
629
            logger.warn("No ir annotations for recording {0}".format(recording_dir))
630
            logfile.write('[NO ANNOTATIONS] ' + ir_dir + '\n')
631
          
632
          # read the original landmarks - depth
633
634
635
          depth_file  = os.path.join(depth_dir, '0.pos')
          try:
            depth_landmarks = get_landmarks(ir_file)
636
          except IOError:
637
            logger.warn("No depth annotations for recording {0}".format(recording_dir))
638
            logfile.write('[NO ANNOTATIONS] ' + depth_dir + '\n')
639

640
          # IR TO COLOR : check if the original landmarks are complete - if not, try to reproject
641
642
          if not is_annotation_complete(color_landmarks) and is_annotation_complete(ir_landmarks):
            logger.warn("Projecting IR to color for recording {0}".format(recording_dir))
643
            logfile.write('[PROJECT] ' + recording_dir + '\n')
644
            projection_counter += 1
645
646
647
648
649
650
651
652
653
654
655
656
657
658
           
            # get color landmarks and eyes from reprojection
            color_landmarks = project_ir_to_color(ir_file, recording_dir)
            color_eyes = get_eyes_center(color_landmarks)
           
            # plot the result of the reprojection
            from matplotlib import pyplot
            color_frame = get_color_frame(color_dir)
            display_color = draw_eyes_center(numpy.copy(color_frame), color_eyes)
            pyplot.title("Projected to color")
            pyplot.imshow(numpy.rollaxis(numpy.rollaxis(display_color, 2),2))
            pyplot.show()

            # ask for re-annotation (i.e. reprojection is not good)
659
            reannotate = input("Want to re-annotate color ? [y/n]: ")
660
661
662
663
664
665
666
667
668
669
670
671
672
673
            if reannotate == 'y':

              # get eyes position in the color frame
              bob.io.base.save(color_frame, '00.png') 
              color_eyes = annotate('00.png', color_eyes, color_eyes_dir)
              
              # save everything and move to the next recording
              write_eyes_pos(color_eyes, color_eyes_dir)
              write_eyes_pos(get_eyes_center(ir_landmarks), ir_eyes_dir)
              write_eyes_pos(get_eyes_center(depth_landmarks), depth_eyes_dir)
              logger.warn("Eyes position saved from the manual annotation")
              continue

          # COLOR TO IR : check if the original landmarks are complete - if not, try to reproject
674
675
          if not is_annotation_complete(ir_landmarks) and is_annotation_complete(color_landmarks):
            logger.warn("Projecting color to IR for recording {0}".format(recording_dir))
676
            logfile.write('[PROJECT] ' + recording_dir + '\n')
677
            projection_counter += 1
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
            
            ir_landmarks = project_color_to_ir(color_file, recording_dir)
            ir_eyes = get_eyes_center(ir_landmarks)
            depth_landmarks = ir_landmarks
            depth_eyes = ir_eyes 

            # plot the projected eyes position 
            from matplotlib import pyplot
            ir_frame = get_data_frame(ir_dir)
            display_ir = draw_eyes_center(numpy.copy(ir_frame), ir_eyes)
            pyplot.title('Projected to IR')
            pyplot.imshow(numpy.rollaxis(numpy.rollaxis(display_ir, 2),2))
            pyplot.show()
            
            # ask for re-annotation (i.e. reprojection is not good)
693
            reannotate = input("Want to re-annotate IR ? [y/n]: ")
694
695
696
697
698
699
700
701
702
703
704
705
706
            if reannotate == 'y':
              
              # get eyes position in the IR frame
              bob.io.base.save(ir_frame, '00.png') 
              ir_eyes = annotate('00.png', ir_eyes, ir_eyes_dir)
              depth_eyes = ir_eyes
              
              # save everything and move to the next recording
              write_eyes_pos(color_eyes, color_eyes_dir)
              write_eyes_pos(ir_eyes, ir_eyes_dir)
              write_eyes_pos(depth_eyes, depth_eyes_dir)
              logger.warn("Eyes position saved from the manual annotation")
              continue
707
708
709
710
711
712
713
714
715
716
717
718
719
720

          # check if we have all we need (possibly after re-projection)
          if is_annotation_complete(color_landmarks) and is_annotation_complete(ir_landmarks) and is_annotation_complete(depth_landmarks):

            # get the eyes center 
            color_eyes = get_eyes_center(color_landmarks)
            ir_eyes = get_eyes_center(ir_landmarks)
            depth_eyes = get_eyes_center(depth_landmarks)

            # and save the file(s)
            write_eyes_pos(color_eyes, color_eyes_dir)
            write_eyes_pos(ir_eyes, ir_eyes_dir)
            write_eyes_pos(depth_eyes, depth_eyes_dir)
          
721
          # annotate both color and IR when eyes center could not be retrieved from any of the annotations
722
          else:
723
724
            logger.warn("Annotations needed for recording {0}".format(recording_dir))
            logfile.write('[ANNOTATIONS] ' + recording_dir + '\n')
725
            heuristic_counter += 1
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
           
            color_eyes = (869, 510, 1040, 509)
            color_frame = get_color_frame(color_dir)
            bob.io.base.save(color_frame, '00.png') 
            color_eyes = annotate('00.png', color_eyes, color_eyes_dir)
           
            ir_eyes = (270, 235, 329, 234)
            ir_frame = get_data_frame(ir_dir)
            bob.io.base.save(ir_frame, '00.png') 
            ir_eyes = annotate('00.png', ir_eyes, ir_eyes_dir)
            depth_eyes = ir_eyes
            
            # and save the file(s)
            write_eyes_pos(color_eyes, color_eyes_dir)
            write_eyes_pos(ir_eyes, ir_eyes_dir)
            write_eyes_pos(depth_eyes, depth_eyes_dir)

          # plot stuff if asked for 
          if bool(args['--plot']):
            color_frame = get_color_frame(color_dir)
746
747
            display_color = color_frame.copy() 
            display_color = draw_eyes_center(display_color, color_eyes)
748
749
750
751
752
753
754
755
756
757
758
759
            ir_frame = get_data_frame(ir_dir)
            display_ir = draw_eyes_center(ir_frame, ir_eyes)
            from matplotlib import pyplot
            f, axarr = pyplot.subplots(1, 2)
            pyplot.suptitle('Inferred eyes center')
            axarr[0].imshow(numpy.rollaxis(numpy.rollaxis(display_color, 2),2))
            axarr[0].set_title("Color")
            axarr[1].imshow(numpy.rollaxis(numpy.rollaxis(display_ir, 2),2))
            axarr[1].set_title("NIR")
            pyplot.show()

            if args['--verbose'] >= 2: 
760
              display_color = color_frame.copy() 
761
762
763
764
765
766
767
768
              display_color = draw_landmarks(color_frame, color_file, color_landmarks)
              display_ir = draw_landmarks(ir_frame, ir_file, ir_landmarks)
              f, axarr = pyplot.subplots(1, 2)
              pyplot.suptitle('Landmarks')
              axarr[0].imshow(numpy.rollaxis(numpy.rollaxis(display_color, 2),2))
              axarr[0].set_title("Color")
              axarr[1].imshow(numpy.rollaxis(numpy.rollaxis(display_ir, 2),2))
              axarr[1].set_title("NIR")
769
              pyplot.show()
770

771
772
773
774
775
776
777
778
          
          total_counter += 1

  logger.info("Processed {0} sequences".format(total_counter))
  logger.info("\t {0} had no annotations".format(inexisting_counter))
  logger.info("\t {0} needed reprojection".format(projection_counter))
  logger.info("\t {0} where incomplete".format(incomplete_counter))
  logger.info("\t {0} needed heuristic (incomplete after reprojection)".format(heuristic_counter))
779
780

  logfile.close()