themehmi commited on
Commit
c28c361
Β·
verified Β·
1 Parent(s): 42fa033

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -4
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import traceback
 
2
  from flask import Flask, render_template, Response, request, jsonify
3
  import base64
4
  import cv2
@@ -122,7 +123,9 @@ def process_frame():
122
  return jsonify({'error': str(e)}), 500
123
 
124
 
 
125
  # NEW: USER REGISTRATION ROUTE
 
126
  @app.route('/register', methods=['POST'])
127
  def register():
128
  """
@@ -135,11 +138,11 @@ def register():
135
  name = payload.get('name', '').strip()
136
  image_data = payload.get('image', '')
137
 
138
- # Validate name
 
139
  return jsonify({'success': False, 'error': 'Name cannot be empty.'}), 400
140
 
141
  # Sanitize: allow only letters, digits, spaces, hyphens, underscores
142
- import re
143
  if not re.match(r'^[\w\s\-]+$', name):
144
  return jsonify({'success': False,
145
  'error': 'Name contains invalid characters. Use letters, numbers, spaces or hyphens.'}), 400
@@ -168,7 +171,7 @@ def register():
168
  return jsonify({'success': False,
169
  'error': 'Multiple faces detected. Please make sure only you are in the frame.'}), 400
170
 
171
- # Encode the detected face
172
  new_encoding = face_recognition.face_encodings(rgb_frame, face_locations)[0]
173
 
174
  # Check for duplicate (same person already registered)
@@ -180,7 +183,7 @@ def register():
180
  return jsonify({'success': False,
181
  'error': f'This face is already registered as "{existing_name}".'}), 409
182
 
183
- # --- Save image to dataset ---
184
  # Folder name uses underscores instead of spaces
185
  folder_name = name.replace(' ', '_')
186
  person_dir = DATASET_DIR / folder_name
 
1
  import traceback
2
+ import re
3
  from flask import Flask, render_template, Response, request, jsonify
4
  import base64
5
  import cv2
 
123
  return jsonify({'error': str(e)}), 500
124
 
125
 
126
+ # ─────────────────────────────────────────────
127
  # NEW: USER REGISTRATION ROUTE
128
+ # ─────────────────────────────────────────────
129
  @app.route('/register', methods=['POST'])
130
  def register():
131
  """
 
138
  name = payload.get('name', '').strip()
139
  image_data = payload.get('image', '')
140
 
141
+ # Validate name
142
+ if not name:
143
  return jsonify({'success': False, 'error': 'Name cannot be empty.'}), 400
144
 
145
  # Sanitize: allow only letters, digits, spaces, hyphens, underscores
 
146
  if not re.match(r'^[\w\s\-]+$', name):
147
  return jsonify({'success': False,
148
  'error': 'Name contains invalid characters. Use letters, numbers, spaces or hyphens.'}), 400
 
171
  return jsonify({'success': False,
172
  'error': 'Multiple faces detected. Please make sure only you are in the frame.'}), 400
173
 
174
+ # Encode the detected face
175
  new_encoding = face_recognition.face_encodings(rgb_frame, face_locations)[0]
176
 
177
  # Check for duplicate (same person already registered)
 
183
  return jsonify({'success': False,
184
  'error': f'This face is already registered as "{existing_name}".'}), 409
185
 
186
+ # Save image to dataset
187
  # Folder name uses underscores instead of spaces
188
  folder_name = name.replace(' ', '_')
189
  person_dir = DATASET_DIR / folder_name