| import gradio as gr
|
| import numpy as np
|
| import cv2
|
| import joblib
|
| import os
|
|
|
|
|
| MODEL_PATH = "ExtraTreesClassifier.pkl"
|
|
|
| if not os.path.exists(MODEL_PATH):
|
| raise FileNotFoundError("Model not found! Train and save Model first.")
|
|
|
| model = joblib.load(MODEL_PATH)
|
|
|
|
|
|
|
| categories = ["Defective", "Normal"]
|
|
|
|
|
|
|
| def predict_image(image):
|
| try:
|
|
|
| img = np.array(image)
|
|
|
|
|
| img = cv2.resize(img, (64, 64))
|
|
|
|
|
| img = img / 255.0
|
|
|
|
|
| img = img.flatten().reshape(1, -1)
|
|
|
|
|
| pred = model.predict(img)[0]
|
| label = categories[pred]
|
|
|
| return f"Predicted Class: {label}"
|
|
|
| except Exception as e:
|
| return f"Error: {str(e)}"
|
|
|
|
|
|
|
| interface = gr.Interface(
|
| fn=predict_image,
|
| inputs=gr.Image(type="pil"),
|
| outputs="text",
|
| title="Casting Product Defect Classification From Images",
|
| description="Upload an image to predict its category"
|
| )
|
|
|
| interface.launch() |