33import json
44import datetime
55import numpy as np
6+ import imantics as im
67
8+ from PIL import Image
79from flask_mongoengine import MongoEngine
810from mongoengine .queryset .visitor import Q
911from flask_login import UserMixin , current_user
1012
1113
12- from .util import color_util
1314from .config import Config
1415from PIL import Image
1516
@@ -129,6 +130,10 @@ def thumbnail_path(self):
129130 os .makedirs (directory )
130131
131132 return '/' .join (folders )
133+
134+ def thumbnail (self ):
135+ image = self ().draw (color_by_category = True , bbox = False )
136+ return Image .fromarray (image )
132137
133138 def copy_annotations (self , annotations ):
134139 """
@@ -148,6 +153,15 @@ def copy_annotations(self, annotations):
148153
149154 return annotations .count ()
150155
156+ def __call__ (self ):
157+
158+ image = im .Image .from_path (self .path )
159+ for annotation in AnnotationModel .objects (image_id = self .id , deleted = False ).all ():
160+ if not annotation .is_empty ():
161+ image .add (annotation ())
162+
163+ return image
164+
151165
152166class AnnotationModel (db .DynamicDocument ):
153167
@@ -196,7 +210,7 @@ def save(self, copy=False, *args, **kwargs):
196210 self .metadata = dataset .default_annotation_metadata .copy ()
197211
198212 if self .color is None :
199- self .color = color_util . random_color_hex ()
213+ self .color = im . Color . random (). hex
200214
201215 if current_user :
202216 self .creator = current_user .username
@@ -225,6 +239,23 @@ def clone(self):
225239
226240 return AnnotationModel (** create )
227241
242+ def __call__ (self ):
243+
244+ category = CategoryModel .objects (id = self .category_id ).first ()
245+ if category :
246+ category = category ()
247+
248+ data = {
249+ 'image' : None ,
250+ 'category' : category ,
251+ 'color' : self .color ,
252+ 'polygons' : self .segmentation ,
253+ 'width' : self .width ,
254+ 'height' : self .height ,
255+ 'metadata' : self .metadata
256+ }
257+
258+ return im .Annotation (** data )
228259
229260class CategoryModel (db .DynamicDocument ):
230261
@@ -260,7 +291,7 @@ def bulk_create(cls, categories):
260291 def save (self , * args , ** kwargs ):
261292
262293 if not self .color :
263- self .color = color_util . random_color_hex ()
294+ self .color = im . Color . random (). hex
264295
265296 if current_user :
266297 self .creator = current_user .username
@@ -269,6 +300,16 @@ def save(self, *args, **kwargs):
269300
270301 return super (CategoryModel , self ).save (* args , ** kwargs )
271302
303+ def __call__ (self ):
304+ """ Generates imantics category object """
305+ data = {
306+ 'name' : self .name ,
307+ 'color' : self .color ,
308+ 'parent' : self .supercategory ,
309+ 'metadata' : self .metadata ,
310+ 'id' : self .id
311+ }
312+ return im .Category (** data )
272313
273314class LicenseModel (db .DynamicDocument ):
274315 id = db .SequenceField (primary_key = True )
0 commit comments