Skip to content

Commit 9bf5fc6

Browse files
author
Mark Bray
committed
Move width/height extraction to where it's actually needed.
1 parent da5c7e1 commit 9bf5fc6

4 files changed

Lines changed: 41 additions & 45 deletions

File tree

cpa/classifier.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,22 @@ def __init__(self, properties=None, parent=None, id=ID_CLASSIFIER, **kwargs):
8484
:] # used to store previous color mappings when toggling colors on/off with ctrl+1,2,3...
8585
self.brightness = 1.0
8686
self.required_fields = []
87+
8788
if not p.classification_type == 'image':
89+
self.image_tile_size = p.image_tile_size
8890
self.scale = 1.0
89-
self.required_fields = ['object_table', 'object_id', 'cell_x_loc', 'cell_y_loc']
9091
else:
91-
self.scale = 100.0/p.image_tile_size
92+
if p.field_defined('image_width') and p.field_defined('image_height'):
93+
self.image_tile_size = min([p.image_width, p.image_height])
94+
else:
95+
cols = [x for x in db.GetColumnNames(p.image_table)]
96+
list_of_cols = [str(x) for x in cols]
97+
image_width, image_height = db.GetImageWidthHeight(list_of_cols)
98+
self.image_tile_size = min([image_width, image_height])
99+
self.scale = 100.0/float(self.image_tile_size)
92100

