heatingma commited on
Commit
99f7d89
1 Parent(s): 32603e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +161 -163
app.py CHANGED
@@ -1,164 +1,162 @@
1
- import time
2
- import shutil
3
- import gradio as gr
4
- from pygm_rrwm import pygm_rrwm
5
-
6
-
7
- PYGM_IMG_DEFAULT_PATH = "src/pygm_default.png"
8
- PYGM_SOLUTION_1_PATH = "src/pygm_image_1.png"
9
- PYGM_SOLUTION_2_PATH = "src/pygm_image_2.png"
10
-
11
-
12
- def _handle_pygm_solve(
13
- img_1_path: str,
14
- img_2_path: str,
15
- kpts1_path: str,
16
- kpts2_path: str,
17
- ):
18
- if img_1_path is None:
19
- raise gr.Error("Please upload file completely!")
20
- if img_2_path is None:
21
- raise gr.Error("Please upload file completely!")
22
- if kpts1_path is None:
23
- raise gr.Error("Please upload file completely!")
24
- if kpts1_path is None:
25
- raise gr.Error("Please upload file completely!")
26
-
27
- start_time = time.time()
28
- pygm_rrwm(
29
- img1_path=img_1_path,
30
- img2_path=img_2_path,
31
- kpts1_path=kpts1_path,
32
- kpts2_path=kpts2_path,
33
- output_path="src",
34
- filename="pygm_image"
35
- )
36
- solved_time = time.time() - start_time
37
-
38
- message = "Successfully solve the TSP problem, using time ({:.3f}s).".format(solved_time)
39
-
40
- return message, PYGM_SOLUTION_1_PATH, PYGM_SOLUTION_2_PATH
41
-
42
-
43
- def handle_pygm_solve(
44
- img_1_path: str,
45
- img_2_path: str,
46
- kpts1_path: str,
47
- kpts2_path: str,
48
- ):
49
- try:
50
- message = _handle_pygm_solve(
51
- img_1_path=img_1_path,
52
- img_2_path=img_2_path,
53
- kpts1_path=kpts1_path,
54
- kpts2_path=kpts2_path,
55
- )
56
- return message
57
- except Exception as e:
58
- message = str(e)
59
- return message, PYGM_SOLUTION_1_PATH, PYGM_SOLUTION_2_PATH
60
-
61
-
62
- def handle_pygm_clear():
63
- shutil.copy(
64
- src=PYGM_IMG_DEFAULT_PATH,
65
- dst=PYGM_SOLUTION_1_PATH
66
- )
67
- shutil.copy(
68
- src=PYGM_IMG_DEFAULT_PATH,
69
- dst=PYGM_SOLUTION_2_PATH
70
- )
71
-
72
- message = "successfully clear the files!"
73
- return message, PYGM_SOLUTION_1_PATH, PYGM_SOLUTION_2_PATH
74
-
75
-
76
- def convert_image_path_to_bytes(image_path):
77
- with open(image_path, "rb") as f:
78
- image_bytes = f.read()
79
- return image_bytes
80
-
81
-
82
- with gr.Blocks() as pygm_page:
83
-
84
- gr.Markdown(
85
- '''
86
- This space displays the solution to the Graph Matching problem.
87
- ## How to use this Space?
88
- - Upload a '.pygm' file from pygmlib .
89
- - The images of the TSP problem and solution will be shown after you click the solve button.
90
- - Click the 'clear' button to clear all the files.
91
- '''
92
- )
93
-
94
- with gr.Row(variant="panel"):
95
- with gr.Column(scale=2):
96
- with gr.Row():
97
- pygm_img_1 = gr.File(
98
- label="Upload .png File",
99
- file_types=[".png"],
100
- min_width=40,
101
- )
102
- pygm_img_2 = gr.File(
103
- label="Upload .png File",
104
- file_types=[".png"],
105
- min_width=40,
106
- )
107
- with gr.Row():
108
- pygm_kpts_1 = gr.File(
109
- label="Upload .mat File",
110
- file_types=[".mat"],
111
- min_width=40,
112
- )
113
- pygm_kpts_2 = gr.File(
114
- label="Upload .mat File",
115
- file_types=[".mat"],
116
- min_width=40,
117
- )
118
- info = gr.Textbox(
119
- value="",
120
- label="Log",
121
- scale=4,
122
- )
123
- with gr.Column(scale=2):
124
- pygm_solution_1 = gr.Image(
125
- value=PYGM_SOLUTION_1_PATH,
126
- type="filepath",
127
- label="Original Images"
128
- )
129
- pygm_solution_2 = gr.Image(
130
- value=PYGM_SOLUTION_2_PATH,
131
- type="filepath",
132
- label="Graph Matching Results"
133
- )
134
- with gr.Row():
135
- with gr.Column(scale=1, min_width=100):
136
- solve_button = gr.Button(
137
- value="Solve",
138
- variant="primary",
139
- scale=1
140
- )
141
- with gr.Column(scale=1, min_width=100):
142
- clear_button = gr.Button(
143
- "Clear",
144
- variant="secondary",
145
- scale=1
146
- )
147
- with gr.Column(scale=8):
148
- pass
149
-
150
- solve_button.click(
151
- handle_pygm_solve,
152
- [pygm_img_1, pygm_img_2, pygm_kpts_1, pygm_kpts_2],
153
- outputs=[info, pygm_solution_1, pygm_solution_2]
154
- )
155
-
156
- clear_button.click(
157
- handle_pygm_clear,
158
- inputs=None,
159
- outputs=[info, pygm_solution_1, pygm_solution_2]
160
- )
161
-
162
-
163
- if __name__ == "__main__":
164
  pygm_page.launch(debug = True)
 
