Yanguan commited on
Commit
c1c16cb
·
1 Parent(s): 37071b1
Files changed (4) hide show
  1. app.py +76 -88
  2. app1.py +0 -87
  3. app_gradio.py +99 -0
  4. requirements.txt +1 -1
app.py CHANGED
@@ -1,99 +1,87 @@
 
1
  """
2
  实现web界面
 
 
3
  """
4
 
 
5
  from pathlib import Path
6
 
7
- import gradio as gr
8
- from detect import detect,opt
 
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
- 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
- demo.launch(share=True)
94
-
95
- if __name__ == "__main__":
96
- main()
97
- # 如果不以`demo`命名,`gradio app.py`会报错`Error loading ASGI app. Attribute "demo.app" not found in module "app".`
98
- # 注意gradio库的reload.py的头信息 $ gradio app.py my_demo, to use variable names other than "demo"
99
- # my_demo 是定义的变量。离谱o(╥﹏╥)o
 
1
+ # 2023年2月23日
2
  """
3
  实现web界面
4
+
5
+ >>> streamlit run app.py
6
  """
7
 
8
+ from io import BytesIO
9
  from pathlib import Path
10
 
11
+ import streamlit as st
12
+ from detect import detect, opt
13
+ from PIL import Image
14
  from util import get_all_weights
15
 
16
+ """
17
+ # CycleGAN
18
+ 功能:上传本地文件、选择转换风格
19
+ """
20
 
 
 
 
 
 
21
 
