Image with area selection

Note

This section describes how to add an area selection editor to an image using the HTML/JS/CSS tools. You can also try creating an area selection editor in Template Builder.

The editor lets you select an area of the image using either a rectangle or an arbitrary polygon:

Note

Don't add training or control tasks to a project that features an image area selection editor.

Adding the editor

Picture
Images with selected objects

If you need to add an image editor without selected objects:

  1. Connect the $TOLOKA_ASSETS/js/image-annotation.js library (click in the Task interface block on the project page).

  2. Include the {{field type="image-annotation"``name="<output field name>"``src=<image URL>}} component in the HTML code of the interface. For example:

    {{field type="image-annotation" name="result" src=image}}

For a complete list of parameters, see the table.

  1. Add a field for entering an image link in the input data.

  2. Add the result field with the json type in the output data.

The result field will be used for a JSON object with the point coordinates. Example:

[{"data":{"p1":{"x":0.472,"y":0.413},"p2":{"x":0.932,"y":0.877}},"type":"rectangle"},
{"data":[{"x":0.143,"y":0.807},{"x":0.317,"y":0.87},{"x":0.511,"y":0.145},{"x":0.328,"y":0.096},{"x":0.096,"y":0.554}],"type":"polygon"}]

The x and y values are numbers from 0 to 1. Length and width of the image are taken as 1, with the center of coordinates in the upper-left corner of the image.

Parameters

ParameterDescriptionRequiredDefault value
srcPage URL.yesno
typeField type: image-annotation, the image area selection editor.yesno
nameAttribute for the output data field. Contains the output field name.yesno
src

Image URL. Supported values:

  • URL from the task input data. For example, from the field with the url: src=image ID.

  • Direct link. The HTTPS protocol is recommended. For example, src="https://mywebsite.ru/img1.png".

  • Relative link.

yesno
annotationsAttribute for the input data field. Contains the input data field name. Here you can provide the selection coordinates for further editing. The data format is a JSON object.nono

Shortcuts

By default, you can use the following keyboard shortcuts in the task:

  • C closes a polygon connecting the first and last points with a line.

  • D deletes the selected point, selected object, or last point when creating a polygon.

  • Arrows move the selected point. With Alt pressed, they move it slower. With Alt+Shift pressed, they move it faster.

  • Tab moves from the selected point to the next one.

  • Shift+Tab moves from the selected point to the previous one.

Tell Tolokers about keyboard shortcuts in the instructions to speed up task completion.

Selection tools

By default, the polygon tool is added to the task interface.

You can change the tool set:

Add a rectangle
Hide a polygon
Hide a rectangle

To add a rectangle, delete the CSS selector:

.image-annotation-editor__shape-rectangle {
display: none;
}

Annotation

To use annotations, insert one of these examples into the JS block:

Text input field
Drop-down list
exports.Task = extend(TolokaHandlebarsTask, function (options) {
TolokaHandlebarsTask.call(this, options);
}, {
onRender: function() {
var field = this.getField('result');
var editor = field.getEditor();
editor.annotationInterface = field.createAnnotationInterface({
createInterfaceElement: function() {
this._input = document.createElement('input');
this._input.addEventListener('input', function() {
this._shape.annotation = this._input.value;
}.bind(this));
return this._input;
},
onShow: function(shape) {
this._shape = shape;
this._input.value = shape.annotation;
}
});
},
onDestroy: function() {
// Task is completed. Global resources can be released (if used)
}
});
function extend(ParentClass, constructorFunction, prototypeHash) {
constructorFunction = constructorFunction || function () {};
prototypeHash = prototypeHash || {};
if (ParentClass) {
constructorFunction.prototype = Object.create(ParentClass.prototype);
}
for (var i in prototypeHash) {
constructorFunction.prototype[i] = prototypeHash[i];
}
return constructorFunction;
}
Explanation of examples

Toloker interactions with the editor cause the following events:

  • shape:start — Starts area selection.

  • shape:finish — Completes area selection.

  • shape:cancel — Deletes a point.

  • shape:remove — Deletes the selection.

To subscribe to these events:

editor.on('shape:start', function() {
/* event handling */
});
Contact support

Last updated: February 15, 2023

Introduction
Getting started
Important tips
Useful recommendations
Working with Toloka
Projects
Pools
Tasks
Results
Project analysis
Toloka settings
Task interface
Template Builder
HTML/CSS/JS editor
Help and support
FAQTroubleshootingSupportGlossary