Spaces:
Sleeping
Sleeping
Commit ·
237c700
1
Parent(s): f25f10c
checed
Browse filesFiles changed (3)
- .gitignore+1-1
- app.py+50-8
- class_indices.jsonAdded+1
1class_indices.json1plant_disease_model.tflite
1234567891011121314import gradio as grdef say_hello(name):return f"Hello {name}, the model is ready on Hugging Face Spaces!"demo = gr.Interface(fn=say_hello,inputs="text",outputs="text",title="Test Space")if __name__ == "__main__":demo.launch()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import osimport jsonfrom PIL import Imageimport numpy as npimport tensorflow as tfimport gradio as gr# Pathsworking_dir = os.path.dirname(os.path.abspath(__file__))model_path = f"{working_dir}/plant_disease_model.tflite"# Load TFLite modelinterpreter = tf.lite.Interpreter(model_path=model_path)interpreter.allocate_tensors()# Get input and output detailsinput_details = interpreter.get_input_details()output_details = interpreter.get_output_details()# Load class indicesclass_indices = json.load(open(f"{working_dir}/class_indices.json"))# Function to preprocess the imagedef load_and_preprocess_image(image, target_size=(224, 224)):img = image.resize(target_size)img_array = np.array(img, dtype=np.float32)img_array = np.expand_dims(img_array, axis=0)img_array = img_array / 255.0return img_array# Prediction functiondef predict_image_class(image):preprocessed_img = load_and_preprocess_image(image)# Set input tensorinterpreter.set_tensor(input_details[0]['index'], preprocessed_img)interpreter.invoke()# Get predictionspredictions = interpreter.get_tensor(output_details[0]['index'])predicted_class_index = np.argmax(predictions, axis=1)[0]predicted_class_name = class_indices[str(predicted_class_index)]return f"Prediction: {predicted_class_name}"# Gradio Interfaceinterface = gr.Interface(fn=predict_image_class,inputs=gr.Image(type="pil", label="Upload an Image"),outputs=gr.Textbox(label="Prediction"),title="🌱 Plant Disease Classifier (TFLite)",description="Upload a plant leaf image to classify its disease using a compressed TFLite model.")if __name__ == "__main__":interface.launch()
1{"0": "Apple___Apple_scab", "1": "Apple___Black_rot", "2": "Apple___Cedar_apple_rust", "3": "Apple___healthy", "4": "Blueberry___healthy", "5": "Cherry_(including_sour)___Powdery_mildew", "6": "Cherry_(including_sour)___healthy", "7": "Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot", "8": "Corn_(maize)___Common_rust_", "9": "Corn_(maize)___Northern_Leaf_Blight", "10": "Corn_(maize)___healthy", "11": "Grape___Black_rot", "12": "Grape___Esca_(Black_Measles)", "13": "Grape___Leaf_blight_(Isariopsis_Leaf_Spot)", "14": "Grape___healthy", "15": "Orange___Haunglongbing_(Citrus_greening)", "16": "Peach___Bacterial_spot", "17": "Peach___healthy", "18": "Pepper,_bell___Bacterial_spot", "19": "Pepper,_bell___healthy", "20": "Potato___Early_blight", "21": "Potato___Late_blight", "22": "Potato___healthy", "23": "Raspberry___healthy", "24": "Soybean___healthy", "25": "Squash___Powdery_mildew", "26": "Strawberry___Leaf_scorch", "27": "Strawberry___healthy", "28": "Tomato___Bacterial_spot", "29": "Tomato___Early_blight", "30": "Tomato___Late_blight", "31": "Tomato___Leaf_Mold", "32": "Tomato___Septoria_leaf_spot", "33": "Tomato___Spider_mites Two-spotted_spider_mite", "34": "Tomato___Target_Spot", "35": "Tomato___Tomato_Yellow_Leaf_Curl_Virus", "36": "Tomato___Tomato_mosaic_virus", "37": "Tomato___healthy"}.gitignore+1-1
app.py+50-8
class_indices.jsonAdded+1