def predict(): file_name = request.args.get('uploaded_file_name') image_object = cv.imread('./temp/' + file_name) print('image_object', image_object) data = p.preprocess_image(image_object) model = load_model('./model/digit_recognition_model.h5') image_class = model.predict_classes(data) return {'class': int(image_class[0])}
def _preprocess_image(image_bytes, label_bytes_list, image_orig_height, image_orig_width): """Preprocess a single raw image.""" image = preprocessor.preprocess_image( image_bytes =image_bytes, label_bytes_list =label_bytes_list, image_orig_height =image_orig_height, image_orig_width =image_orig_width, is_training =False) return image
def predict_draw(self, image) -> int: """[summary] Args: image (list): [description] Returns: int: [description] """ print(type(image)) image = preprocessor.preprocess_image(image) # open pickled cnn model cnn_saved_model = load_model(self.cnn_saved) number = cnn_saved_model.predict(image, verbose=0) return number
def _build_training_graph(self, train_config): self.global_step = tf.Variable(0, trainable=False) filename_queue = tf.train.string_input_producer( [os.path.join(train_config["dataset_dir"], 'train.tfrecords')], num_epochs=train_config["num_epochs"]) frameA, frameB, frameC, frameAmp, amplification_factor = \ read_and_decode_3frames(filename_queue, (train_config["image_height"], train_config["image_width"], self.n_channels)) min_after_dequeue = 1000 num_threads = 16 capacity = min_after_dequeue + \ (num_threads + 2) * train_config["batch_size"] frameA, frameB, frameC, frameAmp, amplification_factor = \ tf.train.shuffle_batch([frameA, frameB, frameC, frameAmp, amplification_factor], batch_size=train_config["batch_size"], capacity=capacity, num_threads=num_threads, min_after_dequeue=min_after_dequeue) frameA = preprocess_image(frameA, train_config) frameB = preprocess_image(frameB, train_config) frameC = preprocess_image(frameC, train_config) self.loss_function = partial(self._loss_function, train_config=train_config) self.output = self.image_transformer( frameA, frameB, amplification_factor, [train_config["image_height"], train_config["image_width"]], self.arch_config, True, False) self.reg_loss = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES) if self.reg_loss and train_config["weight_decay"] > 0.0: print("Adding Regularization Weights.") self.loss = self.loss_function(self.output, frameAmp) + \ train_config["weight_decay"] * tf.add_n(self.reg_loss) else: print("No Regularization Weights.") self.loss = self.loss_function(self.output, frameAmp) # Add regularization more # TODO: Hardcoding the network name scope here. with tf.variable_scope('ynet_3frames/encoder', reuse=True): texture_c, shape_c = self._encoder(frameC) self.loss = self.loss + \ train_config["texture_loss_weight"] * L1_loss(texture_c, self.texture_a) + \ train_config["shape_loss_weight"] * L1_loss(shape_c, self.shape_b) self.loss_sum = tf.summary.scalar('train_loss', self.loss) self.image_sum = tf.summary.image('train_B_OUT', tf.concat([frameB, self.output], axis=2), max_outputs=2) if self.n_channels == 3: self.image_comp_sum = tf.summary.image('train_GT_OUT', frameAmp - self.output, max_outputs=2) self.image_orig_comp_sum = tf.summary.image('train_ORIG_OUT', frameA - self.output, max_outputs=2) else: self.image_comp_sum = tf.summary.image( 'train_GT_OUT', tf.concat([frameAmp, self.output, frameAmp], axis=3), max_outputs=2) self.image_orig_comp_sum = tf.summary.image( 'train_ORIG_OUT', tf.concat([frameA, self.output, frameA], axis=3), max_outputs=2) self.saver = tf.train.Saver(max_to_keep=train_config["ckpt_to_keep"])
print("incorrect path given. Path did not lead to a folder") print("path: ", path) quit() ## process all files files = files = [f for f in os.listdir(path) if isfile(join(path, f))] for file in files: print("Transcribing \"%s\"." % (file)) ## load in image img = cv2.imread(join(path, file)) ## preprocess image preprocessed_lines = preprocess_image(img) ## get root filename for writing the transcribed lines outfile = file.split('.')[0] ## classify lines for line in preprocessed_lines: sentence = '' ## neural network call here sw = SlidingWindow() sw.WRITE_WINDOWS = False # If True, the input images of the cnn will be written to a file sw.load_image(line) transcribed_lines = sw.get_letters() ## apply postprocessing postp = Bayesian_processor()
sys.path.append('preprocessing/') import cython import cv2 import os from os.path import join, abspath import numpy as np #this is the file needed for preprocessing from preprocessor import preprocess_image PATH = join(abspath('..'), 'data') OUTPATH = join(join(abspath('..'), 'data'), 'lines') if __name__ == '__main__': f = join(join(PATH, 'image-data'), 'P632-Fg002-R-C01-R01-fused.jpg') if not os.path.isdir(OUTPATH): print("path " + OUTPATH + "not found.. creating new dir") os.mkdir(OUTPATH) bw_img = cv2.imread(f) print("converting image: " + f) croppings = preprocess_image(bw_img) print("seperated into %d croppings" % (len(croppings))) print(np.shape(croppings)) for line_idx, chars in enumerate(croppings): cv2.imwrite(join(OUTPATH, "%d_.png" % (line_idx)), chars) print("saved %d croppings!" % (len(croppings)))
test_example = True ########################## # PREPROCESS SCROLL DATA # ########################## if not os.path.isdir(os.path.join(processed_image_dir, "File0")): print("Preprocessing real scrolls into lines") files = [ join(image_dir, fn) for fn in os.listdir(image_dir) if os.path.isfile(join(image_dir, fn)) ] os.mkdir(processed_image_dir) for idx_file, file in enumerate(files): print(f"Progress: {idx_file+1} of {len(files)} scrolls...") extracted_lines = preprocess_image(cv2.imread(file)) this_dir = os.path.join(processed_image_dir, f"File{idx_file}") if not os.path.isdir(this_dir): os.mkdir(this_dir) for idx_line, line in enumerate(extracted_lines): cv2.imwrite(join(this_dir, f"Line{idx_line}.png"), line) else: print("Preprocessed images detected!\nSkipping preprocessing.") network_exists = bool(os.path.isfile("../../data/checkpoint/checkpoint")) if not network_exists: ############################# # SPLIT AND AUGMENT LETTERS # #############################