1
+ import time
2
+ import shutil
3
+ import gradio as gr
4
+ from pygm_rrwm import pygm_rrwm
5
+
6
+
7
+ PYGM_IMG_DEFAULT_PATH = "src/pygm_default.png"
8
+ PYGM_SOLUTION_1_PATH = "src/pygm_image_1.png"
9
+ PYGM_SOLUTION_2_PATH = "src/pygm_image_2.png"
10
+
11
+
12
+ def _handle_pygm_solve(
13
+ img_1_path: str,
14
+ img_2_path: str,
15
+ kpts1_path: str,
16
+ kpts2_path: str,
17
+ ):
18
+ if img_1_path is None:
19
+ raise gr.Error("Please upload file completely!")
20
+ if img_2_path is None:
21
+ raise gr.Error("Please upload file completely!")
22
+ if kpts1_path is None:
23
+ raise gr.Error("Please upload file completely!")
24
+ if kpts1_path is None:
25
+ raise gr.Error("Please upload file completely!")
26
+
27
+ start_time = time.time()
28
+ pygm_rrwm(
29
+ img1_path=img_1_path,
30
+ img2_path=img_2_path,
31
+ kpts1_path=kpts1_path,
32
+ kpts2_path=kpts2_path,
33
+ output_path="src",
34
+ filename="pygm_image"
35
+ )
36
+ solved_time = time.time() - start_time
37
+
38
+ message = "Successfully solve the TSP problem, using time ({:.3f}s).".format(solved_time)
39
+
40
+ return message, PYGM_SOLUTION_1_PATH, PYGM_SOLUTION_2_PATH
41
+
42
+
43
+ def handle_pygm_solve(
44
+ img_1_path: str,
45
+ img_2_path: str,
46
+ kpts1_path: str,
47
+ kpts2_path: str,
48
+ ):
49
+ try:
50
+ message = _handle_pygm_solve(
51
+ img_1_path=img_1_path,
52
+ img_2_path=img_2_path,
53
+ kpts1_path=kpts1_path,
54
+ kpts2_path=kpts2_path,
55
+ )
56
+ return message
57
+ except Exception as e:
58
+ message = str(e)
59
+ return message, PYGM_SOLUTION_1_PATH, PYGM_SOLUTION_2_PATH
60
+
61
+
62
+ def handle_pygm_clear():
63
+ shutil.copy(
64
+ src=PYGM_IMG_DEFAULT_PATH,
65
+ dst=PYGM_SOLUTION_1_PATH
66
+ )
67
+ shutil.copy(
68
+ src=PYGM_IMG_DEFAULT_PATH,
69
+ dst=PYGM_SOLUTION_2_PATH
70
+ )
71
+
72
+ message = "successfully clear the files!"
73
+ return message, PYGM_SOLUTION_1_PATH, PYGM_SOLUTION_2_PATH
74
+
75
+
76
+ def convert_image_path_to_bytes(image_path):
77
+ with open(image_path, "rb") as f:
78
+ image_bytes = f.read()
79
+ return image_bytes
80
+
81
+
82
+ with gr.Blocks() as pygm_page:
83
+ gr.Markdown(
84
+ '''
85
+ This space displays the solution to the Graph Matching problem.
86
+ ## How to use this Space?
87
+ - Upload a '.pygm' file from pygmlib .
88
+ - The images of the TSP problem and solution will be shown after you click the solve button.
89
+ - Click the 'clear' button to clear all the files.
90
+ '''
91
+ )
92
+ with gr.Row(variant="panel"):
93
+ with gr.Column(scale=2):
94
+ with gr.Row():
95
+ pygm_img_1 = gr.File(
96
+ label="Upload .png File",
97
+ file_types=[".png"],
98
+ min_width=40,
99
+ )
100
+ pygm_img_2 = gr.File(
101
+ label="Upload .png File",
102
+ file_types=[".png"],
103
+ min_width=40,
104
+ )
105
+ with gr.Row():
106
+ pygm_kpts_1 = gr.File(
107
+ label="Upload .mat File",
108
+ file_types=[".mat"],
109
+ min_width=40,
110
+ )
111
+ pygm_kpts_2 = gr.File(
112
+ label="Upload .mat File",
113
+ file_types=[".mat"],
114
+ min_width=40,
115
+ )
116
+ info = gr.Textbox(
117
+ value="",
118
+ label="Log",
119
+ scale=4,
120
+ )
121
+ with gr.Column(scale=2):
122
+ pygm_solution_1 = gr.Image(
123
+ value=PYGM_SOLUTION_1_PATH,
124
+ type="filepath",
125
+ label="Original Images"
126
+ )
127
+ pygm_solution_2 = gr.Image(
128
+ value=PYGM_SOLUTION_2_PATH,
129
+ type="filepath",
130
+ label="Graph Matching Results"
131
+ )
132
+ with gr.Row():
133
+ with gr.Column(scale=1, min_width=100):
134
+ solve_button = gr.Button(
135
+ value="Solve",
136
+ variant="primary",
137
+ scale=1
138
+ )
139
+ with gr.Column(scale=1, min_width=100):
140
+ clear_button = gr.Button(
141
+ "Clear",
142
+ variant="secondary",
143
+ scale=1
144
+ )
145
+ with gr.Column(scale=8):
146
+ pass
147
+
148
+ solve_button.click(
149
+ handle_pygm_solve,
150
+ [pygm_img_1, pygm_img_2, pygm_kpts_1, pygm_kpts_2],
151
+ outputs=[info, pygm_solution_1, pygm_solution_2]
152
+ )
153
+
154
+ clear_button.click(
155
+ handle_pygm_clear,
156
+ inputs=None,
157
+ outputs=[info, pygm_solution_1, pygm_solution_2]
158
+ )
159
+
160
+
161
+ if __name__ == "__main__":
 
 
162
  pygm_page.launch(debug = True)