| """ |
| Test the new return_image feature |
| """ |
| import requests |
| import json |
| import base64 |
| from pathlib import Path |
|
|
| API_URL = "http://localhost:8000/api/verify" |
|
|
| def test_with_annotated_image(): |
| """Test verification with annotated image""" |
| print("="*60) |
| print("Testing Certificate Verification with Annotated Image") |
| print("="*60) |
| |
| |
| test_file = "cropped_seals/temp_cert_264196_seal_1.png" |
| |
| if not Path(test_file).exists(): |
| print(f"β Test file not found: {test_file}") |
| return |
| |
| print(f"\nπ Testing with: {test_file}") |
| print(f"π Testing WITH annotated image (return_image=true)\n") |
| |
| try: |
| with open(test_file, 'rb') as f: |
| files = {'files': (Path(test_file).name, f, 'image/png')} |
| |
| params = {'return_image': 'true'} |
| response = requests.post(API_URL, files=files, params=params) |
| |
| print(f"β
Status: {response.status_code}") |
| result = response.json() |
| |
| |
| print(f"\nπ Verification Results:") |
| print(f" Decision: {result.get('decision')}") |
| print(f" Confidence: {result.get('confidence')}") |
| print(f" Reason: {result.get('reason')}") |
| print(f" Processing Time: {result.get('processing_time_seconds')}s") |
| |
| |
| if 'annotated_image' in result: |
| print(f"\nπ¨ Annotated Image:") |
| print(f" β
Base64 image included") |
| print(f" Size: {len(result['annotated_image'])} characters") |
| |
| |
| img_data = base64.b64decode(result['annotated_image']) |
| output_path = "annotated_certificate.png" |
| with open(output_path, 'wb') as f: |
| f.write(img_data) |
| print(f" πΎ Saved to: {output_path}") |
| |
| if 'annotated_image_url' in result: |
| print(f" π Data URL available (for direct display in browser)") |
| print(f" URL length: {len(result['annotated_image_url'])} characters") |
| else: |
| print(f"\nβ οΈ No annotated image in response") |
| |
| |
| seal_info = result.get('details', {}).get('seal_verification', {}) |
| if seal_info: |
| print(f"\nπ Seal Detection:") |
| print(f" Total seals: {seal_info.get('total_seals', 0)}") |
| print(f" Authentic: {seal_info.get('authentic_seals', 0)}") |
| print(f" Fake: {seal_info.get('fake_seals', 0)}") |
| print(f" Method: {seal_info.get('detection_method', 'N/A')}") |
| |
| return True |
| |
| except Exception as e: |
| print(f"β Error: {e}") |
| import traceback |
| traceback.print_exc() |
| return False |
|
|
| def test_without_annotated_image(): |
| """Test verification without annotated image (default behavior)""" |
| print("\n" + "="*60) |
| print("Testing WITHOUT Annotated Image (return_image=false)") |
| print("="*60) |
| |
| test_file = "cropped_seals/temp_cert_264196_seal_1.png" |
| |
| print(f"\nπ Testing with: {test_file}") |
| print(f"π Testing WITHOUT annotated image (default)\n") |
| |
| try: |
| with open(test_file, 'rb') as f: |
| files = {'files': (Path(test_file).name, f, 'image/png')} |
| |
| response = requests.post(API_URL, files=files) |
| |
| result = response.json() |
| print(f"β
Status: {response.status_code}") |
| print(f" Decision: {result.get('decision')}") |
| print(f" Time: {result.get('processing_time_seconds')}s") |
| |
| if 'annotated_image' in result: |
| print(f" β οΈ Annotated image included (unexpected)") |
| else: |
| print(f" β
No annotated image (as expected)") |
| |
| return True |
| |
| except Exception as e: |
| print(f"β Error: {e}") |
| return False |
|
|
| if __name__ == "__main__": |
| print("\nπ§ͺ ANNOTATED IMAGE API TESTING\n") |
| |
| |
| import time |
| print("β³ Waiting for API to start...") |
| time.sleep(5) |
| |
| |
| try: |
| response = requests.get("http://localhost:8000/health", timeout=5) |
| print(f"β
API is running\n") |
| except: |
| print(f"β API not running. Start it first!") |
| exit(1) |
| |
| |
| test1 = test_with_annotated_image() |
| test2 = test_without_annotated_image() |
| |
| print("\n" + "="*60) |
| if test1 and test2: |
| print("β
ALL TESTS PASSED!") |
| print("\nπ‘ Open 'annotated_certificate.png' to see the result!") |
| else: |
| print("β SOME TESTS FAILED") |
| print("="*60) |
|
|