Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from tensorflow.keras.models import load_model
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Class names mapping
|
| 7 |
+
class_names = {
|
| 8 |
+
0: 'arduino',
|
| 9 |
+
1: 'battery',
|
| 10 |
+
2: 'DCmotor',
|
| 11 |
+
3: 'DHT-11',
|
| 12 |
+
4: 'ESP8266',
|
| 13 |
+
5: 'LCD',
|
| 14 |
+
6: 'Loadcell',
|
| 15 |
+
7: 'RFID',
|
| 16 |
+
8: 'Tiva',
|
| 17 |
+
9: 'Ultrasonic',
|
| 18 |
+
10: 'Bluetooth Module'
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
# Load the pre-trained model
|
| 22 |
+
model = load_model("Electronic-Component-Detector.keras")
|
| 23 |
+
|
| 24 |
+
# Function to predict image
|
| 25 |
+
def predict_image(img):
|
| 26 |
+
img = img.convert("RGB")
|
| 27 |
+
img = img.resize((224, 224))
|
| 28 |
+
data = np.asarray(img)
|
| 29 |
+
data = data / 255.0
|
| 30 |
+
probs = model.predict(np.expand_dims(data, axis=0))
|
| 31 |
+
top_prob = probs.max()
|
| 32 |
+
top_pred = class_names[np.argmax(probs)]
|
| 33 |
+
return f"This is a {top_pred} with {top_prob * 100:.2f}% confidence."
|
| 34 |
+
|
| 35 |
+
# Create the Gradio interface
|
| 36 |
+
interface = gr.Interface(
|
| 37 |
+
fn=predict_image,
|
| 38 |
+
inputs=gr.Image(type="pil"),
|
| 39 |
+
outputs="text",
|
| 40 |
+
title="Electronic Component Detector",
|
| 41 |
+
description="Upload an image of an electronic component, and the model will classify it.",
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# Run the Gradio app
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
interface.launch()
|