Spaces:
Running
Running
Marlin Lee commited on
Commit ·
1be93bc
1
Parent(s): 61ba489
Sync explorer_app.py and clip_utils.py from main repo
Browse files- scripts/explorer_app.py +31 -5
scripts/explorer_app.py
CHANGED
|
@@ -63,9 +63,10 @@ from clip_utils import load_clip, compute_text_embeddings
|
|
| 63 |
|
| 64 |
from bokeh.io import curdoc
|
| 65 |
from bokeh.layouts import column, row
|
|
|
|
| 66 |
from bokeh.models import (
|
| 67 |
ColumnDataSource, HoverTool, Div, Select, TextInput, Button,
|
| 68 |
-
DataTable, TableColumn, NumberFormatter, IntEditor,
|
| 69 |
Slider, Toggle, CustomJS,
|
| 70 |
)
|
| 71 |
from bokeh.plotting import figure
|
|
@@ -1337,16 +1338,41 @@ patch_bg_source = ColumnDataSource(data=dict(
|
|
| 1337 |
image=[], x=[0], y=[0], dw=[patch_grid], dh=[patch_grid],
|
| 1338 |
))
|
| 1339 |
|
| 1340 |
-
_box_select = BoxSelectTool(mode="append")
|
| 1341 |
patch_fig = figure(
|
| 1342 |
width=_PATCH_FIG_PX, height=_PATCH_FIG_PX,
|
| 1343 |
x_range=(0, patch_grid), y_range=(0, patch_grid),
|
| 1344 |
-
tools=["tap",
|
| 1345 |
-
|
| 1346 |
-
title="Click or drag to select patches | shift+click to add individually",
|
| 1347 |
toolbar_location="above",
|
| 1348 |
visible=False,
|
| 1349 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1350 |
patch_fig.image_rgba(
|
| 1351 |
source=patch_bg_source,
|
| 1352 |
image='image', x='x', y='y', dw='dw', dh='dh',
|
|
|
|
| 63 |
|
| 64 |
from bokeh.io import curdoc
|
| 65 |
from bokeh.layouts import column, row
|
| 66 |
+
from bokeh.events import MouseMove
|
| 67 |
from bokeh.models import (
|
| 68 |
ColumnDataSource, HoverTool, Div, Select, TextInput, Button,
|
| 69 |
+
DataTable, TableColumn, NumberFormatter, IntEditor,
|
| 70 |
Slider, Toggle, CustomJS,
|
| 71 |
)
|
| 72 |
from bokeh.plotting import figure
|
|
|
|
| 1338 |
image=[], x=[0], y=[0], dw=[patch_grid], dh=[patch_grid],
|
| 1339 |
))
|
| 1340 |
|
|
|
|
| 1341 |
patch_fig = figure(
|
| 1342 |
width=_PATCH_FIG_PX, height=_PATCH_FIG_PX,
|
| 1343 |
x_range=(0, patch_grid), y_range=(0, patch_grid),
|
| 1344 |
+
tools=["tap", "reset"],
|
| 1345 |
+
title="Click or drag to paint patch selection",
|
|
|
|
| 1346 |
toolbar_location="above",
|
| 1347 |
visible=False,
|
| 1348 |
)
|
| 1349 |
+
|
| 1350 |
+
# Paint-on-drag selection: any patch the mouse passes over while the button
|
| 1351 |
+
# is held gets added to the selection. We track button state with a
|
| 1352 |
+
# document-level mousedown/mouseup listener (set up lazily on first move).
|
| 1353 |
+
_paint_js = CustomJS(args=dict(source=patch_grid_source, pg=patch_grid), code="""
|
| 1354 |
+
if (!window._patch_paint_init) {
|
| 1355 |
+
window._patch_paint_init = true;
|
| 1356 |
+
window._patch_btn_held = false;
|
| 1357 |
+
document.addEventListener('mousedown', () => { window._patch_btn_held = true; });
|
| 1358 |
+
document.addEventListener('mouseup', () => { window._patch_btn_held = false; });
|
| 1359 |
+
}
|
| 1360 |
+
if (!window._patch_btn_held) return;
|
| 1361 |
+
|
| 1362 |
+
const x = cb_obj.x, y = cb_obj.y;
|
| 1363 |
+
if (x === null || y === null || x < 0 || x >= pg || y < 0 || y >= pg) return;
|
| 1364 |
+
|
| 1365 |
+
const col = Math.floor(x);
|
| 1366 |
+
const row = pg - 1 - Math.floor(y);
|
| 1367 |
+
const flat_idx = row * pg + col;
|
| 1368 |
+
|
| 1369 |
+
const sel = source.selected.indices.slice();
|
| 1370 |
+
if (sel.indexOf(flat_idx) === -1) {
|
| 1371 |
+
sel.push(flat_idx);
|
| 1372 |
+
source.selected.indices = sel;
|
| 1373 |
+
}
|
| 1374 |
+
""")
|
| 1375 |
+
patch_fig.js_on_event(MouseMove, _paint_js)
|
| 1376 |
patch_fig.image_rgba(
|
| 1377 |
source=patch_bg_source,
|
| 1378 |
image='image', x='x', y='y', dw='dw', dh='dh',
|