edgargg commited on
Commit
5c60455
·
verified ·
1 Parent(s): d3a6c80

Upload folder using huggingface_hub

Browse files
README.md CHANGED
@@ -1,19 +1,3 @@
1
- ---
2
- tags:
3
- - gradio-custom-component
4
- - gradio-template-Image
5
- - bounding box
6
- - annotator
7
- - annotate
8
- - boxes
9
- title: gradio_image_annotation V0.2.7
10
- colorFrom: yellow
11
- colorTo: green
12
- sdk: docker
13
- pinned: false
14
- license: apache-2.0
15
- short_description: A Gradio component for image annotation
16
- ---
17
 
18
  # `gradio_image_annotation`
19
  <a href="https://pypi.org/project/gradio_image_annotation/" target="_blank"><img alt="PyPI - Version" src="https://img.shields.io/pypi/v/gradio_image_annotation"></a>
@@ -551,7 +535,7 @@ bool | None
551
  <td align="left" style="width: 25%;">
552
 
553
  ```python
554
- bool
555
  ```
556
 
557
  </td>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
2
  # `gradio_image_annotation`
3
  <a href="https://pypi.org/project/gradio_image_annotation/" target="_blank"><img alt="PyPI - Version" src="https://img.shields.io/pypi/v/gradio_image_annotation"></a>
 
535
  <td align="left" style="width: 25%;">
536
 
537
  ```python
538
+ bool | None
539
  ```
540
 
541
  </td>
space.py CHANGED
@@ -3,7 +3,7 @@ import gradio as gr
3
  from app import demo as app
4
  import os
5
 
6
- _docs = {'image_annotator': {'description': 'Creates a component to annotate images with bounding boxes. The bounding boxes can be created and edited by the user or be passed by code.\nIt is also possible to predefine a set of valid classes and colors.', 'members': {'__init__': {'value': {'type': 'dict | None', 'default': 'None', 'description': "A dict or None. The dictionary must contain a key 'image' with either an URL to an image, a numpy image or a PIL image. Optionally it may contain a key 'boxes' with a list of boxes. Each box must be a dict wit the keys: 'xmin', 'ymin', 'xmax' and 'ymax' with the absolute image coordinates of the box. Optionally can also include the keys 'label' and 'color' describing the label and color of the box. Color must be a tuple of RGB values (e.g. `(255,255,255)`). Optionally can also include the keys 'orientation' with a integer between 0 and 3, describing the number of times the image is rotated by 90 degrees in frontend, the rotation is clockwise."}, 'boxes_alpha': {'type': 'float | None', 'default': 'None', 'description': 'Opacity of the bounding boxes 0 and 1.'}, 'label_list': {'type': 'list[str] | None', 'default': 'None', 'description': 'List of valid labels.'}, 'label_colors': {'type': 'list[str] | None', 'default': 'None', 'description': 'Optional list of colors for each label when `label_list` is used. Colors must be a tuple of RGB values (e.g. `(255,255,255)`).'}, 'box_min_size': {'type': 'int | None', 'default': 'None', 'description': 'Minimum valid bounding box size.'}, 'handle_size': {'type': 'int | None', 'default': 'None', 'description': 'Size of the bounding box resize handles.'}, 'box_thickness': {'type': 'int | None', 'default': 'None', 'description': 'Thickness of the bounding box outline.'}, 'box_selected_thickness': {'type': 'int | None', 'default': 'None', 'description': 'Thickness of the bounding box outline when it is selected.'}, 'disable_edit_boxes': {'type': 'bool | None', 'default': 'None', 'description': 'Disables the ability to set and edit the label and color of the boxes.'}, 'single_box': {'type': 'bool', 'default': 'False', 'description': 'If True, at most one box can be drawn.'}, 'height': {'type': 'int | str | None', 'default': 'None', 'description': 'The height of the displayed image, specified in pixels if a number is passed, or in CSS units if a string is passed.'}, 'width': {'type': 'int | str | None', 'default': 'None', 'description': 'The width of the displayed image, specified in pixels if a number is passed, or in CSS units if a string is passed.'}, 'image_mode': {'type': '"1"\n | "L"\n | "P"\n | "RGB"\n | "RGBA"\n | "CMYK"\n | "YCbCr"\n | "LAB"\n | "HSV"\n | "I"\n | "F"', 'default': '"RGB"', 'description': '"RGB" if color, or "L" if black and white. See https://pillow.readthedocs.io/en/stable/handbook/concepts.html for other supported image modes and their meaning.'}, 'sources': {'type': 'list["upload" | "webcam" | "clipboard"] | None', 'default': '["upload", "webcam", "clipboard"]', 'description': 'List of sources for the image. "upload" creates a box where user can drop an image file, "webcam" allows user to take snapshot from their webcam, "clipboard" allows users to paste an image from the clipboard. If None, defaults to ["upload", "webcam", "clipboard"].'}, 'image_type': {'type': '"numpy" | "pil" | "filepath"', 'default': '"numpy"', 'description': 'The format the image is converted before being passed into the prediction function. "numpy" converts the image to a numpy array with shape (height, width, 3) and values from 0 to 255, "pil" converts the image to a PIL image object, "filepath" passes a str path to a temporary file containing the image. If the image is SVG, the `type` is ignored and the filepath of the SVG is returned.'}, 'label': {'type': 'str | None', 'default': 'None', 'description': 'The label for this component. Appears above the component and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component is assigned to.'}, 'container': {'type': 'bool', 'default': 'True', 'description': 'If True, will place the component in a container - providing some extra padding around the border.'}, 'scale': {'type': 'int | None', 'default': 'None', 'description': 'relative size compared to adjacent Components. For example if Components A and B are in a Row, and A has scale=2, and B has scale=1, A will be twice as wide as B. Should be an integer. scale applies in Rows, and to top-level Components in Blocks where fill_height=True.'}, 'min_width': {'type': 'int', 'default': '160', 'description': 'minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.'}, 'interactive': {'type': 'bool | None', 'default': 'True', 'description': 'if True, will allow users to upload and annotate an image; if False, can only be used to display annotated images.'}, 'visible': {'type': 'bool', 'default': 'True', 'description': 'If False, component will be hidden.'}, 'elem_id': {'type': 'str | None', 'default': 'None', 'description': 'An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.'}, 'elem_classes': {'type': 'list[str] | str | None', 'default': 'None', 'description': 'An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.'}, 'render': {'type': 'bool', 'default': 'True', 'description': 'If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.'}, 'show_label': {'type': 'bool | None', 'default': 'None', 'description': 'if True, will display label.'}, 'show_download_button': {'type': 'bool', 'default': 'True', 'description': 'If True, will show a button to download the image.'}, 'show_share_button': {'type': 'bool | None', 'default': 'None', 'description': 'If True, will show a share icon in the corner of the component that allows user to share outputs to Hugging Face Spaces Discussions. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.'}, 'show_clear_button': {'type': 'bool | None', 'default': 'True', 'description': 'If True, will show a button to clear the current image.'}, 'show_remove_button': {'type': 'bool | None', 'default': 'None', 'description': 'If True, will show a button to remove the selected bounding box.'}, 'handles_cursor': {'type': 'bool | None', 'default': 'True', 'description': 'If True, the cursor will change when hovering over box handles in drag mode. Can be CPU-intensive.'}, 'use_default_label': {'type': 'bool', 'default': 'False', 'description': 'If True, the first item in label_list will be used as the default label when creating boxes.'}}, 'postprocess': {'value': {'type': 'AnnotatedImageValue | None', 'description': 'A dict with an image and an optional list of boxes or None.'}}, 'preprocess': {'return': {'type': 'AnnotatedImageValue | None', 'description': 'A dict with the image and boxes or None.'}, 'value': None}}, 'events': {'clear': {'type': None, 'default': None, 'description': 'This listener is triggered when the user clears the image_annotator using the clear button for the component.'}, 'change': {'type': None, 'default': None, 'description': 'Triggered when the value of the image_annotator changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See `.input()` for a listener that is only triggered by user input.'}, 'upload': {'type': None, 'default': None, 'description': 'This listener is triggered when the user uploads a file into the image_annotator.'}}}, '__meta__': {'additional_interfaces': {'AnnotatedImageValue': {'source': 'class AnnotatedImageValue(TypedDict):\n image: Optional[np.ndarray | PIL.Image.Image | str]\n boxes: Optional[List[dict]]\n orientation: Optional[int]'}}, 'user_fn_refs': {'image_annotator': ['AnnotatedImageValue']}}}
7
 
