forked from plotly/dash-canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp4_measure_length.py
68 lines (56 loc) · 1.89 KB
/
app4_measure_length.py
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
import numpy as np
import pandas as pd
from skimage import io
import dash
from dash.dependencies import Input, Output, State
import dash_html_components as html
import dash_core_components as dcc
import dash_table
import dash_canvas
from dash_canvas.utils.io_utils import (image_string_to_PILImage,
array_to_data_url)
from dash_canvas.utils.parse_json import parse_jsonstring_line
def title():
return "Measure lengths"
def description():
return "Draw lines on objects to measure their lengths."
filename = 'https://upload.wikimedia.org/wikipedia/commons/a/a4/MRI_T2_Brain_axial_image.jpg'
img = io.imread(filename)[..., 0].T
height, width = img.shape
canvas_width = 600
canvas_height = round(height * canvas_width / width)
scale = canvas_width / width
list_columns = ['length', 'width', 'height']
columns = [{"name": i, "id": i} for i in list_columns]
layout = html.Div([
html.Div([
dash_canvas.DashCanvas(
id='canvas-line',
width=canvas_width,
height=canvas_height,
scale=scale,
lineWidth=2,
lineColor='red',
tool='line',
image_content=array_to_data_url(img),
goButtonTitle='Measure',
),
], className="seven columns"),
html.Div([
html.H2('Draw lines and measure object lengths'),
html.H4(children="Objects properties"),
html.Div(id='sh_x', hidden=True),
dash_table.DataTable(
id='table-line',
columns=columns,
editable=True,
)
], className="four columns"),
])
def callbacks(app):
@app.callback(Output('table-line', 'data'),
[Input('canvas-line', 'json_data')])
def show_string(string):
props = parse_jsonstring_line(string)
df = pd.DataFrame(props, columns=list_columns)
return df.to_dict("records")