File size: 4,874 Bytes
3a32bd4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | """
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)
# Use one of the seal images
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')}
# Add return_image=true parameter
params = {'return_image': 'true'}
response = requests.post(API_URL, files=files, params=params)
print(f"β
Status: {response.status_code}")
result = response.json()
# Display main results
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")
# Check if annotated image is present
if 'annotated_image' in result:
print(f"\nπ¨ Annotated Image:")
print(f" β
Base64 image included")
print(f" Size: {len(result['annotated_image'])} characters")
# Save annotated image to file
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")
# Show seal detection details
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')}
# Don't specify return_image parameter (defaults to false)
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")
# Wait for API
import time
print("β³ Waiting for API to start...")
time.sleep(5)
# Check API health
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)
# Run tests
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)
|