Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,12 +18,16 @@ import gradio as gr
|
|
| 18 |
from deepface import DeepFace
|
| 19 |
import cv2
|
| 20 |
|
|
|
|
| 21 |
# Device
|
|
|
|
| 22 |
if not torch.cuda.is_available():
|
| 23 |
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
| 24 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 25 |
|
| 26 |
-
#
|
|
|
|
|
|
|
| 27 |
weights = ResNet50_Weights.DEFAULT
|
| 28 |
model = resnet50(weights=weights).to(device)
|
| 29 |
model.eval()
|
|
@@ -64,7 +68,9 @@ def get_dominant_color(image,num_colors=5):
|
|
| 64 |
hex_color = f"#{dominant_color[0]:02x}{dominant_color[1]:02x}{dominant_color[2]:02x}"
|
| 65 |
return dominant_color, hex_color
|
| 66 |
|
|
|
|
| 67 |
# Core analysis
|
|
|
|
| 68 |
def classify_zip_and_analyze_color(zip_file):
|
| 69 |
results = []
|
| 70 |
images_dict = {} # store images for preview
|
|
@@ -199,15 +205,21 @@ def classify_zip_and_analyze_color(zip_file):
|
|
| 199 |
|
| 200 |
return df, list(images_dict.keys()), images_dict, out_xlsx, plot1_img, plot2_img, plot3_img, plot4_img
|
| 201 |
|
| 202 |
-
#
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
# ---------------------------
|
| 207 |
# Gradio interface
|
| 208 |
# ---------------------------
|
| 209 |
with gr.Blocks() as demo:
|
| 210 |
uploaded_zip = gr.File(label="Upload ZIP of images", file_types=[".zip"])
|
|
|
|
|
|
|
| 211 |
output_df = gr.Dataframe(headers=["Filename","Top 3 Predictions","Confidence","Dominant Color","Basic Color","Face Info"])
|
| 212 |
image_dropdown = gr.Dropdown(label="Select image to preview")
|
| 213 |
image_preview = gr.Image(label="Image Preview")
|
|
@@ -219,8 +231,6 @@ with gr.Blocks() as demo:
|
|
| 219 |
plot3 = gr.Image(label="Gender Distribution")
|
| 220 |
plot4 = gr.Image(label="Age Distribution by Gender")
|
| 221 |
|
| 222 |
-
analyze_btn = gr.Button("Run Analysis")
|
| 223 |
-
|
| 224 |
def run_analysis(zip_file):
|
| 225 |
df, filenames, images_dict, out_xlsx, p1, p2, p3, p4 = classify_zip_and_analyze_color(zip_file)
|
| 226 |
return df, filenames, images_dict, out_xlsx, p1, p2, p3, p4
|
|
|
|
| 18 |
from deepface import DeepFace
|
| 19 |
import cv2
|
| 20 |
|
| 21 |
+
# ---------------------------
|
| 22 |
# Device
|
| 23 |
+
# ---------------------------
|
| 24 |
if not torch.cuda.is_available():
|
| 25 |
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
| 26 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 27 |
|
| 28 |
+
# ---------------------------
|
| 29 |
+
# Load ResNet50
|
| 30 |
+
# ---------------------------
|
| 31 |
weights = ResNet50_Weights.DEFAULT
|
| 32 |
model = resnet50(weights=weights).to(device)
|
| 33 |
model.eval()
|
|
|
|
| 68 |
hex_color = f"#{dominant_color[0]:02x}{dominant_color[1]:02x}{dominant_color[2]:02x}"
|
| 69 |
return dominant_color, hex_color
|
| 70 |
|
| 71 |
+
# ---------------------------
|
| 72 |
# Core analysis
|
| 73 |
+
# ---------------------------
|
| 74 |
def classify_zip_and_analyze_color(zip_file):
|
| 75 |
results = []
|
| 76 |
images_dict = {} # store images for preview
|
|
|
|
| 205 |
|
| 206 |
return df, list(images_dict.keys()), images_dict, out_xlsx, plot1_img, plot2_img, plot3_img, plot4_img
|
| 207 |
|
| 208 |
+
# ---------------------------
|
| 209 |
+
# Preview callback
|
| 210 |
+
# ---------------------------
|
| 211 |
+
def show_preview(selected_file, images_state):
|
| 212 |
+
if images_state is None or selected_file is None:
|
| 213 |
+
return None
|
| 214 |
+
return images_state.get(selected_file, None)
|
| 215 |
|
| 216 |
# ---------------------------
|
| 217 |
# Gradio interface
|
| 218 |
# ---------------------------
|
| 219 |
with gr.Blocks() as demo:
|
| 220 |
uploaded_zip = gr.File(label="Upload ZIP of images", file_types=[".zip"])
|
| 221 |
+
analyze_btn = gr.Button("Run Analysis") # Run button just after upload
|
| 222 |
+
|
| 223 |
output_df = gr.Dataframe(headers=["Filename","Top 3 Predictions","Confidence","Dominant Color","Basic Color","Face Info"])
|
| 224 |
image_dropdown = gr.Dropdown(label="Select image to preview")
|
| 225 |
image_preview = gr.Image(label="Image Preview")
|
|
|
|
| 231 |
plot3 = gr.Image(label="Gender Distribution")
|
| 232 |
plot4 = gr.Image(label="Age Distribution by Gender")
|
| 233 |
|
|
|
|
|
|
|
| 234 |
def run_analysis(zip_file):
|
| 235 |
df, filenames, images_dict, out_xlsx, p1, p2, p3, p4 = classify_zip_and_analyze_color(zip_file)
|
| 236 |
return df, filenames, images_dict, out_xlsx, p1, p2, p3, p4
|