JosephRM commited on
Commit
bb31bd9
·
verified ·
1 Parent(s): 30e6b11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -95
app.py CHANGED
@@ -1,37 +1,27 @@
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) ✅ DO NOT TOUCH
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 YOLOv8 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
-
28
- ONNX_CLASSES = ["paregi", "pelise"]
29
- ONNX_IMG_SIZE = 640
30
-
31
- print("✅ ONNX model loaded")
32
 
33
  # ======================================================
34
- # CNN PREDICTION (UNCHANGED)
35
  # ======================================================
36
  def predict_cnn(image: Image.Image):
37
  if C == 1:
@@ -61,85 +51,18 @@ def predict_cnn(image: Image.Image):
61
  }
62
 
63
  # ======================================================
64
- # 🔥 YOLOv8 ONNX PREDICTION — CORRECT VERSION
65
  # ======================================================
66
- def predict_onnx(image: Image.Image):
67
- # ---------- Preprocess ----------
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
- img = np.transpose(img, (2, 0, 1)) # HWC → CHW
73
- img = np.expand_dims(img, axis=0)
74
-
75
- # ---------- Inference ----------
76
- outputs = onnx_session.run(None, {onnx_input_name: img})
77
- output = outputs[0] # shape: (1, 7, N) → for 2 classes
78
-
79
- output = np.squeeze(output).T # → (N, 7)
80
-
81
- CONF_THRESH = 0.15
82
- best_conf = 0.0
83
- best_cls = None
84
-
85
- for det in output:
86
- obj_conf = det[4]
87
- if obj_conf < CONF_THRESH:
88
- continue
89
 
90
- class_scores = det[5:]
91
- cls_id = int(np.argmax(class_scores))
92
- cls_conf = obj_conf * class_scores[cls_id]
93
-
94
- if cls_conf > best_conf:
95
- best_conf = cls_conf
96
- best_cls = cls_id
97
-
98
- if best_cls is None:
99
- return {
100
- "model": "ONNX Paregi/Pelise",
101
- "prediction": "no_object_detected",
102
- "confidence": 0.0,
103
- "probabilities": {
104
- cls: 0.0 for cls in ONNX_CLASSES
105
- }
106
- }
107
-
108
- probs = {cls: 0.0 for cls in ONNX_CLASSES}
109
- probs[ONNX_CLASSES[best_cls]] = float(best_conf)
110
-
111
- return {
112
- "model": "ONNX Paregi/Pelise",
113
- "prediction": ONNX_CLASSES[best_cls],
114
- "confidence": float(best_conf),
115
- "probabilities": probs
116
- }
117
-
118
- # ======================================================
119
- # MASTER ROUTER
120
- # ======================================================
121
- def predict(image, model_type):
122
- if model_type == "CNN (Good / Breakage)":
123
- return predict_cnn(image)
124
- else:
125
- return predict_onnx(image)
126
 
127
  # ======================================================
128
- # GRADIO APP
129
  # ======================================================
130
- demo = gr.Interface(
131
- fn=predict,
132
- inputs=[
133
- gr.Image(type="pil", label="Upload O-Ring Image"),
134
- gr.Radio(
135
- ["CNN (Good / Breakage)", "ONNX (Paregi / Pelise)"],
136
- label="Select Model",
137
- value="CNN (Good / Breakage)"
138
- )
139
- ],
140
- outputs=gr.JSON(label="Prediction Output"),
141
- title="ann2ann – O-Ring Detection System",
142
- description="CNN (Good/Breakage) + YOLOv8 ONNX (Paregi/Pelise)"
143
- )
144
-
145
- demo.launch(ssr_mode=False)
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ from PIL import Image
3
  import tensorflow as tf
4
  import numpy as np
5
+ import io
 
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
+
16
  print("✅ CNN model loaded")
17
 
18
  # ======================================================
19
+ # FASTAPI APP
20
  # ======================================================
21
+ app = FastAPI(title="O-Ring CNN Inference API")
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  # ======================================================
24
+ # CNN PREDICTION
25
  # ======================================================
26
  def predict_cnn(image: Image.Image):
27
  if C == 1:
 
51
  }
52
 
53
  # ======================================================
54
+ # API ENDPOINT
55
  # ======================================================
56
+ @app.post("/predict")
57
+ async def predict(file: UploadFile = File(...)):
58
+ img_bytes = await file.read()
59
+ image = Image.open(io.BytesIO(img_bytes))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
+ return predict_cnn(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  # ======================================================
64
+ # HEALTH CHECK (IMPORTANT FOR RENDER)
65
  # ======================================================
66
+ @app.get("/")
67
+ def health():
68
+ return {"status": "ok"}