8
  abs_path = os.path.join(os.path.dirname(__file__), "css.css")
9
 
 
3
  from app import demo as app
4
  import os
5
 
6
+ _docs = {'image_annotator': {'description': 'Creates a component to annotate images with bounding boxes. The bounding boxes can be created and edited by the user or be passed by code.\nIt is also possible to predefine a set of valid classes and colors.', 'members': {'__init__': {'value': {'type': 'dict | None', 'default': 'None', 'description': "A dict or None. The dictionary must contain a key 'image' with either an URL to an image, a numpy image or a PIL image. Optionally it may contain a key 'boxes' with a list of boxes. Each box must be a dict wit the keys: 'xmin', 'ymin', 'xmax' and 'ymax' with the absolute image coordinates of the box. Optionally can also include the keys 'label' and 'color' describing the label and color of the box. Color must be a tuple of RGB values (e.g. `(255,255,255)`). Optionally can also include the keys 'orientation' with a integer between 0 and 3, describing the number of times the image is rotated by 90 degrees in frontend, the rotation is clockwise."}, 'boxes_alpha': {'type': 'float | None', 'default': 'None', 'description': 'Opacity of the bounding boxes 0 and 1.'}, 'label_list': {'type': 'list[str] | None', 'default': 'None', 'description': 'List of valid labels.'}, 'label_colors': {'type': 'list[str] | None', 'default': 'None', 'description': 'Optional list of colors for each label when `label_list` is used. Colors must be a tuple of RGB values (e.g. `(255,255,255)`).'}, 'box_min_size': {'type': 'int | None', 'default': 'None', 'description': 'Minimum valid bounding box size.'}, 'handle_size': {'type': 'int | None', 'default': 'None', 'description': 'Size of the bounding box resize handles.'}, 'box_thickness': {'type': 'int | None', 'default': 'None', 'description': 'Thickness of the bounding box outline.'}, 'box_selected_thickness': {'type': 'int | None', 'default': 'None', 'description': 'Thickness of the bounding box outline when it is selected.'}, 'disable_edit_boxes': {'type': 'bool | None', 'default': 'None', 'description': 'Disables the ability to set and edit the label and color of the boxes.'}, 'single_box': {'type': 'bool', 'default': 'False', 'description': 'If True, at most one box can be drawn.'}, 'height': {'type': 'int | str | None', 'default': 'None', 'description': 'The height of the displayed image, specified in pixels if a number is passed, or in CSS units if a string is passed.'}, 'width': {'type': 'int | str | None', 'default': 'None', 'description': 'The width of the displayed image, specified in pixels if a number is passed, or in CSS units if a string is passed.'}, 'image_mode': {'type': '"1"\n | "L"\n | "P"\n | "RGB"\n | "RGBA"\n | "CMYK"\n | "YCbCr"\n | "LAB"\n | "HSV"\n | "I"\n | "F"', 'default': '"RGB"', 'description': '"RGB" if color, or "L" if black and white. See https://pillow.readthedocs.io/en/stable/handbook/concepts.html for other supported image modes and their meaning.'}, 'sources': {'type': 'list["upload" | "webcam" | "clipboard"] | None', 'default': '["upload", "webcam", "clipboard"]', 'description': 'List of sources for the image. "upload" creates a box where user can drop an image file, "webcam" allows user to take snapshot from their webcam, "clipboard" allows users to paste an image from the clipboard. If None, defaults to ["upload", "webcam", "clipboard"].'}, 'image_type': {'type': '"numpy" | "pil" | "filepath"', 'default': '"numpy"', 'description': 'The format the image is converted before being passed into the prediction function. "numpy" converts the image to a numpy array with shape (height, width, 3) and values from 0 to 255, "pil" converts the image to a PIL image object, "filepath" passes a str path to a temporary file containing the image. If the image is SVG, the `type` is ignored and the filepath of the SVG is returned.'}, 'label': {'type': 'str | None', 'default': 'None', 'description': 'The label for this component. Appears above the component and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component is assigned to.'}, 'container': {'type': 'bool', 'default': 'True', 'description': 'If True, will place the component in a container - providing some extra padding around the border.'}, 'scale': {'type': 'int | None', 'default': 'None', 'description': 'relative size compared to adjacent Components. For example if Components A and B are in a Row, and A has scale=2, and B has scale=1, A will be twice as wide as B. Should be an integer. scale applies in Rows, and to top-level Components in Blocks where fill_height=True.'}, 'min_width': {'type': 'int', 'default': '160', 'description': 'minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.'}, 'interactive': {'type': 'bool | None', 'default': 'True', 'description': 'if True, will allow users to upload and annotate an image; if False, can only be used to display annotated images.'}, 'visible': {'type': 'bool', 'default': 'True', 'description': 'If False, component will be hidden.'}, 'elem_id': {'type': 'str | None', 'default': 'None', 'description': 'An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.'}, 'elem_classes': {'type': 'list[str] | str | None', 'default': 'None', 'description': 'An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.'}, 'render': {'type': 'bool', 'default': 'True', 'description': 'If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.'}, 'show_label': {'type': 'bool | None', 'default': 'None', 'description': 'if True, will display label.'}, 'show_download_button': {'type': 'bool', 'default': 'True', 'description': 'If True, will show a button to download the image.'}, 'show_share_button': {'type': 'bool | None', 'default': 'None', 'description': 'If True, will show a share icon in the corner of the component that allows user to share outputs to Hugging Face Spaces Discussions. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.'}, 'show_clear_button': {'type': 'bool | None', 'default': 'True', 'description': 'If True, will show a button to clear the current image.'}, 'show_remove_button': {'type': 'bool | None', 'default': 'None', 'description': 'If True, will show a button to remove the selected bounding box.'}, 'handles_cursor': {'type': 'bool | None', 'default': 'True', 'description': 'If True, the cursor will change when hovering over box handles in drag mode. Can be CPU-intensive.'}, 'use_default_label': {'type': 'bool | None', 'default': 'False', 'description': 'If True, the first item in label_list will be used as the default label when creating boxes.'}}, 'postprocess': {'value': {'type': 'AnnotatedImageValue | None', 'description': 'A dict with an image and an optional list of boxes or None.'}}, 'preprocess': {'return': {'type': 'AnnotatedImageValue | None', 'description': 'A dict with the image and boxes or None.'}, 'value': None}}, 'events': {'clear': {'type': None, 'default': None, 'description': 'This listener is triggered when the user clears the image_annotator using the clear button for the component.'}, 'change': {'type': None, 'default': None, 'description': 'Triggered when the value of the image_annotator changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See `.input()` for a listener that is only triggered by user input.'}, 'upload': {'type': None, 'default': None, 'description': 'This listener is triggered when the user uploads a file into the image_annotator.'}}}, '__meta__': {'additional_interfaces': {'AnnotatedImageValue': {'source': 'class AnnotatedImageValue(TypedDict):\n image: Optional[np.ndarray | PIL.Image.Image | str]\n boxes: Optional[List[dict]]\n orientation: Optional[int]'}}, 'user_fn_refs': {'image_annotator': ['AnnotatedImageValue']}}}
7
 
