Spaces:
Runtime error
Runtime error
File size: 4,764 Bytes
32603e9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 164 |
import time
import shutil
import gradio as gr
from pygm_rrwm import pygm_rrwm
PYGM_IMG_DEFAULT_PATH = "src/pygm_default.png"
PYGM_SOLUTION_1_PATH = "src/pygm_image_1.png"
PYGM_SOLUTION_2_PATH = "src/pygm_image_2.png"
def _handle_pygm_solve(
img_1_path: str,
img_2_path: str,
kpts1_path: str,
kpts2_path: str,
):
if img_1_path is None:
raise gr.Error("Please upload file completely!")
if img_2_path is None:
raise gr.Error("Please upload file completely!")
if kpts1_path is None:
raise gr.Error("Please upload file completely!")
if kpts1_path is None:
raise gr.Error("Please upload file completely!")
start_time = time.time()
pygm_rrwm(
img1_path=img_1_path,
img2_path=img_2_path,
kpts1_path=kpts1_path,
kpts2_path=kpts2_path,
output_path="src",
filename="pygm_image"
)
solved_time = time.time() - start_time
message = "Successfully solve the TSP problem, using time ({:.3f}s).".format(solved_time)
return message, PYGM_SOLUTION_1_PATH, PYGM_SOLUTION_2_PATH
def handle_pygm_solve(
img_1_path: str,
img_2_path: str,
kpts1_path: str,
kpts2_path: str,
):
try:
message = _handle_pygm_solve(
img_1_path=img_1_path,
img_2_path=img_2_path,
kpts1_path=kpts1_path,
kpts2_path=kpts2_path,
)
return message
except Exception as e:
message = str(e)
return message, PYGM_SOLUTION_1_PATH, PYGM_SOLUTION_2_PATH
def handle_pygm_clear():
shutil.copy(
src=PYGM_IMG_DEFAULT_PATH,
dst=PYGM_SOLUTION_1_PATH
)
shutil.copy(
src=PYGM_IMG_DEFAULT_PATH,
dst=PYGM_SOLUTION_2_PATH
)
message = "successfully clear the files!"
return message, PYGM_SOLUTION_1_PATH, PYGM_SOLUTION_2_PATH
def convert_image_path_to_bytes(image_path):
with open(image_path, "rb") as f:
image_bytes = f.read()
return image_bytes
with gr.Blocks() as pygm_page:
gr.Markdown(
'''
This space displays the solution to the Graph Matching problem.
## How to use this Space?
- Upload a '.pygm' file from pygmlib .
- The images of the TSP problem and solution will be shown after you click the solve button.
- Click the 'clear' button to clear all the files.
'''
)
with gr.Row(variant="panel"):
with gr.Column(scale=2):
with gr.Row():
pygm_img_1 = gr.File(
label="Upload .png File",
file_types=[".png"],
min_width=40,
)
pygm_img_2 = gr.File(
label="Upload .png File",
file_types=[".png"],
min_width=40,
)
with gr.Row():
pygm_kpts_1 = gr.File(
label="Upload .mat File",
file_types=[".mat"],
min_width=40,
)
pygm_kpts_2 = gr.File(
label="Upload .mat File",
file_types=[".mat"],
min_width=40,
)
info = gr.Textbox(
value="",
label="Log",
scale=4,
)
with gr.Column(scale=2):
pygm_solution_1 = gr.Image(
value=PYGM_SOLUTION_1_PATH,
type="filepath",
label="Original Images"
)
pygm_solution_2 = gr.Image(
value=PYGM_SOLUTION_2_PATH,
type="filepath",
label="Graph Matching Results"
)
with gr.Row():
with gr.Column(scale=1, min_width=100):
solve_button = gr.Button(
value="Solve",
variant="primary",
scale=1
)
with gr.Column(scale=1, min_width=100):
clear_button = gr.Button(
"Clear",
variant="secondary",
scale=1
)
with gr.Column(scale=8):
pass
solve_button.click(
handle_pygm_solve,
[pygm_img_1, pygm_img_2, pygm_kpts_1, pygm_kpts_2],
outputs=[info, pygm_solution_1, pygm_solution_2]
)
clear_button.click(
handle_pygm_clear,
inputs=None,
outputs=[info, pygm_solution_1, pygm_solution_2]
)
if __name__ == "__main__":
pygm_page.launch(debug = True) |