-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpydd.pyx
413 lines (382 loc) · 14.5 KB
/
pydd.pyx
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
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
#pydd.txt
import numpy as np
cimport numpy as np
cimport cython
import cv2
import dlib
DTYPE = np.int
ctypedef np.int_t DTYPE_t
detector = dlib.cnn_face_detection_model_v1('mmod_human_face_detector.dat')
predictor = dlib.shape_predictor('shape_predictor_5_face_landmarks.dat')
def RGBskin(np.ndarray[unsigned char, ndim=3] frame):
cdef int x = frame.shape[0]
cdef int y = frame.shape[1]
cdef np.ndarray[unsigned char, ndim=2] newpic = np.zeros((x, y), dtype=np.uint8)
cdef np.ndarray[unsigned char, ndim=2] allb = frame[:, :, 0]
cdef np.ndarray[unsigned char, ndim=2] allg = frame[:, :, 1]
cdef np.ndarray[unsigned char, ndim=2] allr = frame[:, :, 2]
cdef int r
cdef int g
cdef int b
for i in range(x):
for j in range(y):
r = allr[i, j]
g = allg[i, j]
b = allb[i, j]
if r>50 and g>20 and b>20 and max(b,g,r)-min(b,g,r)>15 and abs(r-g)>15 and r>g and r>b: # g>40
if r>220 and g>210 and b>170 and g>b:
pass
else:
newpic[i,j] = 1
return newpic
def RGBskinerode(np.ndarray[unsigned char, ndim=3] frame):
cdef np.ndarray[unsigned char, ndim=2] kernel = np.ones((5, 5), np.uint8)
cdef int x = frame.shape[0]
cdef int y = frame.shape[1]
cdef np.ndarray[unsigned char, ndim=2] newpic = np.zeros((x, y), dtype=np.uint8)
cdef np.ndarray[unsigned char, ndim=2] allb = frame[:, :, 0]
cdef np.ndarray[unsigned char, ndim=2] allg = frame[:, :, 1]
cdef np.ndarray[unsigned char, ndim=2] allr = frame[:, :, 2]
cdef int r
cdef int g
cdef int b
cdef double sum = 0
cdef int count = 0
for i in range(x):
for j in range(y):
r = allr[i, j]
g = allg[i, j]
b = allb[i, j]
if r>95 and g>40 and b>20 and max(b,g,r)-min(b,g,r)>15 and abs(r-g)>15 and r>g and r>b:
if r>220 and g>210 and b>170 and g>b:
pass
else:
newpic[i,j] = 255
cdef np.ndarray[unsigned char, ndim=2] after_erode = cv2.erode(newpic, kernel)
for i in range(x):
for j in range(y):
if after_erode[i, j] == 255:
sum = sum + allg[i, j]
count = count + 1
cdef double result
result = sum / count
return result
def RGBskinerodepic(np.ndarray[unsigned char, ndim=3] frame):
cdef np.ndarray[unsigned char, ndim=2] kernel = np.ones((5, 5), np.uint8)
cdef int x = frame.shape[0]
cdef int y = frame.shape[1]
cdef np.ndarray[unsigned char, ndim=2] newpic = np.zeros((x, y), dtype=np.uint8)
cdef np.ndarray[unsigned char, ndim=2] allb = frame[:, :, 0]
cdef np.ndarray[unsigned char, ndim=2] allg = frame[:, :, 1]
cdef np.ndarray[unsigned char, ndim=2] allr = frame[:, :, 2]
cdef int r
cdef int g
cdef int b
cdef double sum = 0
cdef int count = 0
for i in range(x):
for j in range(y):
r = allr[i, j]
g = allg[i, j]
b = allb[i, j]
if r>95 and g>40 and b>20 and max(b,g,r)-min(b,g,r)>15 and abs(r-g)>15 and r>g and r>b:
if r>220 and g>210 and b>170 and g>b:
pass
else:
newpic[i,j] = 255
cdef np.ndarray[unsigned char, ndim=2] after_erode = cv2.erode(newpic, kernel)
return after_erode
def hello():
print("hello")
#专用皮肤提取算法
def RGBskinnum(np.ndarray[unsigned char, ndim=3] frame):
cdef int x = frame.shape[0]
cdef int y = frame.shape[1]
cdef np.ndarray[unsigned char, ndim=2] allb = frame[:,:,0]
cdef np.ndarray[unsigned char, ndim=2] allg = frame[:,:,1]
cdef np.ndarray[unsigned char, ndim=2] allr = frame[:,:,2]
cdef double sum = 0
cdef int count = 0
cdef int r
cdef int g
cdef int b
for i in range(x):
for j in range(y):
r = allr[i, j]
g = allg[i, j]
b = allb[i, j]
if r>95 and g>40 and b>20 and max(b,g,r)-min(b,g,r)>15 and abs(r-g)>15 and r>g and r>b:
if r>220 and g>210 and b>170 and g>b:
pass
else:
sum = sum + g
count = count + 1
cdef double result
if count == 0:
result = 1.0
else:
result = sum / count
return result, sum, count
#检验算法
def RGBskincheck(np.ndarray[unsigned char, ndim=3] frame):
cdef int x = frame.shape[0]
cdef int y = frame.shape[1]
cdef np.ndarray[unsigned char, ndim=3] newpic = np.zeros((x, y, 3), dtype=np.uint8)
cdef np.ndarray[unsigned char, ndim=2] allb = frame[:,:,0]
cdef np.ndarray[unsigned char, ndim=2] allg = frame[:,:,1]
cdef np.ndarray[unsigned char, ndim=2] allr = frame[:,:,2]
cdef double sum = 0
cdef int count = 0
cdef int r
cdef int g
cdef int b
for i in range(x):
for j in range(y):
r = allr[i, j]
g = allg[i, j]
b = allb[i, j]
if r>95 and g>40 and b>20 and max(b,g,r)-min(b,g,r)>15 and abs(r-g)>15 and r>g and r>b:
if r>220 and g>210 and b>170 and g>b:
pass
else:
#if g < 150 and g > 145:
newpic[i,j, 0] = b
newpic[i,j, 1] = g
newpic[i,j, 2] = r
sum = sum + g
count = count + 1
cdef double result
if count == 0:
result = 1.0
else:
result = sum / count
return result, sum, count, newpic
#改进的检验算法(三角形)
def RGBskincheckg(np.ndarray[unsigned char, ndim=3] frame):
rects = detector(frame, 0)
shape = predictor(frame, rects[0].rect)
cdef int distancex = rects[0].rect.left()
cdef int distancey = rects[0].rect.top()
cdef float x0 = shape.part(0).x-distancex
cdef float x2 = shape.part(2).x-distancex
cdef float x4 = shape.part(4).x-distancex
cdef float y0 = shape.part(0).y-distancey
cdef float y2 = shape.part(2).y-distancey
cdef float y4 = shape.part(4).y-distancey
################################
#算斜率
cdef float k02 = (y2-y0)/(x2-x0)
cdef float k04 = (y4-y0)/(x4-x0)
cdef float k24 = (y2-y4)/(x2-x4)
cdef float b02 = (x2*y0-x0*y2)/(x2-x0)
cdef float b04 = (x4*y0-x0*y4)/(x4-x0)
cdef float b24 = (x2*y4-x4*y2)/(x2-x4)
################################
cdef np.ndarray[unsigned char, ndim=3] face = frame[rects[0].rect.top():rects[0].rect.bottom(),rects[0].rect.left():rects[0].rect.right()]
cdef int x = face.shape[0]
cdef int y = face.shape[1]
cdef np.ndarray[unsigned char, ndim=3] newpic = np.zeros((x, y, 3), dtype=np.uint8)
cdef np.ndarray[unsigned char, ndim=2] allb = face[:,:,0]
cdef np.ndarray[unsigned char, ndim=2] allg = face[:,:,1]
cdef np.ndarray[unsigned char, ndim=2] allr = face[:,:,2]
cdef double sum = 0
cdef int count = 0
cdef int r
cdef int g
cdef int b
for i in range(x):
for j in range(y):
if i > k02*j + b02 and i < k04*j + b04 and i < k24*j + b24:
r = allr[i, j]
g = allg[i, j]
b = allb[i, j]
if r>95 and g>40 and b>20 and max(b,g,r)-min(b,g,r)>15 and abs(r-g)>15 and r>g and r>b:
if r>220 and g>210 and b>170 and g>b:
pass
else:
if g < 180 and g > 130:
newpic[i,j, 0] = b
newpic[i,j, 1] = g
newpic[i,j, 2] = r
sum = sum + g
count = count + 1
cdef double result
if count == 0:
result = 1.0
else:
result = sum / count
return result, sum, count, newpic
#改进的检验算法(正方形)
def RGBskincheckzf(np.ndarray[unsigned char, ndim=3] frame):
rects = detector(frame, 0)
shape = predictor(frame, rects[0].rect)
cdef int distancex = rects[0].rect.left()
cdef int distancey = rects[0].rect.top()
cdef float x0 = shape.part(0).x-distancex
cdef float x2 = shape.part(2).x-distancex
cdef float x4 = shape.part(4).x-distancex
cdef float y0 = shape.part(0).y-distancey
cdef float y2 = shape.part(2).y-distancey
cdef float y4 = shape.part(4).y-distancey
################################
#算斜率
cdef float k02 = (y2-y0)/(x2-x0)
#cdef float k04 = (y4-y0)/(x4-x0)
#cdef float k24 = (y2-y4)/(x2-x4)
cdef float b02 = y2 - k02*x2
#cdef float b04 = (x4*y0-x0*y4)/(x4-x0)
cdef float b4 = y4-k02*x4
cdef float b021 = -1.2 * b4 + 2.2 * b02
cdef float b44 = 1.8 * b4 - 0.8 *b02
cdef float b022 = -0.5 * b4 + 1.5 * b02
cdef float b023 = 0.3 * b4 + 0.7 * b02
################################
cdef np.ndarray[unsigned char, ndim=3] face = frame[rects[0].rect.top():rects[0].rect.bottom(),rects[0].rect.left():rects[0].rect.right()]
cdef int x = face.shape[0]
cdef int y = face.shape[1]
cdef np.ndarray[unsigned char, ndim=3] newpic = np.zeros((x, y, 3), dtype=np.uint8)
cdef np.ndarray[unsigned char, ndim=2] allb = face[:,:,0]
cdef np.ndarray[unsigned char, ndim=2] allg = face[:,:,1]
cdef np.ndarray[unsigned char, ndim=2] allr = face[:,:,2]
cdef double sum = 0
cdef double count = 0
cdef int r
cdef int g
cdef int b
for i in range(x):
for j in range(y):
if (i > k02*j + b021 and i < k02*j + b44):
r = allr[i, j]
g = allg[i, j]
b = allb[i, j]
if r>95 and g>40 and b>20 and max(b,g,r)-min(b,g,r)>15 and abs(r-g)>15 and r>g and r>b:
if r>220 and g>210 and b>170 and g>b:
pass
else:
#if g < 180 and g > 120:
newpic[i,j, 0] = b
newpic[i,j, 1] = g
newpic[i,j, 2] = r
sum = sum + g
count = count + 1
cdef double result
if count == 0:
result = 1.0
else:
result = sum / count
return result, sum, count, newpic
#
def RGBskinnumpro(np.ndarray[unsigned char, ndim=3] frame):
cdef list ROIlist = []
cdef int x = frame.shape[0]
cdef int y = frame.shape[1]
cdef np.ndarray[unsigned char, ndim=2] allb = frame[:,:,0]
cdef np.ndarray[unsigned char, ndim=2] allg = frame[:,:,1]
cdef np.ndarray[unsigned char, ndim=2] allr = frame[:,:,2]
cdef double sum = 0
cdef int count = 0
cdef int r
cdef int g
cdef int b
for i in range(x):
for j in range(y):
r = allr[i, j]
g = allg[i, j]
b = allb[i, j]
if r>95 and g>40 and b>20 and max(b,g,r)-min(b,g,r)>15 and abs(r-g)>15 and r>g and r>b:
if r>220 and g>210 and b>170 and g>b:
pass
else:
ROIlist.append(g)
cdef np.ndarray[long, ndim=1] ROIarray = np.array(ROIlist)
cdef double m = np.mean(ROIarray)
cdef double mstd = np.std(ROIarray)
cdef double bottom_threshold = m - mstd*1.5
cdef double top_threshold = m + mstd*1.5
for item in ROIarray:
if item > bottom_threshold and item < top_threshold:
sum = sum + item
count = count + 1
cdef double result
result = sum / count
return result
def histo(np.ndarray[unsigned char, ndim=3] frame):
cdef int x = frame.shape[0]
cdef int y = frame.shape[1]
cdef np.ndarray[unsigned char, ndim=2] newpic = np.zeros((x, y), dtype=np.uint8)
cdef np.ndarray[unsigned char, ndim=2] allg = frame[:,:,1]
cdef np.ndarray[unsigned char, ndim=3] frameHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
cdef np.ndarray[unsigned char, ndim=2] H = frameHSV[:,:,0]
cdef double sum = 0
cdef int count = 0
cdef int g
for i in range(x):
for j in range(y):
if 0<H[i,j]<=20:
newpic[i,j] = 255
g = allg[i, j]
sum = sum + g
count = count + 1
cdef double result
result = sum / count
return newpic
def histonumez(np.ndarray[unsigned char, ndim=3] frame):
cdef int x = frame.shape[0]
cdef int y = frame.shape[1]
cdef np.ndarray[unsigned char, ndim=2] allg = frame[:,:,1]
cdef np.ndarray[unsigned char, ndim=3] frameHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
cdef np.ndarray[unsigned char, ndim=2] H = frameHSV[:,:,0]
cdef double sum = 0
cdef int count = 0
cdef int g
for i in range(x):
for j in range(y):
if 0<H[i,j]<=20:
g = allg[i, j]
sum = sum + g
count = count + 1
cdef double result
result = sum / count
return result
def otsu(np.ndarray[unsigned char, ndim=3] frame):
cdef int x = frame.shape[0]
cdef int y = frame.shape[1]
cdef double sum = 0
cdef int count = 0
cdef int g
cdef np.ndarray[unsigned char, ndim=2] newpic = np.zeros((x, y), dtype=np.uint8)
cdef np.ndarray[unsigned char, ndim=3] frameYCC = cv2.cvtColor(frame, cv2.COLOR_BGR2YCrCb)
cdef np.ndarray[unsigned char, ndim=2] Y = frameYCC[:,:,0]
cdef np.ndarray[unsigned char, ndim=2] Cr = frameYCC[:,:,1]
cdef np.ndarray[unsigned char, ndim=2] Cb = frameYCC[:,:,2]
cdef double ret2
cdef np.ndarray[unsigned char, ndim=2] th2
ret2, th2 = cv2.threshold(Y, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
for i in range(frame.shape[0]):
for j in range(frame.shape[1]):
if th2[i, j] > 0 and Y[i, j]< 195:
newpic[i,j] = 255
return newpic
def otsunum(np.ndarray[unsigned char, ndim=3] frame):
cdef int x = frame.shape[0]
cdef int y = frame.shape[1]
cdef double sum = 0
cdef int count = 0
cdef int g
cdef np.ndarray[unsigned char, ndim=3] frameYCC = cv2.cvtColor(frame, cv2.COLOR_BGR2YCrCb)
cdef np.ndarray[unsigned char, ndim=2] Y = frameYCC[:,:,0]
cdef np.ndarray[unsigned char, ndim=2] Cr = frameYCC[:,:,1]
cdef np.ndarray[unsigned char, ndim=2] Cb = frameYCC[:,:,2]
cdef np.ndarray[unsigned char, ndim=2] allg = frame[:,:,1]
cdef double ret2
cdef np.ndarray[unsigned char, ndim=2] th2
ret2, th2 = cv2.threshold(Y, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
for i in range(x):
for j in range(y):
if th2[i, j] > 0 and Y[i, j]< 195:
g = allg[i, j]
sum = sum + g
count = count + 1
cdef double result
result = sum / count
return result