def save_region_descriptors(self, reg_desc_lst, hog): """ Save RegionDescriptors to a database based on their HOGDescriptor. """ # Get the database for the given type of hog descriptor: db_name = self.db_name_for_hog(hog) print "Retrieving database '{}'...".format(db_name) db = self.client[db_name] print "...[done]" # Get the hog info collection (which will contain a single document): print "Inserting hog info..." hog_info = utils.get_hog_info_dict(hog) hog_coll = db.hog_info if hog_coll.count() > 0: # If the collection already contains a hog_info entry, verify that it # matches the given hog_info: hog_info_entry = hog_coll.find_one() if not utils.hog_info_dicts_match(hog_info_entry, hog_info): raise ValueError("Given hog_info does not match the hog_info in the database.") else: # If not, insert the given hog_info: db.hog_info.insert_one(hog_info) print "...[done]" # Get the region descriptors collection: print "Inserting regions descriptors..." reg_descr_info = [rd.as_dict for rd in reg_desc_lst] reg_desc_coll = db.region_descriptors reg_desc_coll.insert_many(reg_descr_info) print "...[done]" # Close the client (it will automatically reopen if we use it again): self.client.close()
def save_region_descriptors(hog, pos_regions, pos_descriptors, base_fname): # Generate an appropriate file name: output_dir = "output" descr_name = base_fname fname_no_ext = "{}/{}".format(output_dir, descr_name) print "Saving descriptors to '{}.xxx'...".format(fname_no_ext) assert len(pos_descriptors[0].shape) == 2 assert pos_descriptors[0].shape[1] == 1 pairs = zip(pos_regions, pos_descriptors) region_descriptors = [{"region": r.as_dict, "descriptor": np.squeeze(d).tolist()} for r, d in pairs] data = {} data["hog_descriptor_info"] = utils.get_hog_info_dict(hog) data["region_descriptor_list"] = region_descriptors # # Save as YAML: # with open('{}.yaml'.format(fname_no_ext), 'w') as stream: # yaml.dump(data, stream) # Save as pickle (smaller + faster): with open("{}.pickle".format(fname_no_ext), "wb") as fh: import pickle pickle.dump(data, fh, pickle.HIGHEST_PROTOCOL)
def save_region_descriptors(hog, pos_regions, pos_descriptors, base_fname): # Generate an appropriate file name: output_dir = 'output' descr_name = base_fname fname_no_ext = '{}/{}'.format(output_dir, descr_name) print 'Saving descriptors to \'{}.xxx\'...'.format(fname_no_ext) assert (len(pos_descriptors[0].shape) == 2) assert (pos_descriptors[0].shape[1] == 1) pairs = zip(pos_regions, pos_descriptors) region_descriptors = [{ 'region': r.as_dict, 'descriptor': np.squeeze(d).tolist() } for r, d in pairs] data = {} data['hog_descriptor_info'] = utils.get_hog_info_dict(hog) data['region_descriptor_list'] = region_descriptors # # Save as YAML: # with open('{}.yaml'.format(fname_no_ext), 'w') as stream: # yaml.dump(data, stream) # Save as pickle (smaller + faster): with open('{}.pickle'.format(fname_no_ext), 'wb') as fh: import pickle pickle.dump(data, fh, pickle.HIGHEST_PROTOCOL)
def save_region_descriptors(self, reg_desc_lst, hog): """ Save RegionDescriptors to a database based on their HOGDescriptor. """ # Get the database for the given type of hog descriptor: db_name = self.db_name_for_hog(hog) print 'Retrieving database \'{}\'...'.format(db_name) db = self.client[db_name] print '...[done]' # Get the hog info collection (which will contain a single document): print 'Inserting hog info...' hog_info = utils.get_hog_info_dict(hog) hog_coll = db.hog_info if hog_coll.count() > 0: # If the collection already contains a hog_info entry, verify that it # matches the given hog_info: hog_info_entry = hog_coll.find_one() if not utils.hog_info_dicts_match(hog_info_entry, hog_info): raise ValueError( 'Given hog_info does not match the hog_info in the database.' ) else: # If not, insert the given hog_info: db.hog_info.insert_one(hog_info) print '...[done]' # Get the region descriptors collection: print 'Inserting regions descriptors...' reg_descr_info = [rd.as_dict for rd in reg_desc_lst] reg_desc_coll = db.region_descriptors reg_desc_coll.insert_many(reg_descr_info) print '...[done]' # Close the client (it will automatically reopen if we use it again): self.client.close()