Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- AI_Simple_Classifier (1).ipynb +0 -0
- app.py +74 -0
- best (1).pt +3 -0
- req +7 -0
AI_Simple_Classifier (1).ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
+
import time
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Reduce CPU overhead
|
| 9 |
+
os.environ["OMP_NUM_THREADS"] = "1"
|
| 10 |
+
|
| 11 |
+
MODEL_PATH = "best (1).pt"
|
| 12 |
+
|
| 13 |
+
# Load model once
|
| 14 |
+
model = YOLO(MODEL_PATH)
|
| 15 |
+
|
| 16 |
+
CLASS_COLORS = {
|
| 17 |
+
"good": "green",
|
| 18 |
+
"bad": "red"
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
def predict(image):
|
| 22 |
+
if image is None:
|
| 23 |
+
return "No image provided"
|
| 24 |
+
|
| 25 |
+
# Convert PIL → numpy
|
| 26 |
+
img = np.array(image)
|
| 27 |
+
|
| 28 |
+
start = time.time()
|
| 29 |
+
result = model(img, verbose=False)[0]
|
| 30 |
+
latency = (time.time() - start) * 1000
|
| 31 |
+
|
| 32 |
+
probs = result.probs.data.cpu().numpy()
|
| 33 |
+
class_id = int(np.argmax(probs))
|
| 34 |
+
confidence = float(probs[class_id]) * 100
|
| 35 |
+
|
| 36 |
+
label = result.names[class_id]
|
| 37 |
+
|
| 38 |
+
color = CLASS_COLORS.get(label, "black")
|
| 39 |
+
|
| 40 |
+
output = (
|
| 41 |
+
f"<h2 style='color:{color}; text-align:center;'>"
|
| 42 |
+
f"{label.upper()}</h2>"
|
| 43 |
+
f"<p style='text-align:center;'>"
|
| 44 |
+
f"Confidence: <b>{confidence:.2f}%</b><br>"
|
| 45 |
+
f"Inference Time: {latency:.1f} ms"
|
| 46 |
+
f"</p>"
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
return output
|
| 50 |
+
|
| 51 |
+
with gr.Blocks() as demo:
|
| 52 |
+
gr.Markdown("# 🔍 AI Simple Defect Classifier")
|
| 53 |
+
gr.Markdown(
|
| 54 |
+
"Upload or capture an image to classify an industrial part as **GOOD** or **BAD**."
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
with gr.Row():
|
| 58 |
+
image_input = gr.Image(
|
| 59 |
+
type="pil",
|
| 60 |
+
label="Input Image",
|
| 61 |
+
sources=["upload", "webcam"]
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
output = gr.HTML()
|
| 65 |
+
|
| 66 |
+
classify_btn = gr.Button("Run Inspection")
|
| 67 |
+
|
| 68 |
+
classify_btn.click(
|
| 69 |
+
fn=predict,
|
| 70 |
+
inputs=image_input,
|
| 71 |
+
outputs=output
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
demo.launch(theme=gr.themes.Soft(), share=True)
|
best (1).pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d649dc20daa800e4aed759f49b1f330f55659a57ce45f7f6ef606160f858b2d0
|
| 3 |
+
size 2972232
|
req
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics==8.3.241
|
| 2 |
+
torch
|
| 3 |
+
torchvision
|
| 4 |
+
opencv-python-headless
|
| 5 |
+
pillow
|
| 6 |
+
numpy
|
| 7 |
+
gradio
|