A FastAPI application that detects vehicles in images, crops them, and removes the background using AI models.
- Vehicle detection using YOLO11
- Automatic cropping to vehicle bounds
- Background removal using rembg
- RESTful API endpoint
-
Clone or download this project
-
Install Python dependencies
pip install -r requirements.txt
-
Run the application
uvicorn app:app --reload
Or with specific host and port:
uvicorn app:app --host 0.0.0.0 --port 8000 --reload
Once the server is running, you can:
- Visit the interactive API docs: http://localhost:8000/docs
- Make POST requests to
/car-remove-background/with an image file
curl -X POST "http://localhost:8000/car-remove-background/" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "file=@your_car_image.jpg" \
--output result.pngimport requests
url = "http://localhost:8000/car-remove-background/"
with open("car_image.jpg", "rb") as f:
files = {"file": f}
response = requests.post(url, files=files)
with open("result.png", "wb") as f:
f.write(response.content)Build a CPU-only image and run the API on port 8000.
# Build
docker build -t car-bg-api:latest .
# Run
docker run --rm -p 8000:8000 \
-e OMP_NUM_THREADS=8 \
car-bg-api:latestNotes:
- If
yolo11x.ptexists in the project root, it will be included in the image and used directly. - If the weight is missing, Ultralytics will auto-download it at first request.
- On low-memory hosts, you can lower the number of threads via
OMP_NUM_THREADS.
- The YOLO model will be automatically downloaded on first run (if not bundled)
- Supported image formats: JPG, PNG, etc.
- The API returns a PNG image with transparent background
- Vehicles (cars, motorcycles, buses, trucks) are detected and processed