-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaletteProc.py
330 lines (245 loc) · 10.4 KB
/
PaletteProc.py
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
# -*- coding: utf-8 -*-
"""
Standard Procedures
1 - ROI Generator
"""
import cv2
from feat import Detector
from numpy import asarray
import numpy as np
from autocrop import Cropper
from PIL import Image
from skimage.feature import hog
from skimage import data, color, exposure
import pandas as pd
from matplotlib import pyplot as plt
import os
from imutils import build_montages
from imutils import face_utils
# import the necessary packages
from imutils import face_utils
import matplotlib.pyplot as plt
import imutils
import dlib
import cv2
import torch
# imageIn = image_Cropped.copy()
# LandmarkData = np_faceONE
# FacialLandmark = "mouth"
def plt_imshow(title, image):
# convert the image frame BGR to RGB color space and display it
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(12, 12))
plt.imshow(image)
plt.title(title)
plt.grid(False)
plt.show()
# ROI - Region of Interest Generator -------------------------------------------------------------------------------
def ImageROI (imageIn, LandmarkData, FacialLandmark, Verbose = True):
AllowedFeatures = ["mouth","right_eyebrow","left_eyebrow", "right_eye","left_eye","nose", "jaw" ]
featureAllowed = FacialLandmark in AllowedFeatures
if featureAllowed:
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS[FacialLandmark]
rStart = int(rStart)
rEnd = int(rEnd)
# # extract the ROI of the face region as a separate image
(x, y, w, h) = cv2.boundingRect(np.array([LandmarkData[rStart:rEnd]]))
x = x
y = y
h = h
roi = imageIn[y:y + h, x:x + w]
roi = imutils.resize(roi, width=250, inter=cv2.INTER_CUBIC)
# # # show the particular face part
if Verbose == False:
currFeat = "ROI: " + str(FacialLandmark)
plt_imshow(currFeat, roi)
return roi
def ExtendedImageROI (imageIn, LandmarkData, FacialLandmark, Verbose = True):
testImageCropped = imageIn
np_faceONE = LandmarkData
AllowedFeatures = ["mouth", "right_eye","left_eye" ]
print("In Procedure - at facial landmark:" + FacialLandmark)
featureAllowed = FacialLandmark in AllowedFeatures
#proceed = True
roi = 0
if featureAllowed:
if FacialLandmark == "right_eye":
print("Right Eye in Procedure")
LandmarkReferences = [17,18,19,20,21, 27,40,41]
proceed = True
if FacialLandmark == "left_eye":
print("Left Eye in Procedure")
LandmarkReferences = [27,22,23,24,25,26,46,47]
proceed = True
if FacialLandmark == "mouth":
print("At Mouth in Procedure")
#LandmarkReferences = [3,13,8] #All Lower Jaw
LandmarkReferences = [6,33,10] #All Lower Jaw
proceed = True
else:
proceed = False
roi = 0
print("ERROR - Feature NOT ALLOWED")
if proceed == True:
# # extract the ROI of the face region as a separate image
(x, y, w, h) = cv2.boundingRect(np.array([np_faceONE[LandmarkReferences]]))
print("X:" + str(x), " Y:" + str(y) + " w:" + str(w) +" h:" + str(h))
widthAdjust = 0
heightAdjust = 0
roi = testImageCropped[y:y + h + heightAdjust, x:x + w + widthAdjust]
roi = imutils.resize(roi, width=250, inter=cv2.INTER_CUBIC)
# # # show the particular face part
if Verbose == False:
print("Trying to Show Image")
currFeat = "ROI: " + str(FacialLandmark)
plt_imshow(currFeat, roi)
print("Leaving Procedure:::: ROI = " )
return roi
def PaletteMontage(result_Images, Height = 128, Width = 180, Rows = 14, Columns = 6, Verbose = True, Path = "", FileFamily = ""):
## construct the montages for the images
# montages = build_montages(result_Images, (256, 330), (7, 3))
montages = build_montages(result_Images, (128, 180), (14, 6))
i = 1
## loop over the montages and display and or save each of them
for montage in montages:
i = i + 1
if Path != "":
fileName = FileFamily + "_" + i
SaveFileName = Path + "/" + fileName
cv2.imwrite(SaveFileName,montage)
if Verbose == False:
cv2.imshow("Montage - Cluster: %d" % (i), montage)
cv2.waitKey(0)
cv2.destroyAllWindows()
def k_medoids(similarity_matrix, k):
# Step 1: Select initial medoids
num = len(similarity_matrix)
row_sums = torch.sum(similarity_matrix, dim=1)
normalized_sim = similarity_matrix.T / row_sums
normalized_sim = normalized_sim.T
priority_scores = -torch.sum(normalized_sim, dim=0)
values, indices = priority_scores.topk(k)
tmp = -similarity_matrix[:, indices]
tmp_values, tmp_indices = tmp.topk(1, dim=1)
min_distance = -torch.sum(tmp_values)
cluster_assignment = tmp_indices.resize_(num)
print(min_distance)
# Step 2: Update medoids
for i in range(k):
sub_indices = (cluster_assignment == i).nonzero()
sub_num = len(sub_indices)
sub_indices = sub_indices.resize_(sub_num)
sub_similarity_matrix = torch.index_select(similarity_matrix, 0, sub_indices)
sub_similarity_matrix = torch.index_select(sub_similarity_matrix, 1, sub_indices)
sub_row_sums = torch.sum(sub_similarity_matrix, dim=1)
sub_medoid_index = torch.argmin(sub_row_sums)
# update the cluster medoid index
indices[i] = sub_indices[sub_medoid_index]
print(i)
# Step 3: Assign objects to medoids
tmp = -similarity_matrix[:, indices]
tmp_values, tmp_indices = tmp.topk(1, dim=1)
total_distance = -torch.sum(tmp_values)
cluster_assignment = tmp_indices.resize_(num)
print(total_distance)
while (total_distance < min_distance):
min_distance = total_distance
# Step 2: Update medoids
for i in range(k):
sub_indices = (cluster_assignment == i).nonzero()
sub_num = len(sub_indices)
sub_indices = sub_indices.resize_(sub_num)
sub_similarity_matrix = torch.index_select(similarity_matrix, 0, sub_indices)
sub_similarity_matrix = torch.index_select(sub_similarity_matrix, 1, sub_indices)
sub_row_sums = torch.sum(sub_similarity_matrix, dim=1)
sub_medoid_index = torch.argmin(sub_row_sums)
# update the cluster medoid index
indices[i] = sub_indices[sub_medoid_index]
print(total_distance)
# Step 3: Assign objects to medoids
tmp = -similarity_matrix[:, indices]
tmp_values, tmp_indices = tmp.topk(1, dim=1)
total_distance = -torch.sum(tmp_values)
cluster_assignment = tmp_indices.resize_(num)
print(total_distance)
return indices
def plt_imshow(title, image):
# convert the image frame BGR to RGB color space and display it
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(12, 12))
plt.imshow(image)
plt.title(title)
plt.grid(False)
plt.show()
def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
# initialize the dimensions of the image to be resized and
# grab the image size
dim = None
(h, w) = image.shape[:2]
# if both the width and height are None, then return the
# original image
if width is None and height is None:
return image
# check to see if the width is None
if width is None:
# calculate the ratio of the height and construct the
# dimensions
r = height / float(h)
dim = (int(w * r), height)
# otherwise, the height is None
else:
# calculate the ratio of the width and construct the
# dimensions
r = width / float(w)
dim = (width, int(h * r))
# resize the image
resized = cv2.resize(image, dim, interpolation = inter)
# return the resized image
return resized
def Montage_FacePart ( dataFacePart, frameWidth, frameHeight, montageCols, montageRows, Path, FacePart, Cluster ):
import pytz
import datetime
montages = build_montages(dataFacePart,(frameWidth, frameHeight), (montageCols, montageRows))
i = 0
FileFamily = FacePart
print("Path equals: %s" %(Path))
## loop over the montages and display and or save each of them
for montage in montages:
i = i + 1
#if Path != "":
if Path != None:
#Add DateStamp to Filename:
my_date = datetime.datetime.now(pytz.timezone('US/Eastern'))
dateVar = str(my_date.year) + "_" + str(("00" + str(my_date.month))[-2:]) + "_" + str(("00" + str(my_date.day))[-2:]) + "_HR_" + str(("00" + str(my_date.hour))[-2:])
print("SAVING TO DISK")
fileName = FileFamily + "_" + str(i) + dateVar
SaveFileName = Path + "/" + fileName + ".jpg"
print("SAVING: " + SaveFileName)
cv2.imwrite(SaveFileName,montage)
else:
cv2.imshow("Montage - Face Part: %s Cluster: %d Frames: %d" % (FacePart,Cluster, i), montage)
cv2.waitKey(0)
cv2.destroyAllWindows()
def Montage_Cluster ( dataFacePart, frameWidth, frameHeight, montageCols, montageRows, Path, FacePart, ClusterLabel ):
import pytz
import datetime
import cv2
montages = build_montages(dataFacePart,(frameWidth, frameHeight), (montageCols, montageRows))
i = 0
FileFamily = FacePart
my_date = datetime.datetime.now(pytz.timezone('US/Eastern'))
dateVar = str(my_date.year) + "_" + str(("00" + str(my_date.month))[-2:]) + "_" + str(("00" + str(my_date.day))[-2:]) + "_HR_" + str(("00" + str(my_date.hour))[-2:])
print("Path equals: %s" %(Path))
## loop over the montages and display and or save each of them
for montage in montages:
if Path != None:
print("SAVING TO DISK")
fileName = FileFamily + "_" + dateVar
SaveFileName = Path + "/" + fileName + "_CLUSTER_" + str(ClusterLabel) +".jpg"
print("SAVING: " + SaveFileName)
cv2.imwrite(SaveFileName,montage)
else:
i = i + 1
cv2.imshow("Montage - Face Part: %s Cluster: %d Frames: %d" % (FacePart,ClusterLabel, i), montage)
cv2.waitKey(0)
cv2.destroyAllWindows()