Skip to content

Commit

Permalink
feat: Point move for polygon. #1597
Browse files Browse the repository at this point in the history
  • Loading branch information
mturoci committed Sep 28, 2022
1 parent 44bf0a1 commit 0cc8383
Show file tree
Hide file tree
Showing 9 changed files with 274 additions and 47 deletions.
8 changes: 7 additions & 1 deletion py/examples/image_annotator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


@app('/demo')
async def serve(q: Q):
async def serve(q=Q):
if q.args.annotator is not None:
q.page['example'].items = [
ui.text(f'annotator={q.args.annotator}'),
Expand All @@ -25,6 +25,12 @@ async def serve(q: Q):
],
items=[
ui.image_annotator_item(shape=ui.image_annotator_rect(x1=649, y1=393, x2=383, y2=25), tag='p'),
ui.image_annotator_item(tag='p', shape=ui.image_annotator_polygon([
ui.image_annotator_point(x=828.2142857142857, y=135),
ui.image_annotator_point(x=731.7857142857142, y=212.14285714285714),
ui.image_annotator_point(x=890.3571428571429, y=354.6428571428571),
ui.image_annotator_point(x=950.3571428571429, y=247.5)
])),
],
),
ui.button(name='submit', label='Submit', primary=True)
Expand Down
78 changes: 78 additions & 0 deletions py/h2o_wave/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6411,32 +6411,110 @@ def load(__d: Dict) -> 'ImageAnnotatorRect':
)


class ImageAnnotatorPoint:
"""No documentation available.
"""
def __init__(
self,
x: float,
y: float,
):
_guard_scalar('ImageAnnotatorPoint.x', x, (float, int,), False, False, False)
_guard_scalar('ImageAnnotatorPoint.y', y, (float, int,), False, False, False)
self.x = x
"""`x` coordinate of the point."""
self.y = y
"""`y` coordinate of the point."""

def dump(self) -> Dict:
"""Returns the contents of this object as a dict."""
_guard_scalar('ImageAnnotatorPoint.x', self.x, (float, int,), False, False, False)
_guard_scalar('ImageAnnotatorPoint.y', self.y, (float, int,), False, False, False)
return _dump(
x=self.x,
y=self.y,
)

@staticmethod
def load(__d: Dict) -> 'ImageAnnotatorPoint':
"""Creates an instance of this class using the contents of a dict."""
__d_x: Any = __d.get('x')
_guard_scalar('ImageAnnotatorPoint.x', __d_x, (float, int,), False, False, False)
__d_y: Any = __d.get('y')
_guard_scalar('ImageAnnotatorPoint.y', __d_y, (float, int,), False, False, False)
x: float = __d_x
y: float = __d_y
return ImageAnnotatorPoint(
x,
y,
)


class ImageAnnotatorPolygon:
"""Create a polygon annotation shape.
"""
def __init__(
self,
items: List[ImageAnnotatorPoint],
):
_guard_vector('ImageAnnotatorPolygon.items', items, (ImageAnnotatorPoint,), False, False, False)
self.items = items
"""List of points of the polygon."""

def dump(self) -> Dict:
"""Returns the contents of this object as a dict."""
_guard_vector('ImageAnnotatorPolygon.items', self.items, (ImageAnnotatorPoint,), False, False, False)
return _dump(
items=[__e.dump() for __e in self.items],
)

@staticmethod
def load(__d: Dict) -> 'ImageAnnotatorPolygon':
"""Creates an instance of this class using the contents of a dict."""
__d_items: Any = __d.get('items')
_guard_vector('ImageAnnotatorPolygon.items', __d_items, (dict,), False, False, False)
items: List[ImageAnnotatorPoint] = [ImageAnnotatorPoint.load(__e) for __e in __d_items]
return ImageAnnotatorPolygon(
items,
)


class ImageAnnotatorShape:
"""Create a shape to be rendered as an annotation on an image annotator.
"""
def __init__(
self,
rect: Optional[ImageAnnotatorRect] = None,
polygon: Optional[ImageAnnotatorPolygon] = None,
):
_guard_scalar('ImageAnnotatorShape.rect', rect, (ImageAnnotatorRect,), False, True, False)
_guard_scalar('ImageAnnotatorShape.polygon', polygon, (ImageAnnotatorPolygon,), False, True, False)
self.rect = rect
"""No documentation available."""
self.polygon = polygon
"""No documentation available."""

