|
from flask import Flask, request, jsonify ,render_template , redirect |
|
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 |
|
from flask_cors import CORS, cross_origin |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
id = "1dPrnyH7y9ojSHaOOOTkbGkCnhwYvMxab" |
|
output = "disease_new.h5" |
|
gdown.download(id=id, output=output, quiet=False) |
|
|
|
CORS(app) |
|
app.config['CORS_HEADERS'] = 'Content-Type' |
|
|
|
crop_disease_ml=load_model('disease_new.h5') |
|
|
|
@app.route("/upload-image", methods=["POST"]) |
|
@cross_origin() |
|
def upload_image(): |
|
|
|
if request.files: |
|
imag = request.files["image"] |
|
try: |
|
contents = imag.read() |
|
with open(imag.filename, 'wb') as f: |
|
f.write(contents) |
|
except Exception: |
|
return {"message": "There was an error uploading the file"} |
|
finally: |
|
imag.close() |
|
print(imag) |
|
classes = ['Pepper__bell___Bacterial_spot', 'Pepper__bell___healthy', 'Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy', 'Tomato_Bacterial_spot', 'Tomato_Early_blight', 'Tomato_Late_blight', 'Tomato_Leaf_Mold', 'Tomato_Septoria_leaf_spot', 'Tomato_Spider_mites_Two_spotted_spider_mite', 'Tomato__Target_Spot', 'Tomato__Tomato_YellowLeaf__Curl_Virus', 'Tomato__Tomato_mosaic_virus', 'Tomato_healthy'] |
|
img=image.load_img(str(imag.filename),target_size=(224,224)) |
|
x=image.img_to_array(img) |
|
x=x/255 |
|
img_data=np.expand_dims(x,axis=0) |
|
prediction = crop_disease_ml.predict(img_data) |
|
predictions = list(prediction[0]) |
|
max_num = max(predictions) |
|
index = predictions.index(max_num) |
|
print(classes[index]) |
|
os.remove(str(imag.filename)) |
|
response = jsonify(output=classes[index]) |
|
|
|
|
|
|
|
return response |
|
|
|
|
|
if __name__ =="__main__": |
|
app.run(debug=False,host="0.0.0.0",port=5000) |