Netrava commited on
Commit
55e38fd
·
verified ·
1 Parent(s): 17a88c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -71
app.py CHANGED
@@ -1,71 +1,71 @@
1
-
2
- import gradio as gr
3
- import torch
4
- from ultralytics import YOLO
5
- import cv2
6
- import numpy as np
7
- from PIL import Image
8
-
9
- # Load model with error handling
10
- try:
11
- model = YOLO('netrava_ui_model.pt')
12
- print("✅ Custom model loaded successfully")
13
- except Exception as e:
14
- print(f"⚠️ Custom model not found, using default: {e}")
15
- try:
16
- model = YOLO('yolov8n.pt') # Fallback to default
17
- print("✅ Default YOLOv8n model loaded")
18
- except Exception as e2:
19
- print(f"❌ Failed to load any model: {e2}")
20
- model = None
21
-
22
- def detect_ui_elements(image):
23
- """Detect UI elements in uploaded image"""
24
- try:
25
- if model is None:
26
- return Image.new('RGB', (400, 300), color='red'), "❌ Model not available. Please upload netrava_ui_model.pt"
27
-
28
- # Convert PIL to OpenCV format
29
- img_array = np.array(image)
30
- img_bgr = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
31
-
32
- # Run detection
33
- results = model(img_bgr)
34
-
35
- # Draw results
36
- for result in results:
37
- boxes = result.boxes
38
- if boxes is not None:
39
- for box in boxes:
40
- # Get coordinates
41
- x1, y1, x2, y2 = box.xyxy[0].cpu().numpy()
42
- conf = box.conf[0].cpu().numpy()
43
- cls = int(box.cls[0].cpu().numpy())
44
-
45
- # Draw bounding box
46
- cv2.rectangle(img_bgr, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
47
-
48
- # Add label
49
- label = f'{model.names[cls]}: {conf:.2f}'
50
- cv2.putText(img_bgr, label, (int(x1), int(y1)-10),
51
- cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
52
-
53
- # Convert back to RGB
54
- result_img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
55
- return Image.fromarray(result_img)
56
-
57
- except Exception as e:
58
- return f"Error: {str(e)}"
59
-
60
- # Create Gradio interface
61
- interface = gr.Interface(
62
- fn=detect_ui_elements,
63
- inputs=gr.Image(type="pil"),
64
- outputs=gr.Image(type="pil"),
65
- title="🤖 Netrava UI Element Detection",
66
- description="Upload a screenshot to detect UI elements like buttons, textboxes, etc.",
67
- examples=["example1.png", "example2.png"]
68
- )
69
-
70
- if __name__ == "__main__":
71
- interface.launch()
 
1
+
2
+ import gradio as gr
3
+ import torch
4
+ from ultralytics import YOLO
5
+ import cv2
6
+ import numpy as np
7
+ from PIL import Image
8
+
9
+ # Load model with error handling
10
+ try:
11
+ model = YOLO('best.pt')
12
+ print("✅ Custom model loaded successfully")
13
+ except Exception as e:
14
+ print(f"⚠️ Custom model not found, using default: {e}")
15
+ try:
16
+ model = YOLO('yolov8n.pt') # Fallback to default
17
+ print("✅ Default YOLOv8n model loaded")
18
+ except Exception as e2:
19
+ print(f"❌ Failed to load any model: {e2}")
20
+ model = None
21
+
22
+ def detect_ui_elements(image):
23
+ """Detect UI elements in uploaded image"""
24
+ try:
25
+ if model is None:
26
+ return Image.new('RGB', (400, 300), color='red'), "❌ Model not available. Please upload netrava_ui_model.pt"
27
+
28
+ # Convert PIL to OpenCV format
29
+ img_array = np.array(image)
30
+ img_bgr = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
31
+
32
+ # Run detection
33
+ results = model(img_bgr)
34
+
35
+ # Draw results
36
+ for result in results:
37
+ boxes = result.boxes
38
+ if boxes is not None:
39
+ for box in boxes:
40
+ # Get coordinates
41
+ x1, y1, x2, y2 = box.xyxy[0].cpu().numpy()
42
+ conf = box.conf[0].cpu().numpy()
43
+ cls = int(box.cls[0].cpu().numpy())
44
+
45
+ # Draw bounding box
46
+ cv2.rectangle(img_bgr, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
47
+
48
+ # Add label
49
+ label = f'{model.names[cls]}: {conf:.2f}'
50
+ cv2.putText(img_bgr, label, (int(x1), int(y1)-10),
51
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
52
+
53
+ # Convert back to RGB
54
+ result_img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
55
+ return Image.fromarray(result_img)
56
+
57
+ except Exception as e:
58
+ return f"Error: {str(e)}"
59
+
60
+ # Create Gradio interface
61
+ interface = gr.Interface(
62
+ fn=detect_ui_elements,
63
+ inputs=gr.Image(type="pil"),
64
+ outputs=gr.Image(type="pil"),
65
+ title="🤖 Netrava UI Element Detection",
66
+ description="Upload a screenshot to detect UI elements like buttons, textboxes, etc.",
67
+ examples=["example1.png", "example2.png"]
68
+ )
69
+
70
+ if __name__ == "__main__":
71
+ interface.launch()