Spaces:
Running
Running
KB Teo
commited on
Commit
·
32f1589
1
Parent(s):
22844af
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,7 +16,8 @@ if importlib.util.find_spec("openvino"):
|
|
| 16 |
def predict_openvino(
|
| 17 |
image: Union[str, Path, np.ndarray],
|
| 18 |
model_path: Union[str, Path],
|
| 19 |
-
device: str
|
|
|
|
| 20 |
global inferencer, last_model_path
|
| 21 |
if not isinstance(inferencer, OpenVINOInferencer) or last_model_path != str(model_path):
|
| 22 |
inferencer = OpenVINOInferencer(
|
|
@@ -24,6 +25,7 @@ if importlib.util.find_spec("openvino"):
|
|
| 24 |
device=device.upper()
|
| 25 |
)
|
| 26 |
last_model_path = str(model_path)
|
|
|
|
| 27 |
return inferencer(image)
|
| 28 |
|
| 29 |
def draw_contour(image, mask, color=(255,0,0), thickness=3):
|
|
@@ -34,27 +36,31 @@ def convert_to_heatmap(heatmap):
|
|
| 34 |
heatmap = cv2.applyColorMap((heatmap*255).astype("uint8"), cv2.COLORMAP_HSV)
|
| 35 |
return heatmap
|
| 36 |
|
| 37 |
-
def predict(image, device, model_dir):
|
| 38 |
-
outputs = predict_openvino(image, model_dir, device)
|
| 39 |
out_image = draw_contour(image, outputs["pred_mask"])
|
| 40 |
heatmap = convert_to_heatmap(outputs["anomaly_map"])
|
| 41 |
return out_image, heatmap
|
| 42 |
|
| 43 |
def launch():
|
| 44 |
-
input_image = gr.Image(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
devices = gr.Radio(
|
| 46 |
label="Device",
|
| 47 |
-
choices=["AUTO", "CPU", "
|
| 48 |
value="CPU",
|
| 49 |
interactive=False
|
| 50 |
)
|
| 51 |
output_image = gr.Image(label="Output image")
|
| 52 |
output_heatmap = gr.Image(label="Heatmap")
|
| 53 |
-
model = gr.Text(label="Model", interactive=False, value="models/
|
| 54 |
intf = gr.Interface(
|
| 55 |
title="Anomaly Detection",
|
| 56 |
fn=predict,
|
| 57 |
-
inputs=[input_image, devices, model],
|
| 58 |
outputs=[output_image, output_heatmap]
|
| 59 |
)
|
| 60 |
intf.launch()
|
|
|
|
| 16 |
def predict_openvino(
|
| 17 |
image: Union[str, Path, np.ndarray],
|
| 18 |
model_path: Union[str, Path],
|
| 19 |
+
device: str,
|
| 20 |
+
threshold: float) -> Dict[str, np.ndarray]:
|
| 21 |
global inferencer, last_model_path
|
| 22 |
if not isinstance(inferencer, OpenVINOInferencer) or last_model_path != str(model_path):
|
| 23 |
inferencer = OpenVINOInferencer(
|
|
|
|
| 25 |
device=device.upper()
|
| 26 |
)
|
| 27 |
last_model_path = str(model_path)
|
| 28 |
+
inferencer.metadata["pixel_threshold"] = threshold
|
| 29 |
return inferencer(image)
|
| 30 |
|
| 31 |
def draw_contour(image, mask, color=(255,0,0), thickness=3):
|
|
|
|
| 36 |
heatmap = cv2.applyColorMap((heatmap*255).astype("uint8"), cv2.COLORMAP_HSV)
|
| 37 |
return heatmap
|
| 38 |
|
| 39 |
+
def predict(image, threshold, device, model_dir):
|
| 40 |
+
outputs = predict_openvino(image, model_dir, device, threshold)
|
| 41 |
out_image = draw_contour(image, outputs["pred_mask"])
|
| 42 |
heatmap = convert_to_heatmap(outputs["anomaly_map"])
|
| 43 |
return out_image, heatmap
|
| 44 |
|
| 45 |
def launch():
|
| 46 |
+
input_image = gr.Image(
|
| 47 |
+
label="Input image",
|
| 48 |
+
value="images/171436008_Fail.jpeg"
|
| 49 |
+
)
|
| 50 |
+
threshold = gr.Slider(value=5.5, step=0.1, label="Threshold")
|
| 51 |
devices = gr.Radio(
|
| 52 |
label="Device",
|
| 53 |
+
choices=["AUTO", "CPU", "GPU"],
|
| 54 |
value="CPU",
|
| 55 |
interactive=False
|
| 56 |
)
|
| 57 |
output_image = gr.Image(label="Output image")
|
| 58 |
output_heatmap = gr.Image(label="Heatmap")
|
| 59 |
+
model = gr.Text(label="Model", interactive=False, value="models/glass-tr-5-ov")
|
| 60 |
intf = gr.Interface(
|
| 61 |
title="Anomaly Detection",
|
| 62 |
fn=predict,
|
| 63 |
+
inputs=[input_image, threshold, devices, model],
|
| 64 |
outputs=[output_image, output_heatmap]
|
| 65 |
)
|
| 66 |
intf.launch()
|