def binarize_file_adaptive(filename, outFolder): FileHelper.create_or_clear_dir(outFolder) fullFilename = filename path, filename = os.path.split(filename) filename_base, filename_ext = os.path.splitext(filename) for neighbours, shift in itertools.product(range(3, 17, 2), range(10)): image = load_image_adaptive(fullFilename, neighbours, shift, binary_type=cv2.THRESH_TRUNC) ffsave = "%s_%d_%d%s" % (filename_base, neighbours, shift, filename_ext) cv2.imwrite(os.path.join(outFolder, ffsave), image)
def binarize_file_threshold(filename, outFolder): FileHelper.create_or_clear_dir(outFolder) fullFilename = filename path, filename = os.path.split(filename) filename_base, filename_ext = os.path.splitext(filename) for threshold in range(30, 200, 20): image = load_image_thresholding(fullFilename, threshold, binary_type=cv2.THRESH_BINARY) ffsave = "%s_%d%s" % (filename_base, threshold, filename_ext) cv2.imwrite(os.path.join(outFolder, ffsave), image)
def process_folder_group(folder, output, saveStructure=False, rules=default_sample): FileHelper.create_or_clear_dir(output) for d in os.listdir(folder): if os.path.isdir(os.path.join(folder, d)): files = process_folder(os.path.join(folder, d), rules) if saveStructure: copyto = os.path.join(output, d) os.makedirs(copyto) else: copyto = output for f in files: shutil.copy2(os.path.join(folder, d, f), copyto)
def process_sample(classifier, inputFolder, outputFolder=None, jobs=-1, saveCorrects=False): logger = logging.getLogger("TestClassifier") positiveInput = os.path.join(inputFolder, 'positive') negativeInput = os.path.join(inputFolder, 'negative') falseNegativeOutput, falsePositiveOutput = None, None trueNegativeOutput, truePositiveOutput = None, None if outputFolder: falsePositiveOutput = os.path.join(outputFolder, 'falsePositive') falseNegativeOutput = os.path.join(outputFolder, 'falseNegative') FileHelper.create_or_clear_dir(falsePositiveOutput) FileHelper.create_or_clear_dir(falseNegativeOutput) if saveCorrects: truePositiveOutput = os.path.join(outputFolder, 'truePositive') trueNegativeOutput = os.path.join(outputFolder, 'trueNegative') FileHelper.create_or_clear_dir(truePositiveOutput) FileHelper.create_or_clear_dir(trueNegativeOutput) logger.debug('Process positive examples') truePositive, falseNegative = process_folder(classifier, positiveInput, False, truePositiveOutput, falseNegativeOutput, jobs) logger.info('True positives: %d; False negatives: %d' % (truePositive, falseNegative)) logger.debug('Process negative examples') trueNegative, falsePositive = process_folder(classifier, negativeInput, True, falsePositiveOutput, trueNegativeOutput, jobs) logger.info('False positives: %d; True negatives: %d' % (falsePositive, trueNegative)) return truePositive, falseNegative, trueNegative, falsePositive
def binarize_folder_threshold(inFolder, outFolder): fileList = FileHelper.read_images_in_dir(inFolder) FileHelper.create_or_clear_dir(outFolder) for filename in fileList: image = load_image_thresholding(os.path.join(inFolder, filename), 190) cv2.imwrite(os.path.join(outFolder, filename), image)
imageShowed = cv2.imread(filename) for r, (x1, y1, x2, y2) in result: cv2.rectangle(imageShowed, (y1, x1), (y2, x2), colorMap[r], 1) path, filename = os.path.split(filename) bfn, ext = os.path.splitext(filename) cv2.imwrite(os.path.join(output, bfn+'_wclrd'+ext), imageShowed) # cv2.imshow('image', imageShowed) # key = 255 # while key != 27: # key = cv2.waitKey(20) & 0xFF if __name__ == '__main__': import sys classifier = load(sys.argv[1]) output = sys.argv[3] FileHelper.create_or_clear_dir(output) colorMap = ColorMap((0, 255, 255), (0, 0, 255), 0.0, 1.0) wfc = WindowedFeatureClassifier(classifier) if os.path.isdir(sys.argv[2]): dir = sys.argv[2] for filename in os.listdir(dir): if os.path.isfile(os.path.join(dir, filename)): process_file(wfc, os.path.join(dir, filename), output) else: process_file(wfc, sys.argv[2], output)
base_template = drawContours(base_template, template_contours) cv2.imwrite('/home/valeriy/projects/hashtag/logos/bw/twitter_big_template_contours.png', base_template) if len(template_contours) > 1: print 'Warning: too complex template' template_contours = template_contours[0] # print len(template_contours) print '%0.3f %3d %0.2f %0.2f ' % (0.0, len(template_contours), contourSolidity(template_contours), contourExtent(template_contours)) # template_contours = approxContour(template_contours) inFolder = '/home/valeriy/projects/hashtag/logos/twitter_frames/' outFolder = '/home/valeriy/projects/hashtag/logos/bin_twitter_frames/' FileHelper.create_or_clear_dir(outFolder) for filename in sorted(FileHelper.read_images_in_dir(inFolder))[:3]: print filename basefile = os.path.join(inFolder, filename) basefile_without_ext = os.path.join(outFolder, os.path.splitext(filename)[0]) # image = binarization.load_image_adaptive(basefile, 11, 10, inverse=False) image = binarization.load_image_thresholding(basefile, 180) # image = binarization.load_image_thresholding(basefile, 200, binary_type=cv2.THRESH_BINARY_INV) contour_image = image.copy() contours, hierarchy = cv2.findContours(contour_image, cv2.RETR_TREE, findContourMethod) image_drawed = drawContours(image, contours) # cv2.imwrite(basefile_without_ext + '_bin_thresh_100.jpg', image_drawed) # cv2.imwrite(basefile_without_ext + '_contour.jpg', contour_image) # contours = approxAllContours(contours)