panditamey commited on
Commit
1e97531
1 Parent(s): f32505c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +123 -0
app.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify ,render_template , redirect
2
+ from pydantic import BaseModel
3
+ import pickle
4
+ import json
5
+ import pandas as pd
6
+ from tensorflow.keras.models import load_model
7
+ from tensorflow.keras.preprocessing import image
8
+ from tensorflow.keras.applications.inception_v3 import preprocess_input
9
+ import numpy as np
10
+ import os
11
+ import gdown
12
+ import lightgbm as lgb
13
+ from PIL import Image
14
+ from flask_cors import CORS, cross_origin
15
+
16
+
17
+ app = Flask(__name__)
18
+
19
+ id = "1dPrnyH7y9ojSHaOOOTkbGkCnhwYvMxab"
20
+ output = "disease_new.h5"
21
+ gdown.download(id=id, output=output, quiet=False)
22
+
23
+ CORS(app)
24
+ app.config['CORS_HEADERS'] = 'Content-Type'
25
+
26
+ crop_disease_ml=load_model('disease_new.h5')
27
+
28
+ @app.route("/upload-image", methods=["POST"])
29
+ @cross_origin()
30
+ def upload_image():
31
+ # if request.method == "POST":
32
+ if request.files:
33
+ imag = request.files["image"]
34
+ try:
35
+ contents = imag.read()
36
+ with open(imag.filename, 'wb') as f:
37
+ f.write(contents)
38
+ except Exception:
39
+ return {"message": "There was an error uploading the file"}
40
+ finally:
41
+ imag.close()
42
+ print(imag)
43
+ 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']
44
+ img=image.load_img(str(imag.filename),target_size=(224,224))
45
+ x=image.img_to_array(img)
46
+ x=x/255
47
+ img_data=np.expand_dims(x,axis=0)
48
+ prediction = crop_disease_ml.predict(img_data)
49
+ predictions = list(prediction[0])
50
+ max_num = max(predictions)
51
+ index = predictionsfrom flask import Flask, request, jsonify ,render_template , redirect
52
+ from pydantic import BaseModel
53
+ import pickle
54
+ import json
55
+ import pandas as pd
56
+ from tensorflow.keras.models import load_model
57
+ from tensorflow.keras.preprocessing import image
58
+ from tensorflow.keras.applications.inception_v3 import preprocess_input
59
+ import numpy as np
60
+ import os
61
+ import gdown
62
+ import lightgbm as lgb
63
+ from PIL import Image
64
+ from flask_cors import CORS, cross_origin
65
+
66
+
67
+ app = Flask(__name__)
68
+
69
+ id = "1dPrnyH7y9ojSHaOOOTkbGkCnhwYvMxab"
70
+ output = "disease_new.h5"
71
+ gdown.download(id=id, output=output, quiet=False)
72
+
73
+ CORS(app)
74
+ app.config['CORS_HEADERS'] = 'Content-Type'
75
+
76
+ crop_disease_ml=load_model('disease_new.h5')
77
+
78
+ @app.route("/upload-image", methods=["POST"])
79
+ @cross_origin()
80
+ def upload_image():
81
+ # if request.method == "POST":
82
+ if request.files:
83
+ imag = request.files["image"]
84
+ try:
85
+ contents = imag.read()
86
+ with open(imag.filename, 'wb') as f:
87
+ f.write(contents)
88
+ except Exception:
89
+ return {"message": "There was an error uploading the file"}
90
+ finally:
91
+ imag.close()
92
+ print(imag)
93
+ 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']
94
+ img=image.load_img(str(imag.filename),target_size=(224,224))
95
+ x=image.img_to_array(img)
96
+ x=x/255
97
+ img_data=np.expand_dims(x,axis=0)
98
+ prediction = crop_disease_ml.predict(img_data)
99
+ predictions = list(prediction[0])
100
+ max_num = max(predictions)
101
+ index = predictions.index(max_num)
102
+ print(classes[index])
103
+ os.remove(str(imag.filename))
104
+ response = jsonify(output=classes[index])
105
+ # response.headers.add('Access-Control-Allow-Origin', '*')
106
+ # response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
107
+ # response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
108
+ return response
109
+
110
+
111
+ if __name__ =="__main__":
112
+ app.run(debug=False,host="0.0.0.0",port=5000).index(max_num)
113
+ print(classes[index])
114
+ os.remove(str(imag.filename))
115
+ response = jsonify(output=classes[index])
116
+ # response.headers.add('Access-Control-Allow-Origin', '*')
117
+ # response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
118
+ # response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
119
+ return response
120
+
121
+
122
+ if __name__ =="__main__":
123
+ app.run(debug=False,host="0.0.0.0",port=5000)