Spaces:
Sleeping
Sleeping
| """ | |
| Simple API test script that extracts the numerical score | |
| """ | |
| import requests | |
| import base64 | |
| import numpy as np | |
| import cv2 | |
| from PIL import Image | |
| import io | |
| import re | |
| def create_face_image(): | |
| """Create a simple face-like image""" | |
| img = np.zeros((100, 100), dtype=np.uint8) | |
| # Face outline | |
| cv2.ellipse(img, (50, 50), (40, 50), 0, 0, 360, 100, -1) | |
| # Eyes | |
| cv2.circle(img, (35, 40), 5, 200, -1) | |
| cv2.circle(img, (65, 40), 5, 200, -1) | |
| # Nose | |
| cv2.line(img, (50, 45), (50, 60), 150, 2) | |
| # Mouth | |
| cv2.ellipse(img, (50, 70), (15, 8), 0, 0, 180, 150, 2) | |
| # Convert to RGB | |
| img_rgb = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) | |
| return img_rgb | |
| def test_api(): | |
| """Test the API and extract the score""" | |
| url = "https://pavaniyerra-hackthon4.hf.space/predict_similarity/" | |
| print("Testing Face Similarity API") | |
| print("=" * 40) | |
| try: | |
| # Create two test face images | |
| face1 = create_face_image() | |
| face2 = create_face_image() | |
| # Convert to bytes | |
| def img_to_bytes(img): | |
| pil_img = Image.fromarray(img) | |
| buffer = io.BytesIO() | |
| pil_img.save(buffer, format='JPEG') | |
| return buffer.getvalue() | |
| face1_bytes = img_to_bytes(face1) | |
| face2_bytes = img_to_bytes(face2) | |
| # Prepare files for upload | |
| files = { | |
| 'file1': ('face1.jpg', face1_bytes, 'image/jpeg'), | |
| 'file2': ('face2.jpg', face2_bytes, 'image/jpeg') | |
| } | |
| print("Sending request to API...") | |
| response = requests.post(url, files=files, timeout=30) | |
| print(f"Status Code: {response.status_code}") | |
| if response.status_code == 200: | |
| print("SUCCESS! API is working") | |
| # Extract the dissimilarity score from HTML | |
| html_content = response.text | |
| # Look for the dissimilarity score in the HTML | |
| # Pattern: "Dissimilarity: X.X" | |
| pattern = r'Dissimilarity:</span>\s*<span[^>]*>\s*([0-9.]+)' | |
| match = re.search(pattern, html_content) | |
| if match: | |
| score = float(match.group(1)) | |
| print(f"Dissimilarity Score: {score}") | |
| # Convert dissimilarity to similarity (assuming 1.0 = completely different, 0.0 = identical) | |
| similarity = 1.0 - score | |
| print(f"Similarity Score: {similarity:.4f}") | |
| # Interpret the result | |
| if similarity > 0.8: | |
| print("Result: Very High Similarity (likely same person)") | |
| elif similarity > 0.6: | |
| print("Result: High Similarity (possibly same person)") | |
| elif similarity > 0.4: | |
| print("Result: Moderate Similarity (uncertain)") | |
| elif similarity > 0.2: | |
| print("Result: Low Similarity (likely different persons)") | |
| else: | |
| print("Result: Very Low Similarity (definitely different persons)") | |
| else: | |
| print("WARNING: Could not extract score from HTML response") | |
| print("HTML content preview:") | |
| print(html_content[:500] + "..." if len(html_content) > 500 else html_content) | |
| else: | |
| print(f"ERROR: {response.status_code}") | |
| print(f"Response: {response.text}") | |
| except Exception as e: | |
| print(f"ERROR: {e}") | |
| def test_multiple_times(): | |
| """Test the API multiple times to check consistency""" | |
| print("\n" + "=" * 40) | |
| print("Testing API Multiple Times") | |
| print("=" * 40) | |
| scores = [] | |
| for i in range(3): | |
| print(f"\nTest {i+1}/3:") | |
| try: | |
| face1 = create_face_image() | |
| face2 = create_face_image() | |
| def img_to_bytes(img): | |
| pil_img = Image.fromarray(img) | |
| buffer = io.BytesIO() | |
| pil_img.save(buffer, format='JPEG') | |
| return buffer.getvalue() | |
| files = { | |
| 'file1': ('face1.jpg', img_to_bytes(face1), 'image/jpeg'), | |
| 'file2': ('face2.jpg', img_to_bytes(face2), 'image/jpeg') | |
| } | |
| response = requests.post("https://pavaniyerra-hackthon4.hf.space/predict_similarity/", | |
| files=files, timeout=30) | |
| if response.status_code == 200: | |
| # Extract score | |
| pattern = r'Dissimilarity:</span>\s*<span[^>]*>\s*([0-9.]+)' | |
| match = re.search(pattern, response.text) | |
| if match: | |
| score = float(match.group(1)) | |
| scores.append(score) | |
| print(f" Score: {score}") | |
| else: | |
| print(" Could not extract score") | |
| else: | |
| print(f" Error: {response.status_code}") | |
| except Exception as e: | |
| print(f" Error: {e}") | |
| if scores: | |
| print(f"\nScore Statistics:") | |
| print(f" Average: {sum(scores)/len(scores):.4f}") | |
| print(f" Min: {min(scores):.4f}") | |
| print(f" Max: {max(scores):.4f}") | |
| print(f" Range: {max(scores) - min(scores):.4f}") | |
| if __name__ == "__main__": | |
| # Test the API | |
| test_api() | |
| # Test multiple times for consistency | |
| test_multiple_times() | |
| print("\n" + "=" * 50) | |
| print("API Testing Complete!") | |
| print("\nYour API is working correctly!") | |
| print("The API expects:") | |
| print("- Method: POST") | |
| print("- Format: multipart/form-data") | |
| print("- Parameters: file1, file2 (image files)") | |
| print("- Response: HTML with dissimilarity score") | |