Update app.py
Browse files
app.py
CHANGED
|
@@ -11,76 +11,96 @@ from datetime import datetime
|
|
| 11 |
def apply_color_transformation(image, transform_type):
|
| 12 |
"""Apply different color transformations to the image"""
|
| 13 |
try:
|
| 14 |
-
|
|
|
|
| 15 |
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 16 |
|
| 17 |
if transform_type == "Original":
|
| 18 |
-
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 19 |
elif transform_type == "Grayscale":
|
| 20 |
-
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 21 |
elif transform_type == "Binary":
|
| 22 |
-
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 23 |
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
|
| 24 |
return binary
|
| 25 |
elif transform_type == "CLAHE":
|
| 26 |
-
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 27 |
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
|
| 28 |
return clahe.apply(gray)
|
| 29 |
return image
|
| 30 |
except Exception as e:
|
| 31 |
-
print(f"
|
| 32 |
return None
|
| 33 |
|
| 34 |
def process_image(image, transform_type):
|
| 35 |
"""Process uploaded image and extract cell features"""
|
| 36 |
try:
|
| 37 |
if image is None:
|
| 38 |
-
return None
|
| 39 |
|
| 40 |
# Store original image for color transformations
|
| 41 |
original_image = image.copy()
|
| 42 |
|
| 43 |
-
#
|
| 44 |
if len(image.shape) == 3:
|
| 45 |
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 46 |
|
| 47 |
-
#
|
| 48 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 49 |
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
|
| 50 |
enhanced = clahe.apply(gray)
|
| 51 |
blurred = cv2.medianBlur(enhanced, 5)
|
| 52 |
|
| 53 |
-
#
|
|
|
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
|
| 65 |
for region in measure.regionprops(markers):
|
| 66 |
if region.area >= 50:
|
| 67 |
y, x = region.centroid
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
| 84 |
plt.style.use('seaborn')
|
| 85 |
fig, axes = plt.subplots(2, 2, figsize=(15, 12))
|
| 86 |
fig.suptitle('Cell Analysis Results', fontsize=16, y=0.95)
|
|
@@ -90,42 +110,38 @@ def process_image(image, transform_type):
|
|
| 90 |
# Distribution plots
|
| 91 |
df['area'].hist(ax=axes[0,0], bins=20, color='skyblue', edgecolor='black')
|
| 92 |
axes[0,0].set_title('Cell Size Distribution')
|
| 93 |
-
axes[0,0].set_xlabel('Area')
|
| 94 |
-
axes[0,0].set_ylabel('Count')
|
| 95 |
|
| 96 |
df['circularity'].hist(ax=axes[0,1], bins=20, color='lightgreen', edgecolor='black')
|
| 97 |
axes[0,1].set_title('Circularity Distribution')
|
| 98 |
-
axes[0,1].set_xlabel('Circularity')
|
| 99 |
-
axes[0,1].set_ylabel('Count')
|
| 100 |
|
| 101 |
-
# Scatter
|
| 102 |
-
axes[1,0].scatter(df['circularity'], df['mean_intensity'],
|
| 103 |
-
alpha=0.6, c='purple')
|
| 104 |
axes[1,0].set_title('Circularity vs Intensity')
|
| 105 |
-
axes[1,0].set_xlabel('Circularity')
|
| 106 |
-
axes[1,0].set_ylabel('Mean Intensity')
|
| 107 |
|
| 108 |
-
#
|
| 109 |
df.boxplot(column=['area', 'circularity'], ax=axes[1,1])
|
| 110 |
-
axes[1,1].set_title('Feature Distributions')
|
| 111 |
else:
|
| 112 |
for ax in axes.flat:
|
| 113 |
ax.text(0.5, 0.5, 'No cells detected', ha='center', va='center')
|
| 114 |
|
| 115 |
plt.tight_layout()
|
| 116 |
|
| 117 |
-
# Apply color transformation
|
| 118 |
-
|
|
|
|
|
|
|
| 119 |
|
| 120 |
return (
|
| 121 |
-
|
| 122 |
-
|
| 123 |
fig,
|
| 124 |
df
|
| 125 |
)
|
|
|
|
| 126 |
except Exception as e:
|
| 127 |
-
print(f"
|
| 128 |
-
return None
|
|
|
|
| 129 |
|
| 130 |
# Create enhanced Gradio interface
|
| 131 |
with gr.Blocks(title="Advanced Cell Analysis Tool", theme=gr.themes.Soft()) as demo:
|
|
|
|
| 11 |
def apply_color_transformation(image, transform_type):
|
| 12 |
"""Apply different color transformations to the image"""
|
| 13 |
try:
|
| 14 |
+
# Convert to BGR if needed
|
| 15 |
+
if len(image.shape) == 3 and image.shape[2] == 3:
|
| 16 |
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 17 |
|
| 18 |
if transform_type == "Original":
|
| 19 |
+
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if len(image.shape) == 3 else image
|
| 20 |
elif transform_type == "Grayscale":
|
| 21 |
+
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if len(image.shape) == 3 else image
|
| 22 |
elif transform_type == "Binary":
|
| 23 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if len(image.shape) == 3 else image
|
| 24 |
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
|
| 25 |
return binary
|
| 26 |
elif transform_type == "CLAHE":
|
| 27 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if len(image.shape) == 3 else image
|
| 28 |
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
|
| 29 |
return clahe.apply(gray)
|
| 30 |
return image
|
| 31 |
except Exception as e:
|
| 32 |
+
print(f"Transformation error: {str(e)}")
|
| 33 |
return None
|
| 34 |
|
| 35 |
def process_image(image, transform_type):
|
| 36 |
"""Process uploaded image and extract cell features"""
|
| 37 |
try:
|
| 38 |
if image is None:
|
| 39 |
+
return [None]*4
|
| 40 |
|
| 41 |
# Store original image for color transformations
|
| 42 |
original_image = image.copy()
|
| 43 |
|
| 44 |
+
# Convert to BGR for OpenCV processing
|
| 45 |
if len(image.shape) == 3:
|
| 46 |
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 47 |
|
| 48 |
+
# Preprocessing pipeline
|
| 49 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 50 |
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
|
| 51 |
enhanced = clahe.apply(gray)
|
| 52 |
blurred = cv2.medianBlur(enhanced, 5)
|
| 53 |
|
| 54 |
+
# Thresholding
|
| 55 |
+
_, thresh = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 56 |
|
| 57 |
+
# Noise removal
|
| 58 |
+
kernel = np.ones((3,3), np.uint8)
|
| 59 |
+
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2)
|
| 60 |
+
|
| 61 |
+
# Sure background area
|
| 62 |
+
sure_bg = cv2.dilate(opening, kernel, iterations=3)
|
| 63 |
+
|
| 64 |
+
# Finding sure foreground area
|
| 65 |
+
dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 5)
|
| 66 |
+
_, sure_fg = cv2.threshold(dist_transform, 0.7*dist_transform.max(), 255, 0)
|
| 67 |
+
sure_fg = np.uint8(sure_fg)
|
| 68 |
+
|
| 69 |
+
# Unknown region
|
| 70 |
+
unknown = cv2.subtract(sure_bg, sure_fg)
|
| 71 |
+
|
| 72 |
+
# Marker labelling
|
| 73 |
+
_, markers = cv2.connectedComponents(sure_fg)
|
| 74 |
+
markers += 1
|
| 75 |
+
markers[unknown == 255] = 0
|
| 76 |
|
| 77 |
+
# Watershed algorithm
|
| 78 |
+
markers = cv2.watershed(image, markers)
|
| 79 |
+
|
| 80 |
+
# Feature extraction
|
| 81 |
+
features = []
|
| 82 |
+
vis_img = image.copy()
|
| 83 |
|
| 84 |
for region in measure.regionprops(markers):
|
| 85 |
if region.area >= 50:
|
| 86 |
y, x = region.centroid
|
| 87 |
+
# Store features
|
| 88 |
+
features.append({
|
| 89 |
+
'label': region.label,
|
| 90 |
+
'area': region.area,
|
| 91 |
+
'circularity': (4 * np.pi * region.area) / (region.perimeter ** 2) if region.perimeter > 0 else 0,
|
| 92 |
+
'mean_intensity': region.mean_intensity
|
| 93 |
+
})
|
| 94 |
+
# Draw text with contrast
|
| 95 |
+
cv2.putText(vis_img, str(region.label), (int(x), int(y)),
|
| 96 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2)
|
| 97 |
+
cv2.putText(vis_img, str(region.label), (int(x), int(y)),
|
| 98 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 1)
|
| 99 |
+
|
| 100 |
+
# Convert visualization image back to RGB
|
| 101 |
+
vis_img = cv2.cvtColor(vis_img, cv2.COLOR_BGR2RGB)
|
| 102 |
+
|
| 103 |
+
# Create analysis plots
|
| 104 |
plt.style.use('seaborn')
|
| 105 |
fig, axes = plt.subplots(2, 2, figsize=(15, 12))
|
| 106 |
fig.suptitle('Cell Analysis Results', fontsize=16, y=0.95)
|
|
|
|
| 110 |
# Distribution plots
|
| 111 |
df['area'].hist(ax=axes[0,0], bins=20, color='skyblue', edgecolor='black')
|
| 112 |
axes[0,0].set_title('Cell Size Distribution')
|
|
|
|
|
|
|
| 113 |
|
| 114 |
df['circularity'].hist(ax=axes[0,1], bins=20, color='lightgreen', edgecolor='black')
|
| 115 |
axes[0,1].set_title('Circularity Distribution')
|
|
|
|
|
|
|
| 116 |
|
| 117 |
+
# Scatter plot
|
| 118 |
+
axes[1,0].scatter(df['circularity'], df['mean_intensity'], alpha=0.6, c='purple')
|
|
|
|
| 119 |
axes[1,0].set_title('Circularity vs Intensity')
|
|
|
|
|
|
|
| 120 |
|
| 121 |
+
# Box plot
|
| 122 |
df.boxplot(column=['area', 'circularity'], ax=axes[1,1])
|
|
|
|
| 123 |
else:
|
| 124 |
for ax in axes.flat:
|
| 125 |
ax.text(0.5, 0.5, 'No cells detected', ha='center', va='center')
|
| 126 |
|
| 127 |
plt.tight_layout()
|
| 128 |
|
| 129 |
+
# Apply color transformation
|
| 130 |
+
transformed_img = apply_color_transformation(original_image, transform_type)
|
| 131 |
+
if transformed_img is not None and len(transformed_img.shape) == 2:
|
| 132 |
+
transformed_img = cv2.cvtColor(transformed_img, cv2.COLOR_GRAY2RGB)
|
| 133 |
|
| 134 |
return (
|
| 135 |
+
vis_img,
|
| 136 |
+
transformed_img if transformed_img is not None else original_image,
|
| 137 |
fig,
|
| 138 |
df
|
| 139 |
)
|
| 140 |
+
|
| 141 |
except Exception as e:
|
| 142 |
+
print(f"Processing error: {str(e)}")
|
| 143 |
+
return [None]*4
|
| 144 |
+
|
| 145 |
|
| 146 |
# Create enhanced Gradio interface
|
| 147 |
with gr.Blocks(title="Advanced Cell Analysis Tool", theme=gr.themes.Soft()) as demo:
|