Upload inference.py
Browse files- inference.py +69 -0
inference.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
from openvino.runtime import Core
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# --- Configuration ---
|
| 8 |
+
MODEL_PATH = "casting_ir/model.xml"
|
| 9 |
+
THRESHOLD = 0.0004
|
| 10 |
+
IMG_SIZE = 304
|
| 11 |
+
|
| 12 |
+
# --- Initialize OpenVINO ---
|
| 13 |
+
ie = Core()
|
| 14 |
+
model = ie.read_model(model=MODEL_PATH)
|
| 15 |
+
compiled_model = ie.compile_model(model=model, device_name="CPU")
|
| 16 |
+
infer_request = compiled_model.create_infer_request()
|
| 17 |
+
|
| 18 |
+
# --- Preprocessing ---
|
| 19 |
+
def preprocess_image(image_path):
|
| 20 |
+
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
|
| 21 |
+
if img is None:
|
| 22 |
+
raise ValueError(f"Image not found: {image_path}")
|
| 23 |
+
|
| 24 |
+
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE)).astype(np.float32) / 255.0
|
| 25 |
+
img = np.stack([img]*3, axis=0) # Shape: [3, 304, 304]
|
| 26 |
+
img = np.expand_dims(img, 0) # Shape: [1, 3, 304, 304]
|
| 27 |
+
return img
|
| 28 |
+
|
| 29 |
+
# --- Reconstruction error ---
|
| 30 |
+
def reconstruction_error(original, reconstructed):
|
| 31 |
+
return np.mean((original - reconstructed)**2)
|
| 32 |
+
|
| 33 |
+
# --- Save reconstruction ---
|
| 34 |
+
def save_reconstruction(img, reconstructed, output_path="reconstruction.png"):
|
| 35 |
+
original = img[0].transpose(1,2,0)
|
| 36 |
+
recon = reconstructed[0].transpose(1,2,0)
|
| 37 |
+
|
| 38 |
+
plt.figure(figsize=(10,5))
|
| 39 |
+
plt.subplot(1,2,1)
|
| 40 |
+
plt.title("Original")
|
| 41 |
+
plt.imshow(original, cmap='gray')
|
| 42 |
+
plt.subplot(1,2,2)
|
| 43 |
+
plt.title("Reconstructed")
|
| 44 |
+
plt.imshow(recon, cmap='gray')
|
| 45 |
+
plt.savefig(output_path)
|
| 46 |
+
plt.close()
|
| 47 |
+
print(f"[INFO] Reconstruction saved to {output_path}")
|
| 48 |
+
|
| 49 |
+
# --- Detect anomaly ---
|
| 50 |
+
def detect_anomaly(image_path, threshold=THRESHOLD):
|
| 51 |
+
img = preprocess_image(image_path)
|
| 52 |
+
result = infer_request.infer(inputs={compiled_model.inputs[0]: img})
|
| 53 |
+
reconstructed = result[compiled_model.outputs[0]]
|
| 54 |
+
|
| 55 |
+
error = reconstruction_error(img, reconstructed)
|
| 56 |
+
print(f"[INFO] Reconstruction error: {error:.6f}")
|
| 57 |
+
|
| 58 |
+
save_reconstruction(img, reconstructed)
|
| 59 |
+
|
| 60 |
+
if error > threshold:
|
| 61 |
+
print("Defective Casting Detected ✅")
|
| 62 |
+
else:
|
| 63 |
+
print("Casting OK ✅")
|
| 64 |
+
|
| 65 |
+
# --- Run ---
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
test_image_path = "cast_def_0_174.jpeg" # replace with your image
|
| 68 |
+
detect_anomaly(test_image_path)
|
| 69 |
+
|