def extractAndStoreFeatures(inputFolder, items, outputFolder): extension = '.jpg' X = np.zeros(shape=(cfg.num_train_images, cfg.num_features)) y = np.zeros(shape=(cfg.num_train_images, 1)) number_of_images = 0 for index_label, name_label in enumerate(items): # For each item... imagesPath = inputFolder + '/' + name_label # Each label corresponds to a folder fileList = os.listdir(imagesPath) # List all files imagesList = filter( lambda element: extension in element, fileList ) # Select only the ones that ends with the desired extension for filename in imagesList: current_imagePath = imagesPath + '/' + filename print 'Extracting features for ' + current_imagePath image = io.imread(current_imagePath, as_grey=True) image = util.img_as_ubyte( image) # Read the image as bytes (pixels with values 0-255) X[number_of_images] = feature_extractor.extractFeatures( image) # Extract the features y[number_of_images] = index_label # Assign the label at the end of X when saving the data set number_of_images = number_of_images + 1 print number_of_images #Save the data set to .data file in Data folder. np.savetxt( outputFolder, # file name np.c_[X, y], # array to save fmt='%.2f', # formatting, 2 digits in this case delimiter=',', # column delimiter newline='\n', # new line character comments='# ') # character to use for comments
def extractAndStoreFeatures(inputFolder, items, outputFolder): extension = '.jpg' X = np.zeros(shape=(cfg.num_train_images,cfg.num_features)) y = np.zeros(shape=(cfg.num_train_images,1)) number_of_images = 0 for index_label, name_label in enumerate(items): # For each item... imagesPath = inputFolder + '/' + name_label # Each label corresponds to a folder fileList = os.listdir(imagesPath) # List all files imagesList = filter(lambda element: extension in element, fileList) # Select only the ones that ends with the desired extension for filename in imagesList: current_imagePath = imagesPath + '/' + filename print 'Extracting features for ' + current_imagePath image = io.imread(current_imagePath, as_grey=True) image = util.img_as_ubyte(image) # Read the image as bytes (pixels with values 0-255) X[number_of_images] = feature_extractor.extractFeatures(image) # Extract the features y[number_of_images] = index_label # Assign the label at the end of X when saving the data set number_of_images = number_of_images + 1 print number_of_images #Save the data set to .data file in Data folder. np.savetxt( outputFolder, # file name np.c_[X,y], # array to save fmt='%.2f', # formatting, 2 digits in this case delimiter=',', # column delimiter newline='\n', # new line character comments='# ') # character to use for comments
def extractAndStoreFeatures(inputFolder, outputFolder): #List all files fileList = os.listdir(inputFolder) #Select only files that end with .png imagesList = filter(lambda element: '.png' in element, fileList) for filename in imagesList: imagepath = inputFolder + '/' + filename outputpath = outputFolder + '/' + filename + '.feat' if os.path.exists(outputpath): print 'Features for ' + imagepath + '. Delete the file if you want to replace.' continue print 'Extracting features for ' + imagepath image = io.imread(imagepath, as_grey=True) #Read the image as bytes (pixels with values 0-255) image = util.img_as_ubyte(image) #Extract the features feats = feature_extractor.extractFeatures(image) #Save the features to a file outputFile = open(outputpath, "wb") pickle.dump(feats, outputFile) outputFile.close()
def extractAndStoreFeatures(inputFolder, outputFolder): # List all files fileList = os.listdir(inputFolder) # Select only files that end with .png imagesList = filter(lambda element: ".png" in element, fileList) for filename in imagesList: imagepath = inputFolder + "/" + filename outputpath = outputFolder + "/" + filename + ".feat" if os.path.exists(outputpath): print "Features for " + imagepath + ". Delete the file if you want to replace." continue print "Extracting features for " + imagepath image = io.imread(imagepath, as_grey=True) # Read the image as bytes (pixels with values 0-255) image = util.img_as_ubyte(image) # Extract the features feats = feature_extractor.extractFeatures(image) # Save the features to a file outputFile = open(outputpath, "wb") pickle.dump(feats, outputFile) outputFile.close()
def extractFeaturesSingleImage(imageOriginal, outputpath, imagepath): #Read the image as bytes (pixels with values 0-255) image = util.img_as_ubyte(imageOriginal) #Extract the features feats = feature_extractor.extractFeatures(image, imagepath) #Save the features to a file outputFile = open(outputpath, "wb") pickle.dump(feats, outputFile) outputFile.close()
def extractFeaturesSingleImage(imageOriginal, outputpath): #Read the image as bytes (pixels with values 0-255) image = util.img_as_ubyte(imageOriginal) #Extract the features feats = feature_extractor.extractFeatures(image) #Save the features to a file outputFile = open(outputpath, "wb") pickle.dump(feats, outputFile) outputFile.close()
def testImage(imagePath): fileList = os.listdir(cfg.modelRootPath) # Filter all model files modelsList = filter(lambda element: '.model' in element, fileList) # Filter our specific feature method currentModel = cfg.modelFeatures currentModelsList = filter(lambda element: currentModel in element, modelsList) models = [] for modelname in currentModelsList: file = open(cfg.modelRootPath + modelname, 'r') svc = pickle.load(file) models.append(svc) file.close() image = io.imread(imagePath, as_grey=True) image = util.img_as_ubyte(image) #Read the image as bytes (pixels with values 0-255) feats = feature_extractor.extractFeatures(image, imagePath) max_score = -2 counter = 0 model_index = 14 #Background #Obtain prediction score for each model for model in models: decision_func = model.decision_function(feats) score = decision_func[0] if score > max_score: max_score = score modelname = currentModelsList[counter] model_index = modelname.split('_') model_index = model_index[2] model_index = model_index[0:len(model_index)-6] #Parse class index from model name counter += 1 print model_index #Condition by intuition: If score is too low is background? # if max_score < cfg.min_score: # model_index = cfg.index_background #Assign background index return model_index
def testFolder_perImage(): #Load the model bst = xgb.Booster({'nthread': cfg.xgParam['nthread']}) bst.load_model(cfg.modelPath) #Initialize the testing data X = np.zeros(shape=(cfg.num_train_images, cfg.num_features)) y = np.zeros(shape=(cfg.num_train_images, 1)) extension = '.jpg' number_of_images = 0 for index_label, name_label in enumerate( cfg.test_perImage_folders): # For each item... imagesPath = cfg.test_perImage_path + '/' + name_label # Each label corresponds to a folder fileList = os.listdir(imagesPath) # List all files imagesList = filter( lambda element: extension in element, fileList ) # Select only the ones that ends with the desired extension for filename in imagesList: current_imagePath = imagesPath + '/' + filename print('Processing ' + current_imagePath) image = io.imread(current_imagePath, as_grey=True) image = util.img_as_ubyte( image) # Read the image as bytes (pixels with values 0-255) X[number_of_images] = feature_extractor.extractFeatures( image) # Extract the features y[number_of_images] = index_label # Assign the label at the end of X when saving the data set number_of_images = number_of_images + 1 #Save the test data set to .data file in Data folder. np.savetxt( cfg.test_perImage_dataset, # file name np.c_[X, y], # array to save fmt='%.2f', # formatting, 2 digits in this case delimiter=',', # column delimiter newline='\n', # new line character comments='# ') # character to use for comments #Compute the predictions xg_test = xgb.DMatrix(X, label=y) decision_func = bst.predict(xg_test) evaluate_results(y, decision_func)
def testFolder_perImage(): #Load the model bst = xgb.Booster({'nthread':cfg.xgParam['nthread']}) bst.load_model(cfg.modelPath) #Initialize the testing data X = np.zeros(shape=(cfg.num_train_images,cfg.num_features)) y = np.zeros(shape=(cfg.num_train_images,1)) extension = '.jpg' number_of_images = 0 for index_label, name_label in enumerate(cfg.test_perImage_folders): # For each item... imagesPath = cfg.test_perImage_path + '/' + name_label # Each label corresponds to a folder fileList = os.listdir(imagesPath) # List all files imagesList = filter(lambda element: extension in element, fileList) # Select only the ones that ends with the desired extension for filename in imagesList: current_imagePath = imagesPath + '/' + filename print ('Processing '+current_imagePath) image = io.imread(current_imagePath, as_grey=True) image = util.img_as_ubyte(image) # Read the image as bytes (pixels with values 0-255) X[number_of_images] = feature_extractor.extractFeatures(image) # Extract the features y[number_of_images] = index_label # Assign the label at the end of X when saving the data set number_of_images = number_of_images + 1 #Save the test data set to .data file in Data folder. np.savetxt( cfg.test_perImage_dataset, # file name np.c_[X,y], # array to save fmt='%.2f', # formatting, 2 digits in this case delimiter=',', # column delimiter newline='\n', # new line character comments='# ') # character to use for comments #Compute the predictions xg_test = xgb.DMatrix(X, label=y) decision_func = bst.predict(xg_test); evaluate_results(y,decision_func)
def testImage(imagePath, decisionThreshold, applyNMS): fileList = os.listdir(cfg.modelRootPath) # Filter all model files modelsList = filter(lambda element: '.model' in element, fileList) # Filter our specific feature method currentModel = cfg.model + '_' + cfg.modelFeatures currentModelsList = filter(lambda element: currentModel in element, modelsList) models = [] subImages = [] #To save backgorund crops for modelname in currentModelsList: file = open(cfg.modelRootPath + modelname, 'r') svc = pickle.load(file) models.append(svc) file.close() image = io.imread(imagePath, as_grey=True) image = util.img_as_ubyte( image) #Read the image as bytes (pixels with values 0-255) rows, cols = image.shape pyramid = tuple(pyramid_gaussian(image, downscale=cfg.downScaleFactor)) scale = 0 boxes = None scores = None indices = None for p in pyramid[0:]: #We now have the subsampled image in p window_shape = (32, 32) #Add padding to the image, using reflection to avoid border effects if cfg.padding > 0: p = pad(p, cfg.padding, 'reflect') try: views = view_as_windows(p, window_shape, step=cfg.window_step) except ValueError: #block shape is bigger than image break num_rows, num_cols, width, height = views.shape for row in range(0, num_rows): for col in range(0, num_cols): #Get current window subImage = views[row, col] # subImages.append(subImage) #To save backgorund crops: Accumulate them in an array #Extract features feats = feature_extractor.extractFeatures(subImage) #Obtain prediction score for each model for model in models: decision_func = model.decision_function(feats) idx = models.index(model) decision_func += cfg.compensate[idx] if decision_func > decisionThreshold: # if decision_func > -0.2: #For bootstrapping # Signal found! h, w = window_shape scaleMult = math.pow(cfg.downScaleFactor, scale) x1 = int(scaleMult * (col * cfg.window_step - cfg.padding + cfg.window_margin)) y1 = int(scaleMult * (row * cfg.window_step - cfg.padding + cfg.window_margin)) x2 = int(x1 + scaleMult * (w - 2 * cfg.window_margin)) y2 = int(y1 + scaleMult * (h - 2 * cfg.window_margin)) if (y1 > 0) and (y2 > 0): if y2 - y1 > 330: continue #bootstrapping: Save image (if positive) # print(decision_func) # subImages.append(subImage) bbox = (x1, y1, x2, y2) score = decision_func[0] if boxes is not None: boxes = np.vstack((bbox, boxes)) scores = np.hstack((score, scores)) indices = np.hstack((idx, indices)) else: boxes = np.array([bbox]) scores = np.array([score]) indices = np.array([idx]) break scale += 1 # To save backgorund crops # numSubImages = len(subImages) # for x in range(0,10): #Save 10 crops for each background image # randomIndex = random.randint(1,numSubImages-1) #Get a random window index # imageName = imagePath.split('/') #Working on the crop name... # imageName = imageName[len(imageName)-1] # filename = (imageName[:-4]+'-'+str(x)+'.jpg') # io.imsave('Results/'+filename, subImages[randomIndex]) #Save the crop #end To save backgorund crops # To save bootstrapping windows # numSubImages = len(subImages) # length = min(10, len(subImages)) # if length > 0: # for x in range(0,length) : #Save windows with detections (max 10) # if numSubImages == 1: # randomIndex = 0 # else: # randomIndex = random.randint(1, numSubImages-1) #Get a random window index # imageName = imagePath.split('/') #Working on the crop name... # imageName = imageName[len(imageName)-1] # filename = (imageName[:-4]+'-'+str(x)+'.jpg') # io.imsave('Bootstrapping/'+filename, subImages[randomIndex]) #Save the crop #end To save bootstrapping windows if applyNMS: #From all the bounding boxes that are overlapping, take those with maximum score. boxes, scores = nms.non_max_suppression_fast(boxes, scores, cfg.nmsOverlapThresh) #Color boostraping # save_windows(boxes, imagePath) return boxes, scores, indices
def testImage(imagePath, decisionThreshold, applyNMS): fileList = os.listdir(cfg.modelRootPath) # Filter all model files modelsList = filter(lambda element: '.model' in element, fileList) # Filter our specific feature method currentModel = cfg.model+'_'+cfg.modelFeatures currentModelsList = filter(lambda element: currentModel in element, modelsList) models = [] subImages = [] #To save backgorund crops for modelname in currentModelsList: file = open(cfg.modelRootPath + modelname, 'r') svc = pickle.load(file) models.append(svc) file.close() image = io.imread(imagePath, as_grey=True) image = util.img_as_ubyte(image) #Read the image as bytes (pixels with values 0-255) rows, cols = image.shape pyramid = tuple(pyramid_gaussian(image, downscale=cfg.downScaleFactor)) scale = 0 boxes = None scores = None indices = None for p in pyramid[0:]: #We now have the subsampled image in p window_shape = (32,32) #Add padding to the image, using reflection to avoid border effects if cfg.padding > 0: p = pad(p,cfg.padding,'reflect') try: views = view_as_windows(p, window_shape, step=cfg.window_step) except ValueError: #block shape is bigger than image break num_rows, num_cols, width, height = views.shape for row in range(0, num_rows): for col in range(0, num_cols): #Get current window subImage = views[row, col] # subImages.append(subImage) #To save backgorund crops: Accumulate them in an array #Extract features feats = feature_extractor.extractFeatures(subImage) #Obtain prediction score for each model for model in models: decision_func = model.decision_function(feats) idx = models.index(model) decision_func += cfg.compensate[idx] if decision_func > decisionThreshold: # if decision_func > -0.2: #For bootstrapping # Signal found! h, w = window_shape scaleMult = math.pow(cfg.downScaleFactor, scale) x1 = int(scaleMult * (col*cfg.window_step - cfg.padding + cfg.window_margin)) y1 = int(scaleMult * (row*cfg.window_step - cfg.padding + cfg.window_margin)) x2 = int(x1 + scaleMult*(w - 2*cfg.window_margin)) y2 = int(y1 + scaleMult*(h - 2*cfg.window_margin)) if(y1 > 0) and (y2 > 0): if y2 - y1 > 330: continue #bootstrapping: Save image (if positive) # print(decision_func) # subImages.append(subImage) bbox = (x1, y1, x2, y2) score = decision_func[0] if boxes is not None: boxes = np.vstack((bbox, boxes)) scores = np.hstack((score, scores)) indices = np.hstack((idx, indices)) else: boxes = np.array([bbox]) scores = np.array([score]) indices = np.array([idx]) break scale += 1 # To save backgorund crops # numSubImages = len(subImages) # for x in range(0,10): #Save 10 crops for each background image # randomIndex = random.randint(1,numSubImages-1) #Get a random window index # imageName = imagePath.split('/') #Working on the crop name... # imageName = imageName[len(imageName)-1] # filename = (imageName[:-4]+'-'+str(x)+'.jpg') # io.imsave('Results/'+filename, subImages[randomIndex]) #Save the crop #end To save backgorund crops # To save bootstrapping windows # numSubImages = len(subImages) # length = min(10, len(subImages)) # if length > 0: # for x in range(0,length) : #Save windows with detections (max 10) # if numSubImages == 1: # randomIndex = 0 # else: # randomIndex = random.randint(1, numSubImages-1) #Get a random window index # imageName = imagePath.split('/') #Working on the crop name... # imageName = imageName[len(imageName)-1] # filename = (imageName[:-4]+'-'+str(x)+'.jpg') # io.imsave('Bootstrapping/'+filename, subImages[randomIndex]) #Save the crop #end To save bootstrapping windows if applyNMS: #From all the bounding boxes that are overlapping, take those with maximum score. boxes, scores = nms.non_max_suppression_fast(boxes, scores, cfg.nmsOverlapThresh) #Color boostraping # save_windows(boxes, imagePath) return boxes, scores, indices
def testImage_slidingwindow(filename, applyNMS=True): #Load the model bst = xgb.Booster({'nthread': cfg.xgParam['nthread']}) bst.load_model(cfg.modelPath) #Load the current image imagePath = cfg.testFolderPath + '/' + filename image = io.imread(imagePath, as_grey=True) image = util.img_as_ubyte( image) #Read the image as bytes (pixels with values 0-255) #Load the gt dir_gt = cfg.annotationsFolderPath + '/' + filename dir_gt = dir_gt.replace('jpg', 'txt') # png or jpg with open(dir_gt, 'r') as fp: for line in fp: gt_y1, gt_x1, gt_y2, gt_x2, gt_signal = line.split(' ', 4) gt_y1 = float(gt_y1) gt_x1 = float(gt_x1) gt_y2 = float(gt_y2) gt_x2 = float(gt_x2) break #Scan the image iteration = 0 rows, cols = image.shape pyramid = tuple(pyramid_gaussian(image, downscale=cfg.downScaleFactor)) scale = 0 boxes = None scores = None #Test data set X = [] y = [] for p in pyramid[0:]: #We now have the subsampled image in p print p.shape #Add padding to the image, using reflection to avoid border effects if cfg.padding > 0: p = pad(p, cfg.padding, 'reflect') try: views = view_as_windows(p, cfg.window_shape, step=cfg.window_step) except ValueError: #block shape is bigger than image break num_rows, num_cols, width, height = views.shape for row in range(0, num_rows): for col in range(0, num_cols): iteration = iteration + 1 #Get current window subImage = views[row, col] #Window parameters hh, ww = cfg.window_shape xx1 = int(col * cfg.window_step - cfg.padding + cfg.window_margin) yy1 = int(row * cfg.window_step - cfg.padding + cfg.window_margin) xx2 = int(xx1 + (ww - 2 * cfg.window_margin)) yy2 = int(yy1 + (hh - 2 * cfg.window_margin)) print '--------------------[ window, it: ' + str( iteration) + ' ]--------------------' gt_h = gt_y2 - gt_y1 gt_w = gt_x2 - gt_x1 feats = feature_extractor.extractFeatures( subImage) # Extract features if len(feats) > cfg.num_features: print 'Warning: feats length ' + str( len(feats)) + ' is resized to ' + str(cfg.num_features) feats = feats[:cfg. num_features] #The length of the feats must be the same as the training data X.append(feats) #Apriori knowledge: if the current window overlaps the GT data. if (not (gt_x1 > xx1 + ww or gt_x1 + gt_w < xx1 or gt_y1 > yy1 + hh or gt_y1 + gt_h < yy1)): print '=====> overlapping <=====' if gt_signal in cfg.Cuadradas: #Square y.append(2.00) #X.append(feats) elif gt_signal in cfg.Triangulares: #Triangle y.append(3.00) #X.append(feats) else: #Circle y.append(1.00) #X.append(feats) bbox = (xx1, yy1, xx2, yy2) if boxes is not None: boxes = np.vstack((bbox, boxes)) else: boxes = np.array([bbox]) else: #Background y.append(0.00) iteration = iteration + 1 scale += 1 scaleMult = math.pow(cfg.downScaleFactor, scale) break #Save the test data set to .data file in Data folder. np.savetxt( cfg.tDatasetPath, # file name np.c_[X, y], # array to save fmt='%.2f', # formatting, 2 digits in this case delimiter=',', # column delimiter newline='\n', # new line character comments='# ') # character to use for comments #Compute the predictions xg_test = xgb.DMatrix(X, label=y) decision_func = bst.predict(xg_test) evaluate_results(y, decision_func) # Evaluate the results return boxes
def testFolder_colorenh_ccl(): #Load the model bst = xgb.Booster({'nthread': cfg.xgParam['nthread']}) bst.load_model(cfg.modelPath) #Initialize the testing data X = np.zeros(shape=(cfg.num_train_images, cfg.num_features)) y = np.zeros(shape=(cfg.num_train_images, 1)) #Output structure boxes = None img_structure = None candidates = dict() extension = '.jpg' number_of_images = 0 fileList = os.listdir(cfg.test_omatlab_folder_path) # List all files imagesList = filter( lambda element: extension in element, fileList) # Select only the ones that ends with the desired extension for filename in imagesList: current_imagePath = cfg.test_omatlab_folder_path + '/' + filename print('Processing ' + current_imagePath) img_name = filename.rsplit('_', 1)[0] # The name of the original image # Fill X image = io.imread(current_imagePath, as_grey=True) image = util.img_as_ubyte( image) # Read the image as bytes (pixels with values 0-255) X[number_of_images] = feature_extractor.extractFeatures( image) # Extract the features # Fill Y local_dir_gt = current_imagePath local_dir_gt = local_dir_gt.replace('jpg', 'txt') # png or jpg with open(local_dir_gt, 'r') as fp: for line in fp: local_gt_y1, local_gt_x1, local_gt_y2, local_gt_x2, local_gt_sign = line.split( ' ', 4) local_gt_y1 = float(local_gt_y1) local_gt_x1 = float(local_gt_x1) local_gt_y2 = float(local_gt_y2) local_gt_x2 = float(local_gt_x2) local_gt_sign = local_gt_sign.replace('\n', '') bbox = (img_name, cfg.data.index(local_gt_sign), local_gt_x1, local_gt_y1, local_gt_x2, local_gt_y2) if boxes is not None: boxes = np.vstack((bbox, boxes)) else: boxes = np.array([bbox]) fp.close y[number_of_images] = cfg.data.index( local_gt_sign ) # Suponiendo que sean las mismas clases que data print str(local_gt_y1) + ', ' + str(local_gt_x1) + ', ' + str( local_gt_y2) + ', ' + str(local_gt_x2) + ', ' + local_gt_sign number_of_images = number_of_images + 1 #Save the test data set to .data file in Data folder. np.savetxt( cfg.ce_ccl_test_dataset, # file name np.c_[X, y], # array to save fmt='%.2f', # formatting, 2 digits in this case delimiter=',', # column delimiter newline='\n', # new line character comments='# ') # character to use for comments #Compute the predictions xg_test = xgb.DMatrix(X, label=y) decision_func = bst.predict(xg_test) #Return those bounding boxes that its labels predicted are not background. candidates['bboxes'] = boxes # Store the bounding boxes candidates[ 'prediction'] = decision_func[:: -1] # Store the reversed array of predictions outputFile = open(cfg.resultsFolder + '/Ce_ccl.results', "wb") pickle.dump(candidates, outputFile) outputFile.close() #print candidates['bboxes'] #print decision_func #Evaluate the results evaluate_results(y, decision_func)
def testImage_slidingwindow(filename, applyNMS=True): #Load the model bst = xgb.Booster({'nthread':cfg.xgParam['nthread']}) bst.load_model(cfg.modelPath) #Load the current image imagePath = cfg.testFolderPath + '/' + filename image = io.imread(imagePath, as_grey=True) image = util.img_as_ubyte(image) #Read the image as bytes (pixels with values 0-255) #Load the gt dir_gt = cfg.annotationsFolderPath + '/' + filename dir_gt = dir_gt.replace('jpg','txt') # png or jpg with open(dir_gt,'r') as fp: for line in fp: gt_y1, gt_x1, gt_y2, gt_x2, gt_signal = line.split(' ', 4 ) gt_y1 = float(gt_y1) gt_x1 = float(gt_x1) gt_y2 = float(gt_y2) gt_x2 = float(gt_x2) break #Scan the image iteration = 0 rows, cols = image.shape pyramid = tuple(pyramid_gaussian(image, downscale=cfg.downScaleFactor)) scale = 0 boxes = None scores = None #Test data set X = [] y = [] for p in pyramid[0:]: #We now have the subsampled image in p print p.shape #Add padding to the image, using reflection to avoid border effects if cfg.padding > 0: p = pad(p,cfg.padding,'reflect') try: views = view_as_windows(p, cfg.window_shape, step=cfg.window_step) except ValueError: #block shape is bigger than image break num_rows, num_cols, width, height = views.shape for row in range(0, num_rows): for col in range(0, num_cols): iteration = iteration + 1 #Get current window subImage = views[row, col] #Window parameters hh, ww = cfg.window_shape xx1 = int(col*cfg.window_step - cfg.padding + cfg.window_margin) yy1 = int(row*cfg.window_step - cfg.padding + cfg.window_margin) xx2 = int(xx1 + (ww - 2*cfg.window_margin)) yy2 = int(yy1 + (hh - 2*cfg.window_margin)) print '--------------------[ window, it: '+ str(iteration) +' ]--------------------' gt_h = gt_y2-gt_y1; gt_w = gt_x2-gt_x1; feats = feature_extractor.extractFeatures(subImage) # Extract features if len(feats) > cfg.num_features: print 'Warning: feats length '+str(len(feats)) + ' is resized to ' + str(cfg.num_features) feats = feats[:cfg.num_features] #The length of the feats must be the same as the training data X.append(feats) #Apriori knowledge: if the current window overlaps the GT data. if (not(gt_x1 > xx1+ww or gt_x1+gt_w < xx1 or gt_y1 > yy1+hh or gt_y1+gt_h < yy1)): print '=====> overlapping <=====' if gt_signal in cfg.Cuadradas: #Square y.append(2.00) #X.append(feats) elif gt_signal in cfg.Triangulares: #Triangle y.append(3.00) #X.append(feats) else: #Circle y.append(1.00) #X.append(feats) bbox = (xx1, yy1, xx2, yy2) if boxes is not None: boxes = np.vstack((bbox, boxes)) else: boxes = np.array([bbox]) else: #Background y.append(0.00) iteration = iteration + 1 scale += 1 scaleMult = math.pow(cfg.downScaleFactor, scale) break #Save the test data set to .data file in Data folder. np.savetxt( cfg.tDatasetPath, # file name np.c_[X,y], # array to save fmt='%.2f', # formatting, 2 digits in this case delimiter=',', # column delimiter newline='\n', # new line character comments='# ') # character to use for comments #Compute the predictions xg_test = xgb.DMatrix(X, label=y) decision_func = bst.predict(xg_test); evaluate_results(y,decision_func) # Evaluate the results return boxes
def testFolder_colorenh_ccl(): #Load the model bst = xgb.Booster({'nthread':cfg.xgParam['nthread']}) bst.load_model(cfg.modelPath) #Initialize the testing data X = np.zeros(shape=(cfg.num_train_images,cfg.num_features)) y = np.zeros(shape=(cfg.num_train_images,1)) #Output structure boxes = None img_structure = None candidates = dict() extension = '.jpg' number_of_images = 0 fileList = os.listdir(cfg.test_omatlab_folder_path) # List all files imagesList = filter(lambda element: extension in element, fileList) # Select only the ones that ends with the desired extension for filename in imagesList: current_imagePath = cfg.test_omatlab_folder_path + '/' + filename print ('Processing '+current_imagePath) img_name = filename.rsplit('_', 1)[0] # The name of the original image # Fill X image = io.imread(current_imagePath, as_grey=True) image = util.img_as_ubyte(image) # Read the image as bytes (pixels with values 0-255) X[number_of_images] = feature_extractor.extractFeatures(image) # Extract the features # Fill Y local_dir_gt = current_imagePath local_dir_gt = local_dir_gt.replace('jpg','txt') # png or jpg with open(local_dir_gt,'r') as fp: for line in fp: local_gt_y1, local_gt_x1, local_gt_y2, local_gt_x2, local_gt_sign = line.split(' ', 4 ) local_gt_y1 = float(local_gt_y1) local_gt_x1 = float(local_gt_x1) local_gt_y2 = float(local_gt_y2) local_gt_x2 = float(local_gt_x2) local_gt_sign = local_gt_sign.replace('\n','') bbox = (img_name, cfg.data.index(local_gt_sign), local_gt_x1, local_gt_y1, local_gt_x2, local_gt_y2) if boxes is not None: boxes = np.vstack((bbox, boxes)) else: boxes = np.array([bbox]) fp.close y[number_of_images] = cfg.data.index(local_gt_sign) # Suponiendo que sean las mismas clases que data print str(local_gt_y1) + ', ' + str(local_gt_x1) + ', ' + str(local_gt_y2) + ', ' + str(local_gt_x2) + ', ' + local_gt_sign number_of_images = number_of_images + 1 #Save the test data set to .data file in Data folder. np.savetxt( cfg.ce_ccl_test_dataset, # file name np.c_[X,y], # array to save fmt='%.2f', # formatting, 2 digits in this case delimiter=',', # column delimiter newline='\n', # new line character comments='# ') # character to use for comments #Compute the predictions xg_test = xgb.DMatrix(X, label=y) decision_func = bst.predict(xg_test); #Return those bounding boxes that its labels predicted are not background. candidates['bboxes'] = boxes # Store the bounding boxes candidates['prediction'] = decision_func[::-1] # Store the reversed array of predictions outputFile = open(cfg.resultsFolder+'/Ce_ccl.results', "wb") pickle.dump(candidates, outputFile) outputFile.close() #print candidates['bboxes'] #print decision_func #Evaluate the results evaluate_results(y,decision_func)