8
  abs_path = os.path.join(os.path.dirname(__file__), "css.css")
9
 
src/README.md CHANGED
@@ -535,7 +535,7 @@ bool | None
535
  <td align="left" style="width: 25%;">
536
 
537
  ```python
538
- bool
539
  ```
540
 
541
  </td>
 
535
  <td align="left" style="width: 25%;">
536
 
537
  ```python
538
+ bool | None
539
  ```
540
 
541
  </td>
src/backend/gradio_image_annotation/image_annotator.py CHANGED
@@ -88,7 +88,7 @@ class image_annotator(Component):
88
  show_clear_button: bool | None = True,
89
  show_remove_button: bool | None = None,
90
  handles_cursor: bool | None = True,
91
- use_default_label: bool = False,
92
  ):
93
  """
94
  Parameters:
 
88
  show_clear_button: bool | None = True,
89
  show_remove_button: bool | None = None,
90
  handles_cursor: bool | None = True,
91
+ use_default_label: bool | None = False,
92
  ):
93
  """
94
  Parameters:
src/backend/gradio_image_annotation/templates/component/index.js CHANGED
The diff for this file is too large to render. See raw diff
 
src/demo/space.py CHANGED
@@ -3,7 +3,7 @@ import gradio as gr
3
  from app import demo as app
4
  import os
5
 
