-
Notifications
You must be signed in to change notification settings - Fork 15
/
ImageAnnotationController.php
325 lines (299 loc) · 11.6 KB
/
ImageAnnotationController.php
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
namespace Biigle\Http\Controllers\Api;
use Biigle\Http\Requests\StoreImageAnnotation;
use Biigle\Image;
use Biigle\ImageAnnotation;
use Biigle\ImageAnnotationLabel;
use Biigle\Label;
use Biigle\Shape;
use DB;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
class ImageAnnotationController extends Controller
{
/**
* Shows a list of all annotations of the specified image.
*
* @api {get} images/:id/annotations Get all annotations
* @apiGroup Images
* @apiName IndexImageAnnotations
* @apiPermission projectMember
* @apiDescription If there is an active annotation session for the volume of this image, only those annotations will be returned that the user is allowed to access.
*
* @apiParam {Number} id The image ID.
*
* @apiSuccessExample {json} Success response:
* [
* {
* "id": 1,
* "image_id": 1,
* "shape_id": 1,
* "created_at": "2015-02-18 11:45:00",
* "updated_at": "2015-02-18 11:45:00",
* "points": [100, 200],
* "labels": [
* {
* "id": 1,
* "annotation_id": 1,
* "label_id": 3,
* "user_id": 4,
* "label": {
* "color": "bada55",
* "id": 3,
* "name": "My label",
* "parent_id": null,
* "label_tree_id": 1,
* },
* "user": {
* "id": 4,
* "firstname": "Graham",
* "lastname": "Hahn",
* }
* }
* ]
* }
* ]
*
* @param Request $request
* @param int $id image id
* @return \Illuminate\Database\Eloquent\Collection<int, ImageAnnotation>
*/
public function index(Request $request, $id)
{
$image = Image::findOrFail($id);
$this->authorize('access', $image);
$user = $request->user();
$session = $image->volume->getActiveAnnotationSession($user);
$load = [
// Hide confidence.
'labels:id,annotation_id,label_id,user_id',
// Hide label_source_id and source_id.
'labels.label:id,name,parent_id,color,label_tree_id',
// Hide role_id.
'labels.user:id,firstname,lastname',
];
if ($session) {
return $session->getVolumeFileAnnotations($image, $user)->load($load);
}
return $image->annotations()->with($load)->get();
}
/**
* @api {get} annotations/:id Get an annotation
* @apiDeprecated use now (#ImageAnnotations:ShowImageAnnotation).
* @apiGroup Annotations
* @apiName ShowAnnotation
* @apiParam {Number} id The annotation ID.
* @apiPermission projectMember
* @apiDescription Access may be denied by an active annotation session of the volume, the annotation belongs to.
* @apiSuccessExample {json} Success response:
* {
* "id":1,
* "image_id":1,
* "shape_id":1,
* "created_at":"2015-02-13 11:59:23",
* "updated_at":"2015-02-13 11:59:23",
* "points": [100, 100]
* }
*/
/**
* Displays the annotation.
*
* @api {get} image-annotations/:id Get an annotation
* @apiGroup ImageAnnotations
* @apiName ShowImageAnnotation
* @apiParam {Number} id The annotation ID.
* @apiPermission projectMember
* @apiDescription Access may be denied by an active annotation session of the volume, the annotation belongs to.
* @apiSuccessExample {json} Success response:
* {
* "id":1,
* "image_id":1,
* "shape_id":1,
* "created_at":"2015-02-13 11:59:23",
* "updated_at":"2015-02-13 11:59:23",
* "points": [100, 100]
* }
*
* @param int $id
* @return ImageAnnotation
*/
public function show($id)
{
$annotation = ImageAnnotation::findOrFail($id);
$this->authorize('access', $annotation);
return $annotation;
}
/**
* Creates a new annotation in the specified image.
*
* @api {post} images/:id/annotations Create a new annotation
* @apiGroup ImageAnnotations
* @apiName StoreImageAnnotations
* @apiPermission projectEditor
* @apiDescription Only labels may be used that belong to a label tree used by one of
* the projects, the image belongs to.
*
* @apiParam {Number} id The image ID.
*
* @apiParam (Required arguments) {Number} shape_id ID of the shape of the new annotation.
* @apiParam (Required arguments) {Number} label_id ID of the initial category label of the new annotation.
* @apiParam (Required arguments) {Number} confidence Confidence of the initial annotation label of the new annotation. Must be a value between 0 and 1.
* @apiParam (Required arguments) {Number[]} points Array of the initial points of the annotation. Must contain at least one point. The points array is interpreted as alternating x and y coordinates like this `[x1, y1, x2, y2...]`. The interpretation of the points of the different shapes is as follows:
* **Point:** The first point is the center of the annotation point.
* **Rectangle:** The first four points are the vertices of the rectangle (in the given order).
* **Polygon:** Like rectangle with one or more vertices. The first and last vertices of a polygon must have the same coordinates.
* **LineString:** Like rectangle with one or more vertices.
* **Circle:** The first point is the center of the circle. The third value of the points array is the radius of the circle. A valid points array of a circle might look like this: `[10, 10, 5]`.
* **Ellipse:** The four points specify the end points of the semi-major and semi-minor axes of the ellipse in (counter-)clockwise ordering (depending on how the ellipse was drawn). So the first point is the end point of axis 1, the second is the end point of axis 2, the third is the other end point of axis 1 and the fourth is the other end point of axis 2.
*
* @apiParamExample {JSON} Request example (JSON):
* {
* "shape_id": 3,
* "label_id": 1,
* "confidence": 0.75,
* "points": [10, 11, 20, 21]
* }
*
* @apiSuccessExample {json} Success response:
* {
* "created_at": "2015-02-18 11:45:00",
* "id": 1,
* "image_id": 1,
* "shape_id": 3,
* "updated_at": "2015-02-18 11:45:00",
* "points": [10, 11, 20, 21],
* "labels": [
* {
* "confidence": 1,
* "id": 1,
* "label": {
* "color": "bada55",
* "id": 3,
* "name": "My label",
* "parent_id": null,
* "project_id": null
* },
* "user": {
* "id": 4,
* "firstname": "Graham",
* "lastname": "Hahn",
* "role_id": 2
* }
* }
* ]
* }
*
* @param StoreImageAnnotation $request
* @return ImageAnnotation
*/
public function store(StoreImageAnnotation $request)
{
$points = $request->input('points');
$annotation = new ImageAnnotation;
$annotation->shape_id = $request->input('shape_id');
$annotation->image()->associate($request->image);
try {
$annotation->validatePoints($points);
} catch (Exception $e) {
throw ValidationException::withMessages(['points' => [$e->getMessage()]]);
}
$annotation->points = $points;
$label = Label::findOrFail($request->input('label_id'));
$this->authorize('attach-label', [$annotation, $label]);
DB::transaction(function () use ($annotation, $request, $label) {
$annotation->save();
$annotationLabel = new ImageAnnotationLabel;
$annotationLabel->label_id = $label->id;
$annotationLabel->user_id = $request->user()->id;
$annotationLabel->confidence = $request->input('confidence');
$annotation->labels()->save($annotationLabel);
});
$annotation->load('labels.label', 'labels.user');
return $annotation;
}
/**
* @api {put} annotations/:id Update an annotation
* @apiDeprecated use now (#ImageAnnotations:UpdateImageAnnotation).
* @apiGroup Annotations
* @apiName UpdateAnnotation
* @apiPermission projectEditor
*
* @apiParam {Number} id The annotation ID.
* @apiParam (Attributes that can be updated) {Number} shape_id ID of the new shape of the annotation.
* @apiParam (Attributes that can be updated) {Number[]} points Array of new points of the annotation. The new points will replace the old points. See the "Create a new annotation" endpoint for how the points are interpreted for different shapes.
* @apiParamExample {json} Request example (JSON):
* {
* "points": [10, 11, 20, 21],
* "shape_id": 3
* }
*/
/**
* Updates the annotation including its points.
*
* @api {put} image-annotations/:id Update an annotation
* @apiGroup ImageAnnotations
* @apiName UpdateImageAnnotation
* @apiPermission projectEditor
*
* @apiParam {Number} id The annotation ID.
* @apiParam (Attributes that can be updated) {Number} shape_id ID of the new shape of the annotation.
* @apiParam (Attributes that can be updated) {Number[]} points Array of new points of the annotation. The new points will replace the old points. See the "Create a new annotation" endpoint for how the points are interpreted for different shapes.
* @apiParamExample {json} Request example (JSON):
* {
* "points": [10, 11, 20, 21],
* "shape_id": 3
* }
*
* @param Request $request
* @param int $id
*/
public function update(Request $request, $id)
{
$annotation = ImageAnnotation::findOrFail($id);
$this->authorize('update', $annotation);
$request->validate([
'shape_id' => 'required_without:points|integer|exists:shapes,id',
'points' => 'required_without:shape_id|array',
]);
// from a JSON request, the array may already be decoded
$points = $request->input('points', $annotation->points);
$annotation->shape_id = $request->input('shape_id', $annotation->shape_id);
try {
$annotation->validatePoints($points);
} catch (Exception $e) {
throw ValidationException::withMessages(['points' => [$e->getMessage()]]);
}
$annotation->points = $points;
$annotation->save();
}
/**
* @api {delete} annotations/:id Delete an annotation
* @apiDeprecated use now (#ImageAnnotations:DestroyImageAnnotation).
* @apiGroup Annotations
* @apiName DestroyAnnotation
* @apiPermission projectEditor
*
* @apiParam {Number} id The annotation ID.
*/
/**
* Removes the annotation.
*
* @api {delete} image-annotations/:id Delete an annotation
* @apiGroup ImageAnnotations
* @apiName DestroyImageAnnotation
* @apiPermission projectEditor
*
* @apiParam {Number} id The annotation ID.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$annotation = ImageAnnotation::findOrFail($id);
$this->authorize('destroy', $annotation);
$annotation->delete();
return response('Deleted.', 200);
}
}