JosephRM commited on
Commit
bf2da99
·
verified ·
1 Parent(s): 8fe550e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -35
app.py CHANGED
@@ -1,74 +1,121 @@
1
  import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
 
4
  from PIL import Image
5
 
6
  # -----------------------------
7
- # LOAD MODEL
8
  # -----------------------------
9
- MODEL_PATH = "o_ring_cnn_classifier (1).keras"
10
- model = tf.keras.models.load_model(MODEL_PATH)
11
- print("✅ Model loaded successfully")
 
 
 
12
 
13
  # -----------------------------
14
- # READ MODEL INPUT SHAPE
15
  # -----------------------------
16
- # Example: (None, 128, 128, 3) or (None, 64, 64, 1)
17
- _, H, W, C = model.input_shape
 
 
 
18
 
19
- print(f"📐 Model expects: {H}x{W}x{C}")
 
20
 
21
- # -----------------------------
22
- # CONFIG
23
- # -----------------------------
24
- CLASS_NAMES = ["good", "breakage"]
25
 
26
  # -----------------------------
27
- # PREDICTION FUNCTION
28
  # -----------------------------
29
- def predict(image: Image.Image):
30
- # Convert correctly based on channels
31
  if C == 1:
32
- image = image.convert("L") # grayscale
33
  else:
34
  image = image.convert("RGB")
35
 
36
- # Resize EXACTLY to training size
37
  image = image.resize((W, H))
 
38
 
39
- img_array = np.array(image, dtype=np.float32)
40
-
41
- # If grayscale, add channel dim
42
  if C == 1:
43
- img_array = np.expand_dims(img_array, axis=-1)
44
 
45
- img_array = img_array / 255.0
46
- img_array = np.expand_dims(img_array, axis=0)
47
 
48
- preds = model.predict(img_array)[0]
49
  idx = int(np.argmax(preds))
50
 
51
  return {
52
- "prediction": CLASS_NAMES[idx],
 
53
  "confidence": float(np.max(preds)),
54
  "probabilities": {
55
- CLASS_NAMES[i]: float(preds[i])
56
- for i in range(len(CLASS_NAMES))
57
- },
58
- "model_input_shape": f"{H}x{W}x{C}"
59
  }
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  # -----------------------------
62
  # GRADIO APP
63
  # -----------------------------
64
  demo = gr.Interface(
65
  fn=predict,
66
- inputs=gr.Image(type="pil", label="Upload O-Ring Image"),
 
 
 
 
 
 
 
67
  outputs=gr.JSON(label="Prediction Output"),
68
- title="O-Ring Defect Detection",
69
- description="CNN-based classifier (auto-matches model input shape)"
70
  )
71
 
72
- demo.launch(
73
- ssr_mode=False
74
- )
 
1
  import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
4
+ import onnxruntime as ort
5
  from PIL import Image
6
 
7
  # -----------------------------
8
+ # LOAD CNN MODEL (GOOD / BREAKAGE)
9
  # -----------------------------
10
+ CNN_MODEL_PATH = "o_ring_cnn_classifier (1).keras"
11
+ cnn_model = tf.keras.models.load_model(CNN_MODEL_PATH)
12
+ _, H, W, C = cnn_model.input_shape
13
+
14
+ CNN_CLASSES = ["good", "breakage"]
15
+ print("✅ CNN model loaded")
16
 
17
  # -----------------------------
18
+ # LOAD ONNX MODEL (PAREGI / PELISE)
19
  # -----------------------------
20
+ ONNX_MODEL_PATH = "best.onnx"
21
+ onnx_session = ort.InferenceSession(
22
+ ONNX_MODEL_PATH,
23
+ providers=["CPUExecutionProvider"]
24
+ )
25
 
26
+ onnx_input_name = onnx_session.get_inputs()[0].name
27
+ onnx_output_name = onnx_session.get_outputs()[0].name
28
 
29
+ ONNX_CLASSES = ["paregi", "pelise"]
30
+ ONNX_IMG_SIZE = 640 # ⚠️ change ONLY if your model was trained differently
31
+
32
+ print("✅ ONNX model loaded")
33
 
34
  # -----------------------------
35
+ # CNN PREDICTION
36
  # -----------------------------
37
+ def predict_cnn(image: Image.Image):
 
38
  if C == 1:
39
+ image = image.convert("L")
40
  else:
41
  image = image.convert("RGB")
42
 
 
43
  image = image.resize((W, H))
44
+ img = np.array(image, dtype=np.float32) / 255.0
45
 
 
 
 
46
  if C == 1:
47
+ img = np.expand_dims(img, axis=-1)
48
 
49
+ img = np.expand_dims(img, axis=0)
 
50
 
51
+ preds = cnn_model.predict(img)[0]
52
  idx = int(np.argmax(preds))
53
 
54
  return {
55
+ "model": "CNN Classifier",
56
+ "prediction": CNN_CLASSES[idx],
57
  "confidence": float(np.max(preds)),
58
  "probabilities": {
59
+ CNN_CLASSES[i]: float(preds[i])
60
+ for i in range(len(CNN_CLASSES))
61
+ }
 
62
  }
63
 
64
+ # -----------------------------
65
+ # ONNX PREDICTION (PAREGI / PELISE)
66
+ # -----------------------------
67
+ def predict_onnx(image: Image.Image):
68
+ image = image.convert("RGB")
69
+ image = image.resize((ONNX_IMG_SIZE, ONNX_IMG_SIZE))
70
+
71
+ img = np.array(image, dtype=np.float32) / 255.0
72
+
73
+ # HWC → CHW
74
+ img = np.transpose(img, (2, 0, 1))
75
+ img = np.expand_dims(img, axis=0)
76
+
77
+ preds = onnx_session.run(
78
+ [onnx_output_name],
79
+ {onnx_input_name: img}
80
+ )[0][0]
81
+
82
+ idx = int(np.argmax(preds))
83
+
84
+ return {
85
+ "model": "ONNX Paregi/Pelise",
86
+ "prediction": ONNX_CLASSES[idx],
87
+ "confidence": float(np.max(preds)),
88
+ "probabilities": {
89
+ ONNX_CLASSES[i]: float(preds[i])
90
+ for i in range(len(ONNX_CLASSES))
91
+ }
92
+ }
93
+
94
+ # -----------------------------
95
+ # MASTER PREDICT FUNCTION
96
+ # -----------------------------
97
+ def predict(image, model_type):
98
+ if model_type == "CNN (Good / Breakage)":
99
+ return predict_cnn(image)
100
+ else:
101
+ return predict_onnx(image)
102
+
103
  # -----------------------------
104
  # GRADIO APP
105
  # -----------------------------
106
  demo = gr.Interface(
107
  fn=predict,
108
+ inputs=[
109
+ gr.Image(type="pil", label="Upload O-Ring Image"),
110
+ gr.Radio(
111
+ ["CNN (Good / Breakage)", "ONNX (Paregi / Pelise)"],
112
+ label="Select Model",
113
+ value="CNN (Good / Breakage)"
114
+ )
115
+ ],
116
  outputs=gr.JSON(label="Prediction Output"),
117
+ title="O-Ring Defect Detection System",
118
+ description="Supports CNN classification and ONNX Paregi/Pelise defect detection"
119
  )
120
 
121
+ demo.launch(ssr_mode=False)