Yanguan commited on
Commit
8407553
1 Parent(s): 4e0b322
Files changed (2) hide show
  1. app.py +84 -81
  2. weights/detect/test_opt.txt +1 -1
app.py CHANGED
@@ -8,91 +8,94 @@ import gradio as gr
8
  from detect import detect
9
  from util import get_all_weights
10
 
11
- app_introduce = """
12
- # CycleGAN
13
- 功能:上传本地文件、选择转换风格
14
- """
15
-
16
- css = """
17
- # :root{
18
- # --block-background-fill: #f3f3f3;
19
- # }
20
- footer{
21
- display:none!important;
22
- }
23
- """
24
-
25
- demo = gr.Blocks(
26
- # theme=gr.themes.Soft(),
27
- css=css
28
- )
29
-
30
 
31
- def add_img(img):
32
- imgs = []
33
- for style in get_all_weights():
34
- fake_img = detect(img, style=style)
35
- imgs.append(fake_img)
36
- return imgs
37
-
38
-
39
- def tab1(label="上传单张图片"):
40
- default_img_paths = [
41
- [Path.cwd().joinpath("./imgs/horse.jpg"), "horse2zebra"],
42
- [Path.cwd().joinpath("./imgs/monet.jpg"), "monet2photo"],
43
- ]
44
-
45
- with gr.Tab(label):
46
- with gr.Row():
47
- with gr.Column():
48
- img = gr.Image(type="pil", label="选择需要进行风格转换的图片")
49
- style = gr.Dropdown(choices=get_all_weights(), label="转换的风格")
50
- detect_btn = gr.Button("♻️风格转换")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  with gr.Column():
52
- out_img = gr.Image(label="风格图")
53
- detect_btn.click(fn=detect, inputs=[img, style], outputs=[out_img])
54
- gr.Examples(default_img_paths, inputs=[img, style])
55
-
56
-
57
- def tab2(label="单图多风格试转换"):
58
- with gr.Tab(label):
59
- with gr.Row():
60
- with gr.Column(scale=1):
61
- img = gr.Image(type="pil", label="选择需要进行风格转换的图片")
62
- gr.Markdown("上传一张图片,会将所有风格推理一遍。")
63
- btn = gr.Button("♻️风格转换")
64
- with gr.Column(scale=2):
65
- gallery = gr.Gallery(
66
- label="风格图",
67
- elem_id="gallery",
68
- ).style(grid=[3], height="auto")
69
- btn.click(fn=add_img, inputs=[img], outputs=gallery)
70
-
71
-
72
- def tab3(label="参数设置"):
73
- from detect import opt
74
-
75
- with gr.Tab(label):
76
- with gr.Column():
77
- for k, v in sorted(vars(opt).items()):
78
- if type(v) == bool:
79
- gr.Checkbox(label=k, value=v)
80
- elif type(v) == (int or float):
81
- gr.Number(label=k, value=v)
82
- elif type(v) == list:
83
- gr.CheckboxGroup(label=k, value=v)
84
- else:
85
- gr.Textbox(label=k, value=v)
86
-
87
-
88
- with demo:
89
- gr.Markdown(app_introduce)
90
- tab1()
91
- tab2()
92
- tab3()
93
 
94
  if __name__ == "__main__":
95
- demo.launch(share=True)
96
  # 如果不以`demo`命名,`gradio app.py`会报错`Error loading ASGI app. Attribute "demo.app" not found in module "app".`
97
  # 注意gradio库的reload.py的头信息 $ gradio app.py my_demo, to use variable names other than "demo"
98
  # my_demo 是定义的变量。离谱o(╥﹏╥)o
 
8
  from detect import detect
