Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

Sample.py 4.6 KB

You have to be logged in to leave a comment. Sign In
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
  1. from enum import IntEnum
  2. from pathlib import Path
  3. import cv2
  4. import numpy as np
  5. from core.cv2ex import *
  6. from facelib import LandmarksProcessor
  7. from core import imagelib
  8. from core.imagelib import SegIEPolys
  9. class SampleType(IntEnum):
  10. IMAGE = 0 #raw image
  11. FACE_BEGIN = 1
  12. FACE = 1 #aligned face unsorted
  13. FACE_PERSON = 2 #aligned face person
  14. FACE_TEMPORAL_SORTED = 3 #sorted by source filename
  15. FACE_END = 3
  16. QTY = 4
  17. class Sample(object):
  18. __slots__ = ['sample_type',
  19. 'filename',
  20. 'face_type',
  21. 'shape',
  22. 'landmarks',
  23. 'seg_ie_polys',
  24. 'xseg_mask',
  25. 'xseg_mask_compressed',
  26. 'eyebrows_expand_mod',
  27. 'source_filename',
  28. 'person_name',
  29. 'pitch_yaw_roll',
  30. '_filename_offset_size',
  31. ]
  32. def __init__(self, sample_type=None,
  33. filename=None,
  34. face_type=None,
  35. shape=None,
  36. landmarks=None,
  37. seg_ie_polys=None,
  38. xseg_mask=None,
  39. xseg_mask_compressed=None,
  40. eyebrows_expand_mod=None,
  41. source_filename=None,
  42. person_name=None,
  43. pitch_yaw_roll=None,
  44. **kwargs):
  45. self.sample_type = sample_type if sample_type is not None else SampleType.IMAGE
  46. self.filename = filename
  47. self.face_type = face_type
  48. self.shape = shape
  49. self.landmarks = np.array(landmarks) if landmarks is not None else None
  50. if isinstance(seg_ie_polys, SegIEPolys):
  51. self.seg_ie_polys = seg_ie_polys
  52. else:
  53. self.seg_ie_polys = SegIEPolys.load(seg_ie_polys)
  54. self.xseg_mask = xseg_mask
  55. self.xseg_mask_compressed = xseg_mask_compressed
  56. if self.xseg_mask_compressed is None and self.xseg_mask is not None:
  57. xseg_mask = np.clip( imagelib.normalize_channels(xseg_mask, 1)*255, 0, 255 ).astype(np.uint8)
  58. ret, xseg_mask_compressed = cv2.imencode('.png', xseg_mask)
  59. if not ret:
  60. raise Exception("Sample(): unable to generate xseg_mask_compressed")
  61. self.xseg_mask_compressed = xseg_mask_compressed
  62. self.xseg_mask = None
  63. self.eyebrows_expand_mod = eyebrows_expand_mod if eyebrows_expand_mod is not None else 1.0
  64. self.source_filename = source_filename
  65. self.person_name = person_name
  66. self.pitch_yaw_roll = pitch_yaw_roll
  67. self._filename_offset_size = None
  68. def has_xseg_mask(self):
  69. return self.xseg_mask is not None or self.xseg_mask_compressed is not None
  70. def get_xseg_mask(self):
  71. if self.xseg_mask_compressed is not None:
  72. xseg_mask = cv2.imdecode(self.xseg_mask_compressed, cv2.IMREAD_UNCHANGED)
  73. if len(xseg_mask.shape) == 2:
  74. xseg_mask = xseg_mask[...,None]
  75. return xseg_mask.astype(np.float32) / 255.0
  76. return self.xseg_mask
  77. def get_pitch_yaw_roll(self):
  78. if self.pitch_yaw_roll is None:
  79. self.pitch_yaw_roll = LandmarksProcessor.estimate_pitch_yaw_roll(self.landmarks, size=self.shape[1])
  80. return self.pitch_yaw_roll
  81. def set_filename_offset_size(self, filename, offset, size):
  82. self._filename_offset_size = (filename, offset, size)
  83. def read_raw_file(self, filename=None):
  84. if self._filename_offset_size is not None:
  85. filename, offset, size = self._filename_offset_size
  86. with open(filename, "rb") as f:
  87. f.seek( offset, 0)
  88. return f.read (size)
  89. else:
  90. with open(filename, "rb") as f:
  91. return f.read()
  92. def load_bgr(self):
  93. img = cv2_imread (self.filename, loader_func=self.read_raw_file).astype(np.float32) / 255.0
  94. return img
  95. def get_config(self):
  96. return {'sample_type': self.sample_type,
  97. 'filename': self.filename,
  98. 'face_type': self.face_type,
  99. 'shape': self.shape,
  100. 'landmarks': self.landmarks.tolist(),
  101. 'seg_ie_polys': self.seg_ie_polys.dump(),
  102. 'xseg_mask' : self.xseg_mask,
  103. 'xseg_mask_compressed' : self.xseg_mask_compressed,
  104. 'eyebrows_expand_mod': self.eyebrows_expand_mod,
  105. 'source_filename': self.source_filename,
  106. 'person_name': self.person_name
  107. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...