Skip to content

Object Detection Service Template. FastAPI for YOLOv8 in Docker.

License

Notifications You must be signed in to change notification settings

Puddin1066/yolov8-fastapi

Β 
Β 

Repository files navigation

Coverage Status

YOLOv8 FastAPI Backend for Penile Curvature Detection

This FastAPI backend service uses YOLOv8 pose estimation to detect penile curvature from images. It's designed to work with a Next.js + Supabase + Stripe frontend.

Features

  • YOLOv8 pose estimation for keypoint detection
  • Curvature angle calculation
  • Async image processing
  • CORS support for frontend integration
  • Error handling and logging
  • Health check endpoint

Installation

  1. Clone the repository:
git clone https://github.com/yourusername/yolov8-fastapi.git
cd yolov8-fastapi
  1. Create a virtual environment and activate it:
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Create a .env file:
cp .env.example .env
# Edit .env with your configuration

Running the API

  1. Start the server:
uvicorn main:app --reload
  1. The API will be available at http://localhost:8000

API Endpoints

POST /predict

Predict penile curvature from an image URL.

Request:

{
    "imageUrl": "https://example.com/image.jpg"
}

Response:

{
    "angle": 41.2,
    "confidence": 0.96,
    "keypoints": [[122, 301], [198, 308], [145, 209], [175, 200]]
}

GET /health

Health check endpoint.

Response:

{
    "status": "ok"
}

Environment Variables

  • FRONTEND_URL: URL of the frontend application (default: https://realcurve.vercel.app)
  • MODEL_PATH: Path to the YOLOv8 pose model (default: yolov8n-pose.pt)
  • LOG_LEVEL: Logging level (default: INFO)

Development

  1. Install development dependencies:
pip install -r requirements-dev.txt
  1. Run tests:
pytest

Deployment

The API can be deployed to various platforms:

Render

  1. Create a new Web Service
  2. Connect your GitHub repository
  3. Set environment variables
  4. Deploy

Fly.io

  1. Install flyctl
  2. Run fly launch
  3. Set environment variables
  4. Deploy with fly deploy

License

MIT License - see LICENSE file for details

What's inside:

  • YOLOv8: A popular real-time object detection model
  • FastAPI: A modern, fast (high-performance) web framework for building APIs
  • Docker: A platform for easily building, shipping, and running distributed applications


Getting Started

You have two options to start the application: using Docker or locally on your machine.

Using Docker

Start the application with the following command:

docker-compose up

Locally

To start the application locally, follow these steps:

  1. Install the required packages:
pip install -r requirements.txt
  1. Start the application:
uvicorn main:app --reload --host 0.0.0.0 --port 8001

Note: You can change the address and port in the file docker-compose.yaml

FAST API Docs url:

http://0.0.0.0:8001/docs#/

FAST API

Ready to start your object detection journey with YOLOv8-FastAPI? πŸš€


πŸš€ Code Examples

Example 1: Object Detection to JSON

The following code demonstrates how to perform object detection and receive the results in JSON format:

import requests

input_image_name = 'test_image.jpg'
api_host = 'http://0.0.0.0:8001/'
type_rq = 'img_object_detection_to_json'

files = {'file': open(input_image_name, 'rb')}

response = requests.post(api_host+type_rq, files=files)

data = response.json()     
print(data)

Output:

{'detect_objects': [{'name': 'cat', 'confidence': 0.926225245}, {'name': 'dog', 'confidence': 0.9109069705}], 'detect_objects_names': 'cat, dog'}

Example 2: Object Detection to Image

The following code demonstrates how to perform object detection and receive the results in image format.

import requests
from PIL import Image
from io import BytesIO
import matplotlib.pyplot as plt

input_image_name = 'test_image.jpg'
api_host = 'http://0.0.0.0:8001/'
type_rq = 'img_object_detection_to_img'

files = {'file': open(input_image_name, 'rb')}

response = requests.post(api_host+type_rq, files=files)

img = Image.open(BytesIO(response.content)) 
plt.imshow(img)

Overview of the code


Test

This repository contains functional tests for a program to ensure the proper operation of the service.

Getting Started Test

To get started with the testing process, you first need to set up the necessary environment. This can be achieved by either installing the required packages or by running the Docker container.

1. Installing Required Packages:

Run the following command to install the necessary packages:

pip install -r requirements.txt

Alternatively, you can also run the tests inside a Docker container. To do so, follow these steps: Start the Docker container:

docker-compose up

Find the container ID:

docker ps

Connect inside the container:

docker exec -it {CONTAINER_ID}

2. Run the tests from the program directory:

Once you have set up the environment, navigate to the program directory and run the tests using the following command:

pytest -v --disable-warnings

If all tests pass successfully, you will see the following result:


Contact

Telegram Group

About

Object Detection Service Template. FastAPI for YOLOv8 in Docker.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 99.4%
  • Dockerfile 0.6%