|
|
""" |
|
|
Example usage of the Depth Pro Distance Estimation API. |
|
|
This script demonstrates how to use both the Gradio interface and FastAPI endpoints. |
|
|
""" |
|
|
|
|
|
import requests |
|
|
import numpy as np |
|
|
from PIL import Image |
|
|
import io |
|
|
import json |
|
|
|
|
|
def create_sample_image(): |
|
|
"""Create a sample image for testing""" |
|
|
width, height = 640, 480 |
|
|
|
|
|
|
|
|
image = np.zeros((height, width, 3), dtype=np.uint8) |
|
|
|
|
|
|
|
|
for y in range(height): |
|
|
sky_intensity = max(0, 255 - int(255 * y / height)) |
|
|
ground_intensity = min(255, int(128 * y / height)) |
|
|
image[y, :, 0] = sky_intensity |
|
|
image[y, :, 1] = sky_intensity |
|
|
image[y, :, 2] = sky_intensity + ground_intensity |
|
|
|
|
|
|
|
|
|
|
|
image[height//3:height//3+10, :, :] = [255, 255, 255] |
|
|
image[2*height//3:2*height//3+10, :, :] = [200, 200, 200] |
|
|
image[height-50:height-40, :, :] = [150, 150, 150] |
|
|
|
|
|
|
|
|
image[:, width//4:width//4+5, :] = [100, 100, 100] |
|
|
image[:, 3*width//4:3*width//4+5, :] = [100, 100, 100] |
|
|
|
|
|
return Image.fromarray(image) |
|
|
|
|
|
def test_api_endpoint(base_url="http://localhost:7860"): |
|
|
"""Test the FastAPI endpoint""" |
|
|
print("π§ͺ Testing FastAPI Endpoint") |
|
|
print("=" * 40) |
|
|
|
|
|
try: |
|
|
|
|
|
sample_image = create_sample_image() |
|
|
|
|
|
|
|
|
img_byte_arr = io.BytesIO() |
|
|
sample_image.save(img_byte_arr, format='JPEG', quality=95) |
|
|
img_byte_arr.seek(0) |
|
|
|
|
|
|
|
|
files = {'file': ('sample_image.jpg', img_byte_arr, 'image/jpeg')} |
|
|
print(f"Sending request to {base_url}/estimate-depth...") |
|
|
|
|
|
response = requests.post(f'{base_url}/estimate-depth', files=files, timeout=60) |
|
|
|
|
|
if response.status_code == 200: |
|
|
result = response.json() |
|
|
print("β
API Request Successful!") |
|
|
print("\nResults:") |
|
|
print(f" π Distance: {result.get('distance_meters', 'N/A')} meters") |
|
|
print(f" π― Focal Length: {result.get('focal_length_px', 'N/A')} pixels") |
|
|
print(f" π Depth Map Shape: {result.get('depth_map_shape', 'N/A')}") |
|
|
print(f" π Top Pixel: {result.get('topmost_pixel', 'N/A')}") |
|
|
print(f" π½ Bottom Pixel: {result.get('bottommost_pixel', 'N/A')}") |
|
|
|
|
|
depth_stats = result.get('depth_stats', {}) |
|
|
if depth_stats: |
|
|
print(f" π Depth Range: {depth_stats.get('min_depth', 0):.2f}m - {depth_stats.get('max_depth', 0):.2f}m") |
|
|
print(f" π Mean Depth: {depth_stats.get('mean_depth', 0):.2f}m") |
|
|
|
|
|
return True |
|
|
else: |
|
|
print(f"β API Request Failed!") |
|
|
print(f"Status Code: {response.status_code}") |
|
|
print(f"Response: {response.text}") |
|
|
return False |
|
|
|
|
|
except requests.exceptions.ConnectionError: |
|
|
print("β Connection Error!") |
|
|
print("Make sure the server is running with: python app.py") |
|
|
return False |
|
|
except Exception as e: |
|
|
print(f"β Unexpected Error: {e}") |
|
|
return False |
|
|
|
|
|
def save_sample_image(): |
|
|
"""Save a sample image for manual testing""" |
|
|
sample_image = create_sample_image() |
|
|
filename = "sample_test_image.jpg" |
|
|
sample_image.save(filename, quality=95) |
|
|
print(f"πΎ Sample image saved as '{filename}'") |
|
|
print("You can upload this image to test the Gradio interface manually.") |
|
|
return filename |
|
|
|
|
|
def main(): |
|
|
"""Main function to run examples""" |
|
|
print("π Depth Pro Distance Estimation - Example Usage") |
|
|
print("=" * 55) |
|
|
print() |
|
|
|
|
|
|
|
|
sample_file = save_sample_image() |
|
|
print() |
|
|
|
|
|
|
|
|
print("Testing API endpoint...") |
|
|
api_success = test_api_endpoint() |
|
|
print() |
|
|
|
|
|
if not api_success: |
|
|
print("π‘ To test the API:") |
|
|
print("1. Run: python app.py") |
|
|
print("2. Wait for 'Running on http://0.0.0.0:7860'") |
|
|
print("3. Run this script again") |
|
|
print() |
|
|
|
|
|
print("π‘ To test the web interface:") |
|
|
print("1. Run: python app.py") |
|
|
print("2. Open http://localhost:7860 in your browser") |
|
|
print(f"3. Upload the generated image: {sample_file}") |
|
|
print() |
|
|
|
|
|
print("π For Hugging Face Spaces deployment:") |
|
|
print("1. Create a new Space on https://huggingface.co/spaces") |
|
|
print("2. Choose 'Docker' as the SDK") |
|
|
print("3. Upload all files from this directory") |
|
|
print("4. The Space will automatically build and deploy") |
|
|
print() |
|
|
|
|
|
print("π Example curl command:") |
|
|
print("curl -X POST http://localhost:7860/estimate-depth \\") |
|
|
print(f" -F 'file=@{sample_file}' \\") |
|
|
print(" -H 'Content-Type: multipart/form-data'") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|