nineteen_F19 / app.py
ninetynine99's picture
Update app.py
84f94fd verified
# import gradio as gr
# import tensorflow as tf
# import numpy as np
# import json
# from tensorflow.keras.applications.efficientnet import preprocess_input
# from tensorflow.keras.preprocessing import image as keras_image
# # Load Model & Class Indices
# MODEL_PATH = "model.keras"
# CLASS_INDICES_PATH = "class_indices.json"
# FLOWER_INFO_PATH = "flower_info.json"
# def load_model():
# return tf.keras.models.load_model(MODEL_PATH)
# def load_class_indices():
# with open(CLASS_INDICES_PATH, "r") as f:
# return json.load(f)
# def load_flower_info():
# with open(FLOWER_INFO_PATH, "r", encoding="utf-8") as f:
# return json.load(f)
# model = load_model()
# class_indices = load_class_indices()
# flower_info = load_flower_info()
# class_names = list(class_indices.keys())
# def preprocess_image(pil_image):
# # Convert PIL image to numpy array and preprocess
# img_array = keras_image.img_to_array(pil_image.resize((224, 224)))
# img_array = np.expand_dims(img_array, axis=0)
# return preprocess_input(img_array)
# def predict_image(pil_image):
# img_array = preprocess_image(pil_image)
# predictions = model.predict(img_array)
# predicted_class = class_names[np.argmax(predictions[0])]
# info = flower_info.get(predicted_class, "No additional information available.")
# return f"๐ŸŒฟ Identified as: {predicted_class}", info
# def predict(pil_image):
# return predict_image(pil_image)
# interface = gr.Interface(
# fn=predict,
# inputs=gr.Image(type="pil"), # Receive image as a PIL object
# outputs=[gr.Textbox(label="Prediction"), gr.Textbox(label="Flower Information")],
# title="๐ŸŒธ Flower Identification App",
# description="Upload an image of a flower to identify it and get care information."
# )
# if __name__ == "__main__":
# interface.launch()
# import gradio as gr
# import tensorflow as tf
# import numpy as np
# import json
# from tensorflow.keras.applications.efficientnet import preprocess_input
# from tensorflow.keras.preprocessing import image as keras_image
# # Load Model & Class Indices
# MODEL_PATH = "model.keras"
# CLASS_INDICES_PATH = "class_indices.json"
# FLOWER_INFO_PATH = "flower_info.json"
# def load_model():
# try:
# return tf.keras.models.load_model(MODEL_PATH)
# except Exception as e:
# print(f"Error loading model: {e}")
# return None
# def load_class_indices():
# with open(CLASS_INDICES_PATH, "r") as f:
# return json.load(f)
# def load_flower_info():
# with open(FLOWER_INFO_PATH, "r", encoding="utf-8") as f:
# return json.load(f)
# model = load_model()
# class_indices = load_class_indices()
# flower_info = load_flower_info()
# class_names = list(class_indices.keys())
# def preprocess_image(pil_image):
# # Convert PIL image to numpy array and preprocess
# img_array = keras_image.img_to_array(pil_image.resize((224, 224)))
# img_array = np.expand_dims(img_array, axis=0)
# return preprocess_input(img_array)
# def predict_image(pil_image):
# try:
# img_array = preprocess_image(pil_image)
# predictions = model.predict(img_array)
# predicted_class = class_names[np.argmax(predictions[0])]
# info = flower_info.get(predicted_class, "No additional information available.")
# return f"๐ŸŒฟ Identified as: {predicted_class}", info
# except Exception as e:
# return "Error in prediction", str(e)
# def predict(pil_image):
# return predict_image(pil_image)
# interface = gr.Interface(
# fn=predict,
# inputs=gr.Image(type="pil"), # Receive image as a PIL object
# outputs=[gr.Textbox(label="Prediction"), gr.Textbox(label="Flower Information")],
# title="๐ŸŒธ Flower Identification App",
# description="Upload an image of a flower to identify it and get care information.",
# theme="compact" # Optional: Use a compact theme to reduce UI clutter
# )
# if __name__ == "__main__":
# interface.launch(share=True, api=True) # Add api=True if you need API access
import gradio as gr
import tensorflow as tf
import numpy as np
import json
from tensorflow.keras.applications.efficientnet import preprocess_input
from tensorflow.keras.preprocessing import image as keras_image
# Load Model & Class Indices
MODEL_PATH = "model.keras"
CLASS_INDICES_PATH = "class_indices.json"
FLOWER_INFO_PATH = "flower_info.json"
def load_model():
try:
return tf.keras.models.load_model(MODEL_PATH)
except Exception as e:
print(f"Error loading model: {e}")
return None
def load_class_indices():
with open(CLASS_INDICES_PATH, "r") as f:
return json.load(f)
def load_flower_info():
with open(FLOWER_INFO_PATH, "r", encoding="utf-8") as f:
return json.load(f)
model = load_model()
class_indices = load_class_indices()
flower_info = load_flower_info()
class_names = list(class_indices.keys())
def preprocess_image(pil_image):
# Convert PIL image to numpy array and preprocess
img_array = keras_image.img_to_array(pil_image.resize((224, 224)))
img_array = np.expand_dims(img_array, axis=0)
return preprocess_input(img_array)
def predict_image(pil_image):
try:
img_array = preprocess_image(pil_image)
predictions = model.predict(img_array)
predicted_class = class_names[np.argmax(predictions[0])]
info = flower_info.get(predicted_class, "No additional information available.")
return f"Identified as: {predicted_class}", info
except Exception as e:
return "Error in prediction", str(e)
def predict(pil_image):
return predict_image(pil_image)
# Launch the Gradio interface
interface = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil"), # Receive image as a PIL object
outputs=[gr.Textbox(label="Prediction"), gr.Textbox(label="Flower Information")],
title="๐ŸŒธ Flower Identification App",
description="Upload an image of a flower to identify it and get care information."
)
if __name__ == "__main__":
interface.launch(share=True) # No api=True here