Spaces:
Sleeping
Sleeping
File size: 5,929 Bytes
6810092 |
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
#!/usr/bin/env python3
"""
Test script for Wall Color Visualizer API
"""
import requests
import base64
import json
from pathlib import Path
# Configuration
BASE_URL = "http://localhost:8000"
def test_health():
"""Test health endpoint"""
print("Testing health endpoint...")
try:
response = requests.get(f"{BASE_URL}/health")
print(f"Status: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
return response.status_code == 200
except Exception as e:
print(f"Error: {e}")
return False
def test_simple_segment(image_path):
"""Test simple segmentation endpoint"""
print(f"\nTesting simple segmentation with {image_path}...")
if not Path(image_path).exists():
print(f"Error: Image file not found: {image_path}")
return False
try:
with open(image_path, 'rb') as f:
files = {'file': f}
response = requests.post(
f"{BASE_URL}/simple-segment",
files=files,
timeout=60
)
print(f"Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"Success: {data['success']}")
print(f"Number of masks: {data['num_masks']}")
print(f"Method: {data.get('method', 'N/A')}")
return True
else:
print(f"Error: {response.text}")
return False
except Exception as e:
print(f"Error: {e}")
return False
def test_segment_automatic(image_path):
"""Test automatic segmentation endpoint (requires SAM)"""
print(f"\nTesting automatic segmentation with {image_path}...")
if not Path(image_path).exists():
print(f"Error: Image file not found: {image_path}")
return False
try:
with open(image_path, 'rb') as f:
files = {'file': f}
response = requests.post(
f"{BASE_URL}/segment-automatic",
files=files,
timeout=60
)
print(f"Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"Success: {data['success']}")
print(f"Number of masks: {data['num_masks']}")
return True
else:
print(f"Error: {response.text}")
return False
except Exception as e:
print(f"Error: {e}")
return False
def test_apply_color(image_path):
"""Test color application (requires existing segmentation)"""
print(f"\nTesting color application...")
# First, get a segmentation
if not Path(image_path).exists():
print(f"Error: Image file not found: {image_path}")
return False
try:
# Get segmentation
with open(image_path, 'rb') as f:
files = {'file': f}
seg_response = requests.post(
f"{BASE_URL}/simple-segment",
files=files,
timeout=60
)
if seg_response.status_code != 200:
print("Failed to get segmentation")
return False
seg_data = seg_response.json()
if not seg_data['masks']:
print("No masks found")
return False
# Apply color to first mask
image_base64 = seg_data['image_base64']
mask_base64 = seg_data['masks'][0]['mask_base64']
color_request = {
'image_base64': image_base64,
'mask_base64': mask_base64,
'color_hex': '#FF5733', # Orange-red color
'opacity': 0.8
}
response = requests.post(
f"{BASE_URL}/apply-color",
json=color_request,
timeout=60
)
print(f"Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"Success: {data['success']}")
print("Color applied successfully!")
# Optionally save result
if data.get('result_base64'):
result_bytes = base64.b64decode(data['result_base64'])
output_path = 'result_colored.png'
with open(output_path, 'wb') as f:
f.write(result_bytes)
print(f"Result saved to: {output_path}")
return True
else:
print(f"Error: {response.text}")
return False
except Exception as e:
print(f"Error: {e}")
return False
def main():
"""Run all tests"""
print("=" * 60)
print("Wall Color Visualizer API Test Suite")
print("=" * 60)
results = {}
# Test 1: Health check
results['health'] = test_health()
# Ask for test image
print("\n" + "=" * 60)
image_path = input("Enter path to test image (or press Enter to skip): ").strip()
if image_path and Path(image_path).exists():
# Test 2: Simple segmentation
results['simple_segment'] = test_simple_segment(image_path)
# Test 3: Automatic segmentation (SAM)
results['auto_segment'] = test_segment_automatic(image_path)
# Test 4: Color application
results['apply_color'] = test_apply_color(image_path)
else:
print("Skipping image-based tests...")
# Summary
print("\n" + "=" * 60)
print("Test Results Summary")
print("=" * 60)
for test_name, passed in results.items():
status = "✓ PASSED" if passed else "✗ FAILED"
print(f"{test_name:20} : {status}")
total = len(results)
passed = sum(results.values())
print(f"\nTotal: {passed}/{total} tests passed")
print("=" * 60)
if __name__ == "__main__":
main()
|