prithivMLmods commited on
Commit
908c106
·
verified ·
1 Parent(s): f66760e

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +89 -1
README.md CHANGED
@@ -9,4 +9,92 @@ library_name: transformers
9
  tags:
10
  - text-generation-inference
11
  - mobile-gui-detection
12
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  tags:
10
  - text-generation-inference
11
  - mobile-gui-detection
12
+ ---
13
+
14
+ ## Quick Start with Transformers
15
+
16
+ ```
17
+ pip install torch==2.8.0 --index-url https://download.pytorch.org/whl/cu128
18
+ pip install torchvision==0.23.0 transformers==5.9.0 accelerate gradio==6.19.0
19
+ ```
20
+
21
+ ```py
22
+ import gradio as gr
23
+ import torch
24
+ from PIL import Image, ImageDraw
25
+
26
+ from transformers import AutoImageProcessor, RfDetrForObjectDetection
27
+
28
+ # Load model and processor
29
+ model_name = "prithivMLmods/rf-detr-mobile-gui-detection"
30
+
31
+ processor = AutoImageProcessor.from_pretrained(model_name)
32
+ model = RfDetrForObjectDetection.from_pretrained(model_name)
33
+
34
+ # Detection threshold
35
+ THRESHOLD = 0.35
36
+
37
+
38
+ def detect_gui(image):
39
+ image = Image.fromarray(image).convert("RGB")
40
+
41
+ inputs = processor(images=image, return_tensors="pt")
42
+
43
+ with torch.no_grad():
44
+ outputs = model(**inputs)
45
+
46
+ target_sizes = torch.tensor([image.size[::-1]])
47
+ results = processor.post_process_object_detection(
48
+ outputs,
49
+ target_sizes=target_sizes,
50
+ threshold=THRESHOLD,
51
+ )[0]
52
+
53
+ draw = ImageDraw.Draw(image)
54
+
55
+ detections = []
56
+
57
+ for score, label, box in zip(
58
+ results["scores"],
59
+ results["labels"],
60
+ results["boxes"],
61
+ ):
62
+ box = [round(x, 2) for x in box.tolist()]
63
+ label_name = model.config.id2label[label.item()]
64
+ confidence = round(score.item(), 3)
65
+
66
+ # Draw bounding box
67
+ draw.rectangle(box, outline="red", width=3)
68
+
69
+ # Draw label
70
+ draw.text(
71
+ (box[0] + 4, max(0, box[1] - 16)),
72
+ f"{label_name} {confidence:.2f}",
73
+ fill="red",
74
+ )
75
+
76
+ detections.append(
77
+ {
78
+ "Label": label_name,
79
+ "Confidence": confidence,
80
+ "Bounding Box": box,
81
+ }
82
+ )
83
+
84
+ return image, detections
85
+
86
+
87
+ demo = gr.Interface(
88
+ fn=detect_gui,
89
+ inputs=gr.Image(type="numpy", label="Upload Mobile UI Screenshot"),
90
+ outputs=[
91
+ gr.Image(type="pil", label="Detected GUI Elements"),
92
+ gr.JSON(label="Detections"),
93
+ ],
94
+ title="RF-DETR Mobile GUI Detection",
95
+ description="Upload a mobile UI screenshot to detect GUI elements using RF-DETR.",
96
+ )
97
+
98
+ if __name__ == "__main__":
99
+ demo.launch()
100
+ ```