Jason Wu commited on
Commit
47a8e90
1 Parent(s): cc0d6fd

initial commit

Browse files
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ import json
4
+ from torchvision import transforms
5
+ from PIL import Image, ImageDraw, ImageFont
6
+
7
+ TORCHSCRIPT_PATH = "res/screenrecognition-web350k-vins.torchscript"
8
+ LABELS_PATH = "res/class_map_vins_manual.json"
9
+
10
+ model = torch.jit.load(TORCHSCRIPT_PATH)
11
+
12
+ with open(LABELS_PATH, "r") as f:
13
+ idx2Label = json.load(f)["idx2Label"]
14
+
15
+ img_transforms = transforms.ToTensor()
16
+
17
+ def predict(img, conf_thresh=0.4):
18
+ img_input = [img_transforms(img)]
19
+ _, pred = model(img_input)
20
+ out_img = img.copy()
21
+ draw = ImageDraw.Draw(out_img)
22
+ font = ImageFont.truetype("res/Tuffy_Bold.ttf", 25)
23
+ for i in range(len(pred[0]['boxes'])):
24
+ conf_score = pred[0]['scores'][i]
25
+ if conf_score > conf_thresh:
26
+ x1, y1, x2, y2 = pred[0]['boxes'][i]
27
+ x1 = int(x1)
28
+ y1 = int(y1)
29
+ x2 = int(x2)
30
+ y2 = int(y2)
31
+ draw.rectangle([x1, y1, x2, y2], outline='red', width=3)
32
+
33
+ text = idx2Label[str(int(pred[0]['labels'][i]))] + " {:.2f}".format(float(conf_score))
34
+
35
+ bbox = draw.textbbox((x1, y1), text, font=font)
36
+ draw.rectangle(bbox, fill="red")
37
+ draw.text((x1, y1), text, font=font, fill="black")
38
+
39
+ return out_img
40
+
41
+ example_imgs = [
42
+ ["res/example.jpg", 0.4],
43
+ ["res/example_pair1.jpg", 0.4],
44
+ ["res/example_pair2.jpg", 0.4]
45
+ ]
46
+
47
+ interface = gr.Interface(fn=predict, inputs=[gr.Image(type="pil", label="Screenshot"), gr.Slider(0.0, 1.0, step=0.1, value=0.5)], outputs=gr.Image(type="pil", label="Annotated Screenshot"), examples=example_imgs)
48
+
49
+ interface.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ Pillow==9.4.0
2
+ torch==1.13.0
3
+ torchvision==0.14.0
res/Tuffy_Bold.ttf ADDED
Binary file (94.6 kB). View file
 
res/class_map_vins_manual.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"idx2Label": {"1": "OTHER", "2": "Background Image", "3": "Checked View", "4": "Icon", "5": "Input Field", "6": "Image", "7": "Text", "8": "Text Button", "9": "Page Indicator", "10": "Pop-Up Window", "11": "Sliding Menu", "12": "Switch", "0": "BACKGROUND"}, "label2Idx": {"BackgroundImage": 2, "Bottom_Navigation": 1, "Card": 1, "CheckBox": 3, "Checkbox": 3, "CheckedTextView": 3, "Drawer": 11, "EditText": 5, "Icon": 4, "Image": 6, "Map": 1, "Modal": 10, "Multi_Tab": 1, "PageIndicator": 9, "Remember": 3, "Spinner": 5, "Switch": 12, "Text": 7, "TextButton": 8, "Toolbar": 1, "UpperTaskBar": 1, "BACKGROUND": 0}}
res/example.jpg ADDED
res/example_pair1.jpg ADDED
res/example_pair2.jpg ADDED