Ishan commited on
Commit ·
8cdfa09
1
Parent(s): 2ab13f6
Verify embedding change2
Browse files
app.py
CHANGED
|
@@ -171,113 +171,17 @@ async def extract_features(file: UploadFile = File(...)):
|
|
| 171 |
detail=f"Internal server error: {str(e)}"
|
| 172 |
)
|
| 173 |
|
| 174 |
-
# @app.post("/verify-with-embedding")
|
| 175 |
-
# async def verify_with_embedding(
|
| 176 |
-
# file: UploadFile = File(..., description="New video file to verify"),
|
| 177 |
-
# stored_embedding: List[float] = Body(..., description="Stored embedding from database"),
|
| 178 |
-
# threshold: float = Body(0.8, description="Similarity threshold (0.0-1.0)")
|
| 179 |
-
# ):
|
| 180 |
-
# """
|
| 181 |
-
# Verify a new video against a stored embedding from database
|
| 182 |
-
|
| 183 |
-
# - **file**: New video file to verify
|
| 184 |
-
# - **stored_embedding**: Pre-existing embedding array from database
|
| 185 |
-
# - **threshold**: Similarity threshold (default: 0.8)
|
| 186 |
-
# - **returns**: Similarity score and verification result
|
| 187 |
-
# """
|
| 188 |
-
# try:
|
| 189 |
-
# # Validate file type
|
| 190 |
-
# if not file.content_type.startswith('video/') and not file.filename.lower().endswith(('.mp4', '.avi', '.mov', '.mkv')):
|
| 191 |
-
# raise HTTPException(
|
| 192 |
-
# status_code=400,
|
| 193 |
-
# detail="Only video files are supported (mp4, avi, mov, mkv)"
|
| 194 |
-
# )
|
| 195 |
-
|
| 196 |
-
# # Validate stored embedding
|
| 197 |
-
# if not stored_embedding or len(stored_embedding) != 768:
|
| 198 |
-
# raise HTTPException(
|
| 199 |
-
# status_code=400,
|
| 200 |
-
# detail="Stored embedding must be a 768-dimensional array"
|
| 201 |
-
# )
|
| 202 |
-
|
| 203 |
-
# # Validate threshold
|
| 204 |
-
# if not 0.0 <= threshold <= 1.0:
|
| 205 |
-
# raise HTTPException(
|
| 206 |
-
# status_code=400,
|
| 207 |
-
# detail="Threshold must be between 0.0 and 1.0"
|
| 208 |
-
# )
|
| 209 |
-
|
| 210 |
-
# # Convert stored embedding to numpy array
|
| 211 |
-
# try:
|
| 212 |
-
# stored_emb = np.array(stored_embedding, dtype=np.float32)
|
| 213 |
-
# except Exception as e:
|
| 214 |
-
# raise HTTPException(
|
| 215 |
-
# status_code=400,
|
| 216 |
-
# detail=f"Invalid embedding format: {str(e)}"
|
| 217 |
-
# )
|
| 218 |
-
|
| 219 |
-
# # Create temporary file for new video
|
| 220 |
-
# with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp:
|
| 221 |
-
# content = await file.read()
|
| 222 |
-
# if len(content) == 0:
|
| 223 |
-
# raise HTTPException(status_code=400, detail="Video file is empty")
|
| 224 |
-
# tmp.write(content)
|
| 225 |
-
# tmp_path = tmp.name
|
| 226 |
-
|
| 227 |
-
# try:
|
| 228 |
-
# # Extract features from new video
|
| 229 |
-
# new_emb = extract_vit_features_from_video(tmp_path)
|
| 230 |
-
|
| 231 |
-
# if new_emb is None:
|
| 232 |
-
# raise HTTPException(
|
| 233 |
-
# status_code=400,
|
| 234 |
-
# detail="Could not process the video or no frames extracted"
|
| 235 |
-
# )
|
| 236 |
-
|
| 237 |
-
# # Calculate cosine similarity
|
| 238 |
-
# similarity = calculate_cosine_similarity(stored_emb, new_emb)
|
| 239 |
-
|
| 240 |
-
# # Determine result
|
| 241 |
-
# result = "same_person" if similarity > threshold else "different_person"
|
| 242 |
-
|
| 243 |
-
# return {
|
| 244 |
-
# "success": True,
|
| 245 |
-
# "similarity": round(similarity, 4),
|
| 246 |
-
# "threshold_used": threshold,
|
| 247 |
-
# "result": result,
|
| 248 |
-
# "message": "Verification completed successfully",
|
| 249 |
-
# "comparison": {
|
| 250 |
-
# "stored_embedding_length": len(stored_emb),
|
| 251 |
-
# "new_embedding_length": len(new_emb)
|
| 252 |
-
# }
|
| 253 |
-
# }
|
| 254 |
-
|
| 255 |
-
# finally:
|
| 256 |
-
# # Cleanup temp file
|
| 257 |
-
# if os.path.exists(tmp_path):
|
| 258 |
-
# os.remove(tmp_path)
|
| 259 |
-
|
| 260 |
-
# except HTTPException:
|
| 261 |
-
# raise
|
| 262 |
-
# except Exception as e:
|
| 263 |
-
# logger.error(f"Verify with embedding error: {str(e)}")
|
| 264 |
-
# raise HTTPException(
|
| 265 |
-
# status_code=500,
|
| 266 |
-
# detail=f"Internal server error: {str(e)}"
|
| 267 |
-
# )
|
| 268 |
-
|
| 269 |
-
|
| 270 |
@app.post("/verify-with-embedding")
|
| 271 |
async def verify_with_embedding(
|
| 272 |
file: UploadFile = File(..., description="New video file to verify"),
|
| 273 |
-
stored_embedding:
|
| 274 |
threshold: float = Body(0.8, description="Similarity threshold (0.0-1.0)")
|
| 275 |
):
|
| 276 |
"""
|
| 277 |
Verify a new video against a stored embedding from database
|
| 278 |
|
| 279 |
- **file**: New video file to verify
|
| 280 |
-
- **stored_embedding**: Pre-existing embedding array
|
| 281 |
- **threshold**: Similarity threshold (default: 0.8)
|
| 282 |
- **returns**: Similarity score and verification result
|
| 283 |
"""
|
|
@@ -289,34 +193,27 @@ async def verify_with_embedding(
|
|
| 289 |
detail="Only video files are supported (mp4, avi, mov, mkv)"
|
| 290 |
)
|
| 291 |
|
| 292 |
-
#
|
| 293 |
-
|
| 294 |
-
import json
|
| 295 |
-
embedding_list = json.loads(stored_embedding)
|
| 296 |
-
stored_emb = np.array(embedding_list, dtype=np.float32)
|
| 297 |
-
except json.JSONDecodeError as e:
|
| 298 |
-
raise HTTPException(
|
| 299 |
-
status_code=400,
|
| 300 |
-
detail=f"Invalid JSON format for embedding: {str(e)}"
|
| 301 |
-
)
|
| 302 |
-
except Exception as e:
|
| 303 |
raise HTTPException(
|
| 304 |
status_code=400,
|
| 305 |
-
detail=
|
| 306 |
)
|
| 307 |
|
| 308 |
-
# Validate
|
| 309 |
-
if
|
| 310 |
raise HTTPException(
|
| 311 |
status_code=400,
|
| 312 |
-
detail=
|
| 313 |
)
|
| 314 |
|
| 315 |
-
#
|
| 316 |
-
|
|
|
|
|
|
|
| 317 |
raise HTTPException(
|
| 318 |
status_code=400,
|
| 319 |
-
detail="
|
| 320 |
)
|
| 321 |
|
| 322 |
# Create temporary file for new video
|
|
@@ -369,10 +266,6 @@ async def verify_with_embedding(
|
|
| 369 |
detail=f"Internal server error: {str(e)}"
|
| 370 |
)
|
| 371 |
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
@app.post("/verify-two-videos")
|
| 377 |
async def verify_two_videos(
|
| 378 |
file1: UploadFile = File(..., description="First video file"),
|
|
|
|
| 171 |
detail=f"Internal server error: {str(e)}"
|
| 172 |
)
|
| 173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
@app.post("/verify-with-embedding")
|
| 175 |
async def verify_with_embedding(
|
| 176 |
file: UploadFile = File(..., description="New video file to verify"),
|
| 177 |
+
stored_embedding: List[float] = Body(..., description="Stored embedding from database"),
|
| 178 |
threshold: float = Body(0.8, description="Similarity threshold (0.0-1.0)")
|
| 179 |
):
|
| 180 |
"""
|
| 181 |
Verify a new video against a stored embedding from database
|
| 182 |
|
| 183 |
- **file**: New video file to verify
|
| 184 |
+
- **stored_embedding**: Pre-existing embedding array from database
|
| 185 |
- **threshold**: Similarity threshold (default: 0.8)
|
| 186 |
- **returns**: Similarity score and verification result
|
| 187 |
"""
|
|
|
|
| 193 |
detail="Only video files are supported (mp4, avi, mov, mkv)"
|
| 194 |
)
|
| 195 |
|
| 196 |
+
# Validate stored embedding
|
| 197 |
+
if not stored_embedding or len(stored_embedding) != 768:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
raise HTTPException(
|
| 199 |
status_code=400,
|
| 200 |
+
detail="Stored embedding must be a 768-dimensional array"
|
| 201 |
)
|
| 202 |
|
| 203 |
+
# Validate threshold
|
| 204 |
+
if not 0.0 <= threshold <= 1.0:
|
| 205 |
raise HTTPException(
|
| 206 |
status_code=400,
|
| 207 |
+
detail="Threshold must be between 0.0 and 1.0"
|
| 208 |
)
|
| 209 |
|
| 210 |
+
# Convert stored embedding to numpy array
|
| 211 |
+
try:
|
| 212 |
+
stored_emb = np.array(stored_embedding, dtype=np.float32)
|
| 213 |
+
except Exception as e:
|
| 214 |
raise HTTPException(
|
| 215 |
status_code=400,
|
| 216 |
+
detail=f"Invalid embedding format: {str(e)}"
|
| 217 |
)
|
| 218 |
|
| 219 |
# Create temporary file for new video
|
|
|
|
| 266 |
detail=f"Internal server error: {str(e)}"
|
| 267 |
)
|
| 268 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
@app.post("/verify-two-videos")
|
| 270 |
async def verify_two_videos(
|
| 271 |
file1: UploadFile = File(..., description="First video file"),
|