Spaces:
Sleeping
Sleeping
File size: 3,845 Bytes
ddbe9e9 | 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 | ---
title: Medical Phase Simulator Backend
emoji: 🏥
colorFrom: blue
colorTo: purple
sdk: docker
pinned: false
---
# MedTech Image Processing Backend
FastAPI-based backend service for processing medical images with arterial and venous phase filters.
## Features
- **Arterial Phase Processing**: CLAHE-based contrast enhancement for improved visibility
- **Venous Phase Processing**: Gaussian smoothing/blur for noise reduction
- **RESTful API**: Simple POST endpoint for image processing
- **CORS Enabled**: Works with frontend hosted on GitHub Pages
- **Docker Ready**: Containerized for easy deployment on Hugging Face Spaces
## API Endpoints
### `POST /process`
Process a medical image with specified phase filter.
**Parameters:**
- `image` (file): Medical image file (JPG/PNG)
- `phase` (form): Processing phase - `"arterial"` or `"venous"`
**Response:**
- Processed image as PNG
**Example using curl:**
```bash
curl -X POST "http://localhost:7860/process" \
-F "image=@sample.jpg" \
-F "phase=arterial" \
--output processed.png
```
### `GET /`
Health check and API information.
### `GET /health`
Detailed health check with library versions.
## Local Development
### Prerequisites
- Python 3.10+
- pip
### Setup
1. Install dependencies:
```bash
pip install -r requirements.txt
```
2. Run the server:
```bash
python app.py
```
The API will be available at `http://localhost:7860`
### Using uvicorn directly:
```bash
uvicorn app:app --reload --port 7860
```
## Docker Deployment
### Build locally:
```bash
docker build -t medtech-backend .
docker run -p 7860:7860 medtech-backend
```
## Hugging Face Spaces Deployment
1. Create a new Space at [huggingface.co/spaces](https://huggingface.co/spaces)
2. Select **Docker** as the SDK
3. Clone the Space repository:
```bash
git clone https://huggingface.co/spaces/YOUR-USERNAME/YOUR-SPACE-NAME
```
4. Copy backend files:
```bash
cp app.py Dockerfile requirements.txt YOUR-SPACE-NAME/
cd YOUR-SPACE-NAME
```
5. Commit and push:
```bash
git add .
git commit -m "Initial commit"
git push
```
6. Your Space will automatically build and deploy at:
`https://YOUR-USERNAME-YOUR-SPACE-NAME.hf.space`
## Image Processing Details
### Arterial Phase (Contrast Enhancement)
- Uses CLAHE (Contrast Limited Adaptive Histogram Equalization)
- Operates in LAB color space for better results
- Parameters: `clipLimit=3.0`, `tileGridSize=(8,8)`
### Venous Phase (Gaussian Smoothing)
- Applies Gaussian blur with 15x15 kernel
- Reduces noise and smooths fine details
- Simulates venous phase imaging characteristics
## Tech Stack
- **FastAPI**: Modern Python web framework
- **OpenCV**: Image processing library
- **NumPy**: Numerical computing
- **Pillow**: Image I/O
- **Uvicorn**: ASGI server
## Error Handling
The API includes comprehensive error handling:
- Invalid file types → 400 Bad Request
- Invalid phase parameter → 400 Bad Request
- Processing errors → 500 Internal Server Error
## CORS Configuration
CORS is configured to accept requests from:
- GitHub Pages (`*.github.io`)
- Localhost (development)
- All origins (can be restricted for production)
## Testing
Test the API with sample medical images:
```python
import requests
url = "http://localhost:7860/process"
files = {"image": open("sample.jpg", "rb")}
data = {"phase": "arterial"}
response = requests.post(url, files=files, data=data)
with open("output.png", "wb") as f:
f.write(response.content)
```
## License
This is a demonstration project for educational purposes only. Not intended for clinical use.
## Disclaimer
⚠️ **Important**: This application is for educational and demonstration purposes only. It should NOT be used for actual medical diagnosis or clinical decision-making. Always consult qualified healthcare professionals for medical imaging interpretation.
|