File size: 1,825 Bytes
807f483
bc6ddf7
 
807f483
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1d0701
807f483
3ca2f4a
 
 
 
 
 
 
 
807f483
 
 
 
 
 
 
 
 
 
 
 
 
6f47b98
807f483
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from fastapi import FastAPI,UploadFile,File
from fastapi.middleware.cors import CORSMiddleware

from pydantic import BaseModel
import pickle
import json
import pandas as pd
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.inception_v3 import preprocess_input
import numpy as np
import os
import gdown
import lightgbm as lgb
from PIL import Image

CHUNK_SIZE = 1024

app = FastAPI(
    title='Flower Classification API',
    description='API for Flower Classification',
)
origins = ["*"]

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

id = "1ry4L9L1-kyc79F1MnYMemJ5P81Gr_mHP"
output = "model_flowers_classification.h5"
gdown.download(id=id, output=output, quiet=False)
# from zipfile import ZipFile
# with ZipFile("modelcrops.zip", 'r') as zObject:
#     zObject.extractall(
#         path="")
    

predict_ml=load_model('model_flowers_classification.h5')


@app.post('/predict')
async def flowerpredict(file: UploadFile = File(...)):
    try:
        contents = file.file.read()
        with open(file.filename, 'wb') as f:
            f.write(contents)
    except Exception:
        return {"message": "There was an error uploading the file"}
    finally:
        file.file.close()
    classes = ['Lilly','Lotus','Orchid','Sunflower', 'Tulip']
    img=image.load_img(str(file.filename),target_size=(224,224))
    x=image.img_to_array(img)
    x=x/255
    img_data=np.expand_dims(x,axis=0)
    prediction = predict_ml.predict(img_data)
    predictions = list(prediction[0])
    max_num = max(predictions)
    index = predictions.index(max_num)
    print(classes[index])
    os.remove(str(file.filename))
    return {"output":classes[index]}