Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
import joblib
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# ================= LOAD MODEL =================
|
| 8 |
+
MODEL_PATH = "ExtraTreesClassifier.pkl"
|
| 9 |
+
|
| 10 |
+
if not os.path.exists(MODEL_PATH):
|
| 11 |
+
raise FileNotFoundError("Model not found! Train and save Model first.")
|
| 12 |
+
|
| 13 |
+
model = joblib.load(MODEL_PATH)
|
| 14 |
+
|
| 15 |
+
# ================= CATEGORY LABELS =================
|
| 16 |
+
# ⚠️ IMPORTANT: Replace with your actual folder names in SAME ORDER
|
| 17 |
+
categories = ["Defective", "Normal"]
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# ================= PREDICTION FUNCTION =================
|
| 21 |
+
def predict_image(image):
|
| 22 |
+
try:
|
| 23 |
+
# Convert to numpy
|
| 24 |
+
img = np.array(image)
|
| 25 |
+
|
| 26 |
+
# Resize
|
| 27 |
+
img = cv2.resize(img, (64, 64))
|
| 28 |
+
|
| 29 |
+
# Normalize
|
| 30 |
+
img = img / 255.0
|
| 31 |
+
|
| 32 |
+
# Flatten
|
| 33 |
+
img = img.flatten().reshape(1, -1)
|
| 34 |
+
|
| 35 |
+
# Predict
|
| 36 |
+
pred = model.predict(img)[0]
|
| 37 |
+
label = categories[pred]
|
| 38 |
+
|
| 39 |
+
return f"Predicted Class: {label}"
|
| 40 |
+
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return f"Error: {str(e)}"
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# ================= GRADIO UI =================
|
| 46 |
+
interface = gr.Interface(
|
| 47 |
+
fn=predict_image,
|
| 48 |
+
inputs=gr.Image(type="pil"),
|
| 49 |
+
outputs="text",
|
| 50 |
+
title="Casting Product Defect Classification From Images",
|
| 51 |
+
description="Upload an image to predict its category"
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
interface.launch()
|