Spaces:
Sleeping
Sleeping
| # -*- coding: utf-8 -*- | |
| """api | |
| Automatically generated by Colab. | |
| Original file is located at | |
| https://colab.research.google.com/drive/1zg-G3yXyLeOMsGYaU19_7DAivaOyAoF3 | |
| """ | |
| import cv2 | |
| import numpy as np | |
| import asyncio | |
| import base64 | |
| import io | |
| from fastanpr import FastANPR | |
| from fastapi import FastAPI | |
| from pydantic import BaseModel | |
| # --- 1. INITIALIZE THE APP AND MODELS --- | |
| # Create the FastAPI app instance | |
| app = FastAPI(title="FastANPR API") | |
| # Load the FastANPR model ONCE on startup. | |
| # This is crucial for performance. | |
| print("Loading FastANPR (YOLOv8 + PaddleOCR) model...") | |
| fast_anpr = FastANPR() | |
| print("Model loaded successfully.") | |
| # --- 2. DEFINE THE REQUEST DATA SHAPE --- | |
| # This Pydantic model defines what our API expects in the request body. | |
| # We'll expect a JSON object with one key: "image" | |
| # The value will be a base64-encoded string of the image. | |
| class ImageRequest(BaseModel): | |
| image: str # Base64 encoded image string | |
| # --- 3. CREATE THE API ENDPOINT --- | |
| # @app.post("/recognise") defines a POST endpoint at the URL /recognise | |
| # This is what your mobile app will call. | |
| async def recognise_plate(request: ImageRequest): | |
| """ | |
| Receives a base64 encoded image, decodes it, runs ANPR, | |
| and returns any found license plates. | |
| """ | |
| try: | |
| # --- A. DECODE THE IMAGE --- | |
| # Get the base64 string from the request | |
| base64_image_str = request.image | |
| # Decode the base64 string into raw image bytes | |
| image_data = base64.b64decode(base64_image_str) | |
| # Convert the raw bytes into a numpy array | |
| nparr = np.frombuffer(image_data, np.uint8) | |
| # Decode the numpy array into an OpenCV image (BGR format) | |
| img_bgr = cv2.imdecode(nparr, cv2.IMREAD_COLOR) | |
| if img_bgr is None: | |
| return {"error": "Could not decode image."} | |
| # fastanpr expects images in RGB format | |
| img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB) | |
| # --- B. RUN ANPR --- | |
| # Run ANPR (it expects a list of images) | |
| # We await it because fast_anpr.run is an async function | |
| all_results = await fast_anpr.run([img_rgb]) | |
| # Get the results for our single image (it's the first item) | |
| plates_in_image = all_results[0] | |
| # --- C. FORMAT THE RESPONSE --- | |
| # Create a list to hold plate data | |
| formatted_plates = [] | |
| if plates_in_image: | |
| for plate in plates_in_image: | |
| formatted_plates.append({ | |
| "text": plate.rec_text, | |
| "detection_confidence": plate.det_conf, | |
| "recognition_confidence": plate.rec_conf, | |
| "box": plate.det_box | |
| }) | |
| # Return the list of found plates | |
| return {"plates": formatted_plates} | |
| except Exception as e: | |
| print(f"An error occurred: {e}") | |
| return {"error": str(e)} | |
| # --- 4. (Optional) RUN THE SERVER --- | |
| # This part allows you to run the script directly with `python api.py` | |
| # For production, you'd use: uvicorn api:app --host 0.0.0.0 --port 8000 | |
| if __name__ == "__main__": | |
| import uvicorn | |
| print("Starting Uvicorn server... Go to http://127.0.0.1:8000/docs") | |
| uvicorn.run(app, host="127.0.0.1", port=8000) | |