Spaces:
Sleeping
Sleeping
fix
Browse files- __pycache__/app.cpython-312.pyc +0 -0
- app.py +16 -9
- js/interactive_grid.js +49 -2
__pycache__/app.cpython-312.pyc
CHANGED
|
Binary files a/__pycache__/app.cpython-312.pyc and b/__pycache__/app.cpython-312.pyc differ
|
|
|
app.py
CHANGED
|
@@ -212,7 +212,7 @@ def process_example(image_path_bef, image_path_aft):
|
|
| 212 |
image_aft_grid, _, _ = add_grid_to_image(image_path_aft, 14)
|
| 213 |
return image_bef_grid, image_aft_grid # Reset selected cells and store original image
|
| 214 |
|
| 215 |
-
def
|
| 216 |
w, h = Image.open(image_path).convert('RGB').size
|
| 217 |
return w, h
|
| 218 |
|
|
@@ -246,14 +246,21 @@ with gr.Blocks() as demo:
|
|
| 246 |
|
| 247 |
with gr.Column(scale=1):
|
| 248 |
|
| 249 |
-
|
| 250 |
<div id="container">
|
| 251 |
-
<canvas id="
|
| 252 |
</div>
|
| 253 |
"""
|
| 254 |
|
| 255 |
-
|
| 256 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
|
| 259 |
# image_display_with_grid_bef = gr.Image(type="pil", label="Before Image with Grid")
|
|
@@ -279,9 +286,9 @@ with gr.Blocks() as demo:
|
|
| 279 |
examples=[["data/images/CLEVR_default_000572.png", "data/images/CLEVR_semantic_000572.png"],
|
| 280 |
["data/images/CLEVR_default_003339.png", "data/images/CLEVR_semantic_003339.png"]],
|
| 281 |
inputs=[image_bef, image_aft],
|
| 282 |
-
|
| 283 |
label="Example Images",
|
| 284 |
-
|
| 285 |
examples_per_page=5
|
| 286 |
)
|
| 287 |
|
|
@@ -316,14 +323,14 @@ with gr.Blocks() as demo:
|
|
| 316 |
fn=None,
|
| 317 |
inputs=[image_bef],
|
| 318 |
outputs=[],
|
| 319 |
-
_js="(image) => {
|
| 320 |
)
|
| 321 |
|
| 322 |
image_aft.change(
|
| 323 |
fn=None,
|
| 324 |
inputs=[image_aft],
|
| 325 |
outputs=[],
|
| 326 |
-
_js="(image) => {
|
| 327 |
)
|
| 328 |
|
| 329 |
|
|
|
|
| 212 |
image_aft_grid, _, _ = add_grid_to_image(image_path_aft, 14)
|
| 213 |
return image_bef_grid, image_aft_grid # Reset selected cells and store original image
|
| 214 |
|
| 215 |
+
def get_image_size(image_path):
|
| 216 |
w, h = Image.open(image_path).convert('RGB').size
|
| 217 |
return w, h
|
| 218 |
|
|
|
|
| 246 |
|
| 247 |
with gr.Column(scale=1):
|
| 248 |
|
| 249 |
+
html_text_bef = f"""
|
| 250 |
<div id="container">
|
| 251 |
+
<canvas id="before" width="{width}" height="{height}"></canvas><img id="canvas-background" style="display:none;"/>
|
| 252 |
</div>
|
| 253 |
"""
|
| 254 |
|
| 255 |
+
html_text_aft = f"""
|
| 256 |
+
<div id="container">
|
| 257 |
+
<canvas id="after" width="{width}" height="{height}"></canvas><img id="canvas-background" style="display:none;"/>
|
| 258 |
+
</div>
|
| 259 |
+
"""
|
| 260 |
+
|
| 261 |
+
html_bef = gr.HTML(html_text_bef)
|
| 262 |
+
gr.HTML("<br>")
|
| 263 |
+
html_aft = gr.HTML(html_text_aft)
|
| 264 |
|
| 265 |
|
| 266 |
# image_display_with_grid_bef = gr.Image(type="pil", label="Before Image with Grid")
|
|
|
|
| 286 |
examples=[["data/images/CLEVR_default_000572.png", "data/images/CLEVR_semantic_000572.png"],
|
| 287 |
["data/images/CLEVR_default_003339.png", "data/images/CLEVR_semantic_003339.png"]],
|
| 288 |
inputs=[image_bef, image_aft],
|
| 289 |
+
outputs=[width, height],
|
| 290 |
label="Example Images",
|
| 291 |
+
fn=get_image_size,
|
| 292 |
examples_per_page=5
|
| 293 |
)
|
| 294 |
|
|
|
|
| 323 |
fn=None,
|
| 324 |
inputs=[image_bef],
|
| 325 |
outputs=[],
|
| 326 |
+
_js="(image) => { initializeEditorBefore(); importBackground(image); return []; }",
|
| 327 |
)
|
| 328 |
|
| 329 |
image_aft.change(
|
| 330 |
fn=None,
|
| 331 |
inputs=[image_aft],
|
| 332 |
outputs=[],
|
| 333 |
+
_js="(image) => { initializeEditorAfter(); importBackground(image); return []; }",
|
| 334 |
)
|
| 335 |
|
| 336 |
|
js/interactive_grid.js
CHANGED
|
@@ -68,7 +68,7 @@ function drawGrid() {
|
|
| 68 |
}
|
| 69 |
|
| 70 |
|
| 71 |
-
function
|
| 72 |
console.log("initializeEditor");
|
| 73 |
|
| 74 |
if (isInitialized) {
|
|
@@ -77,7 +77,54 @@ function initializeEditor() {
|
|
| 77 |
isInitialized = true;
|
| 78 |
|
| 79 |
image = document.getElementById('image');
|
| 80 |
-
canvas = document.getElementById('
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
ctx = canvas.getContext('2d');
|
| 82 |
|
| 83 |
// Add click event listener to canvas
|
|
|
|
| 68 |
}
|
| 69 |
|
| 70 |
|
| 71 |
+
function initializeEditorBefore() {
|
| 72 |
console.log("initializeEditor");
|
| 73 |
|
| 74 |
if (isInitialized) {
|
|
|
|
| 77 |
isInitialized = true;
|
| 78 |
|
| 79 |
image = document.getElementById('image');
|
| 80 |
+
canvas = document.getElementById('before');
|
| 81 |
+
ctx = canvas.getContext('2d');
|
| 82 |
+
|
| 83 |
+
// Add click event listener to canvas
|
| 84 |
+
canvas.addEventListener('mousedown', handleMouseDown);
|
| 85 |
+
canvas.addEventListener('mousemove', handleMouseMove);
|
| 86 |
+
canvas.addEventListener('mouseup', handleMouseUp);
|
| 87 |
+
canvas.addEventListener('mouseleave', handleMouseLeave);
|
| 88 |
+
|
| 89 |
+
cellSize = canvas.width / gridSize;
|
| 90 |
+
|
| 91 |
+
canvas.addEventListener('click', (event) => {
|
| 92 |
+
const rect = canvas.getBoundingClientRect();
|
| 93 |
+
const scaleX = canvas.width / rect.width;
|
| 94 |
+
const scaleY = canvas.height / rect.height;
|
| 95 |
+
const x = (event.clientX - rect.left) * scaleX;
|
| 96 |
+
const y = (event.clientY - rect.top) * scaleY;
|
| 97 |
+
const row = Math.floor(y / cellSize);
|
| 98 |
+
const col = Math.floor(x / cellSize);
|
| 99 |
+
|
| 100 |
+
// If the cell is already selected, it's always allowed to deselect it
|
| 101 |
+
if (grid[row][col]) {
|
| 102 |
+
grid[row][col] = false;
|
| 103 |
+
selectedCells--; // Decrement the selected cell count
|
| 104 |
+
} else {
|
| 105 |
+
// Only select a new cell if less than 50 cells are already selected
|
| 106 |
+
if (selectedCells < 50) {
|
| 107 |
+
grid[row][col] = true;
|
| 108 |
+
selectedCells++; // Increment the selected cell count
|
| 109 |
+
}
|
| 110 |
+
}
|
| 111 |
+
drawGrid();
|
| 112 |
+
});
|
| 113 |
+
|
| 114 |
+
drawGrid();
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
function initializeEditorAfter() {
|
| 119 |
+
console.log("initializeEditor");
|
| 120 |
+
|
| 121 |
+
if (isInitialized) {
|
| 122 |
+
return;
|
| 123 |
+
}
|
| 124 |
+
isInitialized = true;
|
| 125 |
+
|
| 126 |
+
image = document.getElementById('image');
|
| 127 |
+
canvas = document.getElementById('after');
|
| 128 |
ctx = canvas.getContext('2d');
|
| 129 |
|
| 130 |
// Add click event listener to canvas
|