22
+ def load_css(css_path="./util/streamlit/css.css"):
 
 
 
 
 
 
23
  """
24
+ 加载CSS文件
25
+ :param css_path: CSS文件路径
26
+ """
27
+ if Path(css_path).exists():
28
+ with open(css_path) as f:
29
+ # 将CSS文件内容插入到HTML中
30
+ st.markdown(
31
+ f"""<style>{f.read()}</style>""",
32
+ unsafe_allow_html=True,
33
+ )
34
+
35
+
36
+ def load_img_file(file):
37
+ """读取图片文件"""
38
+ img = Image.open(BytesIO(file.read()))
39
+ st.image(img, use_column_width=True) # 显示图片
40
+ return img
41
+
42
+
43
+ def set_style_options(label: str, frame=st):
44
+ """风格选项"""
45
+ style_options = get_all_weights()
46
+ options = [None] + style_options # 默认空
47
+ style_param = frame.selectbox(label=label, options=options)
48
+ return style_param
49
+
50
+
51
+ # load_css()
52
+ tab_mul2mul, tab_mul2one, tab_set = st.tabs(["多图多风格转换", "多图同风格转换", "参数"])
53
+
54
+ with tab_mul2mul:
55
+ uploaded_files = st.file_uploader(label="选择本地图片", accept_multiple_files=True, key=1)
56
+ if uploaded_files:
57
+ for idx, uploaded_file in enumerate(uploaded_files):
58
+ colL, colR = st.columns(2)
59
+ with colL:
60
+ img = load_img_file(uploaded_file)
61
+ style = set_style_options(label=str(uploaded_file), frame=st)
62
+ with colR:
63
+ if style:
64
+ fake_img = detect(img=img, style=style)
65
+ st.image(fake_img, caption="", use_column_width=True)
66
+
67
+ with tab_set:
68
+ colL, colR = st.columns([1, 3])
69
+ for k, v in sorted(vars(opt).items()):
70
+ st.text_input(label=k, value=v, disabled=True)
71
+ # st.selectbox("ss", options=opt.parse_args())
72
+ confidence_threshold = st.slider("Confidence threshold", 0.0, 1.0, 0.5, 0.01)
73
+ opt.no_dropout = st.radio("no_droput", [True, False])
74
+
75
+ with tab_mul2one:
76
+ uploaded_files = st.file_uploader(label="选择本地图片", accept_multiple_files=True, key=2)
77
+ if uploaded_files:
78
+ colL, colR = st.columns(2)
79
+ with colL:
80
+ imgs = [load_img_file(ii) for ii in uploaded_files]
81
+ with colR:
82
+ style = set_style_options(label="选择风格", frame=st)
83
+ if style:
84
+ if st.button("♻️风格转换", use_container_width=True):
85
+ for img in imgs:
86
+ fake_img = detect(img, style)
87
+ st.image(fake_img, caption="", use_column_width=True)
 
 
 
 
 
 
 
 
 
 
app1.py DELETED
@@ -1,87 +0,0 @@
1
- # 2023年2月23日
2
- """
3
- 实现web界面
4
-
5
- >>> streamlit run app.py
6
- """
7
-
8
- from io import BytesIO
9
- from pathlib import Path
10
-
11
- import streamlit as st
12
- from detect import detect, opt
13
- from PIL import Image
14
- from util import get_all_weights
15
-
16
- """
17
- # CycleGAN
18
- 功能:上传本地文件、选择转换风格
19
- """
20
-
21
-
22
- def load_css(css_path="./util/streamlit/css.css"):
23
- """
24
- 加载CSS文件
25
- :param css_path: CSS文件路径
26
- """
27
- if Path(css_path).exists():
28
- with open(css_path) as f:
29
- # 将CSS文件内容插入到HTML中
30
- st.markdown(
31
- f"""<style>{f.read()}</style>""",
32
- unsafe_allow_html=True,
33
- )
34
-
35
-
36
- def load_img_file(file):
37
- """读取图片文件"""
38
- img = Image.open(BytesIO(file.read()))
39
- st.image(img, use_column_width=True) # 显示图片
40
- return img
41
-
42
-
43
- def set_style_options(label: str, frame=st):
44
- """风格选项"""
45
- style_options = get_all_weights()
46
- options = [None] + style_options # 默认空
47
- style_param = frame.selectbox(label=label, options=options)
48
- return style_param
49
-
50
-
51
- # load_css()
52
- tab_mul2mul, tab_mul2one, tab_set = st.tabs(["多图多风格转换", "多图同风格转换", "参数"])
53
-
54
- with tab_mul2mul:
55
- uploaded_files = st.file_uploader(label="选择本地图片", accept_multiple_files=True, key=1)
56
- if uploaded_files:
57
- for idx, uploaded_file in enumerate(uploaded_files):
58
- colL, colR = st.columns(2)
59
- with colL:
60
- img = load_img_file(uploaded_file)
61
- style = set_style_options(label=str(uploaded_file), frame=st)
62
- with colR:
63
- if style:
64
- fake_img = detect(img=img, style=style)
65
- st.image(fake_img, caption="", use_column_width=True)
66
-
67
- with tab_set:
68
- colL, colR = st.columns([1, 3])
69
- for k, v in sorted(vars(opt).items()):
70
- st.text_input(label=k, value=v, disabled=True)
71
- # st.selectbox("ss", options=opt.parse_args())
72
- confidence_threshold = st.slider("Confidence threshold", 0.0, 1.0, 0.5, 0.01)
73
- opt.no_dropout = st.radio("no_droput", [True, False])
74
-
75
- with tab_mul2one:
76
- uploaded_files = st.file_uploader(label="选择本地图片", accept_multiple_files=True, key=2)
77
- if uploaded_files:
78
- colL, colR = st.columns(2)
79
- with colL:
80
- imgs = [load_img_file(ii) for ii in uploaded_files]
81
- with colR:
82
- style = set_style_options(label="选择风格", frame=st)
83
- if style:
84
- if st.button("♻️风格转换", use_container_width=True):
85
- for img in imgs:
86
- fake_img = detect(img, style)
87
- st.image(fake_img, caption="", use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app_gradio.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ 实现web界面
3
+ """
4
+
5
+ from pathlib import Path
6
+
7
+ import gradio as gr
8
+ from detect import detect,opt
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
+ 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
+ demo.launch(share=True)
94
+
95
+ if __name__ == "__main__":
96
+ main()
97
+ # 如果不以`demo`命名,`gradio app.py`会报错`Error loading ASGI app. Attribute "demo.app" not found in module "app".`
98
+ # 注意gradio库的reload.py的头信息 $ gradio app.py my_demo, to use variable names other than "demo"
99
+ # my_demo 是定义的变量。离谱o(╥﹏╥)o
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- gradio
2
  numpy
3
  Pillow
4
  torch
 
1
+ streamlit
2
  numpy
3
  Pillow
4
  torch