def dump(self) -> Dict:
"""Returns the contents of this object as a dict."""
_guard_scalar('ImageAnnotatorShape.rect', self.rect, (ImageAnnotatorRect,), False, True, False)
_guard_scalar('ImageAnnotatorShape.polygon', self.polygon, (ImageAnnotatorPolygon,), False, True, False)
return _dump(
rect=None if self.rect is None else self.rect.dump(),
polygon=None if self.polygon is None else self.polygon.dump(),
)

@staticmethod
def load(__d: Dict) -> 'ImageAnnotatorShape':
"""Creates an instance of this class using the contents of a dict."""
__d_rect: Any = __d.get('rect')
_guard_scalar('ImageAnnotatorShape.rect', __d_rect, (dict,), False, True, False)
__d_polygon: Any = __d.get('polygon')
_guard_scalar('ImageAnnotatorShape.polygon', __d_polygon, (dict,), False, True, False)
rect: Optional[ImageAnnotatorRect] = None if __d_rect is None else ImageAnnotatorRect.load(__d_rect)
polygon: Optional[ImageAnnotatorPolygon] = None if __d_polygon is None else ImageAnnotatorPolygon.load(__d_polygon)
return ImageAnnotatorShape(
rect,
polygon,
)


Expand Down
33 changes: 33 additions & 0 deletions py/h2o_wave/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2391,6 +2391,39 @@ def image_annotator_rect(
))


def image_annotator_point(
x: float,
y: float,
) -> ImageAnnotatorPoint:
"""No documentation available.
Args:
x: `x` coordinate of the point.
y: `y` coordinate of the point.
Returns:
A `h2o_wave.types.ImageAnnotatorPoint` instance.
"""
return ImageAnnotatorPoint(
x,
y,
)


def image_annotator_polygon(
items: List[ImageAnnotatorPoint],
) -> ImageAnnotatorShape:
"""Create a polygon annotation shape.
Args:
items: List of points of the polygon.
Returns:
A `h2o_wave.types.ImageAnnotatorPolygon` instance.
"""
return ImageAnnotatorShape(polygon=ImageAnnotatorPolygon(
items,
))