6
- _docs = {'image_annotator': {'description': 'Creates a component to annotate images with bounding boxes. The bounding boxes can be created and edited by the user or be passed by code.\nIt is also possible to predefine a set of valid classes and colors.', 'members': {'__init__': {'value': {'type': 'dict | None', 'default': 'None', 'description': "A dict or None. The dictionary must contain a key 'image' with either an URL to an image, a numpy image or a PIL image. Optionally it may contain a key 'boxes' with a list of boxes. Each box must be a dict wit the keys: 'xmin', 'ymin', 'xmax' and 'ymax' with the absolute image coordinates of the box. Optionally can also include the keys 'label' and 'color' describing the label and color of the box. Color must be a tuple of RGB values (e.g. `(255,255,255)`). Optionally can also include the keys 'orientation' with a integer between 0 and 3, describing the number of times the image is rotated by 90 degrees in frontend, the rotation is clockwise."}, 'boxes_alpha': {'type': 'float | None', 'default': 'None', 'description': 'Opacity of the bounding boxes 0 and 1.'}, 'label_list': {'type': 'list[str] | None', 'default': 'None', 'description': 'List of valid labels.'}, 'label_colors': {'type': 'list[str] | None', 'default': 'None', 'description': 'Optional list of colors for each label when `label_list` is used. Colors must be a tuple of RGB values (e.g. `(255,255,255)`).'}, 'box_min_size': {'type': 'int | None', 'default': 'None', 'description': 'Minimum valid bounding box size.'}, 'handle_size': {'type': 'int | None', 'default': 'None', 'description': 'Size of the bounding box resize handles.'}, 'box_thickness': {'type': 'int | None', 'default': 'None', 'description': 'Thickness of the bounding box outline.'}, 'box_selected_thickness': {'type': 'int | None', 'default': 'None', 'description': 'Thickness of the bounding box outline when it is selected.'}, 'disable_edit_boxes': {'type': 'bool | None', 'default': 'None', 'description': 'Disables the ability to set and edit the label and color of the boxes.'}, 'single_box': {'type': 'bool', 'default': 'False', 'description': 'If True, at most one box can be drawn.'}, 'height': {'type': 'int | str | None', 'default': 'None', 'description': 'The height of the displayed image, specified in pixels if a number is passed, or in CSS units if a string is passed.'}, 'width': {'type': 'int | str | None', 'default': 'None', 'description': 'The width of the displayed image, specified in pixels if a number is passed, or in CSS units if a string is passed.'}, 'image_mode': {'type': '"1"\n | "L"\n | "P"\n | "RGB"\n | "RGBA"\n | "CMYK"\n | "YCbCr"\n | "LAB"\n | "HSV"\n | "I"\n | "F"', 'default': '"RGB"', 'description': '"RGB" if color, or "L" if black and white. See https://pillow.readthedocs.io/en/stable/handbook/concepts.html for other supported image modes and their meaning.'}, 'sources': {'type': 'list["upload" | "webcam" | "clipboard"] | None', 'default': '["upload", "webcam", "clipboard"]', 'description': 'List of sources for the image. "upload" creates a box where user can drop an image file, "webcam" allows user to take snapshot from their webcam, "clipboard" allows users to paste an image from the clipboard. If None, defaults to ["upload", "webcam", "clipboard"].'}, 'image_type': {'type': '"numpy" | "pil" | "filepath"', 'default': '"numpy"', 'description': 'The format the image is converted before being passed into the prediction function. "numpy" converts the image to a numpy array with shape (height, width, 3) and values from 0 to 255, "pil" converts the image to a PIL image object, "filepath" passes a str path to a temporary file containing the image. If the image is SVG, the `type` is ignored and the filepath of the SVG is returned.'}, 'label': {'type': 'str | None', 'default': 'None', 'description': 'The label for this component. Appears above the component and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component is assigned to.'}, 'container': {'type': 'bool', 'default': 'True', 'description': 'If True, will place the component in a container - providing some extra padding around the border.'}, 'scale': {'type': 'int | None', 'default': 'None', 'description': 'relative size compared to adjacent Components. For example if Components A and B are in a Row, and A has scale=2, and B has scale=1, A will be twice as wide as B. Should be an integer. scale applies in Rows, and to top-level Components in Blocks where fill_height=True.'}, 'min_width': {'type': 'int', 'default': '160', 'description': 'minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.'}, 'interactive': {'type': 'bool | None', 'default': 'True', 'description': 'if True, will allow users to upload and annotate an image; if False, can only be used to display annotated images.'}, 'visible': {'type': 'bool', 'default': 'True', 'description': 'If False, component will be hidden.'}, 'elem_id': {'type': 'str | None', 'default': 'None', 'description': 'An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.'}, 'elem_classes': {'type': 'list[str] | str | None', 'default': 'None', 'description': 'An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.'}, 'render': {'type': 'bool', 'default': 'True', 'description': 'If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.'}, 'show_label': {'type': 'bool | None', 'default': 'None', 'description': 'if True, will display label.'}, 'show_download_button': {'type': 'bool', 'default': 'True', 'description': 'If True, will show a button to download the image.'}, 'show_share_button': {'type': 'bool | None', 'default': 'None', 'description': 'If True, will show a share icon in the corner of the component that allows user to share outputs to Hugging Face Spaces Discussions. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.'}, 'show_clear_button': {'type': 'bool | None', 'default': 'True', 'description': 'If True, will show a button to clear the current image.'}, 'show_remove_button': {'type': 'bool | None', 'default': 'None', 'description': 'If True, will show a button to remove the selected bounding box.'}, 'handles_cursor': {'type': 'bool | None', 'default': 'True', 'description': 'If True, the cursor will change when hovering over box handles in drag mode. Can be CPU-intensive.'}, 'use_default_label': {'type': 'bool', 'default': 'False', 'description': 'If True, the first item in label_list will be used as the default label when creating boxes.'}}, 'postprocess': {'value': {'type': 'AnnotatedImageValue | None', 'description': 'A dict with an image and an optional list of boxes or None.'}}, 'preprocess': {'return': {'type': 'AnnotatedImageValue | None', 'description': 'A dict with the image and boxes or None.'}, 'value': None}}, 'events': {'clear': {'type': None, 'default': None, 'description': 'This listener is triggered when the user clears the image_annotator using the clear button for the component.'}, 'change': {'type': None, 'default': None, 'description': 'Triggered when the value of the image_annotator changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See `.input()` for a listener that is only triggered by user input.'}, 'upload': {'type': None, 'default': None, 'description': 'This listener is triggered when the user uploads a file into the image_annotator.'}}}, '__meta__': {'additional_interfaces': {'AnnotatedImageValue': {'source': 'class AnnotatedImageValue(TypedDict):\n image: Optional[np.ndarray | PIL.Image.Image | str]\n boxes: Optional[List[dict]]\n orientation: Optional[int]'}}, 'user_fn_refs': {'image_annotator': ['AnnotatedImageValue']}}}
7
 
8
  abs_path = os.path.join(os.path.dirname(__file__), "css.css")
9
 
 
3
  from app import demo as app
4
  import os
5
 
