taneemishere/html-code-generation-from-images-with-deep-neural-networks
58
1from __future__ import absolute_import2from __future__ import print_function3 4__author__ = 'Taneem Jan, taneemishere.github.io'5 6import os.path7from os.path import basename8 9from classes.Sampler import *10from classes.model.Main_Model import *11 12 13def dsl_code_generation(input_image):14 trained_weights_path = "classes/model/bin"15 trained_model_name = "Main_Model"16 input_path = input_image17 output_path = "data/output/"18 search_method = "greedy"19 meta_dataset = np.load("{}/meta_dataset.npy".format(trained_weights_path), allow_pickle=True)20 input_shape = meta_dataset[0]21 output_size = meta_dataset[1]22 23 model = Main_Model(input_shape, output_size, trained_weights_path)24 model.load(trained_model_name)25 26 sampler = Sampler(trained_weights_path, input_shape, output_size, CONTEXT_LENGTH)27 28 file_name = 'input_image_from_interface.png'29 file_name = basename(file_name)[:basename(file_name).find(".")]30 evaluation_img = Utils.get_preprocessed_img(input_path, IMAGE_SIZE)31 32 if search_method == "greedy":33 result, _ = sampler.predict_greedy(model, np.array([evaluation_img]))34 print("Result greedy: \n {}".format(result))35 36 with open("{}/{}.gui".format(output_path, file_name), 'w') as out_f:37 out_f.write(result.replace(START_TOKEN, "").replace(END_TOKEN, ""))38 39 return file_name, output_path40 41 42def compile_gui(file_path, filename):43 from os.path import basename44 from compiler.Utils import Utils45 from compiler.Compiler import Compiler46 47 input_path = (file_path + filename)48 49 # remove the path50 file_ = os.path.basename(input_path)51 # remove the extension52 file_ = os.path.splitext(file_)[0]53 # add the extension of gui54 file_ = "data/output/" + file_ + ".gui"55 56 input_file = file_57 58 FILL_WITH_RANDOM_TEXT = True59 TEXT_PLACE_HOLDER = "[]"60 61 dsl_path = "compiler/assets/web-dsl-mapping.json"62 compiler = Compiler(dsl_path)63 64 def render_content_with_text(key, value):65 if FILL_WITH_RANDOM_TEXT:66 if key.find("btn") != -1:67 value = value.replace(TEXT_PLACE_HOLDER, Utils.get_random_text())68 elif key.find("title") != -1:69 value = value.replace(TEXT_PLACE_HOLDER, Utils.get_random_text(length_text=5, space_number=0))70 elif key.find("text") != -1:71 value = value.replace(TEXT_PLACE_HOLDER,72 Utils.get_random_text(length_text=56, space_number=7, with_upper_case=False))73 return value74 75 file_uid = basename(input_file)[:basename(input_file).find(".")]76 path = input_file[:input_file.find(file_uid)]77 78 input_file_path = "{}{}.gui".format(path, file_uid)79 output_file_path = "{}{}.html".format(path, file_uid)80 81 html_code = compiler.compile(input_file_path, output_file_path, rendering_function=render_content_with_text)82 print("Generated code is compiled..!!")83 return html_code84 85 86def main_method(input_image_from_interface):87 file_name, file_output_path = dsl_code_generation(input_image_from_interface)88 result = compile_gui(file_output_path, file_name)89 return result90 