lp128396 commited on
Commit
ea0edbd
·
verified ·
1 Parent(s): 0c0c7fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -58
app.py CHANGED
@@ -1,58 +1,57 @@
1
- from flask_cors import CORS
2
- app = Flask(__name__)
3
- CORS(app)
4
-
5
- from flask import Flask, request, jsonify
6
- from tensorflow.keras.models import load_model
7
- import numpy as np
8
- from PIL import Image
9
- import io
10
-
11
- app = Flask(__name__)
12
- model = load_model("final_resnet.h5")
13
- class_names = {
14
- 0: 'FreshApple', 1: 'FreshBanana', 2: 'FreshGrape', 3: 'FreshGuava',
15
- 4: 'FreshJujube', 5: 'FreshOrange', 6: 'FreshPomegranate', 7: 'FreshStrawberry',
16
- 8: 'RottenApple', 9: 'RottenBanana', 10: 'RottenGrape', 11: 'RottenGuava',
17
- 12: 'RottenJujube', 13: 'RottenOrange', 14: 'RottenPomegranate', 15: 'RottenStrawberry'
18
- }
19
-
20
- def preprocess_image(img_bytes):
21
- img = Image.open(io.BytesIO(img_bytes)).resize((224, 224))
22
- img = np.array(img) / 255.0
23
- return np.expand_dims(img, axis=0)
24
-
25
- @app.route('/')
26
- def home():
27
- return "API is running!"
28
-
29
- @app.route('/predict', methods=['POST'])
30
- def predict():
31
- file = request.files.get('image')
32
- if not file:
33
- return jsonify({'error': 'No image provided'}), 400
34
-
35
- img = preprocess_image(file.read())
36
- pred = model.predict(img)[0]
37
- sorted_indices = np.argsort(pred)[::-1]
38
- top1, top2 = sorted_indices[:2]
39
- top1_label = class_names[top1]
40
- top2_label = class_names[top2]
41
- top1_conf = pred[top1] * 100
42
- top2_conf = pred[top2] * 100
43
-
44
- result = {}
45
-
46
- if top1_conf >= 80:
47
- result['message'] = 'Fresh' if 'Fresh' in top1_label else 'Rotten'
48
- elif ('Fresh' in top1_label and 'Rotten' in top2_label) or ('Rotten' in top1_label and 'Fresh' in top2_label):
49
- result['message'] = f"{top1_label} ({top1_conf:.1f}%) vs {top2_label} ({top2_conf:.1f}%)"
50
- elif 'Rotten' in top1_label:
51
- result['message'] = '⚠️ Do not eat this fruit.'
52
- elif 'Fresh' in top1_label and top1_conf < 80:
53
- result['message'] = '🍃 Seems fresh, but be cautious.'
54
-
55
- result['confidence'] = round(top1_conf, 2)
56
- result['fruitType'] = top1_label
57
- result['fresh'] = 'Fresh' in top1_label
58
- return jsonify(result)
 
1
+ from flask import Flask, request, jsonify
2
+ from flask_cors import CORS
3
+ from tensorflow.keras.models import load_model
4
+ import numpy as np
5
+ from PIL import Image
6
+ import io
7
+
8
+ app = Flask(__name__)
9
+ CORS(app)
10
+
11
+ model = load_model("final_resnet.h5")
12
+ class_names = {
13
+ 0: 'FreshApple', 1: 'FreshBanana', 2: 'FreshGrape', 3: 'FreshGuava',
14
+ 4: 'FreshJujube', 5: 'FreshOrange', 6: 'FreshPomegranate', 7: 'FreshStrawberry',
15
+ 8: 'RottenApple', 9: 'RottenBanana', 10: 'RottenGrape', 11: 'RottenGuava',
16
+ 12: 'RottenJujube', 13: 'RottenOrange', 14: 'RottenPomegranate', 15: 'RottenStrawberry'
17
+ }
18
+
19
+ def preprocess_image(img_bytes):
20
+ img = Image.open(io.BytesIO(img_bytes)).resize((224, 224))
21
+ img = np.array(img) / 255.0
22
+ return np.expand_dims(img, axis=0)
23
+
24
+ @app.route('/')
25
+ def home():
26
+ return "API is running!"
27
+
28
+ @app.route('/predict', methods=['POST'])
29
+ def predict():
30
+ file = request.files.get('image')
31
+ if not file:
32
+ return jsonify({'error': 'No image provided'}), 400
33
+
34
+ img = preprocess_image(file.read())
35
+ pred = model.predict(img)[0]
36
+ sorted_indices = np.argsort(pred)[::-1]
37
+ top1, top2 = sorted_indices[:2]
38
+ top1_label = class_names[top1]
39
+ top2_label = class_names[top2]
40
+ top1_conf = pred[top1] * 100
41
+ top2_conf = pred[top2] * 100
42
+
43
+ result = {}
44
+
45
+ if top1_conf >= 80:
46
+ result['message'] = 'Fresh' if 'Fresh' in top1_label else 'Rotten'
47
+ elif ('Fresh' in top1_label and 'Rotten' in top2_label) or ('Rotten' in top1_label and 'Fresh' in top2_label):
48
+ result['message'] = f"{top1_label} ({top1_conf:.1f}%) vs {top2_label} ({top2_conf:.1f}%)"
49
+ elif 'Rotten' in top1_label:
50
+ result['message'] = '⚠️ Do not eat this fruit.'
51
+ elif 'Fresh' in top1_label and top1_conf < 80:
52
+ result['message'] = '🍃 Seems fresh, but be cautious.'
53
+
54
+ result['confidence'] = round(top1_conf, 2)
55
+ result['fruitType'] = top1_label
56
+ result['fresh'] = 'Fresh' in top1_label
57
+ return jsonify(result)