Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
-
|
| 89 |
-
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
)
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 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)
|