Update app.py
Browse files
app.py
CHANGED
@@ -10,69 +10,24 @@ if not os.path.exists(MODEL_PATH):
|
|
10 |
model = YOLO(MODEL_PATH)
|
11 |
|
12 |
def detect_defects(image):
|
13 |
-
"""Run
|
14 |
try:
|
15 |
-
# Run inference
|
16 |
results = model(image)
|
17 |
-
|
18 |
-
|
19 |
-
boxes = results[0].boxes
|
20 |
-
class_names = model.names
|
21 |
-
detections = []
|
22 |
-
|
23 |
-
for box in boxes:
|
24 |
-
class_id = int(box.cls)
|
25 |
-
confidence = float(box.conf)
|
26 |
-
detections.append({
|
27 |
-
"class": class_names[class_id],
|
28 |
-
"confidence": confidence,
|
29 |
-
"box": box.xyxy[0].tolist() # [x1,y1,x2,y2]
|
30 |
-
})
|
31 |
-
|
32 |
-
# Plot results
|
33 |
-
annotated_img = results[0].plot(line_width=2, font_size=10)
|
34 |
-
annotated_img = cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB)
|
35 |
-
|
36 |
-
return annotated_img, detections
|
37 |
-
|
38 |
except Exception as e:
|
39 |
-
print(f"Error
|
40 |
-
return image
|
41 |
|
42 |
-
#
|
43 |
-
|
44 |
-
|
45 |
-
.
|
46 |
-
""
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
gr.Markdown("Upload or drag-and-drop an image of steel surface to detect defects")
|
52 |
-
|
53 |
-
with gr.Row():
|
54 |
-
with gr.Column():
|
55 |
-
image_input = gr.Image(label="Input Image", type="numpy")
|
56 |
-
submit_btn = gr.Button("Detect Defects", variant="primary")
|
57 |
-
|
58 |
-
with gr.Column():
|
59 |
-
image_output = gr.Image(label="Detected Defects", elem_classes=["output-image"])
|
60 |
-
json_output = gr.JSON(label="Detection Results")
|
61 |
-
|
62 |
-
# Example images
|
63 |
-
gr.Examples(
|
64 |
-
examples=[["example1.jpg"], ["example2.jpg"]],
|
65 |
-
inputs=image_input,
|
66 |
-
outputs=[image_output, json_output],
|
67 |
-
fn=detect_defects,
|
68 |
-
cache_examples=False
|
69 |
-
)
|
70 |
-
|
71 |
-
submit_btn.click(
|
72 |
-
fn=detect_defects,
|
73 |
-
inputs=image_input,
|
74 |
-
outputs=[image_output, json_output]
|
75 |
-
)
|
76 |
|
77 |
if __name__ == "__main__":
|
78 |
-
|
|
|
10 |
model = YOLO(MODEL_PATH)
|
11 |
|
12 |
def detect_defects(image):
|
13 |
+
"""Run detection and return annotated image + detection data"""
|
14 |
try:
|
|
|
15 |
results = model(image)
|
16 |
+
annotated_img = results[0].plot(line_width=2)
|
17 |
+
return cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
except Exception as e:
|
19 |
+
print(f"Error: {e}")
|
20 |
+
return image
|
21 |
|
22 |
+
# Simple UI
|
23 |
+
interface = gr.Interface(
|
24 |
+
fn=detect_defects,
|
25 |
+
inputs=gr.Image(label="Upload Steel Surface Image", type="numpy"),
|
26 |
+
outputs=gr.Image(label="Detected Defects"),
|
27 |
+
title="🔧 Steel Surface Defect Detector",
|
28 |
+
description="Upload an image to detect surface defects (crazing, scratches, etc.)",
|
29 |
+
allow_flagging="never"
|
30 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
if __name__ == "__main__":
|
33 |
+
interface.launch()
|