Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,254 +1,277 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
import os
|
|
|
|
| 4 |
import tempfile
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
import
|
| 8 |
-
import
|
| 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 |
-
return
|
| 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 |
if __name__ == "__main__":
|
| 254 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import zipfile
|
| 3 |
import tempfile
|
| 4 |
+
import requests
|
| 5 |
+
import numpy as np
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import torch
|
| 9 |
+
import torch.nn.functional as F
|
| 10 |
+
from torchvision import transforms
|
| 11 |
+
from torchvision.models import resnet50, ResNet50_Weights
|
| 12 |
+
from sklearn.cluster import MiniBatchKMeans
|
| 13 |
+
import matplotlib.pyplot as plt
|
| 14 |
+
import io
|
| 15 |
+
from datetime import datetime
|
| 16 |
+
|
| 17 |
+
import gradio as gr
|
| 18 |
+
|
| 19 |
+
# Face analysis
|
| 20 |
+
from deepface import DeepFace
|
| 21 |
+
import cv2
|
| 22 |
+
|
| 23 |
+
# ---------------------------
|
| 24 |
+
# Force CPU if no CUDA
|
| 25 |
+
# ---------------------------
|
| 26 |
+
if not torch.cuda.is_available():
|
| 27 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
| 28 |
+
|
| 29 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 30 |
+
|
| 31 |
+
# ---------------------------
|
| 32 |
+
# Load ResNet50
|
| 33 |
+
# ---------------------------
|
| 34 |
+
weights = ResNet50_Weights.DEFAULT
|
| 35 |
+
model = resnet50(weights=weights).to(device)
|
| 36 |
+
model.eval()
|
| 37 |
+
|
| 38 |
+
# ---------------------------
|
| 39 |
+
# Transformations
|
| 40 |
+
# ---------------------------
|
| 41 |
+
transform = transforms.Compose([
|
| 42 |
+
transforms.Resize(256),
|
| 43 |
+
transforms.CenterCrop(224),
|
| 44 |
+
transforms.ToTensor(),
|
| 45 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
| 46 |
+
std=[0.229, 0.224, 0.225]),
|
| 47 |
+
])
|
| 48 |
+
|
| 49 |
+
# ---------------------------
|
| 50 |
+
# ImageNet labels
|
| 51 |
+
# ---------------------------
|
| 52 |
+
LABELS_URL = "https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt"
|
| 53 |
+
imagenet_classes = [line.strip() for line in requests.get(LABELS_URL).text.splitlines()]
|
| 54 |
+
|
| 55 |
+
# ---------------------------
|
| 56 |
+
# Color utilities
|
| 57 |
+
# ---------------------------
|
| 58 |
+
BASIC_COLORS = {
|
| 59 |
+
"Red": (255, 0, 0),
|
| 60 |
+
"Green": (0, 255, 0),
|
| 61 |
+
"Blue": (0, 0, 255),
|
| 62 |
+
"Yellow": (255, 255, 0),
|
| 63 |
+
"Cyan": (0, 255, 255),
|
| 64 |
+
"Magenta": (255, 0, 255),
|
| 65 |
+
"Black": (0, 0, 0),
|
| 66 |
+
"White": (255, 255, 255),
|
| 67 |
+
"Gray": (128, 128, 128),
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
def closest_basic_color(rgb):
|
| 71 |
+
r, g, b = rgb
|
| 72 |
+
min_dist = float("inf")
|
| 73 |
+
closest_color = None
|
| 74 |
+
for name, (cr, cg, cb) in BASIC_COLORS.items():
|
| 75 |
+
dist = (r - cr) ** 2 + (g - cg) ** 2 + (b - cb) ** 2
|
| 76 |
+
if dist < min_dist:
|
| 77 |
+
min_dist = dist
|
| 78 |
+
closest_color = name
|
| 79 |
+
return closest_color
|
| 80 |
+
|
| 81 |
+
def get_dominant_color(image, num_colors=5):
|
| 82 |
+
image = image.resize((100, 100))
|
| 83 |
+
pixels = np.array(image).reshape(-1, 3)
|
| 84 |
+
kmeans = MiniBatchKMeans(n_clusters=num_colors, random_state=0, n_init=5)
|
| 85 |
+
kmeans.fit(pixels)
|
| 86 |
+
dominant_color = kmeans.cluster_centers_[np.argmax(np.bincount(kmeans.labels_))]
|
| 87 |
+
dominant_color = tuple(dominant_color.astype(int))
|
| 88 |
+
hex_color = f"#{dominant_color[0]:02x}{dominant_color[1]:02x}{dominant_color[2]:02x}"
|
| 89 |
+
return dominant_color, hex_color
|
| 90 |
+
|
| 91 |
+
# ---------------------------
|
| 92 |
+
# Core function
|
| 93 |
+
# ---------------------------
|
| 94 |
+
def classify_zip_and_analyze_color(zip_file):
|
| 95 |
+
results = []
|
| 96 |
+
|
| 97 |
+
zip_name = os.path.splitext(os.path.basename(zip_file.name))[0]
|
| 98 |
+
date_str = datetime.now().strftime("%Y%m%d")
|
| 99 |
+
|
| 100 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 101 |
+
with zipfile.ZipFile(zip_file.name, 'r') as zip_ref:
|
| 102 |
+
zip_ref.extractall(tmpdir)
|
| 103 |
+
|
| 104 |
+
for fname in sorted(os.listdir(tmpdir)):
|
| 105 |
+
if fname.lower().endswith(('.png', '.jpg', '.jpeg')):
|
| 106 |
+
img_path = os.path.join(tmpdir, fname)
|
| 107 |
+
try:
|
| 108 |
+
image = Image.open(img_path).convert("RGB")
|
| 109 |
+
except Exception:
|
| 110 |
+
continue
|
| 111 |
+
|
| 112 |
+
# Classification
|
| 113 |
+
input_tensor = transform(image).unsqueeze(0).to(device)
|
| 114 |
+
with torch.no_grad():
|
| 115 |
+
output = model(input_tensor)
|
| 116 |
+
probs = F.softmax(output, dim=1)[0]
|
| 117 |
+
|
| 118 |
+
top3_prob, top3_idx = torch.topk(probs, 3)
|
| 119 |
+
preds = [(imagenet_classes[idx], f"{prob.item()*100:.2f}%") for idx, prob in zip(top3_idx, top3_prob)]
|
| 120 |
+
|
| 121 |
+
# Dominant color
|
| 122 |
+
rgb, hex_color = get_dominant_color(image)
|
| 123 |
+
basic_color = closest_basic_color(rgb)
|
| 124 |
+
|
| 125 |
+
# Face detection & characterization
|
| 126 |
+
faces_data = []
|
| 127 |
+
try:
|
| 128 |
+
img_cv2 = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 129 |
+
detected_faces = DeepFace.analyze(
|
| 130 |
+
img_cv2, actions=["age", "gender", "emotion"], enforce_detection=False
|
| 131 |
+
)
|
| 132 |
+
if isinstance(detected_faces, list):
|
| 133 |
+
for f in detected_faces:
|
| 134 |
+
faces_data.append({
|
| 135 |
+
"age": f["age"],
|
| 136 |
+
"gender": f["gender"],
|
| 137 |
+
"emotion": f["dominant_emotion"]
|
| 138 |
+
})
|
| 139 |
+
else:
|
| 140 |
+
faces_data.append({
|
| 141 |
+
"age": detected_faces["age"],
|
| 142 |
+
"gender": detected_faces["gender"],
|
| 143 |
+
"emotion": detected_faces["dominant_emotion"]
|
| 144 |
+
})
|
| 145 |
+
except Exception:
|
| 146 |
+
faces_data = []
|
| 147 |
+
|
| 148 |
+
# Thumbnail preview
|
| 149 |
+
thumbnail = image.copy()
|
| 150 |
+
thumbnail.thumbnail((64, 64))
|
| 151 |
+
|
| 152 |
+
results.append((
|
| 153 |
+
fname,
|
| 154 |
+
", ".join([p[0] for p in preds]),
|
| 155 |
+
", ".join([p[1] for p in preds]),
|
| 156 |
+
hex_color,
|
| 157 |
+
basic_color,
|
| 158 |
+
faces_data,
|
| 159 |
+
thumbnail
|
| 160 |
+
))
|
| 161 |
+
|
| 162 |
+
# Build dataframe
|
| 163 |
+
df = pd.DataFrame(results, columns=[
|
| 164 |
+
"Filename", "Top 3 Predictions", "Confidence",
|
| 165 |
+
"Dominant Color", "Basic Color", "Face Info", "Thumbnail"
|
| 166 |
+
])
|
| 167 |
+
|
| 168 |
+
# Save XLSX with zip name + date
|
| 169 |
+
out_xlsx = os.path.join(tempfile.gettempdir(), f"{zip_name}_{date_str}_results.xlsx")
|
| 170 |
+
df.to_excel(out_xlsx, index=False)
|
| 171 |
+
|
| 172 |
+
# ---------------------------
|
| 173 |
+
# Plot 1: Basic color frequency
|
| 174 |
+
# ---------------------------
|
| 175 |
+
fig1, ax1 = plt.subplots()
|
| 176 |
+
color_counts = df["Basic Color"].value_counts()
|
| 177 |
+
ax1.bar(color_counts.index, color_counts.values, color="skyblue")
|
| 178 |
+
ax1.set_title("Basic Color Frequency")
|
| 179 |
+
ax1.set_ylabel("Count")
|
| 180 |
+
buf1 = io.BytesIO()
|
| 181 |
+
plt.savefig(buf1, format="png")
|
| 182 |
+
plt.close(fig1)
|
| 183 |
+
buf1.seek(0)
|
| 184 |
+
plot1_img = Image.open(buf1)
|
| 185 |
+
|
| 186 |
+
# ---------------------------
|
| 187 |
+
# Plot 2: Top prediction distribution
|
| 188 |
+
# ---------------------------
|
| 189 |
+
fig2, ax2 = plt.subplots()
|
| 190 |
+
preds_flat = []
|
| 191 |
+
for p in df["Top 3 Predictions"]:
|
| 192 |
+
preds_flat.extend(p.split(", "))
|
| 193 |
+
pred_counts = pd.Series(preds_flat).value_counts().head(20)
|
| 194 |
+
ax2.barh(pred_counts.index[::-1], pred_counts.values[::-1], color="salmon")
|
| 195 |
+
ax2.set_title("Top Prediction Distribution")
|
| 196 |
+
ax2.set_xlabel("Count")
|
| 197 |
+
buf2 = io.BytesIO()
|
| 198 |
+
plt.savefig(buf2, format="png", bbox_inches="tight")
|
| 199 |
+
plt.close(fig2)
|
| 200 |
+
buf2.seek(0)
|
| 201 |
+
plot2_img = Image.open(buf2)
|
| 202 |
+
|
| 203 |
+
# ---------------------------
|
| 204 |
+
# Extract ages and genders
|
| 205 |
+
# ---------------------------
|
| 206 |
+
ages_male, ages_female = [], []
|
| 207 |
+
gender_confidence = {"Homme": 0, "Femme": 0}
|
| 208 |
+
|
| 209 |
+
for face_list in df["Face Info"]:
|
| 210 |
+
for face in face_list:
|
| 211 |
+
age = face["age"]
|
| 212 |
+
gender_dict = face["gender"]
|
| 213 |
+
gender = max(gender_dict, key=gender_dict.get)
|
| 214 |
+
conf = float(gender_dict[gender]) / 100
|
| 215 |
+
weight = min(conf, 0.9)
|
| 216 |
+
gender_trans = "Homme" if gender == "Man" else "Femme"
|
| 217 |
+
gender_confidence[gender_trans] += weight
|
| 218 |
+
if gender_trans == "Homme":
|
| 219 |
+
ages_male.append(age)
|
| 220 |
+
else:
|
| 221 |
+
ages_female.append(age)
|
| 222 |
+
|
| 223 |
+
# ---------------------------
|
| 224 |
+
# Plot 3: Gender distribution
|
| 225 |
+
# ---------------------------
|
| 226 |
+
fig3, ax3 = plt.subplots()
|
| 227 |
+
ax3.bar(gender_confidence.keys(), gender_confidence.values(), color=["lightblue", "pink"])
|
| 228 |
+
ax3.set_title("Gender Distribution (Weighted ≤90%)")
|
| 229 |
+
ax3.set_ylabel("Sum of Confidence")
|
| 230 |
+
buf3 = io.BytesIO()
|
| 231 |
+
plt.savefig(buf3, format="png")
|
| 232 |
+
plt.close(fig3)
|
| 233 |
+
buf3.seek(0)
|
| 234 |
+
plot3_img = Image.open(buf3)
|
| 235 |
+
|
| 236 |
+
# ---------------------------
|
| 237 |
+
# Plot 4: Age distribution by gender
|
| 238 |
+
# ---------------------------
|
| 239 |
+
fig4, ax4 = plt.subplots()
|
| 240 |
+
bins = range(0, 101, 5)
|
| 241 |
+
ax4.hist([ages_male, ages_female], bins=bins, color=["lightblue", "pink"], label=["Homme", "Femme"], edgecolor="black")
|
| 242 |
+
ax4.set_title("Age Distribution by Gender")
|
| 243 |
+
ax4.set_xlabel("Age")
|
| 244 |
+
ax4.set_ylabel("Count")
|
| 245 |
+
ax4.legend()
|
| 246 |
+
buf4 = io.BytesIO()
|
| 247 |
+
plt.savefig(buf4, format="png")
|
| 248 |
+
plt.close(fig4)
|
| 249 |
+
buf4.seek(0)
|
| 250 |
+
plot4_img = Image.open(buf4)
|
| 251 |
+
|
| 252 |
+
return df, out_xlsx, plot1_img, plot2_img, plot3_img, plot4_img
|
| 253 |
+
|
| 254 |
+
# ---------------------------
|
| 255 |
+
# Gradio Interface
|
| 256 |
+
# ---------------------------
|
| 257 |
+
demo = gr.Interface(
|
| 258 |
+
fn=classify_zip_and_analyze_color,
|
| 259 |
+
inputs=gr.File(file_types=[".zip"], label="Upload ZIP of images"),
|
| 260 |
+
outputs=[
|
| 261 |
+
gr.Dataframe(
|
| 262 |
+
headers=["Filename", "Top 3 Predictions", "Confidence",
|
| 263 |
+
"Dominant Color", "Basic Color", "Face Info", "Thumbnail"],
|
| 264 |
+
datatype=["str","str","str","str","str","str","pil"]
|
| 265 |
+
),
|
| 266 |
+
gr.File(label="Download XLSX"),
|
| 267 |
+
gr.Image(type="pil", label="Basic Color Frequency"),
|
| 268 |
+
gr.Image(type="pil", label="Top Prediction Distribution"),
|
| 269 |
+
gr.Image(type="pil", label="Gender Distribution (Weighted ≤90%)"),
|
| 270 |
+
gr.Image(type="pil", label="Age Distribution by Gender"),
|
| 271 |
+
],
|
| 272 |
+
title="Image Classifier with Color & Face Analysis",
|
| 273 |
+
description="Upload a ZIP of images. Classifies images, analyzes dominant color, detects/characterizes faces (age, gender, emotion), and shows thumbnails.",
|
| 274 |
+
)
|
| 275 |
|
| 276 |
if __name__ == "__main__":
|
| 277 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|