def __init__(gm, hs=None): super(ImageManager, gm).__init__(hs) logdbg('Creating Image Manager') # --- Flat Table --- gm.gx2_gid = array([], dtype=uint32) gm.gx2_gname = array([], dtype=object) gm.gx2_aif_bit = array([], dtype=bool) gm.gx2_cx_list = array([], dtype=list) # --- Reverse Indexes --- gm.gname2_gid = {} gm.gid2_gx = array([], dtype=uint32) # --- Statistics -- gm.next_gx = 1 gm.next_gid = 1 gm.num_g = 0 gm.max_gx = 0 gm.max_gid = 0 gm.max_gnamelen = 0 gm.max_gname = '' #---------- gm.hs = hs #Parent gm.x2_lbl = \ { 'gid' : lambda x: gm.gx2_gid[x],\ 'aif' : lambda x: gm.gx2_aif_bit[x],\ 'cxs' : lambda x: gm.gx2_cx_list[x],\ 'cids' : lambda x: str(gm.gx2_cids(x)),\ 'gname' : lambda x: gm.gx2_gname[x] ,\ 'num_c' : lambda x: gm.gx2_num_c(x),\ } gm.valid_img_extensions = img_ext_set
def _quick_and_dirty_batch_rename(fac): from front.ChangeNameDialog import Ui_changeNameDialog cm, nm, uim = fac.hs.get_managers('cm','nm', 'uim') try: if uim.sel_cid != None: name = cm.cx2_name(uim.sel_cx()) else: name = '' except Exception as ex: print 'A quick and dirty exception was caught' logdbg(str(ex)) name = '' class ChangeNameDialog(QDialog): def __init__(self, name, fac): super( ChangeNameDialog, self ).__init__() self.dlg_skel = Ui_changeNameDialog() self.dlg_skel.setupUi(self) self.dlg_skel.oldNameEdit.setText(name) def qad_batch_rename(): print 'qad batch renaming' try: name1 = str(self.dlg_skel.oldNameEdit.text()) name2 = str(self.dlg_skel.newNameEdit.text()) fac.hs.batch_rename(name1, name2) except Exception as ex: logerr(str(ex)) fac.hs.uim.populate_tables() fac.hs.uim.draw() self.close() self.dlg_skel.buttonBox.ApplyRole = self.dlg_skel.buttonBox.AcceptRole self.dlg_skel.buttonBox.accepted.connect(qad_batch_rename) self.show() changeNameDlg = ChangeNameDialog(name, fac) self = changeNameDlg
def __init__(gm,hs=None): super( ImageManager, gm ).__init__( hs ) logdbg('Creating Image Manager') # --- Flat Table --- gm.gx2_gid = array([], dtype=uint32) gm.gx2_gname = array([], dtype=object) gm.gx2_aif_bit = array([], dtype=bool) gm.gx2_cx_list = array([], dtype=list) # --- Reverse Indexes --- gm.gname2_gid = {} gm.gid2_gx = array([], dtype=uint32) # --- Statistics -- gm.next_gx = 1 gm.next_gid = 1 gm.num_g = 0 gm.max_gx = 0 gm.max_gid = 0 gm.max_gnamelen = 0 gm.max_gname = '' #---------- gm.hs = hs #Parent gm.x2_lbl = \ { 'gid' : lambda x: gm.gx2_gid[x],\ 'aif' : lambda x: gm.gx2_aif_bit[x],\ 'cxs' : lambda x: gm.gx2_cx_list[x],\ 'cids' : lambda x: str(gm.gx2_cids(x)),\ 'gname' : lambda x: gm.gx2_gname[x] ,\ 'num_c' : lambda x: gm.gx2_num_c(x),\ } gm.valid_img_extensions = img_ext_set
def match_all_above_thresh(fac, threshold=None): 'do matching and assign all above thresh' if threshold == None: # User ask dlg = QInputDialog() threshres = dlg.getText( None, 'Threshold Selector', 'Enter a matching threshold.\n' + 'The system will query each chip and assign all matches above this thresh' ) if not threshres[1]: logmsg('Cancelled all match') return try: threshold = float(str(threshres[0])) except ValueError: logerr('The threshold must be a number') qm = fac.hs.qm cm = fac.hs.cm nm = fac.hs.nm vm = fac.hs.vm # Get model ready vm.sample_train_set() fac.hs.ensure_model() # Do all queries for qcx in iter(cm.get_valid_cxs()): qcid = cm.cx2_cid[qcx] logmsg('Querying CID=' + str(qcid)) query_name = cm.cx2_name(qcx) logdbg(str(qcx)) logdbg(str(type(qcx))) cm.load_features(qcx) res = fac.hs.query(qcid) # Match only those above a thresh res.num_top_min = 0 res.num_extra_return = 0 res.top_thresh = threshold top_cx = res.top_cx() if len(top_cx) == 0: print('No matched for cid=' + str(qcid)) continue top_names = cm.cx2_name(top_cx) all_names = np.append(top_names, [query_name]) if all([nm.UNIDEN_NAME() == name for name in all_names]): # If all names haven't been identified, make a new one new_name = nm.get_new_name() else: # Rename to the most frequent non ____ name seen from collections import Counter name_freq = Counter(np.append(top_names, [query_name])).most_common() new_name = name_freq[0][0] if new_name == nm.UNIDEN_NAME(): new_name = name_freq[1][0] # Do renaming cm.rename_chip(qcx, new_name) for cx in top_cx: cm.rename_chip(cx, new_name) fac.hs.uim.populate_tables()
def name_alloc(nm, xAlloc): logdbg('Allocating room for '+str(xAlloc)+' more names') nm.nx2_nid = append(nm.nx2_nid, zeros(xAlloc,dtype=uint32)) nm.nx2_name = append(nm.nx2_name, empty(xAlloc,dtype=object)) nm.nx2_cx_list = append(nm.nx2_cx_list, alloc_lists(xAlloc)) idAlloc = len(nm.nx2_nid) - len(nm.nid2_nx) + 2 if idAlloc > 0: logdbg('Allocating room for '+str(idAlloc)+' more reverse nids') nm.nid2_nx = append(nm.nid2_nx, zeros(idAlloc,dtype=uint32))
def match_all_above_thresh(fac, threshold=None): 'do matching and assign all above thresh' if threshold == None: # User ask dlg = QInputDialog() threshres = dlg.getText(None, 'Threshold Selector', 'Enter a matching threshold.\n'+ 'The system will query each chip and assign all matches above this thresh') if not threshres[1]: logmsg('Cancelled all match') return try: threshold = float(str(threshres[0])) except ValueError: logerr('The threshold must be a number') qm = fac.hs.qm cm = fac.hs.cm nm = fac.hs.nm vm = fac.hs.vm # Get model ready vm.sample_train_set() fac.hs.ensure_model() # Do all queries for qcx in iter(cm.get_valid_cxs()): qcid = cm.cx2_cid[qcx] logmsg('Querying CID='+str(qcid)) query_name = cm.cx2_name(qcx) logdbg(str(qcx)) logdbg(str(type(qcx))) cm.load_features(qcx) res = fac.hs.query(qcid) # Match only those above a thresh res.num_top_min = 0 res.num_extra_return = 0 res.top_thresh = threshold top_cx = res.top_cx() if len(top_cx) == 0: print('No matched for cid='+str(qcid)) continue top_names = cm.cx2_name(top_cx) all_names = np.append(top_names,[query_name]) if all([nm.UNIDEN_NAME() == name for name in all_names]): # If all names haven't been identified, make a new one new_name = nm.get_new_name() else: # Rename to the most frequent non ____ name seen from collections import Counter name_freq = Counter(np.append(top_names,[query_name])).most_common() new_name = name_freq[0][0] if new_name == nm.UNIDEN_NAME(): new_name = name_freq[1][0] # Do renaming cm.rename_chip(qcx, new_name) for cx in top_cx: cm.rename_chip(cx, new_name) fac.hs.uim.populate_tables()
def name_alloc(nm, xAlloc): logdbg('Allocating room for ' + str(xAlloc) + ' more names') nm.nx2_nid = append(nm.nx2_nid, zeros(xAlloc, dtype=uint32)) nm.nx2_name = append(nm.nx2_name, empty(xAlloc, dtype=object)) nm.nx2_cx_list = append(nm.nx2_cx_list, alloc_lists(xAlloc)) idAlloc = len(nm.nx2_nid) - len(nm.nid2_nx) + 2 if idAlloc > 0: logdbg('Allocating room for ' + str(idAlloc) + ' more reverse nids') nm.nid2_nx = append(nm.nid2_nx, zeros(idAlloc, dtype=uint32))
def img_alloc(gm, nAlloc): logdbg('Allocating room for %d more images' % nAlloc) #-- Forward Allocation gm.gx2_gid = append(gm.gx2_gid, zeros(nAlloc,dtype=uint32)) gm.gx2_gname = append(gm.gx2_gname, zeros(nAlloc,dtype=object)) gm.gx2_aif_bit = append(gm.gx2_aif_bit, zeros(nAlloc,dtype=bool)) gm.gx2_cx_list = append(gm.gx2_cx_list, alloc_lists(nAlloc)) #-- Inverse Allocation idAlloc = len(gm.gx2_gid) - len(gm.gid2_gx) if idAlloc > 0: gm.gid2_gx = append(gm.gid2_gx, zeros(idAlloc,dtype=uint32))
def img_alloc(gm, nAlloc): logdbg('Allocating room for %d more images' % nAlloc) #-- Forward Allocation gm.gx2_gid = append(gm.gx2_gid, zeros(nAlloc, dtype=uint32)) gm.gx2_gname = append(gm.gx2_gname, zeros(nAlloc, dtype=object)) gm.gx2_aif_bit = append(gm.gx2_aif_bit, zeros(nAlloc, dtype=bool)) gm.gx2_cx_list = append(gm.gx2_cx_list, alloc_lists(nAlloc)) #-- Inverse Allocation idAlloc = len(gm.gx2_gid) - len(gm.gid2_gx) if idAlloc > 0: gm.gid2_gx = append(gm.gid2_gx, zeros(idAlloc, dtype=uint32))
def gx2_img_thumb(gm, gx): iom = gm.hs.iom gname = gm.gx2_gname[gx] gid = gm.gx2_gid[gx] img_thumb_fpath = iom.get_img_thumb_fpath(gname) if not os.path.exists(img_thumb_fpath): logdbg('Computing thumbnail of GID=' + str(gid)) raw_img = gm.gx2_img(gx) thumb_size = cm.hs.dm.draw_prefs.thumbnail_size raw_img.thumbnail((thumb_size,thumb_size), Image.ANTIALIAS).save\ (img_thumb_fpath, 'JPEG') logdbg('Wrote thumbnail.') return asarray(Image.open(img_thumb_fpath))
def add_name(nm, nid_, name_): 'Adds a name. If nid == -1 a new nid will be assigned. Returns nid' logdbg('Adding nid=' + str(nid_) + ' name=' + name_) nid = nid_ name = name_.strip() if name == '': logerr('Cannot add an empty name!') if nid < 0: # Generate new nid if not specified nid = nm.next_nid else: #If NID already exists and has an entry, #do not increment, and do nothing #This is essentially like doing a rename #it doesn't actually change anything if (nid < len(nm.nid2_nx) and nm.nid2_nx[nid] > 0): logwarn('NID ' + str(nid) + 'already exists') nx = nm.nid2_nx[nid] return -1 if name in nm.name2_nx.keys(): conflict_nx = nm.name2_nx[name] conflict_nid = nm.nx2_nid[conflict_nx] conflict_msg = 'Name %s already exists in database!\n' % name + \ 'NID=%d; NEXT_NX=%d\n' % (nid, nm.next_nx) + \ 'CONFLICT_NID=%d\n' % conflict_nid if nid_ == -1: logwarn(conflict_msg) elif nid_ > -1: logerr(conflict_msg) nid = conflict_nid nx = conflict_nx return nid #Manage Memory nx = nm.next_nx logdbg(' * nx = ' + str(nx)) if nx >= len(nm.nx2_nid): nm.name_alloc((len(nm.nx2_nid) + 1) * 2 + 1) #Add to flat table nm.nx2_nid[nx] = nid nm.nx2_name[nx] = name if len(name) > nm.max_namelen: nm.max_namelen = len(name) nm.max_name = name #X Reverse indexing if len(nm.nid2_nx) <= nid: idAlloc = (nid - len(nm.nid2_nx)) + 1 nm.nid2_nx = append(nm.nid2_nx, zeros(idAlloc, dtype=uint32)) logdbg(' * nid2_nx[' + str(nid) + ']=' + str(nx)) nm.nid2_nx[nid] = nx nm.name2_nx[name] = nx #Increment nm.next_nx = nm.next_nx + 1 nm.next_nid = max(nm.next_nid + 1, nid + 1) nm.max_nx = max(nm.max_nx, nx) nm.max_nid = max(nm.max_nid, nid) nm.num_n = nm.num_n + 1 logdbg('Added nid=' + str(nid) + ' name=' + name) return nid
def add_name(nm, nid_, name_): 'Adds a name. If nid == -1 a new nid will be assigned. Returns nid' logdbg('Adding nid='+str(nid_)+' name='+name_) nid = nid_ name = name_.strip() if name == '': logerr('Cannot add an empty name!') if nid < 0: # Generate new nid if not specified nid = nm.next_nid else: #If NID already exists and has an entry, #do not increment, and do nothing #This is essentially like doing a rename #it doesn't actually change anything if (nid < len(nm.nid2_nx) and nm.nid2_nx[nid] > 0): logwarn('NID '+str(nid)+'already exists') nx = nm.nid2_nx[nid] return -1 if name in nm.name2_nx.keys(): conflict_nx = nm.name2_nx[name] conflict_nid = nm.nx2_nid[conflict_nx] conflict_msg = 'Name %s already exists in database!\n' % name + \ 'NID=%d; NEXT_NX=%d\n' % (nid, nm.next_nx) + \ 'CONFLICT_NID=%d\n' % conflict_nid if nid_ == -1: logwarn(conflict_msg) elif nid_ > -1: logerr(conflict_msg) nid = conflict_nid nx = conflict_nx return nid #Manage Memory nx = nm.next_nx logdbg(' * nx = '+str(nx)) if nx >= len(nm.nx2_nid): nm.name_alloc((len(nm.nx2_nid)+1)*2+1) #Add to flat table nm.nx2_nid[nx] = nid nm.nx2_name[nx] = name if len(name) > nm.max_namelen: nm.max_namelen = len(name) nm.max_name = name #X Reverse indexing if len(nm.nid2_nx) <= nid: idAlloc = (nid - len(nm.nid2_nx)) + 1 nm.nid2_nx = append(nm.nid2_nx, zeros(idAlloc,dtype=uint32)) logdbg( ' * nid2_nx['+str(nid)+']='+str(nx) ) nm.nid2_nx[nid] = nx nm.name2_nx[name] = nx #Increment nm.next_nx = nm.next_nx + 1 nm.next_nid = max(nm.next_nid + 1, nid + 1) nm.max_nx = max(nm.max_nx , nx) nm.max_nid = max(nm.max_nid, nid) nm.num_n = nm.num_n + 1 logdbg('Added nid='+str(nid)+' name='+name) return nid
def __init__(fac, use_gui=True, autoload=True): super( Facade, fac ).__init__() # Create API fac.hs = HotSpotterAPI(autoload=False) if use_gui: #Make GUI? logdbg('Starting with gui') uim = fac.hs.uim uim.start_gui(fac) fac.show_main_window() else: #HACKY HACKY HACK logdbg('Starting without gui') fac.hs.dm.fignum = 1 fac.hs.uim.start_gui(fac) #TODO: Remove try: # Open previous database fac.open_db(None, autoload) except Exception as ex: import traceback print("Error occurred in autoload") print(str(ex)) print('<<<<<< Traceback >>>>>') traceback.print_exc() print("Error occurred in autoload")
def __init__(fac, use_gui=True, autoload=True): super(Facade, fac).__init__() # Create API fac.hs = HotSpotterAPI(autoload=False) if use_gui: #Make GUI? logdbg('Starting with gui') uim = fac.hs.uim uim.start_gui(fac) fac.show_main_window() else: #HACKY HACKY HACK logdbg('Starting without gui') fac.hs.dm.fignum = 1 fac.hs.uim.start_gui(fac) #TODO: Remove try: # Open previous database fac.open_db(None, autoload) except Exception as ex: import traceback print("Error occurred in autoload") print(str(ex)) print('<<<<<< Traceback >>>>>') traceback.print_exc() print("Error occurred in autoload")
def _quick_and_dirty_batch_rename(fac): from front.ChangeNameDialog import Ui_changeNameDialog cm, nm, uim = fac.hs.get_managers('cm', 'nm', 'uim') try: if uim.sel_cid != None: name = cm.cx2_name(uim.sel_cx()) else: name = '' except Exception as ex: print 'A quick and dirty exception was caught' logdbg(str(ex)) name = '' class ChangeNameDialog(QDialog): def __init__(self, name, fac): super(ChangeNameDialog, self).__init__() self.dlg_skel = Ui_changeNameDialog() self.dlg_skel.setupUi(self) self.dlg_skel.oldNameEdit.setText(name) def qad_batch_rename(): print 'qad batch renaming' try: name1 = str(self.dlg_skel.oldNameEdit.text()) name2 = str(self.dlg_skel.newNameEdit.text()) fac.hs.batch_rename(name1, name2) except Exception as ex: logerr(str(ex)) fac.hs.uim.populate_tables() fac.hs.uim.draw() self.close() self.dlg_skel.buttonBox.ApplyRole = self.dlg_skel.buttonBox.AcceptRole self.dlg_skel.buttonBox.accepted.connect(qad_batch_rename) self.show() changeNameDlg = ChangeNameDialog(name, fac) self = changeNameDlg
def logdbgSlot(fac, msg): # This function is a hack so MainWin can call logdbg logdbg(msg)
def query(fac, qcid=None): 'Performs a query' uim, cm, qm, vm, nm = fac.hs.get_managers('uim', 'cm', 'qm', 'vm', 'nm') try: if qcid is None: qcid = uim.sel_cid else: uim.select_cid(qcid) qcx = cm.cx(qcid) uim.update_state('Querying') print('Querying Chip: ' + cm.cx2_info(qcx, clbls)) logdbg('\n\nQuerying Chip: ' + cm.cx2_info(qcx, clbls)) uim.sel_res = fac.hs.query(qcid) logmsg('\n\nFinished Query') uim.update_state('done_querying') logmsg(str(uim.sel_res)) logdbg('\n\n*** Populating Results Tables ***') uim.populate_result_table() logdbg('\n\n*** Switching To Result Views ***') uim.update_state('result_view') logdbg('\n\n*** Redrawing UI ***') uim.draw() logdbg('\n\n*** Done Redrawing UI ***') # QUICK AND DIRTY CODE. PLEASE FIXME try: cx1 = uim.sel_res.rr.qcx cx2 = uim.sel_res.top_cx()[0] if uim.ui_prefs.prompt_after_result and cm.cx2_nx[ cx1] == nm.UNIDEN_NX( ) and cm.cx2_nx[cx2] != nm.UNIDEN_NX(): logdbg('Quick and dirty prompting') fac._quick_and_dirty_result_prompt(uim.sel_res.rr.qcx, uim.sel_res.top_cx()[0]) else: logdbg('No Quick and dirty prompting') except Exception as ex: logdbg('bad quick and dirty facade code: ' + str(ex)) pass logdbg('\n\n-----------Query OVER-------------\n\n') except Exception as ex: uim.update_state('done_querying') uim.update_state('query_failed') raise
def query(fac, qcid=None): 'Performs a query' uim, cm, qm, vm, nm = fac.hs.get_managers('uim', 'cm','qm','vm', 'nm') try: if qcid is None: qcid = uim.sel_cid else: uim.select_cid(qcid) qcx = cm.cx(qcid) uim.update_state('Querying') print('Querying Chip: '+cm.cx2_info(qcx, clbls)) logdbg('\n\nQuerying Chip: '+cm.cx2_info(qcx, clbls)) uim.sel_res = fac.hs.query(qcid) logmsg('\n\nFinished Query') uim.update_state('done_querying') logmsg(str(uim.sel_res)) logdbg('\n\n*** Populating Results Tables ***') uim.populate_result_table() logdbg('\n\n*** Switching To Result Views ***') uim.update_state('result_view') logdbg('\n\n*** Redrawing UI ***') uim.draw() logdbg('\n\n*** Done Redrawing UI ***') # QUICK AND DIRTY CODE. PLEASE FIXME try: cx1 = uim.sel_res.rr.qcx cx2 = uim.sel_res.top_cx()[0] if uim.ui_prefs.prompt_after_result and cm.cx2_nx[cx1] == nm.UNIDEN_NX() and cm.cx2_nx[cx2] != nm.UNIDEN_NX(): logdbg('Quick and dirty prompting') fac._quick_and_dirty_result_prompt(uim.sel_res.rr.qcx, uim.sel_res.top_cx()[0]) else: logdbg('No Quick and dirty prompting') except Exception as ex: logdbg('bad quick and dirty facade code: '+str(ex)) pass logdbg('\n\n-----------Query OVER-------------\n\n') except Exception as ex: uim.update_state('done_querying') uim.update_state('query_failed') raise
def add_img(gm, gid=None, gname=None, aif=False, src_img=''): logdbg('Adding Image: gid=%r, gname=%r, aif=%r, src_img=%r' % (gid, gname, aif, src_img)) if src_img != '': #This is an new image (dir_part, nameext_part) = os.path.split(src_img) (name_part, ext_part) = os.path.splitext(nameext_part) if not ext_part.lower() in gm.valid_img_extensions: logerr('Invalid Image: %s' % src_img) if gname is None: gname = name_part + ext_part db_img = os.path.join(gm.hs.iom.get_img_dpath(), gname) if os.path.abspath(src_img) == os.path.abspath(db_img): logmsg('Readding existing dbimg:' + src_img) else: logmsg('Copying ' + src_img + ' to ' + db_img) copyfile(src_img, db_img) db_img = os.path.join(gm.hs.iom.get_img_dpath(), gname) if not os.path.exists(db_img): # Try to add an extension if it wasn't given ext_fallback_list = [ '.jpg', '.jpeg', '.JPG', '.JPEG', '.png', '.tif' ] fb_sucess_bit = False (db_img_noext, old_ext) = os.path.splitext(db_img) (gname_noext, old_ext2) = os.path.splitext(gname) for ext_fb in ext_fallback_list: db_img_fb = db_img_noext + ext_fb if os.path.exists(db_img_fb): db_img = db_img_fb gname = gname_noext + ext_fb fb_sucess_bit = True break if not fb_sucess_bit: logwarn('Trying to add a nonexistant image: ' + db_img) return if gname in gm.gname2_gid.keys(): logdbg('Trying to add a GNAME that is already managed: ' + gname) return if gid is None or gid < 1: gid = gm.next_gid else: if (gid < len(gm.gid2_gx) - 1 and gm.gid2_gx[gid] > 0): logdbg('Trying to add a GID that is already managed: ' + str(gid)) return gid #Check key values before memory managment #Manage Memory gx = gm.next_gx if gx >= len(gm.gx2_gid): gm.img_alloc((len(gm.gx2_gid) + 1) * 2 + 1) #Flat indexing gm.gx2_gid[gx] = gid gm.gx2_gname[gx] = gname if len(gname) > gm.max_gnamelen: gm.max_gnamelen = len(gname) gm.max_gname = gname gm.gx2_aif_bit[gx] = aif #X Reverse indexing if len(gm.gid2_gx) <= gid: gid_extend = (gid - len(gm.gid2_gx)) + 1 gm.gid2_gx = append(gm.gid2_gx, zeros(gid_extend, dtype=uint32)) gm.gid2_gx[gid] = gx gm.gname2_gid[gname] = gid #Increment gm.next_gx = gm.next_gx + 1 gm.next_gid = max(gm.next_gid + 1, gid + 1) gm.num_g = gm.num_g + 1 gm.max_gx = max(gm.max_gx, gx) gm.max_gid = max(gm.max_gid, gx) return gid
def add_img(gm, gid=None, gname=None, aif=False, src_img=''): logdbg('Adding Image: gid=%r, gname=%r, aif=%r, src_img=%r' % (gid, gname, aif, src_img)) if src_img != '': #This is an new image (dir_part, nameext_part) = os.path.split(src_img) (name_part, ext_part) = os.path.splitext(nameext_part) if not ext_part.lower() in gm.valid_img_extensions: logerr('Invalid Image: %s' % src_img) if gname is None: gname = name_part + ext_part db_img = os.path.join(gm.hs.iom.get_img_dpath(), gname) if os.path.abspath(src_img) == os.path.abspath(db_img): logmsg('Readding existing dbimg:'+src_img) else: logmsg('Copying '+src_img+' to '+db_img) copyfile(src_img, db_img) db_img = os.path.join(gm.hs.iom.get_img_dpath(), gname) if not os.path.exists(db_img): # Try to add an extension if it wasn't given ext_fallback_list = ['.jpg','.jpeg','.JPG','.JPEG','.png','.tif'] fb_sucess_bit = False (db_img_noext, old_ext) = os.path.splitext(db_img) (gname_noext, old_ext2) = os.path.splitext(gname) for ext_fb in ext_fallback_list: db_img_fb = db_img_noext + ext_fb if os.path.exists(db_img_fb): db_img = db_img_fb gname = gname_noext+ext_fb fb_sucess_bit = True; break if not fb_sucess_bit: logwarn('Trying to add a nonexistant image: '+db_img) return if gname in gm.gname2_gid.keys(): logdbg('Trying to add a GNAME that is already managed: '+gname) return if gid is None or gid < 1: gid = gm.next_gid else: if (gid < len(gm.gid2_gx)-1 and gm.gid2_gx[gid] > 0): logdbg('Trying to add a GID that is already managed: '+str(gid)) return gid #Check key values before memory managment #Manage Memory gx = gm.next_gx if gx >= len(gm.gx2_gid): gm.img_alloc((len(gm.gx2_gid)+1)*2+1) #Flat indexing gm.gx2_gid[gx] = gid gm.gx2_gname[gx] = gname if len(gname) > gm.max_gnamelen: gm.max_gnamelen = len(gname) gm.max_gname = gname gm.gx2_aif_bit[gx] = aif #X Reverse indexing if len(gm.gid2_gx) <= gid: gid_extend = (gid - len(gm.gid2_gx)) + 1 gm.gid2_gx = append(gm.gid2_gx, zeros(gid_extend,dtype=uint32)) gm.gid2_gx[gid] = gx gm.gname2_gid[gname] = gid #Increment gm.next_gx = gm.next_gx + 1 gm.next_gid = max(gm.next_gid+1, gid+1) gm.num_g = gm.num_g + 1 gm.max_gx = max(gm.max_gx, gx) gm.max_gid = max(gm.max_gid, gx) return gid