9
  from util import get_all_weights
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ def main():
13
+ app_introduce = """
14
+ # CycleGAN
15
+ 功能:上传本地文件、选择转换风格
16
+ """
17
+
18
+ css = """
19
+ # :root{
20
+ # --block-background-fill: #f3f3f3;
21
+ # }
22
+ # footer{
23
+ # display:none!important;
24
+ # }
25
+ """
26
+
27
+ demo = gr.Blocks(
28
+ # theme=gr.themes.Soft(),
29
+ css=css
30
+ )
31
+
32
+
33
+ def add_img(img):
34
+ imgs = []
35
+ for style in get_all_weights():
36
+ fake_img = detect(img, style=style)
37
+ imgs.append(fake_img)
38
+ return imgs
39
+
40
+
41
+ def tab1(label="上传单张图片"):
42
+ default_img_paths = [
43
+ [Path.cwd().joinpath("./imgs/horse.jpg"), "horse2zebra"],
44
+ [Path.cwd().joinpath("./imgs/monet.jpg"), "monet2photo"],
45
+ ]
46
+
47
+ with gr.Tab(label):
48
+ with gr.Row():
49
+ with gr.Column():
50
+ img = gr.Image(type="pil", label="选择需要进行风格转换的图片")
51
+ style = gr.Dropdown(choices=get_all_weights(), label="转换的风格")
52
+ detect_btn = gr.Button("♻️风格转换")
53
+ with gr.Column():
54
+ out_img = gr.Image(label="风格图")
55
+ detect_btn.click(fn=detect, inputs=[img, style], outputs=[out_img])
56
+ gr.Examples(default_img_paths, inputs=[img, style])
57
+
58
+
59
+ def tab2(label="单图多风格试转换"):
60
+ with gr.Tab(label):
61
+ with gr.Row():
62
+ with gr.Column(scale=1):
63
+ img = gr.Image(type="pil", label="选择需要进行风格转换的图片")
64
+ gr.Markdown("上传一张图片,会将所有风格推理一遍。")
65
+ btn = gr.Button("♻️风格转换")
66
+ with gr.Column(scale=2):
67
+ gallery = gr.Gallery(
68
+ label="风格图",
69
+ elem_id="gallery",
70
+ ).style(grid=[3], height="auto")
71
+ btn.click(fn=add_img, inputs=[img], outputs=gallery)
72
+
73
+
74
+ def tab3(label="参数设置"):
75
+ from detect import opt
76
+
77
+ with gr.Tab(label):
78
  with gr.Column():
79
+ for k, v in sorted(vars(opt).items()):
80
+ if type(v) == bool:
81
+ gr.Checkbox(label=k, value=v)
82
+ elif type(v) == (int or float):
83
+ gr.Number(label=k, value=v)
84
+ elif type(v) == list:
85
+ gr.CheckboxGroup(label=k, value=v)
86
+ else:
87
+ gr.Textbox(label=k, value=v)
88
+
89
+
90
+ with demo:
91
+ gr.Markdown(app_introduce)
92
+ tab1()
93
+ tab2()
94
+ tab3()
95
+ demo.launch(server_name="0.0.0.0", server_port = ARGS.port)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
  if __name__ == "__main__":
98
+ main()
99
  # 如果不以`demo`命名,`gradio app.py`会报错`Error loading ASGI app. Attribute "demo.app" not found in module "app".`
100
  # 注意gradio库的reload.py的头信息 $ gradio app.py my_demo, to use variable names other than "demo"
101
  # my_demo 是定义的变量。离谱o(╥﹏╥)o
weights/detect/test_opt.txt CHANGED
@@ -9,7 +9,7 @@
9
  display_winsize: 256
10
  epoch: latest
11
  eval: False
12
- gpu_ids: 0
13
  help: folder: /imgs/ or file: xx.jpg
14
  init_gain: 0.02
15
  init_type: normal
 
9
  display_winsize: 256
10
  epoch: latest
11
  eval: False
12
+ gpu_ids: -1
13
  help: folder: /imgs/ or file: xx.jpg
14
  init_gain: 0.02
15
  init_type: normal