def image_annotator_item(
shape: ImageAnnotatorShape,
tag: str,
Expand Down
32 changes: 32 additions & 0 deletions r/R/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -2787,6 +2787,38 @@ ui_image_annotator_rect <- function(
return(.o)
}

#' No documentation available.
#'
#' @param x `x` coordinate of the point.
#' @param y `y` coordinate of the point.
#' @return A ImageAnnotatorPoint instance.
#' @export
ui_image_annotator_point <- function(
x,
y) {
.guard_scalar("x", "numeric", x)
.guard_scalar("y", "numeric", y)
.o <- list(
x=x,
y=y)
class(.o) <- append(class(.o), c(.wave_obj, "WaveImageAnnotatorPoint"))
return(.o)
}

#' Create a polygon annotation shape.
#'
#' @param items List of points of the polygon.
#' @return A ImageAnnotatorPolygon instance.
#' @export
ui_image_annotator_polygon <- function(
items) {
.guard_vector("items", "WaveImageAnnotatorPoint", items)
.o <- list(polygon=list(
items=items))
class(.o) <- append(class(.o), c(.wave_obj, "WaveImageAnnotatorShape"))
return(.o)
}

#' Create an annotator item with initial selected tags or no tag for plaintext.
#'
#' @param shape The annotation shape.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,19 @@
<option name="Python" value="true"/>
</context>
</template>
<template name="w_image_annotator_point" value="ui.image_annotator_point(x=$x$,y=$y$),$END$" description="Create a minimal Wave ImageAnnotatorPoint." toReformat="true" toShortenFQNames="true">
<variable name="x" expression="" defaultValue="&quot;None&quot;" alwaysStopAt="true"/>
<variable name="y" expression="" defaultValue="&quot;None&quot;" alwaysStopAt="true"/>
<context>
<option name="Python" value="true"/>
</context>
</template>
<template name="w_image_annotator_polygon" value="ui.image_annotator_polygon(items=[&#10; $items$ &#10;]),$END$" description="Create a minimal Wave ImageAnnotatorPolygon." toReformat="true" toShortenFQNames="true">
<variable name="items" expression="" defaultValue="" alwaysStopAt="true"/>
<context>
<option name="Python" value="true"/>
</context>
</template>
<template name="w_image_annotator_rect" value="ui.image_annotator_rect(x1=$x1$,y1=$y1$,x2=$x2$,y2=$y2$),$END$" description="Create a minimal Wave ImageAnnotatorRect." toReformat="true" toShortenFQNames="true">
<variable name="x1" expression="" defaultValue="&quot;None&quot;" alwaysStopAt="true"/>
<variable name="y1" expression="" defaultValue="&quot;None&quot;" alwaysStopAt="true"/>
Expand Down Expand Up @@ -1395,6 +1408,19 @@
<option name="Python" value="true"/>
</context>
</template>
<template name="w_full_image_annotator_point" value="ui.image_annotator_point(x=$x$,y=$y$),$END$" description="Create Wave ImageAnnotatorPoint with full attributes." toReformat="true" toShortenFQNames="true">
<variable name="x" expression="" defaultValue="&quot;None&quot;" alwaysStopAt="true"/>
<variable name="y" expression="" defaultValue="&quot;None&quot;" alwaysStopAt="true"/>
<context>
<option name="Python" value="true"/>
</context>
</template>
<template name="w_full_image_annotator_polygon" value="ui.image_annotator_polygon(items=[&#10; $items$ &#10;]),$END$" description="Create Wave ImageAnnotatorPolygon with full attributes." toReformat="true" toShortenFQNames="true">
<variable name="items" expression="" defaultValue="" alwaysStopAt="true"/>
<context>
<option name="Python" value="true"/>
</context>
</template>
<template name="w_full_image_annotator_rect" value="ui.image_annotator_rect(x1=$x1$,y1=$y1$,x2=$x2$,y2=$y2$),$END$" description="Create Wave ImageAnnotatorRect with full attributes." toReformat="true" toShortenFQNames="true">
<variable name="x1" expression="" defaultValue="&quot;None&quot;" alwaysStopAt="true"/>
<variable name="y1" expression="" defaultValue="&quot;None&quot;" alwaysStopAt="true"/>
Expand All @@ -1404,8 +1430,9 @@
<option name="Python" value="true"/>
</context>
</template>
<template name="w_full_image_annotator_shape" value="ui.image_annotator_shape(rect=$rect$),$END$" description="Create Wave ImageAnnotatorShape with full attributes." toReformat="true" toShortenFQNames="true">
<template name="w_full_image_annotator_shape" value="ui.image_annotator_shape(rect=$rect$,polygon=$polygon$),$END$" description="Create Wave ImageAnnotatorShape with full attributes." toReformat="true" toShortenFQNames="true">
<variable name="rect" expression="" defaultValue="" alwaysStopAt="true"/>
<variable name="polygon" expression="" defaultValue="" alwaysStopAt="true"/>
<context>
<option name="Python" value="true"/>
</context>
Expand Down
30 changes: 29 additions & 1 deletion tools/vscode-extension/component-snippets.json
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,20 @@
],
"description": "Create a minimal Wave Image."
},
"Wave ImageAnnotatorPoint": {
"prefix": "w_image_annotator_point",
"body": [
"ui.image_annotator_point(x=${1:None}, y=${2:None}),$0"
],
"description": "Create a minimal Wave ImageAnnotatorPoint."
},
"Wave ImageAnnotatorPolygon": {
"prefix": "w_image_annotator_polygon",
"body": [
"ui.image_annotator_polygon(items=[\n\t\t$1\t\t\n]),$0"
],
"description": "Create a minimal Wave ImageAnnotatorPolygon."
},
"Wave ImageAnnotatorRect": {
"prefix": "w_image_annotator_rect",
"body": [
Expand Down Expand Up @@ -1161,6 +1175,20 @@
],
"description": "Create a full Wave Image."
},
"Wave Full ImageAnnotatorPoint": {
"prefix": "w_full_image_annotator_point",
"body": [
"ui.image_annotator_point(x=${1:None}, y=${2:None}),$0"
],
"description": "Create a full Wave ImageAnnotatorPoint."
},
"Wave Full ImageAnnotatorPolygon": {
"prefix": "w_full_image_annotator_polygon",
"body": [
"ui.image_annotator_polygon(items=[\n\t\t$1\t\t\n]),$0"
],
"description": "Create a full Wave ImageAnnotatorPolygon."
},
"Wave Full ImageAnnotatorRect": {
"prefix": "w_full_image_annotator_rect",
"body": [
Expand All @@ -1171,7 +1199,7 @@
"Wave Full ImageAnnotatorShape": {
"prefix": "w_full_image_annotator_shape",
"body": [
"ui.image_annotator_shape(rect=$1),$0"
"ui.image_annotator_shape(rect=$1, polygon=$2),$0"
],
"description": "Create a full Wave ImageAnnotatorShape."
},
Expand Down
Loading

0 comments on commit 0cc8383

Please sign in to comment.