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.
- YOLOv8 pose estimation for keypoint detection
- Curvature angle calculation
- Async image processing
- CORS support for frontend integration
- Error handling and logging
- Health check endpoint
- Clone the repository:
git clone https://github.com/yourusername/yolov8-fastapi.git
cd yolov8-fastapi
- Create a virtual environment and activate it:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
- Create a .env file:
cp .env.example .env
# Edit .env with your configuration
- Start the server:
uvicorn main:app --reload
- The API will be available at http://localhost:8000
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]]
}
Health check endpoint.
Response:
{
"status": "ok"
}
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)
- Install development dependencies:
pip install -r requirements-dev.txt
- Run tests:
pytest
The API can be deployed to various platforms:
- Create a new Web Service
- Connect your GitHub repository
- Set environment variables
- Deploy
- Install flyctl
- Run
fly launch
- Set environment variables
- Deploy with
fly deploy
MIT License - see LICENSE file for details
- 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
You have two options to start the application: using Docker or locally on your machine.
Start the application with the following command:
docker-compose up
To start the application locally, follow these steps:
- Install the required packages:
pip install -r requirements.txt
- 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
Ready to start your object detection journey with YOLOv8-FastAPI? π
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'}
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)
This repository contains functional tests for a program to ensure the proper operation of the service.
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.
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}
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: