#Utils.showImage(transtd) blurred = transtd.copy() blurred[:, :, 0] = gaussian(blurred[:, :, 0], sigma, preserve_range=True) print("blurred=" + str(np.mean(blurred[:, :, 0:3]))) #Utils.showImage(blurred) resized = resize(blurred, (ratio * blurred.shape[0], ratio * blurred.shape[1]), anti_aliasing=True, preserve_range=True) print("resized=" + str(np.mean(resized[:, :, 0:3]))) #Utils.showImage(resized) print("Treating file: " + newFile) attributes = pd.DataFrame( PreProcessor.preprocessImage(resized).reshape(1, TRAIN_WIDTH * TRAIN_HEIGHT)) attributes["class"] = number print(type(attributes)) print(attributes.shape) print(attributes) ################### from DigitPositionDetector import DigitPositionDetector dpd = DigitPositionDetector(resized) dpd.detect() detectedDigits = dpd.getDetectedDigits() for dd in detectedDigits: dd.display()
def generate(self): if self._dataSet == None: # Create outdirs if not os.path.exists(Generator.DIGITS_OUTDIR): os.makedirs(Generator.DIGITS_OUTDIR) if not os.path.exists(Generator.TRAINSET_OUTDIR): os.makedirs(Generator.TRAINSET_OUTDIR) # Treat all digit images found in source dir files = Generator.listFiles(Generator.DIGITS_SRCDIR) outDF = pd.DataFrame(data=[], columns=self._dataColumns+self._classColumns) for imgFile in files: outDF1Nb = pd.DataFrame(data=[], columns=self._dataColumns+self._classColumns) number = Generator.extractNumber(imgFile) img = Utils.readImage(Generator.DIGITS_SRCDIR + "/" + imgFile); # double image canvas and center digit transform = AffineTransform(scale=(2,2), translation=(-img.shape[1]/2,-img.shape[0]/2)) img = warp(img, transform, output_shape=img.shape, mode='edge') #imgSize = img.shape # Apply the modifications/augmentations on the digit image for tilt in Generator.tiltRange: tiltTform = AffineTransform(shear=tilt, translation=(215*tilt/4, 0)) for angle in Generator.angleRange: for xtransl in Generator.xTranslRange: for ytransl in Generator.yTranslRange: transTform= AffineTransform(translation=(xtransl,ytransl)) for sigma in Generator.blurRange: for ratio in Generator.zoomRange: #print(np.mean(img)) zoomTform = AffineTransform(scale=(ratio,ratio), translation=(img.shape[1]/ratio/3-img.shape[1]/3,img.shape[0]/ratio/3-img.shape[0]/3)) tilted = warp(img, tiltTform, output_shape=img.shape, mode='edge') #print(np.mean(tilted)) rotated = rotate(tilted, angle, mode='edge'); #print(np.mean(rotated)) transtd = warp(rotated, transTform, output_shape=img.shape, mode='edge') #print(np.mean(transtd)) blured = transtd.copy() blured[:, :, 0] = gaussian(blured[:, :, 0], sigma, preserve_range = True) #print(np.mean(blured)) resized =- warp(blured, zoomTform, output_shape=img.shape, mode='edge') print("resized="+str(np.mean(resized))) resized = -1*resized ## TODO: why does resize invert all the values? # Save the new image newFile = Generator.DIGITS_OUTDIR + "/" + str(number) + \ "_t" + str(tilt) + "_r" + str(angle) + "_t" + str(xtransl) + "_" + str(ytransl) + \ "_b" + str(sigma) + "_s" + str(ratio) + ".png" print("Writing image file: " + newFile) #Utils.showImage(resized); Utils.writeImage(resized, newFile) print("Treating file: " + newFile) attributes = pd.DataFrame(PreProcessor.preprocessImage(resized).reshape(1,PreProcessor.TRAIN_WIDTH*PreProcessor.TRAIN_HEIGHT)) attributes["class"] = number # print(attributes) # print(type(attributes)) # print(attributes.shape) attributes.columns=outDF1Nb.columns outDF1Nb = outDF1Nb.append(attributes) # , ignore_index=True # Save the partial dataframe for the current digits (in case of crash, allows to restarts only on untreated digits) outDF1Nb.to_csv(Generator.TRAINSET_OUTDIR + "/trainset_" + number + ".csv"); outDF = outDF.append(outDF1Nb, ignore_index=True) # Save the whole dataframe for all digits outDF.to_csv(Generator.TRAINSET_OUTDIR + "/trainset_full.csv") self._dataSet = outDF