101+
self.required_fields = ['object_table', 'object_id', 'cell_x_loc', 'cell_y_loc']
102+
93103
for field in self.required_fields:
94104
if not p.field_defined(field):
95105
errdlg = wx.MessageDialog(self, 'Properties field "%s" is required for Classifier.'% (field),
@@ -968,7 +978,7 @@ def OnFetch(self, evt):
968978
attempts = 0
969979
statusMsg += loopMsg
970980

971-
self.unclassifiedBin.AddObjects(obKeys[:nObjects], self.chMap, pos='last')
981+
self.unclassifiedBin.AddObjects(obKeys[:nObjects], self.chMap, pos='last',display_whole_image=p.classification_type == 'image')
972982
self.PostMessage(statusMsg)
973983

974984
def OnTileUpdated(self, evt):
@@ -1124,7 +1134,7 @@ def LoadTrainingSet(self, filename):
11241134
num_objs = 0
11251135
for bin in self.classBins:
11261136
if bin.label in keysPerBin.keys():
1127-
bin.AddObjects(keysPerBin[bin.label], self.chMap, priority=2)
1137+
bin.AddObjects(keysPerBin[bin.label], self.chMap, priority=2,display_whole_image=p.classification_type == 'image')
11281138
num_objs += len(keysPerBin[bin.label])
11291139

11301140
self.PostMessage('Training set loaded (%d %s).'%(num_objs,p.object_name[1]))

cpa/dbconnect.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,27 +1437,27 @@ def CreateSQLiteDBFromCSVs(self):
14371437
if dlg:
14381438
dlg.Destroy()
14391439

1440+
def GetImageWidthHeight(self,list_of_cols):
1441+
# Get image width and height
1442+
try:
1443+
width_col = next(name for name in list_of_cols if 'width' in name.lower())
1444+
height_col = next(name for name in list_of_cols if 'height' in name.lower())
1445+
width_query = 'SELECT %s FROM %s LIMIT 1'%(width_col, p.image_table)
1446+
height_query = 'SELECT %s FROM %s LIMIT 1'%(height_col, p.image_table)
1447+
width = self.execute(width_query)
1448+
height = self.execute(height_query)
1449+
width = int(width[0][0])
1450+
height = int(height[0][0])
1451+
except:
1452+
if p.image_width and p.image_height:
1453+
width = int(p.image_width)
1454+
height = int(p.image_height)
1455+
else:
1456+
raise Exception('Input image_width and image_height fields in properties file')
1457+
return width, height
1458+
14401459
def CreateObjectImageTable(self):
14411460
# Create object table for image classification
1442-
def getImageWidthHeight(list_of_cols):
1443-
#get width and height
1444-
try:
1445-
width_col = next(name for name in list_of_cols if 'width' in name.lower())
1446-
height_col = next(name for name in list_of_cols if 'height' in name.lower())
1447-
width_query = 'SELECT %s FROM %s LIMIT 1'%(width_col, p.image_table)
1448-
height_query = 'SELECT %s FROM %s LIMIT 1'%(height_col, p.image_table)
1449-
width = self.execute(width_query)
1450-
height = self.execute(height_query)
1451-
width = int(width[0][0])
1452-
height = int(height[0][0])
1453-
except:
1454-
if p.image_width and p.image_height:
1455-
width = int(p.image_width)
1456-
height = int(p.image_height)
1457-
else:
1458-
raise Exception('Input image_width and image_height fields in properties file')
1459-
return width, height
1460-
14611461
DB_NAME = p.db_name
14621462
DB_TYPE = p.db_type.lower()
14631463
if DB_TYPE == 'mysql':
@@ -1466,7 +1466,7 @@ def getImageWidthHeight(list_of_cols):
14661466
list_of_cols = []
14671467
cols = [x for x in self.GetColumnNames(p.image_table)]
14681468
list_of_cols.extend([str(x) for x in cols])
1469-
width, height = getImageWidthHeight(list_of_cols)
1469+
width, height = self.GetImageWidthHeight(list_of_cols)
14701470

14711471
query = "CREATE OR REPLACE VIEW %s AS SELECT 1 AS %s, %d AS %s, %d AS %s, %s.* FROM %s"%(p.object_table, p.object_id, width/2, p.cell_x_loc, height/2, p.cell_y_loc, p.image_table,p.image_table)
14721472
self.execute(query)
@@ -1488,7 +1488,7 @@ def getImageWidthHeight(list_of_cols):
14881488
self.execute(query)
14891489

14901490
#Get info on image width and height (assuming they are fields in the image table) to get image center
1491-
width, height = getImageWidthHeight(list_of_cols)
1491+
width, height = self.GetImageWidthHeight(list_of_cols)
14921492

14931493
query = "UPDATE %s SET %s=1, %s=%s, %s=%s"%(p.object_table, p.object_id,
14941494
p.cell_x_loc, width/2,

cpa/imagegallery.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,6 @@ def __init__(self, properties=None, parent=None, id=ID_IMAGE_GALLERY, **kwargs):
7575
logging.critical('ImageGallery requires a properties file. Exiting.')
7676
raise Exception('ImageGallery requires a properties file. Exiting.')
7777

78-
# self.required_fields = []
79-
80-
# if not p.classification_type == 'image':
81-
# self.scale = 1.0
82-
# self.required_fields = ['object_table', 'object_id', 'cell_x_loc', 'cell_y_loc']
83-
# else:
84-
# self.scale = 100.0/p.image_tile_size
85-
86-
# for field in self.required_fields:
87-
# if not p.field_defined(field):
88-
# raise Exception('Properties field "%s" is required for ImageGallery.' % (field))
89-
# self.Destroy()
90-
# return
91-
9278
self.pmb = None
9379
self.worker = None
9480
self.trainingSet = None
@@ -98,10 +84,8 @@ def __init__(self, properties=None, parent=None, id=ID_IMAGE_GALLERY, **kwargs):
9884
self.toggleChMap = p.image_channel_colors[
9985
:] # used to store previous color mappings when toggling colors on/off with ctrl+1,2,3...
10086
self.brightness = 1.0
101-
if p.classification_type == 'image':
102-
self.scale = 100.0 / float(p.image_tile_size) # guarantee it is parsed as float
103-
else:
104-
self.scale = 1.0
87+
self.scale = 1.0
88+
10589
self.contrast = 'Linear'
10690
self.defaultTSFileName = None
10791
self.defaultModelFileName = None

cpa/properties.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,12 @@ def load_file(self, filename):
313313
self.cell_y_loc = 'Image_y_loc'
314314
self.object_name = ['image','images']
315315

316-
# image_width and image_height refer to the full image width and height while image_tile_size refers to the size of tiles for classification
316+
# image_width and image_height refer to the full image width and height while
317+
# image_tile_size refers to the size of tiles for classification
317318
# NB: if image_width and image_height are defined, they override image_tile_size
318319
if self.field_defined('image_width') and self.field_defined('image_height'):
319-
self.image_tile_size = min([self.image_width, self.image_height])
320+
self.image_width, self.image_height = int(self.image_width), int(self.image_height)
321+
self.image_tile_size = min([self.image_width,self.image_height])
320322

321323
self.image_tile_size = int(self.image_tile_size)
322324
else:

0 commit comments

Comments
 (0)