File size: 3,002 Bytes
3754759
eae62a9
 
 
 
3754759
eae62a9
3754759
eae62a9
3754759
 
eae62a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: Depth Pro Distance Estimation
emoji: πŸ“
colorFrom: blue
colorTo: green
sdk: docker
app_port: 7860
pinned: false
license: mit
---

# Depth Pro Distance Estimation

This Hugging Face Space uses Apple's Depth Pro model via Transformers pipeline to estimate depth and calculate distances in images. The application runs entirely on CPU and provides a FastAPI REST API with a simple web interface.

## Features

- **Depth Estimation**: Uses Apple's Depth Pro model via Transformers pipeline
- **Distance Calculation**: Estimates real-world distances between key points in images
- **CPU-Only**: Optimized to run on CPU hardware
- **Transformers Integration**: Simple pipeline-based implementation
- **FastAPI**: REST API with automatic documentation
- **Web Interface**: Simple HTML interface for easy testing
- **Real-time Processing**: Fast inference suitable for interactive use

## How it Works

1. **Upload Image**: Provide an image through the web interface or API
2. **Depth Estimation**: The Transformers pipeline uses Depth Pro to generate a depth map
3. **Pixel Detection**: Finds topmost and bottommost edge pixels
4. **Distance Calculation**: Calculates real-world distance using depth information
5. **Results**: Returns detailed measurements and statistics

## API Endpoints

### POST /estimate-depth
Upload an image to get depth estimation and distance calculation results.

**Parameters:**
- `file`: Image file (JPG, PNG, etc.)

**Response:**
```json
{
  "depth_map_shape": [384, 512],
  "focal_length_px": 614.4,
  "topmost_pixel": [50, 256],
  "bottommost_pixel": [300, 256],
  "distance_meters": 2.45,
  "depth_stats": {
    "min_depth": 1.2,
    "max_depth": 8.7,
    "mean_depth": 4.1
  }
}
```

## Technical Details

- **Model**: Apple Depth Pro via Transformers Pipeline
- **Framework**: FastAPI + Transformers
- **Processing**: CPU-only inference
- **Image Processing**: OpenCV, PIL
- **Precision**: Float32 for CPU compatibility
- **Pipeline**: `pipeline("depth-estimation", model="apple/DepthPro")`

## Usage Examples

### Web Interface

1. Visit the main page at your deployed Space URL
2. Upload an image using the file input
3. Click "Analyze Image" to get results
4. View detailed depth statistics and distance measurements

### API Usage

```python
import requests

files = {'file': open('image.jpg', 'rb')}
response = requests.post('https://your-space-url/estimate-depth', files=files)
result = response.json()
print(f"Distance: {result['distance_meters']:.2f} meters")
```

### cURL Example

```bash
curl -X POST https://your-space-url/estimate-depth \
  -F "file=@image.jpg" \
  -H "Content-Type: multipart/form-data"
```

## Limitations

- CPU-only processing may be slower than GPU
- Distance accuracy depends on image quality and scene structure
- Focal length estimation is heuristic-based
- Best results with clear, well-lit outdoor scenes

## Credits

- **Depth Pro Model**: Apple Inc.
- **Framework**: Gradio, FastAPI
- **Computer Vision**: OpenCV, PIL