lihao57 commited on
Commit
e602299
·
1 Parent(s): 08092af

update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -23
app.py CHANGED
@@ -8,14 +8,18 @@
8
  @Contact : 2909171338@qq.com
9
  """
10
 
 
11
  import gradio as gr
12
- from PIL import Image
13
  import io
14
  import matplotlib.pyplot as plt
15
  import numpy as np
16
  from datasets import load_dataset
17
 
18
  ds = None
 
 
 
19
 
20
 
21
  def get_dataset():
@@ -30,7 +34,7 @@ def get_dataset():
30
  """
31
  global ds
32
  if ds is None:
33
- ds = load_dataset("lh9171338/Wireframe")
34
  return ds
35
 
36
 
@@ -63,25 +67,35 @@ def draw_lines(image, lines):
63
  Returns:
64
  image (PIL.Image): drawn image
65
  """
66
- height, width = image.shape[:2]
67
- fig = plt.figure()
68
- fig.set_size_inches(width / height, 1, forward=False)
69
- ax = plt.Axes(fig, [0.0, 0.0, 1.0, 1.0])
70
- ax.set_axis_off()
71
- fig.add_axes(ax)
72
- plt.xlim([-0.5, width - 0.5])
73
- plt.ylim([height - 0.5, -0.5])
74
- plt.imshow(image)
75
- for pts in lines:
76
- pts = pts - 0.5
77
- plt.plot(pts[:, 0], pts[:, 1], color="orange", linewidth=0.5)
78
- plt.scatter(pts[[0, -1], 0], pts[[0, -1], 1], color="#33FFFF", s=1.2, edgecolors="none", zorder=5)
79
-
80
- buf = io.BytesIO()
81
- fig.savefig(buf, format="png", dpi=height, bbox_inches=0)
82
- buf.seek(0)
83
- plt.close(fig)
84
- image = Image.open(buf)
 
 
 
 
 
 
 
 
 
 
85
  return image
86
 
87
 
@@ -104,9 +118,22 @@ def show_image(split, index):
104
  return image
105
 
106
 
107
- if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
108
  with gr.Blocks() as demo:
109
- split_selector = gr.Dropdown(["train", "test"], label="Split", value="train")
 
 
 
 
110
  index_slider = gr.Slider(0, 1, step=1, label="Index", value=0)
111
  output = gr.Image()
112
 
@@ -114,3 +141,16 @@ if __name__ == "__main__":
114
  index_slider.change(show_image, [split_selector, index_slider], output)
115
  demo.load(selector_change_callback, split_selector, [index_slider, output])
116
  demo.launch(share=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  @Contact : 2909171338@qq.com
9
  """
10
 
11
+ import argparse
12
  import gradio as gr
13
+ from PIL import Image, ImageDraw
14
  import io
15
  import matplotlib.pyplot as plt
16
  import numpy as np
17
  from datasets import load_dataset
18
 
19
  ds = None
20
+ DATASET_NAME = None
21
+ FAST = True
22
+ SPLIT = "all"
23
 
24
 
25
  def get_dataset():
 
34
  """
35
  global ds
36
  if ds is None:
37
+ ds = load_dataset(DATASET_NAME)
38
  return ds
39
 
40
 
 
67
  Returns:
68
  image (PIL.Image): drawn image
69
  """
70
+ if FAST:
71
+ image = Image.fromarray(image)
72
+ draw = ImageDraw.Draw(image)
73
+ for pts in lines:
74
+ pts = pts - 0.5
75
+ pts_list = [tuple(p) for p in pts]
76
+ draw.line(pts_list, fill="orange", width=2)
77
+ draw.circle(pts_list[0], 3, fill="#33FFFF")
78
+ draw.circle(pts_list[-1], 3, fill="#33FFFF")
79
+ else:
80
+ height, width = image.shape[:2]
81
+ fig = plt.figure()
82
+ fig.set_size_inches(width / height, 1, forward=False)
83
+ ax = plt.Axes(fig, [0.0, 0.0, 1.0, 1.0])
84
+ ax.set_axis_off()
85
+ fig.add_axes(ax)
86
+ plt.xlim([-0.5, width - 0.5])
87
+ plt.ylim([height - 0.5, -0.5])
88
+ plt.imshow(image)
89
+ for pts in lines:
90
+ pts = pts - 0.5
91
+ plt.plot(pts[:, 0], pts[:, 1], color="orange", linewidth=0.5)
92
+ plt.scatter(pts[[0, -1], 0], pts[[0, -1], 1], color="#33FFFF", s=1.2, edgecolors="none", zorder=5)
93
+
94
+ buf = io.BytesIO()
95
+ fig.savefig(buf, format="png", dpi=height, bbox_inches=0)
96
+ buf.seek(0)
97
+ plt.close(fig)
98
+ image = Image.open(buf)
99
  return image
100
 
101
 
 
118
  return image
119
 
120
 
121
+ def main():
122
+ """
123
+ main
124
+
125
+ Args:
126
+ None
127
+
128
+ Returns:
129
+ None
130
+ """
131
  with gr.Blocks() as demo:
132
+ if SPLIT == "all":
133
+ choices = ["train", "test"]
134
+ else:
135
+ choices = [SPLIT]
136
+ split_selector = gr.Dropdown(choices, label="Split", value=choices[0])
137
  index_slider = gr.Slider(0, 1, step=1, label="Index", value=0)
138
  output = gr.Image()
139
 
 
141
  index_slider.change(show_image, [split_selector, index_slider], output)
142
  demo.load(selector_change_callback, split_selector, [index_slider, output])
143
  demo.launch(share=False)
144
+
145
+
146
+ if __name__ == "__main__":
147
+ argparser = argparse.ArgumentParser()
148
+ argparser.add_argument("-n", "--dataset_name", type=str, help="dataset name", default="lh9171338/Wireframe")
149
+ argparser.add_argument("-f", "--fast", type=bool, help="whether to use fast drawing method", default=True)
150
+ argparser.add_argument("-s", "--split", type=str, help="split", default="all", choices=["all", "train", "test"])
151
+ args = argparser.parse_args()
152
+ print(args)
153
+ DATASET_NAME = args.dataset_name
154
+ FAST = args.fast
155
+ SPLIT = args.split
156
+ main()