π BULLETPROOF: Based on working HF Spaces pattern - no schema bugs
Browse files
app.py
CHANGED
|
@@ -25,11 +25,8 @@ def setup_models():
|
|
| 25 |
face_app = FaceAnalysis(name='buffalo_l', providers=['CPUExecutionProvider'])
|
| 26 |
face_app.prepare(ctx_id=0, det_thresh=0.5, det_size=(640, 640))
|
| 27 |
print("β InsightFace models loaded")
|
| 28 |
-
return "β InsightFace models loaded successfully"
|
| 29 |
except Exception as e:
|
| 30 |
print(f"Failed to load InsightFace: {e}")
|
| 31 |
-
return f"Demo mode (InsightFace failed: {str(e)[:50]})"
|
| 32 |
-
return "Demo mode (InsightFace not available)"
|
| 33 |
|
| 34 |
def load_database():
|
| 35 |
global face_database
|
|
@@ -51,8 +48,7 @@ def save_database():
|
|
| 51 |
def get_embedding(image):
|
| 52 |
global face_app
|
| 53 |
if not face_app or not image:
|
| 54 |
-
|
| 55 |
-
if image:
|
| 56 |
seed = int(np.array(image).mean() * 1000) % 1000
|
| 57 |
np.random.seed(seed)
|
| 58 |
emb = np.random.rand(512)
|
|
@@ -69,7 +65,7 @@ def get_embedding(image):
|
|
| 69 |
except Exception as e:
|
| 70 |
return None, f"Error: {str(e)}"
|
| 71 |
|
| 72 |
-
def
|
| 73 |
if not name or not name.strip():
|
| 74 |
return "Please enter a name"
|
| 75 |
|
|
@@ -84,7 +80,7 @@ def add_face_to_database(image, name):
|
|
| 84 |
|
| 85 |
return f"β Added {name} ({msg}). Database now has {len(face_database)} faces."
|
| 86 |
|
| 87 |
-
def
|
| 88 |
if not face_database:
|
| 89 |
return "Database is empty. Please add faces first."
|
| 90 |
|
|
@@ -110,13 +106,10 @@ def match_face_in_database(image):
|
|
| 110 |
else:
|
| 111 |
return f"β No match found. Best score: {best_score*100:.1f}% (threshold: 60%)"
|
| 112 |
|
| 113 |
-
def
|
| 114 |
-
if
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
if len(face_database) > 5:
|
| 118 |
-
names.append(f"... and {len(face_database)-5} more")
|
| 119 |
-
return f"Database has {len(face_database)} faces: {', '.join(names)}"
|
| 120 |
|
| 121 |
def clear_database():
|
| 122 |
global face_database
|
|
@@ -126,60 +119,62 @@ def clear_database():
|
|
| 126 |
|
| 127 |
# Initialize
|
| 128 |
print("Starting FaceMatch system...")
|
| 129 |
-
|
| 130 |
load_database()
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
-
# Add Face Interface
|
| 142 |
-
gr.Markdown("## Add Face to Database")
|
| 143 |
with gr.Row():
|
| 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 |
-
# Database Management
|
| 169 |
-
gr.Markdown("## Database Management")
|
| 170 |
-
status_button = gr.Button("Check Database Status")
|
| 171 |
-
clear_button = gr.Button("Clear Database", variant="stop")
|
| 172 |
-
db_output = gr.Textbox(label="Database Status", lines=2)
|
| 173 |
-
|
| 174 |
-
status_button.click(
|
| 175 |
-
fn=get_database_status,
|
| 176 |
-
outputs=db_output
|
| 177 |
-
)
|
| 178 |
-
|
| 179 |
-
clear_button.click(
|
| 180 |
-
fn=clear_database,
|
| 181 |
-
outputs=db_output
|
| 182 |
-
)
|
| 183 |
|
| 184 |
if __name__ == "__main__":
|
| 185 |
demo.launch()
|
|
|
|
| 25 |
face_app = FaceAnalysis(name='buffalo_l', providers=['CPUExecutionProvider'])
|
| 26 |
face_app.prepare(ctx_id=0, det_thresh=0.5, det_size=(640, 640))
|
| 27 |
print("β InsightFace models loaded")
|
|
|
|
| 28 |
except Exception as e:
|
| 29 |
print(f"Failed to load InsightFace: {e}")
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def load_database():
|
| 32 |
global face_database
|
|
|
|
| 48 |
def get_embedding(image):
|
| 49 |
global face_app
|
| 50 |
if not face_app or not image:
|
| 51 |
+
if image is not None:
|
|
|
|
| 52 |
seed = int(np.array(image).mean() * 1000) % 1000
|
| 53 |
np.random.seed(seed)
|
| 54 |
emb = np.random.rand(512)
|
|
|
|
| 65 |
except Exception as e:
|
| 66 |
return None, f"Error: {str(e)}"
|
| 67 |
|
| 68 |
+
def add_face(image, name):
|
| 69 |
if not name or not name.strip():
|
| 70 |
return "Please enter a name"
|
| 71 |
|
|
|
|
| 80 |
|
| 81 |
return f"β Added {name} ({msg}). Database now has {len(face_database)} faces."
|
| 82 |
|
| 83 |
+
def match_face(image):
|
| 84 |
if not face_database:
|
| 85 |
return "Database is empty. Please add faces first."
|
| 86 |
|
|
|
|
| 106 |
else:
|
| 107 |
return f"β No match found. Best score: {best_score*100:.1f}% (threshold: 60%)"
|
| 108 |
|
| 109 |
+
def get_status():
|
| 110 |
+
status = "β InsightFace loaded" if face_app else "Demo mode"
|
| 111 |
+
db_count = len(face_database)
|
| 112 |
+
return f"System: {status} | Database: {db_count} faces"
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
def clear_database():
|
| 115 |
global face_database
|
|
|
|
| 119 |
|
| 120 |
# Initialize
|
| 121 |
print("Starting FaceMatch system...")
|
| 122 |
+
setup_models()
|
| 123 |
load_database()
|
| 124 |
|
| 125 |
+
css = """
|
| 126 |
+
#col-left {
|
| 127 |
+
margin: 0 auto;
|
| 128 |
+
max-width: 450px;
|
| 129 |
+
}
|
| 130 |
+
#col-right {
|
| 131 |
+
margin: 0 auto;
|
| 132 |
+
max-width: 450px;
|
| 133 |
+
}
|
| 134 |
+
"""
|
| 135 |
+
|
| 136 |
+
with gr.Blocks(css=css) as demo:
|
| 137 |
+
gr.HTML("""
|
| 138 |
+
<div style="text-align: center; margin-bottom: 20px;">
|
| 139 |
+
<h1>π― FaceMatch Pro</h1>
|
| 140 |
+
<p>Professional Face Recognition System</p>
|
| 141 |
+
</div>
|
| 142 |
+
""")
|
| 143 |
|
|
|
|
|
|
|
| 144 |
with gr.Row():
|
| 145 |
+
with gr.Column(elem_id="col-left"):
|
| 146 |
+
gr.HTML("""
|
| 147 |
+
<div style="text-align: center; font-size: 18px; margin-bottom: 10px;">
|
| 148 |
+
<b>π₯ Add Face to Database</b>
|
| 149 |
+
</div>
|
| 150 |
+
""")
|
| 151 |
+
add_image = gr.Image(label="Upload Photo", sources='upload', type="pil")
|
| 152 |
+
add_name = gr.Textbox(label="Person Name", placeholder="Enter name...")
|
| 153 |
+
add_button = gr.Button("Add to Database", variant="primary")
|
| 154 |
+
add_result = gr.Textbox(label="Result")
|
| 155 |
+
|
| 156 |
+
with gr.Column(elem_id="col-right"):
|
| 157 |
+
gr.HTML("""
|
| 158 |
+
<div style="text-align: center; font-size: 18px; margin-bottom: 10px;">
|
| 159 |
+
<b>π Find Face Match</b>
|
| 160 |
+
</div>
|
| 161 |
+
""")
|
| 162 |
+
match_image = gr.Image(label="Upload Photo to Match", sources='upload', type="pil")
|
| 163 |
+
match_button = gr.Button("Find Match", variant="primary")
|
| 164 |
+
match_result = gr.Textbox(label="Match Result")
|
| 165 |
+
|
| 166 |
+
gr.HTML("<hr>")
|
| 167 |
|
| 168 |
+
with gr.Row():
|
| 169 |
+
status_button = gr.Button("Check Status")
|
| 170 |
+
clear_button = gr.Button("Clear Database", variant="stop")
|
| 171 |
+
status_output = gr.Textbox(label="System Status")
|
| 172 |
+
|
| 173 |
+
# Event handlers
|
| 174 |
+
add_button.click(fn=add_face, inputs=[add_image, add_name], outputs=add_result)
|
| 175 |
+
match_button.click(fn=match_face, inputs=match_image, outputs=match_result)
|
| 176 |
+
status_button.click(fn=get_status, outputs=status_output)
|
| 177 |
+
clear_button.click(fn=clear_database, outputs=status_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
if __name__ == "__main__":
|
| 180 |
demo.launch()
|