Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,49 @@
|
|
| 1 |
-
# Description: This is the main file to run the Gradio interface for the object detection model.
|
| 2 |
-
from ultralytics import YOLO
|
| 3 |
-
from PIL import Image
|
| 4 |
-
import gradio as gr
|
| 5 |
-
from huggingface_hub import snapshot_download
|
| 6 |
-
import os
|
| 7 |
-
|
| 8 |
-
model_path = "best_int8_openvino_model"
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# Load the model
|
| 12 |
-
def load_model(
|
| 13 |
-
download_dir = snapshot_download(repo_id) # download the model from the Hugging Face Hub
|
| 14 |
-
print(download_dir)
|
| 15 |
-
path = os.path.join(download_dir, "best_int8_openvino_model") # path to the model
|
| 16 |
-
print(
|
| 17 |
-
detection_model = YOLO(
|
| 18 |
-
return detection_model
|
| 19 |
-
|
| 20 |
-
# Predict the image
|
| 21 |
-
def predict(pilimg):
|
| 22 |
-
source = pilimg
|
| 23 |
-
# x = np.asarray(pilimg)
|
| 24 |
-
# print(x.shape)
|
| 25 |
-
result = detection_model.predict(source, conf=0.5) # confidence threshold, intersection over union threshold
|
| 26 |
-
|
| 27 |
-
#print("Result: ", result)
|
| 28 |
-
if not result or len(result[0].boxes) == 0: # if no object detected
|
| 29 |
-
gr.Warning("Image not recognized for classfication!")
|
| 30 |
-
else:
|
| 31 |
-
img_bgr = result[0].plot() # plot the image
|
| 32 |
-
out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # RGB-order PIL image
|
| 33 |
-
|
| 34 |
-
return out_pilimg
|
| 35 |
-
|
| 36 |
-
REPO_ID = "best_int8_openvino_model" # The repo ID of the model
|
| 37 |
-
|
| 38 |
-
detection_model = load_model(
|
| 39 |
-
|
| 40 |
-
title = "Classify whether the image is Defective or Good"
|
| 41 |
-
interface = gr.Interface(
|
| 42 |
-
fn=predict,
|
| 43 |
-
inputs=gr.Image(type="pil", label="Input Image"),
|
| 44 |
-
outputs=gr.Image(type="pil", label="Classified Imamge"),
|
| 45 |
-
title=title,
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
# Launch the interface
|
| 49 |
-
interface.launch(share=True)
|
|
|
|
| 1 |
+
# Description: This is the main file to run the Gradio interface for the object detection model.
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from huggingface_hub import snapshot_download
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
model_path = "best_int8_openvino_model"
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# Load the model
|
| 12 |
+
def load_model():
|
| 13 |
+
#download_dir = snapshot_download(repo_id) # download the model from the Hugging Face Hub
|
| 14 |
+
#print(download_dir)
|
| 15 |
+
#path = os.path.join(download_dir, "best_int8_openvino_model") # path to the model
|
| 16 |
+
#print(model_path)
|
| 17 |
+
detection_model = YOLO(model_path, task='classify') # load the model
|
| 18 |
+
return detection_model
|
| 19 |
+
|
| 20 |
+
# Predict the image
|
| 21 |
+
def predict(pilimg):
|
| 22 |
+
source = pilimg
|
| 23 |
+
# x = np.asarray(pilimg)
|
| 24 |
+
# print(x.shape)
|
| 25 |
+
result = detection_model.predict(source, conf=0.5) # confidence threshold, intersection over union threshold
|
| 26 |
+
|
| 27 |
+
#print("Result: ", result)
|
| 28 |
+
if not result or len(result[0].boxes) == 0: # if no object detected
|
| 29 |
+
gr.Warning("Image not recognized for classfication!")
|
| 30 |
+
else:
|
| 31 |
+
img_bgr = result[0].plot() # plot the image
|
| 32 |
+
out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # RGB-order PIL image
|
| 33 |
+
|
| 34 |
+
return out_pilimg
|
| 35 |
+
|
| 36 |
+
REPO_ID = "best_int8_openvino_model" # The repo ID of the model
|
| 37 |
+
|
| 38 |
+
detection_model = load_model()
|
| 39 |
+
|
| 40 |
+
title = "Classify whether the image is Defective or Good"
|
| 41 |
+
interface = gr.Interface(
|
| 42 |
+
fn=predict,
|
| 43 |
+
inputs=gr.Image(type="pil", label="Input Image"),
|
| 44 |
+
outputs=gr.Image(type="pil", label="Classified Imamge"),
|
| 45 |
+
title=title,
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Launch the interface
|
| 49 |
+
interface.launch(share=True)
|