File size: 1,270 Bytes
31a2e2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dffc641
31a2e2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from fastapi import FastAPI, UploadFile, HTTPException, Body
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from transformers import pipeline
from PIL import Image
import base64
from io import BytesIO

app = FastAPI()

# Load the image classification pipeline
classifier = pipeline(model="Diginsa/Plant-Disease-Detection-Project")

# CORS configuration
origins = [""]  # Replace "" with the actual list of allowed origins

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Endpoint to perform image classification
@app.post("/classify")
async def classify_image(encoded_image: str= Body(..., embed=True)):
    try:
        # Decode the base64 encoded image string
        decoded_image = base64.b64decode(encoded_image)

        # Create an Image object from the decoded content
        image = Image.open(BytesIO(decoded_image))

        # Use the classifier with the decoded image
        result = classifier(images=image)

        # Return the classification result as JSON
        return JSONResponse(content=result, status_code=200)

    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))