This is a FastAPI application uses the Mediapipe library to perform face and object detection on images. It provides endpoints to detect faces and objects from file, base64 encoded image or numpy array.
- A Dockerfile for building the image from scratch using Python 3.9, Pillow (for handling images), NumPy (for array operations), and mediapipe (a library for computer vision tasks).
- An endpoint
/
that returns a simple health check message to ensure the server is running smoothly. - Endpoints for detecting faces in an image from different sources:
- A file upload endpoint (
/detect_face_from_file
) that accepts a single file and returns the detected face coordinates as JSON data. - An array input endpoint (
/detect_face_from_array
) that takes a list of integers representing RGB values for an image and returns the same JSON data. - A base64-encoded string input endpoint (
/detect_face_from_base64
) that accepts a base64-encoded image file and returns the same JSON data.
- A file upload endpoint (
- Endpoints for detecting objects in an image from different sources:
- Similar to the face detection endpoints, but with object detector options set to return up to 5 results with a score threshold of 0.5.
The code uses Pydantic models and HTTPExceptions for request validation and error handling. The detect_faces
and detect_objects
functions use MediaPipe's FaceDetector and ObjectDetector classes, respectively, to perform the actual detection tasks on NumPy arrays or image files.
- Clone the repository:
git clone https://github.com/<username>/image-detection-fastapi.git
cd image-detection-fastapi
- Create a virtual environment and activate it:
python3 -m venv env
source env/bin/activate (on Linux/macOS)
env\Scripts\activate (on Windows)
- Install the required packages:
pip install --no-cache-dir mediapipe fastapi uvicorn sentencepiece python-multipart
- Run the application using uvicorn:
uvicorn main:app --host 0.0.0.0 --port 8000
Check if the server is running smoothly. Returns a JSON object with status information.
Example response:
{
"status": "Server is running smoothly!"
}
Detect faces in an image from a file. Returns the detection results as JSON object.
Request body: File (image)
Detect faces in a numpy array image. Returns the detection results as JSON object.
Request body: List[int] (numpy array)
Detect faces in an image from a base64 encoded string. Returns the detection results as JSON object.
Request body: DetectionRequest (image_path: str)
{
"face_detector_result": [
{
"bounding_box": [x1, y1, x2, y2],
"score": 0.9876543
},
...
]
}
Detect objects in an image from a file. Returns the detection results as JSON object.
Request body: File (image)
Detect objects in a numpy array image. Returns the detection results as JSON object.
Request body: List[int] (numpy array)
Detect objects in an image from a base64 encoded string. Returns the detection results as JSON object.
Request body: DetectionRequest (image_path: str)
{
"object_detector_result": [
{
"bounding_box": [x1, y1, x2, y2],
"label": "person",
"score": 0.9876543
},
...
]
}