def run(): fileList = os.listdir(cfg.resultsFolder) resultsFileList = filter(lambda element: '.result' in element, fileList) for resultsFile in resultsFileList: resultsFilePath = cfg.resultsFolder + '/' +resultsFile file = open(resultsFilePath, 'r') imageResults = pickle.load(file) boxes = imageResults['bboxes'] scores = imageResults['scores'] imagepath = imageResults['imagepath'] filename = os.path.basename(imagepath) if boxes is None: print 'No pedestrians found for image '+imagepath continue print 'Saving results for image '+filename idx = np.where(scores > cfg.decision_threshold) boxes = boxes[idx] scores = scores[idx] boxes, scores = nms.non_max_suppression_fast(boxes, scores, overlapthresh= cfg.nmsOverlapThresh) img = Image.open(imagepath) #Show the results on a colored image img = drawing.drawResultsOnImage(img, boxes, scores) io.imsave('Results/'+filename, img) file.close() print 'Finished!'
def run(): fileList = os.listdir(cfg.resultsFolder) resultsFileList = filter(lambda element: '.result' in element, fileList) for resultsFile in resultsFileList: resultsFilePath = cfg.resultsFolder + '/' + resultsFile file = open(resultsFilePath, 'rb') imageResults = pickle.load(file) boxes = imageResults['bboxes'] scores = imageResults['scores'] imagepath = imageResults['imagepath'] filename = os.path.basename(imagepath) if boxes is None: print('No pedestrians found for image ' + imagepath) continue print('Saving results for image ' + filename) idx = np.where(scores > cfg.decision_threshold) boxes = boxes[idx] scores = scores[idx] boxes, scores = nms.non_max_suppression_fast( boxes, scores, overlapthresh=cfg.nmsOverlapThresh) img = Image.open(imagepath) #Show the results on a colored image img = drawing.drawResultsOnImage(img, boxes, scores) # print(type(img)) io.imsave('Results/' + filename, np.array(img)) file.close() print('Finished!')
def f(resultsFile): totalTP = np.zeros(len(detection_thresholds)) totalFN = np.zeros(len(detection_thresholds)) totalFP = np.zeros(len(detection_thresholds)) resultsFilePath = cfg.resultsFolder + '/' + resultsFile file = open(resultsFilePath, 'r') imageResults = pickle.load(file) file.close() #Retrieve the data for this result detectedBoxes = imageResults['bboxes'] detectedScores = imageResults['scores'] imagePath = imageResults['imagepath'] modelIndexes = imageResults['model'] curThreshIDX = 0 imageFilename = os.path.basename(imagePath) # Get the filename imageBasename = os.path.splitext(imageFilename)[0] #Take out the extension #Find the annotations for this image. annotationsFilePath = cfg.annotationsFolderPath + 'gt.' + imageBasename + '.txt' annotatedBoxes = utils.readINRIAAnnotations(annotationsFilePath) for thresh in detection_thresholds: #Select only the bounding boxes that passed the current detection threshold # for i in range(5): # idx, = np.where(modelIndexes == i) # if idx == []: # continue # else: # detectedScores[idx] = detectedScores[idx] + cfg.compensate[i] idx, = np.where(detectedScores > thresh) if len(idx) > 0: detectedBoxes = detectedBoxes[idx] detectedScores = detectedScores[idx] #Apply NMS on the selected bounding boxes detectedBoxes, detectedScores = nms.non_max_suppression_fast( detectedBoxes, detectedScores, overlapthresh=cfg.nmsOverlapThresh) else: detectedBoxes = [] detectedScores = [] #Compute the statistics for the current detected boxes TP, FP, FN = eval.evaluateImage(annotatedBoxes, detectedBoxes, detectedScores) totalTP[curThreshIDX] += TP totalFP[curThreshIDX] += FP totalFN[curThreshIDX] += FN curThreshIDX += 1 return [totalTP, totalFP, totalFN]
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 run(): print ('Start evaluating results') fileList = os.listdir(cfg.resultsFolder) resultsFileList = list(filter(lambda element: '.result' in element, fileList)) detection_thresholds = np.arange(cfg.decision_threshold_min, cfg.decision_threshold_max, cfg.decision_threshold_step) totalTP = np.zeros(len(detection_thresholds)) totalFN = np.zeros(len(detection_thresholds)) totalFP = np.zeros(len(detection_thresholds)) for resultsFile in resultsFileList: resultsFilePath = cfg.resultsFolder+'/'+resultsFile file = open(resultsFilePath, 'rb') imageResults = pickle.load(file) file.close() #Retrieve the data for this result detectedBoxes = imageResults['bboxes'] detectedScores = imageResults['scores'] imagePath = imageResults['imagepath'] curThreshIDX = 0 imageFilename = os.path.basename(imagePath) # Get the filename imageBasename = os.path.splitext(imageFilename)[0] #Take out the extension #Find the annotations for this image. annotationsFilePath = cfg.annotationsFolderPath+'/'+imageBasename+'.txt' annotatedBoxes = utils.readINRIAAnnotations(annotationsFilePath) for thresh in detection_thresholds: #Select only the bounding boxes that passed the current detection threshold idx, = np.where(detectedScores > thresh) if len(idx) > 0: detectedBoxes = detectedBoxes[idx] detectedScores = detectedScores[idx] #Apply NMS on the selected bounding boxes detectedBoxes, detectedScores = nms.non_max_suppression_fast(detectedBoxes, detectedScores, overlapthresh= cfg.nmsOverlapThresh) else: detectedBoxes = [] detectedScores = [] #Compute the statistics for the current detected boxes TP, FP, FN = eval.evaluateImage(annotatedBoxes, detectedBoxes, detectedScores ) totalTP[curThreshIDX] += TP totalFP[curThreshIDX] += FP totalFN[curThreshIDX] += FN curThreshIDX += 1 #Compute metrics print (totalTP + totalFP) detection_rate = totalTP / (totalTP + totalFN) #Tasa de deteccion miss_rate = 1 - detection_rate #Tasa de error fppi = totalFP / len(resultsFileList) #FPPI (Falsos positivos por imagen) #Plot the results plt.figure() plt.plot(fppi, miss_rate, 'r', label='Miss-Rate vs FPPI') plt.xlabel('FPPI ') plt.ylabel('Error rate') plt.title(cfg.model + ' ' + cfg.modelFeatures) plt.legend() plt.show()
def run(): print 'Start evaluating results' fileList = os.listdir(cfg.resultsFolder) resultsFileList = filter(lambda element: '.result' in element, fileList) detection_thresholds = np.arange(cfg.decision_threshold_min, cfg.decision_threshold_max, cfg.decision_threshold_step) totalTP = np.zeros(len(detection_thresholds)) totalFN = np.zeros(len(detection_thresholds)) totalFP = np.zeros(len(detection_thresholds)) for resultsFile in resultsFileList: resultsFilePath = cfg.resultsFolder+'/'+resultsFile file = open(resultsFilePath, 'r') imageResults = pickle.load(file) file.close() #Retrieve the data for this result detectedBoxes = imageResults['bboxes'] detectedScores = imageResults['scores'] imagePath = imageResults['imagepath'] curThreshIDX = 0 imageFilename = os.path.basename(imagePath) # Get the filename imageBasename = os.path.splitext(imageFilename)[0] #Take out the extension #Find the annotations for this image. annotationsFilePath = cfg.annotationsFolderPath+'/'+imageBasename+'.txt' annotatedBoxes = utils.readINRIAAnnotations(annotationsFilePath) for thresh in detection_thresholds: #Select only the bounding boxes that passed the current detection threshold idx, = np.where(detectedScores > thresh) if len(idx) > 0: detectedBoxes = detectedBoxes[idx] detectedScores = detectedScores[idx] #Apply NMS on the selected bounding boxes detectedBoxes, detectedScores = nms.non_max_suppression_fast(detectedBoxes, detectedScores, overlapthresh= cfg.nmsOverlapThresh) else: detectedBoxes = [] detectedScores = [] #Compute the statistics for the current detected boxes TP, FP, FN = eval.evaluateImage(annotatedBoxes, detectedBoxes, detectedScores ) totalTP[curThreshIDX] += TP totalFP[curThreshIDX] += FP totalFN[curThreshIDX] += FN curThreshIDX += 1 #Compute metrics print totalTP + totalFP detection_rate = totalTP / (totalTP + totalFN) #Tasa de deteccion miss_rate = 1 - detection_rate #Tasa de error fppi = totalFP / len(resultsFileList) #FPPI (Falsos positivos por imagen) #Plot the results plt.figure() plt.plot(fppi, miss_rate, 'r', label='Miss-Rate vs FPPI') plt.xlabel('FPPI ') plt.ylabel('Error rate') plt.title(cfg.model + ' ' + cfg.modelFeatures) plt.legend() plt.show()
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