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 |
-
#
|
|
|
|
|
|
|
| 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,10 +68,12 @@ 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 |
-
#
|
|
|
|
|
|
|
| 68 |
def classify_zip_and_analyze_color(zip_file):
|
| 69 |
results = []
|
| 70 |
-
images_dict = {} # store images
|
| 71 |
zip_name = os.path.splitext(os.path.basename(zip_file.name))[0]
|
| 72 |
date_str = datetime.now().strftime("%Y%m%d")
|
| 73 |
|
|
@@ -80,7 +86,7 @@ def classify_zip_and_analyze_color(zip_file):
|
|
| 80 |
img_path = os.path.join(tmpdir,fname)
|
| 81 |
try:
|
| 82 |
image = Image.open(img_path).convert("RGB")
|
| 83 |
-
images_dict[fname] = image.copy()
|
| 84 |
except:
|
| 85 |
continue
|
| 86 |
|
|
@@ -118,38 +124,34 @@ def classify_zip_and_analyze_color(zip_file):
|
|
| 118 |
faces_data
|
| 119 |
))
|
| 120 |
|
| 121 |
-
# DataFrame
|
| 122 |
df = pd.DataFrame(results, columns=["Filename","Top 3 Predictions","Confidence","Dominant Color","Basic Color","Face Info"])
|
| 123 |
|
| 124 |
-
# XLSX output
|
| 125 |
out_xlsx = os.path.join(tempfile.gettempdir(), f"{zip_name}_{date_str}_results.xlsx")
|
| 126 |
df.to_excel(out_xlsx,index=False)
|
| 127 |
|
| 128 |
return df, images_dict, out_xlsx
|
| 129 |
|
| 130 |
-
# Callback
|
| 131 |
def show_preview(filename, images_dict):
|
| 132 |
-
|
| 133 |
-
return images_dict[filename]
|
| 134 |
-
else:
|
| 135 |
-
return None
|
| 136 |
|
|
|
|
| 137 |
# Gradio interface
|
|
|
|
| 138 |
with gr.Blocks() as demo:
|
| 139 |
uploaded_zip = gr.File(label="Upload ZIP of images", file_types=[".zip"])
|
| 140 |
output_df = gr.Dataframe(headers=["Filename","Top 3 Predictions","Confidence","Dominant Color","Basic Color","Face Info"])
|
| 141 |
image_preview = gr.Image(label="Image Preview")
|
| 142 |
download_file = gr.File(label="Download XLSX")
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
-
# Run analysis
|
| 145 |
def run_analysis(zip_file):
|
| 146 |
df, images_dict, out_xlsx = classify_zip_and_analyze_color(zip_file)
|
| 147 |
return df, images_dict, out_xlsx
|
| 148 |
|
| 149 |
-
analyze_btn =
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
# Update preview when clicking filename
|
| 153 |
-
output_df.select(show_preview, inputs=[output_df, "state"], outputs=image_preview)
|
| 154 |
|
| 155 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 18 |
from deepface import DeepFace
|
| 19 |
import cv2
|
| 20 |
|
| 21 |
+
# ---------------------------
|
| 22 |
+
# Device setup
|
| 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
|
| 77 |
zip_name = os.path.splitext(os.path.basename(zip_file.name))[0]
|
| 78 |
date_str = datetime.now().strftime("%Y%m%d")
|
| 79 |
|
|
|
|
| 86 |
img_path = os.path.join(tmpdir,fname)
|
| 87 |
try:
|
| 88 |
image = Image.open(img_path).convert("RGB")
|
| 89 |
+
images_dict[fname] = image.copy()
|
| 90 |
except:
|
| 91 |
continue
|
| 92 |
|
|
|
|
| 124 |
faces_data
|
| 125 |
))
|
| 126 |
|
|
|
|
| 127 |
df = pd.DataFrame(results, columns=["Filename","Top 3 Predictions","Confidence","Dominant Color","Basic Color","Face Info"])
|
| 128 |
|
|
|
|
| 129 |
out_xlsx = os.path.join(tempfile.gettempdir(), f"{zip_name}_{date_str}_results.xlsx")
|
| 130 |
df.to_excel(out_xlsx,index=False)
|
| 131 |
|
| 132 |
return df, images_dict, out_xlsx
|
| 133 |
|
| 134 |
+
# Callback for preview
|
| 135 |
def show_preview(filename, images_dict):
|
| 136 |
+
return images_dict.get(filename, None)
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
+
# ---------------------------
|
| 139 |
# Gradio interface
|
| 140 |
+
# ---------------------------
|
| 141 |
with gr.Blocks() as demo:
|
| 142 |
uploaded_zip = gr.File(label="Upload ZIP of images", file_types=[".zip"])
|
| 143 |
output_df = gr.Dataframe(headers=["Filename","Top 3 Predictions","Confidence","Dominant Color","Basic Color","Face Info"])
|
| 144 |
image_preview = gr.Image(label="Image Preview")
|
| 145 |
download_file = gr.File(label="Download XLSX")
|
| 146 |
+
images_state = gr.State() # store images dict
|
| 147 |
+
|
| 148 |
+
analyze_btn = gr.Button("Run Analysis")
|
| 149 |
|
|
|
|
| 150 |
def run_analysis(zip_file):
|
| 151 |
df, images_dict, out_xlsx = classify_zip_and_analyze_color(zip_file)
|
| 152 |
return df, images_dict, out_xlsx
|
| 153 |
|
| 154 |
+
analyze_btn.click(run_analysis, inputs=uploaded_zip, outputs=[output_df, images_state, download_file])
|
| 155 |
+
output_df.select(show_preview, inputs=[output_df, images_state], outputs=image_preview)
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|