Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -64,7 +64,9 @@ def process_image(pilimg):
|
|
| 64 |
if classification == "Good":
|
| 65 |
out_pilimg = pilimg.convert("RGB")
|
| 66 |
draw = ImageDraw.Draw(out_pilimg)
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
|
| 69 |
draw.text((250, 10), "Good", fill="green", font=font)
|
| 70 |
gr.Info("No defect detected,GOOD!", duration=3)
|
|
@@ -75,7 +77,9 @@ def process_image(pilimg):
|
|
| 75 |
out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # RGB-order PIL image
|
| 76 |
|
| 77 |
draw = ImageDraw.Draw(out_pilimg)
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
|
| 80 |
draw.text((250, 10), "Defective", fill="red", font=font)
|
| 81 |
|
|
@@ -97,118 +101,3 @@ interface = gr.Interface(
|
|
| 97 |
|
| 98 |
# Launch the interface
|
| 99 |
interface.launch(share=True)
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
# In[ ]:
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
# In[ ]:
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
# In[ ]:
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
# In[37]:
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
from ultralytics import YOLO
|
| 124 |
-
from PIL import Image, ImageDraw, ImageFont
|
| 125 |
-
import gradio as gr
|
| 126 |
-
from huggingface_hub import snapshot_download
|
| 127 |
-
import os
|
| 128 |
-
from torchvision import transforms
|
| 129 |
-
|
| 130 |
-
classes = {0: "Defective", 1: "Good"}
|
| 131 |
-
|
| 132 |
-
model_path = "best_int8_openvino_model"
|
| 133 |
-
|
| 134 |
-
def load_model_local():
|
| 135 |
-
detection_model = YOLO(model_path, task='classify') # Load the model
|
| 136 |
-
return detection_model
|
| 137 |
-
|
| 138 |
-
def load_model(repo_id):
|
| 139 |
-
download_dir = snapshot_download(repo_id)
|
| 140 |
-
print(download_dir)
|
| 141 |
-
path = os.path.join(download_dir, "best_int8_openvino_model")
|
| 142 |
-
print(path)
|
| 143 |
-
detection_model = YOLO(path, task='classify')
|
| 144 |
-
return detection_model
|
| 145 |
-
|
| 146 |
-
def predict(pilimg):
|
| 147 |
-
source = pilimg
|
| 148 |
-
|
| 149 |
-
# Call the model to transform image size
|
| 150 |
-
transform = transforms.Compose([
|
| 151 |
-
transforms.Resize((224, 224)),
|
| 152 |
-
transforms.ToTensor(),
|
| 153 |
-
])
|
| 154 |
-
|
| 155 |
-
source = transform(source) # Update the source image size to 224x224, 1 of 2 sizes accepted by Yolo classification model
|
| 156 |
-
|
| 157 |
-
#result = detection_model.predict(source, conf=0.55, iou=0.6)
|
| 158 |
-
result = detection_model.predict(source) # Make prediction
|
| 159 |
-
# Get the top prediction
|
| 160 |
-
label = result[0].probs.top1
|
| 161 |
-
|
| 162 |
-
class_names = detection_model.names # Retrieves the class names mapping (dict-like)
|
| 163 |
-
classified_type = class_names[label] # Map numeric label to class name
|
| 164 |
-
print (">>> Class : ", classified_type)
|
| 165 |
-
|
| 166 |
-
confidence = result[0].probs.top1conf # Get the top class confidence
|
| 167 |
-
print(">>> Confidence : ", confidence)
|
| 168 |
-
|
| 169 |
-
annotated_image = pilimg.convert("RGB")
|
| 170 |
-
draw = ImageDraw.Draw(annotated_image)
|
| 171 |
-
font = ImageFont.truetype("arialbd.ttf", 30) # Use arial.ttf for bold font
|
| 172 |
-
|
| 173 |
-
if classified_type == classes[0]:
|
| 174 |
-
draw.text((300, 10), classified_type, fill="red", font=font)
|
| 175 |
-
gr.Warning("Defect detected, BAD!.")
|
| 176 |
-
else:
|
| 177 |
-
draw.text((300, 10), classified_type, fill="green", font=font)
|
| 178 |
-
gr.Info("No defect detected,GOOD!")
|
| 179 |
-
|
| 180 |
-
#draw.text((300, 10), classified_type, fill="red", font=font)
|
| 181 |
-
|
| 182 |
-
return annotated_image
|
| 183 |
-
|
| 184 |
-
detection_model = load_model_local()
|
| 185 |
-
|
| 186 |
-
title = "Detect the status of the cap, DEFECTIVE or GOOD"
|
| 187 |
-
interface = gr.Interface(
|
| 188 |
-
fn=predict,
|
| 189 |
-
inputs=gr.Image(type="pil", label="Input Image"),
|
| 190 |
-
outputs=gr.Image(type="pil", label="Classification result"),
|
| 191 |
-
title=title,
|
| 192 |
-
)
|
| 193 |
-
|
| 194 |
-
# Launch the interface
|
| 195 |
-
interface.launch(share=True)
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
# In[ ]:
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
# In[ ]:
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
# In[ ]:
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
|
|
|
| 64 |
if classification == "Good":
|
| 65 |
out_pilimg = pilimg.convert("RGB")
|
| 66 |
draw = ImageDraw.Draw(out_pilimg)
|
| 67 |
+
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
|
| 68 |
+
font = ImageFont.truetype(font_path, 30)
|
| 69 |
+
#font = ImageFont.truetype("arialbd.ttf", 30) # Use arial.ttf for bold font
|
| 70 |
|
| 71 |
draw.text((250, 10), "Good", fill="green", font=font)
|
| 72 |
gr.Info("No defect detected,GOOD!", duration=3)
|
|
|
|
| 77 |
out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # RGB-order PIL image
|
| 78 |
|
| 79 |
draw = ImageDraw.Draw(out_pilimg)
|
| 80 |
+
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
|
| 81 |
+
font = ImageFont.truetype(font_path, 30)
|
| 82 |
+
#font = ImageFont.truetype("arialbd.ttf", 30) # Use arial.ttf for bold font
|
| 83 |
|
| 84 |
draw.text((250, 10), "Defective", fill="red", font=font)
|
| 85 |
|
|
|
|
| 101 |
|
| 102 |
# Launch the interface
|
| 103 |
interface.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|