6
+ _docs = {'image_annotator': {'description': 'Creates a component to annotate images with bounding boxes. The bounding boxes can be created and edited by the user or be passed by code.\nIt is also possible to predefine a set of valid classes and colors.', 'members': {'__init__': {'value': {'type': 'dict | None', 'default': 'None', 'description': "A dict or None. The dictionary must contain a key 'image' with either an URL to an image, a numpy image or a PIL image. Optionally it may contain a key 'boxes' with a list of boxes. Each box must be a dict wit the keys: 'xmin', 'ymin', 'xmax' and 'ymax' with the absolute image coordinates of the box. Optionally can also include the keys 'label' and 'color' describing the label and color of the box. Color must be a tuple of RGB values (e.g. `(255,255,255)`). Optionally can also include the keys 'orientation' with a integer between 0 and 3, describing the number of times the image is rotated by 90 degrees in frontend, the rotation is clockwise."}, 'boxes_alpha': {'type': 'float | None', 'default': 'None', 'description': 'Opacity of the bounding boxes 0 and 1.'}, 'label_list': {'type': 'list[str] | None', 'default': 'None', 'description': 'List of valid labels.'}, 'label_colors': {'type': 'list[str] | None', 'default': 'None', 'description': 'Optional list of colors for each label when `label_list` is used. Colors must be a tuple of RGB values (e.g. `(255,255,255)`).'}, 'box_min_size': {'type': 'int | None', 'default': 'None', 'description': 'Minimum valid bounding box size.'}, 'handle_size': {'type': 'int | None', 'default': 'None', 'description': 'Size of the bounding box resize handles.'}, 'box_thickness': {'type': 'int | None', 'default': 'None', 'description': 'Thickness of the bounding box outline.'}, 'box_selected_thickness': {'type': 'int | None', 'default': 'None', 'description': 'Thickness of the bounding box outline when it is selected.'}, 'disable_edit_boxes': {'type': 'bool | None', 'default': 'None', 'description': 'Disables the ability to set and edit the label and color of the boxes.'}, 'single_box': {'type': 'bool', 'default': 'False', 'description': 'If True, at most one box can be drawn.'}, 'height': {'type': 'int | str | None', 'default': 'None', 'description': 'The height of the displayed image, specified in pixels if a number is passed, or in CSS units if a string is passed.'}, 'width': {'type': 'int | str | None', 'default': 'None', 'description': 'The width of the displayed image, specified in pixels if a number is passed, or in CSS units if a string is passed.'}, 'image_mode': {'type': '"1"\n | "L"\n | "P"\n | "RGB"\n | "RGBA"\n | "CMYK"\n | "YCbCr"\n | "LAB"\n | "HSV"\n | "I"\n | "F"', 'default': '"RGB"', 'description': '"RGB" if color, or "L" if black and white. See https://pillow.readthedocs.io/en/stable/handbook/concepts.html for other supported image modes and their meaning.'}, 'sources': {'type': 'list["upload" | "webcam" | "clipboard"] | None', 'default': '["upload", "webcam", "clipboard"]', 'description': 'List of sources for the image. "upload" creates a box where user can drop an image file, "webcam" allows user to take snapshot from their webcam, "clipboard" allows users to paste an image from the clipboard. If None, defaults to ["upload", "webcam", "clipboard"].'}, 'image_type': {'type': '"numpy" | "pil" | "filepath"', 'default': '"numpy"', 'description': 'The format the image is converted before being passed into the prediction function. "numpy" converts the image to a numpy array with shape (height, width, 3) and values from 0 to 255, "pil" converts the image to a PIL image object, "filepath" passes a str path to a temporary file containing the image. If the image is SVG, the `type` is ignored and the filepath of the SVG is returned.'}, 'label': {'type': 'str | None', 'default': 'None', 'description': 'The label for this component. Appears above the component and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component is assigned to.'}, 'container': {'type': 'bool', 'default': 'True', 'description': 'If True, will place the component in a container - providing some extra padding around the border.'}, 'scale': {'type': 'int | None', 'default': 'None', 'description': 'relative size compared to adjacent Components. For example if Components A and B are in a Row, and A has scale=2, and B has scale=1, A will be twice as wide as B. Should be an integer. scale applies in Rows, and to top-level Components in Blocks where fill_height=True.'}, 'min_width': {'type': 'int', 'default': '160', 'description': 'minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.'}, 'interactive': {'type': 'bool | None', 'default': 'True', 'description': 'if True, will allow users to upload and annotate an image; if False, can only be used to display annotated images.'}, 'visible': {'type': 'bool', 'default': 'True', 'description': 'If False, component will be hidden.'}, 'elem_id': {'type': 'str | None', 'default': 'None', 'description': 'An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.'}, 'elem_classes': {'type': 'list[str] | str | None', 'default': 'None', 'description': 'An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.'}, 'render': {'type': 'bool', 'default': 'True', 'description': 'If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.'}, 'show_label': {'type': 'bool | None', 'default': 'None', 'description': 'if True, will display label.'}, 'show_download_button': {'type': 'bool', 'default': 'True', 'description': 'If True, will show a button to download the image.'}, 'show_share_button': {'type': 'bool | None', 'default': 'None', 'description': 'If True, will show a share icon in the corner of the component that allows user to share outputs to Hugging Face Spaces Discussions. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.'}, 'show_clear_button': {'type': 'bool | None', 'default': 'True', 'description': 'If True, will show a button to clear the current image.'}, 'show_remove_button': {'type': 'bool | None', 'default': 'None', 'description': 'If True, will show a button to remove the selected bounding box.'}, 'handles_cursor': {'type': 'bool | None', 'default': 'True', 'description': 'If True, the cursor will change when hovering over box handles in drag mode. Can be CPU-intensive.'}, 'use_default_label': {'type': 'bool | None', 'default': 'False', 'description': 'If True, the first item in label_list will be used as the default label when creating boxes.'}}, 'postprocess': {'value': {'type': 'AnnotatedImageValue | None', 'description': 'A dict with an image and an optional list of boxes or None.'}}, 'preprocess': {'return': {'type': 'AnnotatedImageValue | None', 'description': 'A dict with the image and boxes or None.'}, 'value': None}}, 'events': {'clear': {'type': None, 'default': None, 'description': 'This listener is triggered when the user clears the image_annotator using the clear button for the component.'}, 'change': {'type': None, 'default': None, 'description': 'Triggered when the value of the image_annotator changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See `.input()` for a listener that is only triggered by user input.'}, 'upload': {'type': None, 'default': None, 'description': 'This listener is triggered when the user uploads a file into the image_annotator.'}}}, '__meta__': {'additional_interfaces': {'AnnotatedImageValue': {'source': 'class AnnotatedImageValue(TypedDict):\n image: Optional[np.ndarray | PIL.Image.Image | str]\n boxes: Optional[List[dict]]\n orientation: Optional[int]'}}, 'user_fn_refs': {'image_annotator': ['AnnotatedImageValue']}}}
7
 
8
  abs_path = os.path.join(os.path.dirname(__file__), "css.css")
9
 
src/frontend/shared/Box.ts CHANGED
@@ -55,11 +55,13 @@ export default class Box {
55
  cursor: string;
56
  }[];
57
  canvasWindow: WindowViewer;
 
58
 
