-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from arvindrajan92/FASTANPR-3
FASTANPR-3 - Adding Support for Running FastAPI Server hosting FastANPR
- Loading branch information
Showing
10 changed files
with
147 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Use the official Python base image | ||
FROM python:3.11-slim | ||
|
||
# Install other dependencies | ||
RUN apt-get update && apt-get install ffmpeg libsm6 libxext6 -y | ||
|
||
# Set the working directory inside the container | ||
WORKDIR /app | ||
|
||
# Copy the requirements file to the working directory | ||
COPY requirements.txt . | ||
|
||
# Install the Python dependencies | ||
RUN pip install -r requirements.txt | ||
|
||
# Copy the application code to the working directory | ||
COPY . . | ||
|
||
# Expose the port on which the application will run | ||
EXPOSE 8000 | ||
|
||
# Run the FastAPI application using uvicorn server | ||
CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "8000"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import io | ||
import base64 | ||
import uvicorn | ||
import fastanpr | ||
import numpy as np | ||
|
||
from PIL import Image | ||
from fastapi import FastAPI | ||
from pydantic import BaseModel | ||
|
||
app = FastAPI( | ||
title="FastANPR", | ||
description="A web server for FastANPR hosted using FastAPI", | ||
version=fastanpr.__version__ | ||
) | ||
fast_anpr = fastanpr.FastANPR() | ||
|
||
|
||
class FastANPRRequest(BaseModel): | ||
image: str | ||
|
||
|
||
class FastANPRResponse(BaseModel): | ||
number_plates: list[fastanpr.NumberPlate] = None | ||
|
||
|
||
def base64_image_to_ndarray(base64_image_str: str) -> np.ndarray: | ||
image_data = base64.b64decode(base64_image_str) | ||
image = Image.open(io.BytesIO(image_data)) | ||
return np.array(image, dtype=np.uint8) | ||
|
||
|
||
@app.post("/recognise", response_model=FastANPRResponse) | ||
async def recognise(request: FastANPRRequest): | ||
image = base64_image_to_ndarray(request.image) | ||
number_plates = (await fast_anpr.run(image))[0] | ||
return FastANPRResponse( | ||
number_plates=[fastanpr.NumberPlate.parse_obj(number_plate.__dict__) for number_plate in number_plates] | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
uvicorn.run(app, host="0.0.0.0", port=8000, log_level="debug") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from .fastanpr import FastANPR | ||
from .version import __version__ | ||
from .fastanpr import FastANPR, NumberPlate | ||
|
||
__version__ = __version__ | ||
FastANPR = FastANPR | ||
NumberPlate = NumberPlate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
from dataclasses import dataclass | ||
from typing import List | ||
from pydantic import BaseModel | ||
|
||
|
||
@dataclass(frozen=True) | ||
class NumberPlate: | ||
class NumberPlate(BaseModel): | ||
det_box: List[int] | ||
det_conf: float | ||
rec_poly: List[List[int]] = None | ||
rec_text: str = None | ||
rec_conf: float = None | ||
|
||
class Config: | ||
frozen = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
ultralytics>=8.1.34 | ||
paddlepaddle>=2.6.1 | ||
paddleocr==2.7.2 | ||
fastapi>=0.110.0 | ||
pydantic>=2.6.4 | ||
uvicorn>=0.29.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters