1- # import sys
21import string
3- # import os
4- # import time
52import logging
63import random
74from enum import IntEnum , unique
85import getpass
96import itertools
10-
117from ..db_provider import DatabaseInfo , PostGresProvider
128
9+
1310@unique
1411class ImageTagState (IntEnum ):
1512 NOT_READY = 0
@@ -19,6 +16,7 @@ class ImageTagState(IntEnum):
1916 INCOMPLETE_TAG = 4
2017 ABANDONED = 5
2118
19+
2220# An entity class for a VOTT image
2321class ImageInfo (object ):
2422 def __init__ (self , image_name , image_location , height , width ):
@@ -27,14 +25,25 @@ def __init__(self, image_name, image_location, height, width):
2725 self .height = height
2826 self .width = width
2927
28+
29+ # Entity class for Tags stored in DB
3030class ImageTag (object ):
3131 def __init__ (self , image_id , x_min , x_max , y_min , y_max , classification_names ):
32- self .image_id = image_id
33- self .x_min = x_min
34- self .x_max = x_max
35- self .y_min = y_min
36- self .y_max = y_max
37- self .classification_names = classification_names
32+ self .image_id = image_id
33+ self .x_min = x_min
34+ self .x_max = x_max
35+ self .y_min = y_min
36+ self .y_max = y_max
37+ self .classification_names = classification_names
38+
39+
40+ # Vott tags have image height & width data as well.
41+ class VottImageTag (ImageTag ):
42+ def __init__ (self , image_id , x_min , x_max , y_min , y_max , classification_names , image_height , image_width ):
43+ super ().__init__ (image_id , x_min , x_max , y_min , y_max , classification_names )
44+ self .image_height = image_height
45+ self .image_width = image_width
46+
3847
3948class ImageTagDataAccess (object ):
4049 def __init__ (self , db_provider ):
@@ -69,7 +78,7 @@ def create_user(self,user_name):
6978 finally : conn .close ()
7079 return user_id
7180
72- def get_new_images (self , number_of_images , user_id ):
81+ def get_images_for_tagging (self , number_of_images , user_id ):
7382 if number_of_images <= 0 :
7483 raise ArgumentException ("Parameter must be greater than zero" )
7584
@@ -84,14 +93,16 @@ def get_new_images(self, number_of_images, user_id):
8493 cursor .execute (query .format (number_of_images , ImageTagState .READY_TO_TAG , ImageTagState .INCOMPLETE_TAG ))
8594 for row in cursor :
8695 logging .debug ('Image Id: {0} \t \t Image Name: {1} \t \t Tag State: {2}' .format (row [0 ], row [1 ], row [2 ]))
87- selected_images_to_tag [str ( row [0 ]) ] = str (row [1 ])
96+ selected_images_to_tag [row [0 ]] = str (row [1 ])
8897 self ._update_images (selected_images_to_tag ,ImageTagState .TAG_IN_PROGRESS , user_id , conn )
89- finally : cursor .close ()
98+ finally :
99+ cursor .close ()
90100 except Exception as e :
91101 logging .error ("An errors occured getting images: {0}" .format (e ))
92102 raise
93- finally : conn .close ()
94- return selected_images_to_tag .values ()
103+ finally :
104+ conn .close ()
105+ return selected_images_to_tag
95106
96107 def add_new_images (self ,list_of_image_infos , user_id ):
97108
@@ -119,6 +130,106 @@ def add_new_images(self,list_of_image_infos, user_id):
119130 finally : conn .close ()
120131 return url_to_image_id_map
121132
133+ def get_tag_complete_images (self , number_of_images , user_id ):
134+ if number_of_images <= 0 :
135+ raise ArgumentException ("Parameter must be greater than zero" )
136+
137+ tag_complete_images = {}
138+ try :
139+ conn = self ._db_provider .get_connection ()
140+ try :
141+ cursor = conn .cursor ()
142+ query = ("SELECT b.ImageId, b.ImageLocation, a.TagStateId FROM Image_Tagging_State a "
143+ "JOIN Image_Info b ON a.ImageId = b.ImageId WHERE a.TagStateId = {1} order by "
144+ "a.createddtim DESC limit {0}" )
145+ cursor .execute (query .format (number_of_images , ImageTagState .COMPLETED_TAG ))
146+ for row in cursor :
147+ logging .debug ('Image Id: {0} \t \t Image Name: {1} \t \t Tag State: {2}' .format (row [0 ], row [1 ], row [2 ]))
148+ tag_complete_images [row [0 ]] = str (row [1 ])
149+ finally :
150+ cursor .close ()
151+ except Exception as e :
152+ logging .error ("An errors occured getting images: {0}" .format (e ))
153+ raise
154+ finally :
155+ conn .close ()
156+ return tag_complete_images
157+
158+ def get_image_tags (self , image_id ):
159+ if type (image_id ) is not int :
160+ raise TypeError ('image_id must be an integer' )
161+
162+ try :
163+ conn = self ._db_provider .get_connection ()
164+ try :
165+ cursor = conn .cursor ()
166+ query = ("SELECT image_tags.imagetagid, image_info.imageid, x_min, x_max, y_min, y_max, "
167+ "classification_info.classificationname, image_info.height, image_info.width "
168+ "FROM image_tags "
169+ "inner join tags_classification on image_tags.imagetagid = tags_classification.imagetagid "
170+ "inner join classification_info on tags_classification.classificationid = classification_info.classificationid "
171+ "inner join image_info on image_info.imageid = image_tags.imageid "
172+ "WHERE image_tags.imageid = {0};" )
173+ cursor .execute (query .format (image_id ,))
174+
175+ logging .debug ("Got image tags back for image_id={}" .format (image_id ))
176+ tag_id_to_VottImageTag = self .__build_id_to_VottImageTag (cursor )
177+
178+ finally :
179+ cursor .close ()
180+ except Exception as e :
181+ logging .error ("An error occurred getting image tags for image id = {0}: {1}" .format (image_id , e ))
182+ raise
183+ finally :
184+ conn .close ()
185+ return list (tag_id_to_VottImageTag .values ())
186+
187+ def __build_id_to_VottImageTag (self , tag_db_cursor ):
188+ tag_id_to_VottImageTag = {}
189+ try :
190+ for row in tag_db_cursor :
191+ logging .debug (row )
192+ tag_id = row [0 ]
193+ if tag_id in tag_id_to_VottImageTag :
194+ logging .debug ("Existing ImageTag found, appending classification {}" , row [6 ])
195+ tag_id_to_VottImageTag [tag_id ].classification_names .append (row [6 ].strip ())
196+ else :
197+ logging .debug ("No existing ImageTag found, creating new ImageTag: "
198+ "id={0} x_min={1} x_max={2} x_min={3} x_max={4} classification={5} "
199+ "image_height={6} image_width={7}"
200+ .format (row [1 ], float (row [2 ]), float (row [3 ]), float (row [4 ]), float (row [5 ]),
201+ [row [6 ].strip ()], row [7 ], row [8 ]))
202+ tag_id_to_VottImageTag [tag_id ] = VottImageTag (row [1 ], float (row [2 ]), float (row [3 ]),
203+ float (row [4 ]), float (row [5 ]), [row [6 ].strip ()],
204+ row [7 ], row [8 ])
205+ except Exception as e :
206+ logging .error ("An error occurred building VottImageTag dict: {0}" .format (e ))
207+ raise
208+ return tag_id_to_VottImageTag
209+
210+
211+ def get_existing_classifications (self ):
212+ try :
213+ conn = self ._db_provider .get_connection ()
214+ try :
215+ cursor = conn .cursor ()
216+ query = "SELECT classificationname from classification_info order by classificationname asc"
217+ cursor .execute (query )
218+
219+ classification_set = set ()
220+ for row in cursor :
221+ logging .debug (row )
222+ classification_set .add (row [0 ])
223+ logging .debug ("Got back {0} classifications existing in db." .format (len (classification_set )))
224+ finally :
225+ cursor .close ()
226+ except Exception as e :
227+ logging .error ("An error occurred getting classifications from DB: {0}" .format (e ))
228+ raise
229+ finally :
230+ conn .close ()
231+ return list (classification_set )
232+
122233 def update_incomplete_images (self , list_of_image_ids , user_id ):
123234 #TODO: Make sure the image ids are in a TAG_IN_PROGRESS state
124235 self ._update_images (list_of_image_ids ,ImageTagState .INCOMPLETE_TAG ,user_id , self ._db_provider .get_connection ())
@@ -229,6 +340,12 @@ def main():
229340 # Checking in images been tagged
230341 #################################################################
231342
343+ # import sys
344+ # import os
345+ # sys.path.append("..")
346+ # sys.path.append(os.path.abspath('db_provider'))
347+ # from db_provider import DatabaseInfo, PostGresProvider
348+
232349 #Replace me for testing
233350 db_config = DatabaseInfo ("" ,"" ,"" ,"" )
234351 data_access = ImageTagDataAccess (PostGresProvider (db_config ))
@@ -241,7 +358,9 @@ def main():
241358 image_tags = generate_test_image_tags (list (url_to_image_id_map .values ()),4 ,4 )
242359 data_access .update_tagged_images (image_tags ,user_id )
243360
244- TestClassifications = ("maine coon" ,"german shephard" ,"goldfinch" ,"mackerel" ," african elephant" ,"rattlesnake" )
361+
362+ TestClassifications = ("maine coon" ,"german shephard" ,"goldfinch" ,"mackerel" ,"african elephant" ,"rattlesnake" )
363+
245364
246365def generate_test_image_infos (count ):
247366 list_of_image_infos = []
0 commit comments