levi15 commited on
Commit
2e14777
1 Parent(s): 9a3739f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -36
app.py CHANGED
@@ -1,50 +1,49 @@
1
- # from flask import Flask, request, jsonify, render_template
2
- # from tensorflow.keras.models import load_model
3
- # from tensorflow.keras.preprocessing import image
4
- # from efficientnet.tfkeras import preprocess_input
5
- # import numpy as np
6
-
7
- # app = Flask(__name__)
8
 
9
- # model = load_model('EfficientNet_ModelWeights.keras')
 
10
 
11
- # def preprocess_and_predict(model, img_path, target_size=(224, 224)):
12
- # # Load and preprocess the image
13
- # img = image.load_img(img_path, target_size=target_size)
14
- # img_array = image.img_to_array(img)
15
- # img_array = np.expand_dims(img_array, axis=0)
16
- # img_array = preprocess_input(img_array)
17
 
18
- # # Make prediction
19
- # prediction = model.predict(img_array)
20
- # predicted_class = np.argmax(prediction)
21
 
22
- # # Return the predicted class
23
- # return predicted_class
24
 
25
- # @app.route('/')
26
- # def home():
27
- # return render_template('index.html')
28
 
29
- # @app.route('/predict', methods=['POST'])
30
- # def predict():
31
- # if 'file' not in request.files:
32
- # return jsonify({'error': 'No file part'})
33
 
34
- # file = request.files['file']
35
 
36
- # # Save the uploaded file temporarily
37
- # file_path = 'temp_image.jpg'
38
- # file.save(file_path)
39
 
40
- # # Make prediction
41
- # predicted_class = preprocess_and_predict(model, file_path)
42
 
43
- # # Return the predicted class as a response
44
- # return render_template('index.html', prediction=predicted_class)
45
 
46
- # if __name__ == '__main__':
47
- # app.run(debug=True)
48
 
49
  import streamlit as st
50
  from tensorflow.keras.models import load_model
 
1
+ from flask import Flask, request, jsonify, render_template
2
+ from tensorflow.keras.models import load_model
3
+ from tensorflow.keras.preprocessing import image
4
+ from efficientnet.tfkeras import preprocess_input
5
+ import numpy as np
 
 
6
 
7
+ app = Flask(__name__)
8
+ model = load_model('EfficientNet_ModelWeights.keras')
9
 
10
+ def preprocess_and_predict(model, img_path, target_size=(224, 224)):
11
+ #Load and preprocess the image
12
+ img = image.load_img(img_path, target_size=target_size)
13
+ img_array = image.img_to_array(img)
14
+ img_array = np.expand_dims(img_array, axis=0)
15
+ img_array = preprocess_input(img_array)
16
 
17
+ # Make prediction
18
+ prediction = model.predict(img_array)
19
+ predicted_class = np.argmax(prediction)
20
 
21
+ # Return the predicted class
22
+ return predicted_class
23
 
24
+ @app.route('/')
25
+ def home():
26
+ return render_template('index.html')
27
 
28
+ @app.route('/predict', methods=['POST'])
29
+ def predict():
30
+ if 'file' not in request.files:
31
+ return jsonify({'error': 'No file part'})
32
 
33
+ file = request.files['file']
34
 
35
+ # Save the uploaded file temporarily
36
+ file_path = 'temp_image.jpg'
37
+ file.save(file_path)
38
 
39
+ # Make prediction
40
+ predicted_class = preprocess_and_predict(model, file_path)
41
 
42
+ # Return the predicted class as a response
43
+ return render_template('index.html', prediction=predicted_class)
44
 
45
+ if __name__ == '__main__':
46
+ app.run(debug=True)
47
 
48
  import streamlit as st
49
  from tensorflow.keras.models import load_model