DEV Community

Cover image for How I Built and Deployed an AI Vision API from Scratch with YOLOv8 and FastAPI
Lokendra S Parihar
Lokendra S Parihar

Posted on

How I Built and Deployed an AI Vision API from Scratch with YOLOv8 and FastAPI

The Problem That Started Everything

I am a CS and Mathematics student from Ujjain, India. A few months ago
I was trying to learn computer vision and build real projects with it.

Then I hit a wall.

Every tutorial, every library, every framework assumed you had an NVIDIA
GPU. CUDA this. GPU memory that. "Just spin up a cloud GPU instance" —
at $2-3 per hour, which adds up to hundreds of dollars per month.

I have an Intel integrated graphics chip. 8GB RAM. No NVIDIA card. No
budget for cloud GPUs.

I couldn't do the thing I wanted to learn.

So I decided to build the solution I wished existed.


What I Built

LARA — Lightweight Adaptive Recognition API

A computer vision API where you send an image and get back detected
objects, confidence scores, and bounding box coordinates in one simple
API call. The heavy AI computation runs on my server. You get the
results instantly. No GPU needed on your end. Ever.

Here is all the code a developer needs to use LARA:

import requests

response = requests.post(
    "https://web-production-41d94.up.railway.app/detect",
    headers={"x-api-key": "your-api-key"},
    files={"file": open("image.jpg", "rb")}
)

print(response.json())
Enter fullscreen mode Exit fullscreen mode

That's it. Three lines. You get back:

{
  "status": "success",
  "count": 2,
  "detections": [
    {"label": "person", "confidence": 0.92, "box": [10, 20, 150, 300]},
    {"label": "car",    "confidence": 0.87, "box": [200, 50, 500, 400]}
  ]
}
Enter fullscreen mode Exit fullscreen mode

The Tech Stack

Here is exactly what I used to build LARA:

AI Model — YOLOv8 nano
I chose YOLOv8 nano specifically because it is the smallest and fastest
variant of YOLOv8. It detects 80+ object classes accurately while being
light enough to run on a basic CPU server without timing out.

API Framework — FastAPI
FastAPI auto-generates beautiful API documentation, handles file uploads
cleanly, and is fast enough for production workloads. It also generates
OpenAPI specs automatically which made listing on RapidAPI easy.

Database — Supabase
Three tables: users, usage, billing. Every API call gets logged. Every
developer gets tracked. Supabase gave me a production-grade PostgreSQL
database with a clean Python client and Row Level Security — for free.

Payments — Razorpay
For Indian developers and businesses. Razorpay handles the billing so
I don't have to.

Hosting — Railway
Deployed via Docker. Railway auto-deploys every time I push to GitHub.
The entire deployment pipeline took about 30 minutes to set up.


How I Built It — Step by Step

Step 1 — The API skeleton

I started with a basic FastAPI app:

from fastapi import FastAPI, File, UploadFile, Header, HTTPException
from PIL import Image
from ultralytics import YOLO
import io

app = FastAPI(title="LARA API")
model = YOLO("yolov8n.pt")

@app.post("/detect")
async def detect(file: UploadFile = File(...), x_api_key: str = Header(...)):
    contents = await file.read()
    img = Image.open(io.BytesIO(contents)).convert("RGB")
    results = model(img, verbose=False)

    detections = []
    for box in results[0].boxes:
        label = model.names[int(box.cls)]
        confidence = round(float(box.conf), 3)
        x1, y1, x2, y2 = [round(float(v)) for v in box.xyxy[0]]
        detections.append({
            "label": label,
            "confidence": confidence,
            "box": [x1, y1, x2, y2]
        })

    return {"status": "success", "detections": detections, "count": len(detections)}
Enter fullscreen mode Exit fullscreen mode

Step 2 — Authentication and usage tracking

I added API key validation and usage logging with Supabase:

def get_user(api_key: str):
    result = supabase.table("users").select("*").eq("api_key", api_key).execute()
    if result.data:
        return result.data[0]
    return None

def log_usage(api_key: str, endpoint: str):
    supabase.table("usage").insert({
        "api_key": api_key,
        "endpoint": endpoint,
        "timestamp": datetime.now().isoformat()
    }).execute()
Enter fullscreen mode Exit fullscreen mode

Step 3 — Fixing the double detection bug

YOLOv8 nano sometimes detects the same object twice with slightly
different bounding boxes. I implemented NMS (Non-Maximum Suppression)
manually to remove duplicates:

seen_boxes = []
for box in results[0].boxes:
    x1, y1, x2, y2 = [round(float(v)) for v in box.xyxy[0]]

    duplicate = False
    for seen in seen_boxes:
        sx1, sy1, sx2, sy2 = seen
        inter_area = max(0, min(x2,sx2)-max(x1,sx1)) * max(0, min(y2,sy2)-max(y1,sy1))
        union_area = (x2-x1)*(y2-y1) + (sx2-sx1)*(sy2-sy1) - inter_area
        iou = inter_area / union_area if union_area > 0 else 0
        if iou > 0.5:
            duplicate = True
            break

    if not duplicate and confidence >= 0.3:
        detections.append({...})
        seen_boxes.append((x1, y1, x2, y2))
Enter fullscreen mode Exit fullscreen mode

Step 4 — Deployment with Docker on Railway

The biggest challenge was the Linux server not having the display
libraries OpenCV needs. The fix was switching to
opencv-python-headless and building with a proper Dockerfile:

FROM python:3.10-slim
WORKDIR /app
RUN apt-get update && apt-get install -y libglib2.0-0 libgl1 libxcb1
COPY requirements.txt .
RUN pip install --no-cache-dir opencv-python-headless==4.10.0.84
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Enter fullscreen mode Exit fullscreen mode

What LARA Can Do Right Now

  • Detects 80+ object classes — people, vehicles, animals, furniture, electronics and more
  • Returns confidence scores and exact pixel bounding boxes
  • Processes images in under 2 seconds
  • Handles authentication and usage tracking per API key
  • Free tier — 100 calls/month, no credit card needed

Who This Is For

If you are a developer, student, or researcher who wants to add computer
vision to your project without:

  • Buying expensive hardware
  • Managing ML infrastructure
  • Spending hours setting up models

LARA is for you.

A parking management system. A security camera app. A retail analytics
dashboard. A wildlife monitoring tool. All of these need object
detection. None of them should need a $3000 GPU setup to get started.


Try LARA for Free

Register at the landing page and get your API key instantly — no credit
card, no waiting:

👉 https://lokendraparihar-9977.github.io/lara-landing

Full API docs:

👉 https://web-production-41d94.up.railway.app/docs

Free tier includes 100 API calls/month. Paid plans start at ₹249/month
(≈ $2.99) for 500 calls — cheaper than 30 minutes on a cloud GPU.


What's Next for LARA

I am building specialized models for:

  • Indian traffic detection (rickshaws, auto rickshaws, Indian road signs)
  • Global traffic intelligence (regionalized by continent)
  • Agriculture (crop disease detection)
  • Safety (helmet and gear detection for construction sites)

If you have a use case you want LARA to support, let me know in the
comments. I am building this based on real developer needs.


Built by Lokendra Singh Parihar — CS + Mathematics student,
Vikram University, Ujjain, India.

Connect with me on GitHub: github.com/lokendraparihar-9977

Top comments (1)

Collapse
 
lokendra_parihar profile image
Lokendra S Parihar

Happy to answer any questions about the build.

Giving free Pro tier (2,000 calls/month) to the first 5 developers
who try LARA and share feedback. Just email me at
lokendraparihar9977@gmail.com with subject "LARA Pro Access"
after signing up at the landing page.