Spaces:
Runtime error
Runtime error
Phuong Anh
commited on
Commit
•
37d4cf3
1
Parent(s):
89a1fb5
Initial commit with model and app
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from anomalib.deploy import OpenVINOInferencer
|
5 |
+
from huggingface_hub import hf_hub_download
|
6 |
+
|
7 |
+
# Load the model
|
8 |
+
model_path = hf_hub_download(repo_id="suidinpa/bottle-anomaly-detection", filename="model.bin")
|
9 |
+
metadata_path = hf_hub_download(repo_id="suidinpa/bottle-anomaly-detection", filename="metadata.json")
|
10 |
+
|
11 |
+
# Initialize OpenVINO inferencer
|
12 |
+
inferencer = OpenVINOInferencer(
|
13 |
+
path=model_path,
|
14 |
+
metadata=metadata_path,
|
15 |
+
device="CPU",
|
16 |
+
task="classification"
|
17 |
+
)
|
18 |
+
|
19 |
+
def predict(image):
|
20 |
+
"""Function to process the image and predict with the model"""
|
21 |
+
# Convert image to a format suitable for the model
|
22 |
+
img_array = np.array(image)
|
23 |
+
img_bgr = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
|
24 |
+
|
25 |
+
# Predict anomaly
|
26 |
+
predictions = inferencer.predict(img_bgr)
|
27 |
+
|
28 |
+
# Return results
|
29 |
+
return predictions.pred_label, predictions.pred_score
|
30 |
+
|
31 |
+
# Define the Gradio interface
|
32 |
+
interface = gr.Interface(
|
33 |
+
fn=predict, # The prediction function
|
34 |
+
inputs=gr.Image(type="numpy", label="Upload Image"), # Image input from the user
|
35 |
+
outputs=["text", "number"], # Text (for label) and number (for prediction score)
|
36 |
+
live=True # Make the app live for real-time predictions
|
37 |
+
)
|
38 |
+
|
39 |
+
# Launch the app
|
40 |
+
interface.launch()
|