CoolFace
Apppublic

panditamey/flowerClassification

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
api.py66 linesDownload Raw Back to root
1from fastapi import FastAPI,UploadFile,File2from fastapi.middleware.cors import CORSMiddleware3 4from pydantic import BaseModel5import pickle6import json7import pandas as pd8from tensorflow.keras.models import load_model9from tensorflow.keras.preprocessing import image10from tensorflow.keras.applications.inception_v3 import preprocess_input11import numpy as np12import os13import gdown14import lightgbm as lgb15from PIL import Image16 17CHUNK_SIZE = 102418 19app = FastAPI(20    title='Flower Classification API',21    description='API for Flower Classification',22)23origins = ["*"]24 25app.add_middleware(26    CORSMiddleware,27    allow_origins=origins,28    allow_credentials=True,29    allow_methods=["*"],30    allow_headers=["*"],31)32 33id = "1ry4L9L1-kyc79F1MnYMemJ5P81Gr_mHP"34output = "model_flowers_classification.h5"35gdown.download(id=id, output=output, quiet=False)36# from zipfile import ZipFile37# with ZipFile("modelcrops.zip", 'r') as zObject:38#     zObject.extractall(39#         path="")40    41 42predict_ml=load_model('model_flowers_classification.h5')43 44 45@app.post('/predict')46async def flowerpredict(file: UploadFile = File(...)):47    try:48        contents = file.file.read()49        with open(file.filename, 'wb') as f:50            f.write(contents)51    except Exception:52        return {"message": "There was an error uploading the file"}53    finally:54        file.file.close()55    classes = ['Lilly','Lotus','Orchid','Sunflower', 'Tulip']56    img=image.load_img(str(file.filename),target_size=(224,224))57    x=image.img_to_array(img)58    x=x/25559    img_data=np.expand_dims(x,axis=0)60    prediction = predict_ml.predict(img_data)61    predictions = list(prediction[0])62    max_num = max(predictions)63    index = predictions.index(max_num)64    print(classes[index])65    os.remove(str(file.filename))66    return {"output":classes[index]}