-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_process.py
176 lines (136 loc) · 5.68 KB
/
image_process.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
import os
import cv2
import csv
import numpy as np
from skimage import measure
import joblib
from tensorflow.keras.models import load_model
MODEL_PATH = 'model/SegModels'
IMAGE_PATH = 'uploads'
target_size = (512, 512)
def segment_image(original_image, mask_image_path):
'''
This function segments the original image using the mask image and saves the segmented image.
Args:
original_image_path (str): The path to the original image.
mask_image_path (str): The path to the mask image.
output_image_path (str): The path to save the segmented image.
Returns:
bool: segmented image
'''
#original_image = cv2.imread(original_image_path)
original_image = cv2.resize(original_image, (512, 512))
mask_image = cv2.imread(mask_image_path, cv2.IMREAD_GRAYSCALE)
mask_image = cv2.resize(mask_image, (512, 512))
# if original_image is None or mask_image is None:
# print(f"Error reading image or mask for {original_image_path}")
# return False
# Make sure the mask is binary
_, binary_mask = cv2.threshold(mask_image, 0, 255, cv2.THRESH_BINARY)
segmented_image = cv2.bitwise_and(original_image, original_image, mask=binary_mask)
return segmented_image
# cv2.imwrite(output_image_path, segmented_image)
# return True
def predict_segmentation(model, preprocessed_image):
prediction = model.predict(preprocessed_image)
prediction = np.squeeze(prediction, axis=0)
prediction = (prediction > 0.5).astype(np.uint8)
return prediction
def calculate_average_rgb(image):
'''
This function calculates the average RGB values of an image.
Args:
image_path (str): The path to the image.
Returns:
numpy.ndarray: The average RGB values as a NumPy array.
'''
average_rgb = np.mean(image, axis=(0, 1))
return average_rgb
def blur_image(image):
'''
This function blurs an image using a Gaussian blur and saves the blurred image.
Args:
Returns:
bool: True if successful, False otherwise.
'''
blurred = cv2.GaussianBlur(image, (5, 5), 0)
_, binary = cv2.threshold(blurred, 127, 255, cv2.THRESH_BINARY)
labels = measure.label(binary, connectivity=2)
properties = measure.regionprops(labels)
largest_region = max(properties, key=lambda x: x.area)
return largest_region
def preprocess_image(image_path, target_size):
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, target_size)
image = image / 255.0
image = np.expand_dims(image, axis=0)
return image
def read_and_reshape_image(image_path, target_size):
image = cv2.imread(image_path)
image = cv2.resize(image, target_size)
image = np.expand_dims(image, axis=0)
print(image.shape)
return image
def image_to_mean_rgb(file_names, body_parts, age, gender):
'''['tongue', 'right_fingernail', 'left_fingernail', 'left_palm', 'right_palm', 'left_eye', 'right_eye']'''
# average_rgb_values = []
results = {}
for i in range(7):
filename = file_names[i]
body_part = body_parts[i]
model_name = ''
print(body_part)
# Load the appropriate model
if body_part.endswith("eye"):
model_name = 'eyelid_model.h5'
elif body_part.endswith("fingernail"):
model_name = 'fingernail_model.h5'
elif body_part.endswith("palm"):
model_name = 'palm_model.h5'
else:
continue
# Load image and model
image_path = os.path.join(IMAGE_PATH, filename)
model_path = os.path.join(MODEL_PATH, model_name)
print(model_name)
model = load_model(model_path)
preprocessed_image = preprocess_image(image_path, target_size)
prediction = predict_segmentation(model, preprocessed_image)
# If the body part is not a fingernail, blur the image using gaussian blur
if not body_part.endswith("fingernail"):
image = prediction * 255
blurred = cv2.GaussianBlur(image, (5, 5), 0)
_, binary = cv2.threshold(blurred, 127, 255, cv2.THRESH_BINARY)
labels = measure.label(binary, connectivity=2)
properties = measure.regionprops(labels)
if len(properties) == 0:
print("No regions found")
largest_region_mask = np.zeros_like(binary)
else:
largest_region = max(properties, key=lambda x: x.area)
largest_region_mask = np.zeros_like(binary)
largest_region_mask[labels == largest_region.label] = 255
mask = largest_region_mask
else:
mask = prediction * 255
cv2.imwrite(os.path.join('mask', filename), mask)
print('blur done')
# Segment the image
original_image = read_and_reshape_image(image_path, target_size)
print(original_image.shape)
segmented_image = segment_image(original_image, os.path.join('mask', filename))
cv2.imwrite(os.path.join('cropped', filename), segmented_image)
print('segmentation done')
# calculate average rgb
average_rgb = calculate_average_rgb(segmented_image)
# average_rgb_values.append(average_rgb)
results[body_part] = average_rgb
results['age'] = age
results['gender'] = gender
return results
if __name__ == '__main__':
values = image_to_mean_rgb(['tongue.jpg', 'right_nail.jpg', 'left_nail.jpg', 'left_palm.jpg', 'right_palm.jpg', 'left_eye.jpg', 'right_eye.jpg'],
['tongue', 'right_fingernail', 'left_fingernail', 'left_palm', 'right_palm', 'left_eye', 'right_eye'])
print(values)
print("Done")