59
  constructor(
60
  renderCallBack: () => void,
61
  onFinishCreation: () => void,
62
  canvasWindow: WindowViewer,
 
63
  canvasXmin: number,
64
  canvasYmin: number,
65
  canvasXmax: number,
@@ -80,6 +82,7 @@ export default class Box {
80
  this.renderCallBack = renderCallBack;
81
  this.onFinishCreation = onFinishCreation;
82
  this.canvasWindow = canvasWindow;
 
83
  this.canvasXmin = canvasXmin;
84
  this.canvasYmin = canvasYmin;
85
  this.canvasXmax = canvasXmax;
@@ -319,7 +322,7 @@ export default class Box {
319
  };
320
 
321
  handleDrag = (event: MouseEvent): void => {
322
- if (this.isDragging) {
323
  let deltaX = (event.clientX - this.offsetMouseX) / this.canvasWindow.scale - this._xmin;
324
  let deltaY = (event.clientY - this.offsetMouseY) / this.canvasWindow.scale - this._ymin;
325
 
@@ -374,7 +377,7 @@ export default class Box {
374
  }
375
 
376
  handleCreating = (event: MouseEvent): void => {
377
- if (this.isCreating) {
378
  let [x, y] = this.toBoxCoordinates(event.clientX, event.clientY);
379
  x = (x - this.offsetMouseX) / this.canvasWindow.scale;
380
  y = (y - this.offsetMouseY) / this.canvasWindow.scale;
@@ -480,7 +483,7 @@ export default class Box {
480
  }
481
 
482
  handleResize = (event: MouseEvent): void => {
483
- if (this.isResizing) {
484
  const mouseX = event.clientX;
485
  const mouseY = event.clientY;
486
  const deltaX = (mouseX - this.offsetMouseX - this.resizeHandles[this.resizingHandleIndex].xmin) / this.canvasWindow.scale;
 
55
  cursor: string;
56
  }[];
57
  canvasWindow: WindowViewer;
58
+ pointersCache: Map<number, PointerEvent>;
59
 
60
  constructor(
61
  renderCallBack: () => void,
62
  onFinishCreation: () => void,
63
  canvasWindow: WindowViewer,
64
+ pointersCache: Map<number, PointerEvent>,
65
  canvasXmin: number,
66
  canvasYmin: number,
67
  canvasXmax: number,
 
82
  this.renderCallBack = renderCallBack;
83
  this.onFinishCreation = onFinishCreation;
84
  this.canvasWindow = canvasWindow;
85
+ this.pointersCache = pointersCache;
86
  this.canvasXmin = canvasXmin;
87
  this.canvasYmin = canvasYmin;
88
  this.canvasXmax = canvasXmax;
 
322
  };
323
 
324
  handleDrag = (event: MouseEvent): void => {
325
+ if (this.isDragging && this.pointersCache.size === 1) {
326
  let deltaX = (event.clientX - this.offsetMouseX) / this.canvasWindow.scale - this._xmin;
327
  let deltaY = (event.clientY - this.offsetMouseY) / this.canvasWindow.scale - this._ymin;
328
 
 
377
  }
378
 
379
  handleCreating = (event: MouseEvent): void => {
380
+ if (this.isCreating && this.pointersCache.size === 1) {
381
  let [x, y] = this.toBoxCoordinates(event.clientX, event.clientY);
382
  x = (x - this.offsetMouseX) / this.canvasWindow.scale;
383
  y = (y - this.offsetMouseY) / this.canvasWindow.scale;
 
483
  }
484
 
485
  handleResize = (event: MouseEvent): void => {
486
+ if (this.isResizing && this.pointersCache.size === 1) {
487
  const mouseX = event.clientX;
488
  const mouseY = event.clientY;
489
  const deltaX = (mouseX - this.offsetMouseX - this.resizeHandles[this.resizingHandleIndex].xmin) / this.canvasWindow.scale;
src/frontend/shared/Canvas.svelte CHANGED
@@ -37,7 +37,8 @@
37
  let image = null;
38
  let selectedBox = -1;
39
  let mode: Mode = Mode.drag;
40
- let canvasWindow: WindowViewer = new WindowViewer(draw);
 
41
 
42
  if (value !== null && value.boxes.length == 0) {
43
  mode = Mode.creation;
@@ -62,6 +63,12 @@
62
  color: ""
63
  };
64
 
 
 
 
 
 
 
65
  const dispatch = createEventDispatcher<{
66
  change: undefined;
67
  }>();
@@ -130,23 +137,45 @@
130
  draw();
131
  }
132
 
 
 
 
 
 
 
 
133
  function handlePointerDown(event: PointerEvent) {
134
  if (!interactive) {
135
  return;
136
  }
137
-
138
- if (
139
- event.target instanceof Element &&
140
- event.target.hasPointerCapture(event.pointerId)
141
- ) {
142
- event.target.releasePointerCapture(event.pointerId);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
  }
144
 
145
- if (mode === Mode.creation) {
146
- createBox(event);
147
- } else if (mode === Mode.drag) {
148
- clickBox(event);
149
- }
150
  }
151
 
152
  function clickBox(event: PointerEvent) {
@@ -186,31 +215,91 @@
186
  }
187
 
188
  function handlePointerUp(event: PointerEvent) {
 
 
 
 
 
189
  dispatch("change");
190
  }
191
 
192
- function handlePointerMove(event: PointerEvent) {
193
- if (value === null) {
194
  return;
195
  }
 
 
 
 
196
 
197
- if (mode !== Mode.drag) {
 
198
  return;
199
  }
200
 
201
- const rect = canvas.getBoundingClientRect();
202
- const mouseX = event.clientX - rect.left;
203
- const mouseY = event.clientY - rect.top;
 
204
 
205
- for (const [_, box] of value.boxes.entries()) {
206
- const handleIndex = box.indexOfPointInsideHandle(mouseX, mouseY);
207
- if (handleIndex >= 0) {
208
- canvas.style.cursor = box.resizeHandles[handleIndex].cursor;
209
  return;
210
  }
211
- }
212
 
213
- canvas.style.cursor = "default";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
  }
215
 
216
  function handleKeyPress(event: KeyboardEvent) {
@@ -226,6 +315,10 @@
226
  }
227
 
228
  function handleMouseWheel(event: WheelEvent) {
 
 
 
 
229
  event.preventDefault();
230
  const delta = 1 / (1 + (event.deltaY / 1000) * 0.5);
231
 
@@ -266,6 +359,7 @@
266
  draw,
267
  onBoxFinishCreation,
268
  canvasWindow,
 
269
  canvasXmin,
270
  canvasYmin,
271
  canvasXmax,
@@ -507,6 +601,7 @@
507
  draw,
508
  onBoxFinishCreation,
509
  canvasWindow,
 
510
  canvasXmin,
511
  canvasYmin,
512
  canvasXmax,
@@ -530,6 +625,7 @@
530
 
531
  $: {
532
  value;
 
533
  setImage();
534
  parseInputBoxes();
535
  resize();
@@ -596,7 +692,8 @@
596
  bind:this={canvas}
597
  on:pointerdown={handlePointerDown}
598
  on:pointerup={handlePointerUp}
599
- on:pointermove={handlesCursor ? handlePointerMove : null}
 
600
  on:dblclick={handleDoubleClick}
601
  on:wheel={handleMouseWheel}
602
  style="height: {height}; width: {width};"
 
37
  let image = null;
38
  let selectedBox = -1;
39
  let mode: Mode = Mode.drag;
40
+ let pointersCache: Map<number, PointerEvent> = new Map();
41
+ let canvasWindow: WindowViewer = new WindowViewer(draw, pointersCache);
42
 
43
  if (value !== null && value.boxes.length == 0) {
44
  mode = Mode.creation;
 
63
  color: ""
64
  };
65
 
66
+ let touchScaleValues = {
67
+ x: 0,
68
+ y: 0,
69
+ distance: 0,
70
+ }
71
+
72
  const dispatch = createEventDispatcher<{
73
  change: undefined;
74
  }>();
 
137
  draw();
138
  }
139
 
140
+ const getDistance = (touch1: PointerEvent, touch2: PointerEvent) => {
141
+ return Math.sqrt(
142
+ Math.pow(touch1.clientX - touch2.clientX, 2) +
143
+ Math.pow(touch1.clientY - touch2.clientY, 2)
144
+ );
145
+ };
146
+
147
  function handlePointerDown(event: PointerEvent) {
148
  if (!interactive) {
149
  return;
150
  }
151
+ canvas.setPointerCapture(event.pointerId);
152
+ pointersCache.set(event.pointerId, event);
153
+
154
+ if (pointersCache.size == 1) {
155
+ if (mode === Mode.creation) {
156
+ createBox(event);
157
+ } else if (mode === Mode.drag) {
158
+ clickBox(event);
159
+ }
160
+ } else if (pointersCache.size == 2) {
161
+ canvasWindow.isDragging = false;
162
+ value.boxes.forEach(box => {
163
+ box.isCreating = false;
164
+ box.isDragging = false;
165
+ box.isResizing = false;
166
+ });
167
+ const pointerArray = Array.from(pointersCache.values());
168
+ const touch1 = pointerArray[0];
169
+ const touch2 = pointerArray[1];
170
+ const distance = getDistance(touch1, touch2);
171
+ const centerX = (touch1.clientX + touch2.clientX) / 2;
172
+ const centerY = (touch1.clientY + touch2.clientY) / 2;
173
+
174
+ touchScaleValues.distance = distance;
175
+ touchScaleValues.x = centerX;
176
+ touchScaleValues.y = centerY;
177
  }
178
 
 
 
 
 
 
179
  }
180
 
181
  function clickBox(event: PointerEvent) {
 
215
  }
216
 
217
  function handlePointerUp(event: PointerEvent) {
218
+ if (!interactive) {
219
+ return;
220
+ }
221
+ pointersCache.delete(event.pointerId);
222
+ canvas.releasePointerCapture(event.pointerId);
223
  dispatch("change");
224
  }
225
 
226
+ function handlePointerCancel(event: PointerEvent) {
227
+ if (!interactive) {
228
  return;
229
  }
230
+ pointersCache.delete(event.pointerId);
231
+ canvas.releasePointerCapture(event.pointerId);
232
+ dispatch("change");
233
+ }
234
 
235
+ function handlePointerMove(event: PointerEvent) {
236
+ if (!interactive) {
237
  return;
238
  }
239
 
240
+ if (event.pointerType === "mouse") {
241
+ if (!handlesCursor) {
242
+ return;
243
+ }
244
 
245
+ if (value === null) {
 
 
 
246
  return;
247
  }
 
248
 
249
+ if (mode !== Mode.drag) {
250
+ return;
251
+ }
252
+
253
+ const rect = canvas.getBoundingClientRect();
254
+ const mouseX = event.clientX - rect.left;
255
+ const mouseY = event.clientY - rect.top;
256
+
257
+ for (const [_, box] of value.boxes.entries()) {
258
+ const handleIndex = box.indexOfPointInsideHandle(mouseX, mouseY);
259
+ if (handleIndex >= 0) {
260
+ canvas.style.cursor = box.resizeHandles[handleIndex].cursor;
261
+ return;
262
+ }
263
+ }
264
+
265
+ canvas.style.cursor = "default";
266
+
267
+ } else { // touch
268
+ if (!pointersCache.has(event.pointerId)) {
269
+ return;
270
+ }
271
+ pointersCache.set(event.pointerId, event);
272
+
273
+ if (pointersCache.size === 2) {
274
+ const pointerArray = Array.from(pointersCache.values());
275
+ const touch1 = pointerArray[0];
276
+ const touch2 = pointerArray[1];
277
+ const distance = getDistance(touch1, touch2);
278
+ const centerX = (touch1.clientX + touch2.clientX) / 2;
279
+ const centerY = (touch1.clientY + touch2.clientY) / 2;
280
+
281
+ const newScaleTmp = parseFloat(
282
+ (canvasWindow.scale * (distance / touchScaleValues.distance)).toFixed(2)
283
+ );
284
+ const newScale = newScaleTmp < 1 ? 1 : newScaleTmp;
285
+
286
+ const scaleDelta = newScale / canvasWindow.scale;
287
+ canvasWindow.offsetX = touchScaleValues.x - (touchScaleValues.x - canvasWindow.offsetX) * scaleDelta;
288
+ canvasWindow.offsetY = touchScaleValues.y - (touchScaleValues.y - canvasWindow.offsetY) * scaleDelta;
289
+
290
+ const dx = centerX - touchScaleValues.x;
291
+ const dy = centerY - touchScaleValues.y;
292
+ canvasWindow.offsetX += dx;
293
+ canvasWindow.offsetY += dy;
294
+ canvasWindow.scale = newScale;
295
+
296
+ touchScaleValues.x = centerX;
297
+ touchScaleValues.y = centerY;
298
+ touchScaleValues.distance = distance;
299
+
300
+ draw();
301
+ }
302
+ }
303
  }
304
 
305
  function handleKeyPress(event: KeyboardEvent) {
 
315
  }
316
 
317
  function handleMouseWheel(event: WheelEvent) {
318
+ if (!interactive) {
319
+ return;
320
+ }
321
+
322
  event.preventDefault();
323
  const delta = 1 / (1 + (event.deltaY / 1000) * 0.5);
324
 
 
359
  draw,
360
  onBoxFinishCreation,
361
  canvasWindow,
362
+ pointersCache,
363
  canvasXmin,
364
  canvasYmin,
365
  canvasXmax,
 
601
  draw,
602
  onBoxFinishCreation,
603
  canvasWindow,
604
+ pointersCache,
605
  canvasXmin,
606
  canvasYmin,
607
  canvasXmax,
 
625
 
626
  $: {
627
  value;
628
+ canvasWindow.orientation = value.orientation;
629
  setImage();
630
  parseInputBoxes();
631
  resize();
 
692
  bind:this={canvas}
693
  on:pointerdown={handlePointerDown}
694
  on:pointerup={handlePointerUp}
695
+ on:pointermove={handlePointerMove}
696
+ on:pointercancel={handlePointerCancel}
697
  on:dblclick={handleDoubleClick}
698
  on:wheel={handleMouseWheel}
699
  style="height: {height}; width: {width};"
src/frontend/shared/WindowViewer.ts CHANGED
@@ -15,9 +15,14 @@ export default class WindowViewer {
15
  startDragY: number;
16
  orientation: number;
17
  renderCallBack: () => void;
 
18
 
19
- constructor(renderCallBack: () => void) {
 
 
 
20
  this.renderCallBack = renderCallBack;
 
21
  this.scale = 1.0;
22
  this.offsetX = 0;
23
  this.offsetY = 0;
@@ -34,24 +39,37 @@ export default class WindowViewer {
34
  }
35
  startDrag(event: MouseEvent): void {
36
  this.isDragging = true;
37
- this.startDragX = event.clientX - this.offsetX;
38
- this.startDragY = event.clientY - this.offsetY;
39
 
40
  document.addEventListener("pointermove", this.handleDrag);
41
  document.addEventListener("pointerup", this.stopDrag);
42
  }
43
 
44
  stopDrag = (): void => {
45
- this.isDragging = false;
46
- document.removeEventListener("pointermove", this.handleDrag);
47
- document.removeEventListener("pointerup", this.stopDrag);
 
 
 
 
 
 
48
  };
49
 
50
  handleDrag = (event: MouseEvent): void => {
51
- if (this.isDragging) {
 
 
 
 
 
 
 
52
 
53
- let deltaX = event.clientX - this.startDragX - this.offsetX;
54
- let deltaY = event.clientY - this.startDragY - this.offsetY;
55
 
56
  if (this.imageWidth * this.scale > this.canvasWidth){
57
  deltaX = clamp(deltaX, this.canvasWidth-this.offsetX-(this.imageWidth*this.scale), -this.offsetX);
@@ -67,6 +85,8 @@ export default class WindowViewer {
67
 
68
  this.offsetX += deltaX;
69
  this.offsetY += deltaY;
 
 
70
  this.renderCallBack();
71
  }
72
  };
 
15
  startDragY: number;
16
  orientation: number;
17
  renderCallBack: () => void;
18
+ pointersCache: Map<number, PointerEvent>;
19
 
20
+ constructor(
21
+ renderCallBack: () => void,
22
+ pointersCache: Map<number, PointerEvent>,
23
+ ) {
24
  this.renderCallBack = renderCallBack;
25
+ this.pointersCache = pointersCache;
26
  this.scale = 1.0;
27
  this.offsetX = 0;
28
  this.offsetY = 0;
 
39
  }
40
  startDrag(event: MouseEvent): void {
41
  this.isDragging = true;
42
+ this.startDragX = event.clientX;
43
+ this.startDragY = event.clientY;
44
 
45
  document.addEventListener("pointermove", this.handleDrag);
46
  document.addEventListener("pointerup", this.stopDrag);
47
  }
48
 
49
  stopDrag = (): void => {
50
+ if (this.pointersCache.size === 0) {
51
+ this.isDragging = false;
52
+ document.removeEventListener("pointermove", this.handleDrag);
53
+ document.removeEventListener("pointerup", this.stopDrag);
54
+ } else if (this.pointersCache.size === 1) {
55
+ this.isDragging = true;
56
+ this.startDragX = this.pointersCache.values().next().value.clientX;
57
+ this.startDragY = this.pointersCache.values().next().value.clientY;
58
+ }
59
  };
60
 
61
  handleDrag = (event: MouseEvent): void => {
62
+ if (this.isDragging && this.pointersCache.size === 1) {
63
+
64
+ if (this.scale == 1.0){
65
+ this.offsetX = (this.canvasWidth - this.imageWidth) / 2;
66
+ this.offsetY = 0;
67
+ this.renderCallBack();
68
+ return;
69
+ }
70
 
71
+ let deltaX = event.clientX - this.startDragX;
72
+ let deltaY = event.clientY - this.startDragY;
73
 
74
  if (this.imageWidth * this.scale > this.canvasWidth){
75
  deltaX = clamp(deltaX, this.canvasWidth-this.offsetX-(this.imageWidth*this.scale), -this.offsetX);
 
85
 
86
  this.offsetX += deltaX;
87
  this.offsetY += deltaY;
88
+ this.startDragX = event.clientX;
89
+ this.startDragY = event.clientY;
90
  this.renderCallBack();
91
  }
92
  };
src/pyproject.toml CHANGED
@@ -8,7 +8,7 @@ build-backend = "hatchling.build"
8
 
9
  [project]
10
  name = "gradio_image_annotation"
11
- version = "0.2.7"
12
  description = "A Gradio component that can be used to annotate images with bounding boxes."
13
  readme = "README.md"
14
  license = "MIT"
 
8
 
9
  [project]
10
  name = "gradio_image_annotation"
11
+ version = "0.3.0"
12
  description = "A Gradio component that can be used to annotate images with bounding boxes."
13
  readme = "README.md"
14
  license = "MIT"