Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. from enum import IntEnum
  2. from pathlib import Path
  3.  
  4. import cv2
  5. import numpy as np
  6.  
  7. from utils.cv2_utils import *
  8. from utils.DFLJPG import DFLJPG
  9. from utils.DFLPNG import DFLPNG
  10.  
  11.  
  12. class SampleType(IntEnum):
  13. IMAGE = 0 #raw image
  14.  
  15. FACE_BEGIN = 1
  16. FACE = 1 #aligned face unsorted
  17. FACE_YAW_SORTED = 2 #sorted by yaw
  18. FACE_YAW_SORTED_AS_TARGET = 3 #sorted by yaw and included only yaws which exist in TARGET also automatic mirrored
  19. FACE_TEMPORAL_SORTED = 4
  20. FACE_END = 4
  21.  
  22. QTY = 5
  23.  
  24. class Sample(object):
  25. def __init__(self, sample_type=None, filename=None, face_type=None, shape=None, landmarks=None, ie_polys=None, pitch_yaw_roll=None, source_filename=None, mirror=None, close_target_list=None, fanseg_mask_exist=False):
  26. self.sample_type = sample_type if sample_type is not None else SampleType.IMAGE
  27. self.filename = filename
  28. self.face_type = face_type
  29. self.shape = shape
  30. self.landmarks = np.array(landmarks) if landmarks is not None else None
  31. self.ie_polys = ie_polys
  32. self.pitch_yaw_roll = pitch_yaw_roll
  33. self.source_filename = source_filename
  34. self.mirror = mirror
  35. self.close_target_list = close_target_list
  36. self.fanseg_mask_exist = fanseg_mask_exist
  37.  
  38. def copy_and_set(self, sample_type=None, filename=None, face_type=None, shape=None, landmarks=None, ie_polys=None, pitch_yaw_roll=None, source_filename=None, mirror=None, close_target_list=None, fanseg_mask=None, fanseg_mask_exist=None):
  39. return Sample(
  40. sample_type=sample_type if sample_type is not None else self.sample_type,
  41. filename=filename if filename is not None else self.filename,
  42. face_type=face_type if face_type is not None else self.face_type,
  43. shape=shape if shape is not None else self.shape,
  44. landmarks=landmarks if landmarks is not None else self.landmarks.copy(),
  45. ie_polys=ie_polys if ie_polys is not None else self.ie_polys,
  46. pitch_yaw_roll=pitch_yaw_roll if pitch_yaw_roll is not None else self.pitch_yaw_roll,
  47. source_filename=source_filename if source_filename is not None else self.source_filename,
  48. mirror=mirror if mirror is not None else self.mirror,
  49. close_target_list=close_target_list if close_target_list is not None else self.close_target_list,
  50. fanseg_mask_exist=fanseg_mask_exist if fanseg_mask_exist is not None else self.fanseg_mask_exist)
  51.  
  52. def load_bgr(self):
  53. # DEBUG: sharpen
  54. face_image = cv2_imread (self.filename)
  55. blurred_image = cv2.GaussianBlur(face_image, [0, 0], 3);
  56. sharpened_image = cv2.addWeighted(face_image, 1.5, blurred_image, -0.5, 0) if self.mirror:
  57. img = sharpened_image.astype(np.float32) / 255.0
  58.  
  59. img = img[:,::-1].copy()
  60. return img
  61.  
  62. def load_fanseg_mask(self):
  63. if self.fanseg_mask_exist:
  64. filepath = Path(self.filename)
  65. if filepath.suffix == '.png':
  66. dflimg = DFLPNG.load ( str(filepath) )
  67. elif filepath.suffix == '.jpg':
  68. dflimg = DFLJPG.load ( str(filepath) )
  69. else:
  70. dflimg = None
  71. return dflimg.get_fanseg_mask()
  72.  
  73. return None
  74.  
  75. def get_random_close_target_sample(self):
  76. if self.close_target_list is None:
  77. return None
  78. return self.close_target_list[randint (0, len(self.close_target_list)-1)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement