File size: 3,483 Bytes
6555329
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# import streamlit as st
# import tensorflow as tf
# import numpy as np
# from flask import Flask, jsonify, request
# import requests

# # Create a Flask app
# app = Flask(__name__)

# # Tensorflow Model Prediction
# def model_prediction(test_image):
#     model = tf.keras.models.load_model("image_to_food_model1.h5")
#     image = tf.keras.preprocessing.image.load_img(test_image, target_size=(256, 256))
#     input_arr = tf.keras.preprocessing.image.img_to_array(image)
#     input_arr = np.array([input_arr])  # convert single image to batch
#     predictions = model.predict(input_arr)
#     return np.argmax(predictions)  # return index of max element


# # Define an endpoint to handle predictions
# @app.route('/', methods=['POST'])
# def predict():
#     test_image = request.files['file']
#     result_index = model_prediction(test_image)
#     # Reading Labels
#     with open("labels.txt") as f:
#         content = f.readlines()
#     label = [i.strip() for i in content]
#     prediction = label[result_index]
#     return jsonify({"prediction": prediction})


# # Sidebar
# st.sidebar.title("Dashboard")
# app_mode = st.sidebar.selectbox("Select Page", ["Prediction"])

# # Main Page
# # if app_mode == "Home":
# #     st.header("Food Recipe")
# #     image_path = "home_page.jpg"
# #     st.image(image_path)

# # Prediction Page
# if app_mode == "Prediction":
#     st.header("Model Prediction")
#     test_image = st.file_uploader("Choose an Image:")
#     if st.button("Show Image"):
#         st.image(test_image, width=4, use_column_width=True)

#     # Predict button
#     if st.button("Predict"):
#         st.write("Our Prediction")
#         # Send image to backend for prediction
#         files = {'file': test_image.getvalue()}
#         response = requests.post('http://localhost:8501', files=files)
#         prediction = response.json()["prediction"]
#         st.success("Model is Predicting it's a {}".format(prediction))




import streamlit as st
import tensorflow as tf
import numpy as np



#Tensorflow Model Prediction
def model_prediction(test_image):
    model = tf.keras.models.load_model("image_to_food_model1.h5")
    image = tf.keras.preprocessing.image.load_img(test_image,target_size=(256,256))
    input_arr = tf.keras.preprocessing.image.img_to_array(image)
    input_arr = np.array([input_arr]) #convert single image to batch
    predictions = model.predict(input_arr)
    return np.argmax(predictions) #return index of max element


#Sidebar
st.sidebar.title("Dashboard")
app_mode = st.sidebar.selectbox("Select Page",["Prediction"])

# #Main Page
# if(app_mode=="Home"):
#     st.header("Food Recipe")
#     image_path = "home_page.jpg"
#     st.image(image_path)


#Prediction Page
if(app_mode=="Prediction"):
    st.header("Model Prediction")
    test_image = st.file_uploader("Choose an Image:")
    if(st.button("Show Image")):
        st.image(test_image,width=4,use_column_width=True)
    
    #Predict button
    if(st.button("Predict")):
        st.snow()
        st.write("Our Prediction")
        result_index = model_prediction(test_image)
        #Reading Labels
        with open("labels.txt") as f:
            content = f.readlines()
        label = []
        
        for i in content:
            label.append(i[:-1])
        st.success("Model is Predicting it's a {}".format(label[result_index]))