Most engineering teams default to "buy" before "build." For object detection, they plug in AWS Rekognition or Google Vision API, pat themselves on the back, and then panic when the bill hits $5,000/month. If you have engineering talent, this is laziness.
Today, we're building a production-ready Object Detection API using FastAPI and YOLOv7. Cost? Minimal. Performance? Blazing. Security? Baked in. Whether you're a startup or screening senior python developers, this is the baseline for modern backend work.
Part 1: The Stack (Don't Overcomplicate It)
We are using FastAPI because Flask is too slow for ML inference and Django is too heavy. FastAPI gives us automatic Swagger documentation and async support out of the box.
cd secure-vision-api
python3 -m venv venv
source venv/bin/activate
pip install fastapi uvicorn yolov7onnxruntime
(Note: Replace yolov7onnxruntime with your preferred model wrapper, but for this demo, we're keeping it simple.)
Part 2: The Foundation
Create a main.py. We start with a basic check. If this doesn't work, your environment is broken.
Run it: python main.py. Go to http://localhost:8001/. Start your engine.
Part 3: The Intelligence (Object Detection)
Now easy mode is over. We integrate the YOLOv7 model. We aren't training it (that's expensive); we are using it (inference is cheap).
Performance Tip: Always load your ML models globally at startup. If you load the model inside the request function, your latency will jump from 50ms to 500ms per request.
Part 4: The Security (The Lock)
Right now, your API is public. Anyone with the IP address can use your server to detecting cats, cars, or people. You're paying for their fun. Let's fix that with HTTPBearer token authentication.
We don't need OAuth2 complexity for an internal microservice. A strong, rotated API key is sufficient.
What just happened?
- ✓We added a dependency to the endpoint.
- ✓FastAPI checks the
Authorization: Bearerheader automatically. - ✓If the token is missing or wrong, the request stops dead with a 403 error.
Frequently Asked Questions
Is a static API token secure enough?
For internal microservices or MVPs, yes. It prevents unauthorized public access. For public-facing SaaS products with user accounts, you absolutely need robust OAuth2 or JWT implementation. Don't confuse "secure enough for internal use" with "bank-grade security."
Why use FastAPI over Flask?
Performance and types. FastAPI is built on Starlette and Pydantic, making it significantly faster than Flask for I/O bound operations. More importantly, Pydantic data validation ensures you never crash your ML model with malformed JSON inputs. Flask requires manual validation.
How do I deploy this to production?
Dockerize it. Create a Dockerfile, install dependencies, and run it with Gunicorn + Uvicorn workers. Deploy the container to a managed service like AWS ECS or simpler alternatives like DigitalOcean App Platform. Never run uvicorn main:app --reload in production.
Build Assets, Don't Rent Them
Building your own APIs builds internal IP. Renting everything from AWS builds Jeff Bezos's IP. With Python and FastAPI, the barrier to entry is gone. The only excuse left is laziness.
Need a team that builds systems like this by default? Read about how we build high-performance teams that optimize costs from day one.
Stop Overpaying for Cloud APIs
We connect you with senior Python engineers who build efficient, secure, and self-owned infrastructure.
Hire Python Experts