Spaces:
Paused
Paused
Commit ·
0246ff9
1
Parent(s): 1bcdbad
Update
Browse filesFiles changed (10)
- app.py+53-30
- screencoder/UIED/detect_compo/ip_region_proposal.py+36
- screencoder/UIED/detect_compo/lib_ip/ip_detection.py+4-4
- screencoder/UIED/run_single.py+35-5
- screencoder/block_parsor.py+90-103
- screencoder/html_generator.py+105-234
- screencoder/image_box_detection.py+43-26
- screencoder/image_replacer.py+43-26
- screencoder/main.py+96-105
- screencoder/mapping.py+41-23
4 unmodified lines5678910111213141516171819202122232410 unmodified lines35363738394041421 unmodified line444546474849509 unmodified lines60616263646566676869707172736 unmodified lines80818283848586878889909192939495969798991001011021034 unmodified linesimport numpy as npfrom screencoder.main import generate_html_for_demo# Default Demo ExamplesSAMPLE_IMAGES_DIR = "screencoder/data/input"examples_data = []if os.path.exists(SAMPLE_IMAGES_DIR):sample_files = [f for f in sorted(os.listdir(SAMPLE_IMAGES_DIR)) if f.endswith(('.png', '.jpg', '.jpeg')) and not f.startswith('.')]for filename in sample_files:path = os.path.join(SAMPLE_IMAGES_DIR, filename)prompt = f"Generate a modern UI based on the '{filename}' example, focusing on a clean and intuitive layout."examples_data.append([path, prompt, path])else:print(f"Warning: Sample images directory not found at {SAMPLE_IMAGES_DIR}. Examples will be empty.")def process_image_and_prompt(image_np, image_path_from_state, prompt):final_image_path = ""is_temp_file = False10 unmodified lineselse:return "<html><body><h1 style='font-family: sans-serif; text-align: center; margin-top: 40px;'>Please provide an image.</h1></body></html>", ""print(f"With prompt: '{prompt}'")html_content = generate_html_for_demo(final_image_path, prompt)if is_temp_file:os.unlink(final_image_path)1 unmodified linereturn html_content, html_contentwith gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), fill_height=True) as demo:active_image_path_state = gr.State(value=examples_data[0][2] if examples_data else None)gr.Markdown("# ScreenCoder: Screenshot to Code")9 unmodified linesupload_button = gr.UploadButton("Click to Upload or Drag-and-Drop", file_types=["image"], variant="primary")gr.Markdown("### Step 2: Write a Prompt (Optional)")prompt_input = gr.Textbox(label="Instructions",placeholder="e.g., 'Make this a dark theme and change the text.'",lines=3,value=examples_data[0][1] if examples_data else "Based on the layout, please fill in appropriate English text and beautify the image blocks.")generate_btn = gr.Button("Generate HTML", variant="primary", scale=2)with gr.Column(scale=2):6 unmodified linesif examples_data:gr.Examples(examples=examples_data,inputs=[active_image],label="Click an example to try it out",)def handle_upload(uploaded_image_np):"""On upload, update image, clear state, and set a generic prompt for user input."""default_prompt = "Based on the layout, please fill in appropriate English text and beautify the image blocks."return uploaded_image_np, None, default_promptupload_button.upload(fn=handle_upload,inputs=upload_button,outputs=[active_image, active_image_path_state, prompt_input])generate_btn.click(fn=process_image_and_prompt,inputs=[active_image, active_image_path_state, prompt_input],outputs=[html_preview, html_code_output],show_progress="full")4 unmodified lines5678910111213141516171819202122232425262728293031323334353637383910 unmodified lines5051525354555657585960616263641 unmodified line666768697071729 unmodified lines828384858687888990919293946 unmodified lines1011021031041051061071081091101111121131141151161171181191201211221231241251264 unmodified linesimport numpy as npfrom screencoder.main import generate_html_for_demo# Manually defined examplesexamples_data = [["screencoder/data/input/test1.png","","","","","screencoder/data/input/test1.png"],["screencoder/data/input/test2.png","","","","","screencoder/data/input/test2.png"],["screencoder/data/input/test3.png","","","","","screencoder/data/input/test3.png"],]def process_image_and_prompt(image_np, image_path_from_state, sidebar_prompt, header_prompt, navigation_prompt, main_content_prompt):final_image_path = ""is_temp_file = False10 unmodified lineselse:return "<html><body><h1 style='font-family: sans-serif; text-align: center; margin-top: 40px;'>Please provide an image.</h1></body></html>", ""instructions = {"sidebar": sidebar_prompt,"header": header_prompt,"navigation": navigation_prompt,"main content": main_content_prompt}print(f"With instructions: {instructions}")html_content = generate_html_for_demo(final_image_path, instructions)if is_temp_file:os.unlink(final_image_path)1 unmodified linereturn html_content, html_contentwith gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), fill_height=True) as demo:active_image_path_state = gr.State(value=examples_data[0][5] if examples_data else None)gr.Markdown("# ScreenCoder: Screenshot to Code")9 unmodified linesupload_button = gr.UploadButton("Click to Upload or Drag-and-Drop", file_types=["image"], variant="primary")gr.Markdown("### Step 2: Write Prompts (Optional)")with gr.Accordion("Component-specific Prompts", open=True):sidebar_prompt = gr.Textbox(label="Sidebar Prompt", placeholder="Instructions for the sidebar...", value="")header_prompt = gr.Textbox(label="Header Prompt", placeholder="Instructions for the header...", value="")navigation_prompt = gr.Textbox(label="Navigation Prompt", placeholder="Instructions for the navigation...", value="")main_content_prompt = gr.Textbox(label="Main Content Prompt", placeholder="Instructions for the main content...", value="")generate_btn = gr.Button("Generate HTML", variant="primary", scale=2)with gr.Column(scale=2):6 unmodified linesif examples_data:gr.Examples(examples=examples_data,fn=lambda *args: args, # Simply return all inputsinputs=[active_image, sidebar_prompt, header_prompt, navigation_prompt, main_content_prompt, active_image_path_state],outputs=[active_image, sidebar_prompt, header_prompt, navigation_prompt, main_content_prompt, active_image_path_state],label="Click an example to try it out",cache_examples=False,)def handle_upload(uploaded_image_np):"""On upload, update image, clear state, and set empty prompts."""return uploaded_image_np, None, "", "", "", ""upload_button.upload(fn=handle_upload,inputs=upload_button,outputs=[active_image, active_image_path_state, sidebar_prompt, header_prompt, navigation_prompt, main_content_prompt])generate_btn.click(fn=process_image_and_prompt,inputs=[active_image, active_image_path_state, sidebar_prompt, header_prompt, navigation_prompt, main_content_prompt],outputs=[html_preview, html_code_output],show_progress="full")
12 unmodified lines13141516171868 unmodified lines87888990919212 unmodified linesC = Config()def nesting_inspection(org, grey, compos, ffl_block):'''Inspect all big compos through block division by flood-fill68 unmodified lines# *** Step 7 *** save detection resultCompo.compos_update(uicompos, org.shape)file.save_corners_json(pjoin(ip_root, name + '.json'), uicompos)print("[Compo Detection Completed in %.3f s] Input: %s Output: %s" % (time.perf_counter() - start, input_img_path, pjoin(ip_root, name + '.json')))return uicompos12 unmodified lines131415161718192021222324252627282930313233343536373839404142434445464748495068 unmodified lines11912012112212312412512612712812 unmodified linesC = Config()def resolve_uicompo_containment(uicompos):"""Resolves containment issues among UI components.If a component's bounding box is fully contained within another's, it is removed."""def contains(bbox_a, bbox_b):"""Checks if bbox_a completely contains bbox_b."""return bbox_a.col_min <= bbox_b.col_min and \bbox_a.row_min <= bbox_b.row_min and \bbox_a.col_max >= bbox_b.col_max and \bbox_a.row_max >= bbox_b.row_maxcompos_to_remove = set()for i, compo1 in enumerate(uicompos):for j, compo2 in enumerate(uicompos):if i == j:continue# Check if compo1 contains compo2if contains(compo1.bbox, compo2.bbox):compos_to_remove.add(j)# Filter out the contained componentsfinal_compos = [compo for i, compo in enumerate(uicompos) if i not in compos_to_remove]if len(final_compos) < len(uicompos):print(f"Containment resolved: Removed {len(uicompos) - len(final_compos)} contained components.")return final_composdef nesting_inspection(org, grey, compos, ffl_block):'''Inspect all big compos through block division by flood-fill68 unmodified lines# *** Step 7 *** save detection resultCompo.compos_update(uicompos, org.shape)# *** Step 8 *** Resolve containment before savinguicompos = resolve_uicompo_containment(uicompos)file.save_corners_json(pjoin(ip_root, name + '.json'), uicompos)print("[Compo Detection Completed in %.3f s] Input: %s Output: %s" % (time.perf_counter() - start, input_img_path, pjoin(ip_root, name + '.json')))return uicompos
360 unmodified lines3613623633643653663673683693703713723731 unmodified line375376377378379380381382383384385386387360 unmodified lines# top border - scan top downblank_count = 0for i in range(1, 5):if sum(clip[side + i]) / 255 > thread * clip.shape[1]:blank_count += 1if blank_count > 2: return False# left border - scan left to rightblank_count = 0for i in range(1, 5):if sum(clip[:, side + i]) / 255 > thread * clip.shape[0]:blank_count += 1if blank_count > 2: return False1 unmodified line# bottom border - scan bottom upblank_count = 0for i in range(-1, -5, -1):if sum(clip[side + i]) / 255 > thread * clip.shape[1]:blank_count += 1if blank_count > 2: return False# right border - scan right to leftblank_count = 0for i in range(-1, -5, -1):if sum(clip[:, side + i]) / 255 > thread * clip.shape[0]:blank_count += 1if blank_count > 2: return Falsereturn True360 unmodified lines3613623633643653663673683693703713723731 unmodified line375376377378379380381382383384385386387360 unmodified lines# top border - scan top downblank_count = 0for i in range(1, 5):if sum(clip[side + i].astype(np.int64)) / 255 > thread * clip.shape[1]:blank_count += 1if blank_count > 2: return False# left border - scan left to rightblank_count = 0for i in range(1, 5):if sum(clip[:, side + i].astype(np.int64)) / 255 > thread * clip.shape[0]:blank_count += 1if blank_count > 2: return False1 unmodified line# bottom border - scan bottom upblank_count = 0for i in range(-1, -5, -1):if sum(clip[side + i].astype(np.int64)) / 255 > thread * clip.shape[1]:blank_count += 1if blank_count > 2: return False# right border - scan right to leftblank_count = 0for i in range(-1, -5, -1):if sum(clip[:, side + i].astype(np.int64)) / 255 > thread * clip.shape[0]:blank_count += 1if blank_count > 2: return Falsereturn True
12345678920 unmodified lines30313233343526 unmodified lines626364656667686970717226 unmodified lines99100101102from os.path import join as pjoinimport cv2import osimport numpy as npimport multiprocessingdef resize_height_by_longest_edge(img_path, resize_length=800):org = cv2.imread(img_path)20 unmodified linesif __name__ == '__main__':# Set multiprocessing start method to 'spawn' for macOS compatibility.# This must be done at the very beginning of the main block.try:26 unmodified lines'merge-contained-ele':True, 'merge-line-to-paragraph':False, 'remove-bar':True}# set input image pathinput_path_img = 'data/test1.png'output_root = 'data'resized_height = resize_height_by_longest_edge(input_path_img, resize_length=800)color_tips()is_ip = Trueis_clf = False26 unmodified linescompo_path = pjoin(output_root, 'ip', str(name) + '.json')ocr_path = pjoin(output_root, 'ocr', str(name) + '.json')merge.merge(input_path_img, compo_path, ocr_path, pjoin(output_root, 'merge'),is_remove_bar=key_params['remove-bar'], is_paragraph=key_params['merge-line-to-paragraph'], show=True)123456789101112131420 unmodified lines353637383940414243444546474849505152535455565758596061626326 unmodified lines9091929394959697989910026 unmodified lines127128129130131132import cv2import osimport numpy as npimport multiprocessingimport argparsefrom os.path import join as pjoindef get_args():parser = argparse.ArgumentParser(description="Processes a single image for UI element detection.")parser.add_argument('--run_id', type=str, required=True, help='A unique identifier for the processing run.')return parser.parse_args()def resize_height_by_longest_edge(img_path, resize_length=800):org = cv2.imread(img_path)20 unmodified linesif __name__ == '__main__':args = get_args()# --- Dynamic Path Construction ---# Construct paths based on the provided run_idbase_dir = os.path.dirname(os.path.abspath(__file__))run_id = args.run_id# The temporary directory for this specific runtmp_dir = os.path.join(base_dir, '..', 'data', 'tmp', run_id)# Input image pathinput_path_img = os.path.join(tmp_dir, f"{run_id}.png")# Output directory for this script's resultsoutput_root = tmp_dir # All results (ip, ocr, etc.) will go into the run's tmp subdir.if not os.path.exists(input_path_img):print(f"Error: Input image not found at {input_path_img}")exit(1)print(f"--- Starting UIED processing for run_id: {run_id} ---")print(f"Input image: {input_path_img}")print(f"Output root: {output_root}")# Set multiprocessing start method to 'spawn' for macOS compatibility.# This must be done at the very beginning of the main block.try:26 unmodified lines'merge-contained-ele':True, 'merge-line-to-paragraph':False, 'remove-bar':True}# set input image path# input_path_img = 'data/test1.png'# output_root = 'data'resized_height = resize_height_by_longest_edge(input_path_img, resize_length=800)# color_tips() # This shows a window, which is not suitable for a script.is_ip = Trueis_clf = False26 unmodified linescompo_path = pjoin(output_root, 'ip', str(name) + '.json')ocr_path = pjoin(output_root, 'ocr', str(name) + '.json')merge.merge(input_path_img, compo_path, ocr_path, pjoin(output_root, 'merge'),is_remove_bar=key_params['remove-bar'], is_paragraph=key_params['merge-line-to-paragraph'], show=False)print(f"--- UIED processing complete for run_id: {run_id} ---")
12345613 unmodified lines20212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465662 unmodified lines697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491503 unmodified lines15415515615715815916016127 unmodified lines18919019119219319419542 unmodified lines238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271import osimport cv2import jsonfrom utils import Doubao, encode_image, image_maskDEFAULT_IMAGE_PATH = "data/input/test1.png"13 unmodified lines# PROMPT_navigation = "框出网页中的navigation的位置,请你只返回对应的bounding box。"# PROMPT_main_content = "框出网页中的main content的位置,请你只返回对应的bounding box。"# simple version of bbox parsingdef parse_bboxes(bbox_input: str, image_path: str) -> dict[str, tuple[int, int, int, int]]:"""Parse bounding box string to dictionary of named coordinate tuples"""bboxes = {}# print("Raw bbox input:", bbox_input) # Debug printimage = cv2.imread(image_path)if image is None:print(f"Error: Failed to read image {image_path}")return bboxesh, w = image.shape[:2]try:components = bbox_input.strip().split('\n')# print("Split components:", components) # Debug printfor component in components:component = component.strip()if not component:continueif ':' in component:name, bbox_str = component.split(':', 1)else:bbox_str = componentif 'sidebar' in component.lower():name = 'sidebar'elif 'header' in component.lower():name = 'header'elif 'navigation' in component.lower():name = 'navigation'elif 'main content' in component.lower():name = 'main content'else:name = 'unknown'name = name.strip().lower()bbox_str = bbox_str.strip()# print(f"Processing component: {name}, bbox_str: {bbox_str}") # Debug printif BBOX_TAG_START in bbox_str and BBOX_TAG_END in bbox_str:start_idx = bbox_str.find(BBOX_TAG_START) + len(BBOX_TAG_START)end_idx = bbox_str.find(BBOX_TAG_END)2 unmodified linestry:norm_coords = list(map(int, coords_str.split()))if len(norm_coords) == 4:x_min = int(norm_coords[0])y_min = int(norm_coords[1])x_max = int(norm_coords[2])y_max = int(norm_coords[3])bboxes[name] = (x_min, y_min, x_max, y_max)print(f"Successfully parsed {name}: {bboxes[name]}")else:print(f"Invalid number of coordinates for {name}: {norm_coords}")except ValueError as e:print(f"Failed to parse coordinates for {name}: {e}")else:print(f"No bbox tags found in: {bbox_str}")except Exception as e:print(f"Coordinate parsing failed: {str(e)}")import tracebacktraceback.print_exc()print("Final parsed bboxes:", bboxes)return bboxesdef draw_bboxes(image_path: str, bboxes: dict[str, tuple[int, int, int, int]]) -> str:"""Draw bounding boxes on image and save with different colors for each component"""image = cv2.imread(image_path)if image is None:print(f"Error: Failed to read image {image_path}")return ""h, w = image.shape[:2]colors = {'sidebar': (0, 0, 255), # Red'header': (0, 255, 0), # Green'navigation': (255, 0, 0), # Blue'main content': (255, 255, 0), # Cyan'unknown': (0, 0, 0), # Black}for component, norm_bbox in bboxes.items():# Convert normalized coordinates to pixel coordinates for drawingx_min = int(norm_bbox[0] * w / 1000)y_min = int(norm_bbox[1] * h / 1000)x_max = int(norm_bbox[2] * w / 1000)y_max = int(norm_bbox[3] * h / 1000)color = colors.get(component.lower(), (0, 0, 255))cv2.rectangle(image, (x_min, y_min), (x_max, y_max), color, 3)# Add labelcv2.putText(image, component, (x_min, y_min - 10),cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)# Output directoryoutput_dir = "data/tmp"os.makedirs(output_dir, exist_ok=True)# Get the original filename without pathoriginal_filename = os.path.basename(image_path)output_path = os.path.join(output_dir, os.path.splitext(original_filename)[0] + "_with_bboxes.png")if cv2.imwrite(output_path, image):print(f"Successfully saved annotated image: {output_path}")return output_pathprint("Error: Failed to save image")return ""def save_bboxes_to_json(bboxes: dict[str, tuple[int, int, int, int]], image_path: str) -> str:"""Save bounding boxes information to a JSON file"""# Output directoryoutput_dir = "data/tmp"os.makedirs(output_dir, exist_ok=True)original_filename = os.path.basename(image_path)json_path = os.path.join(output_dir, os.path.splitext(original_filename)[0] + "_bboxes.json")bboxes_dict = {k: list(v) for k, v in bboxes.items()}try:with open(json_path, 'w', encoding='utf-8') as f:json.dump(bboxes_dict, f, indent=4, ensure_ascii=False)3 unmodified linesprint(f"Error saving JSON file: {str(e)}")return ""# sequential version of bbox parsing: Using recursive detection with maskdef sequential_component_detection(image_path: str, api_path: str) -> dict[str, tuple[int, int, int, int]]:"""Sequential processing flow: detect each component in turn, mask the image after each detection"""27 unmodified linesmasked_image = image_mask(current_image_path, norm_bbox)temp_image_path = f"data/temp_{component_name}_masked.png"masked_image.save(temp_image_path)current_image_path = temp_image_path42 unmodified linesint(bbox[3] * h / 1000))if __name__ == "__main__":image_path = DEFAULT_IMAGE_PATHapi_path = DEFAULT_API_PATHprint("=== Starting Simple Component Detection ===")print(f"Input image: {image_path}")print(f"API path: {api_path}")client = Doubao(api_path)bbox_content = client.ask(PROMPT_MERGE, encode_image(image_path))print(f"Model response: {bbox_content}\n")bboxes = parse_bboxes(bbox_content, image_path)# print("=== Starting Sequential Component Detection ===")# print(f"Input image: {image_path}")# print(f"API path: {api_path}")# bboxes = sequential_component_detection(image_path, api_path)if bboxes:print(f"\n=== Detection Complete ===")print(f"Found bounding boxes for components: {list(bboxes.keys())}")print(f"Total components detected: {len(bboxes)}")json_path = save_bboxes_to_json(bboxes, image_path)draw_bboxes(image_path, bboxes)print(f"\n=== Results ===")for component, bbox in bboxes.items():print(f"{component}: {bbox}")else:print("\nNo valid bounding box coordinates found")exit(1)123456713 unmodified lines212223242526272829303132333435363738394041424344454647484950515253542 unmodified lines575859606162636465666768697071727374757677787980818283848586878889909192939495969798991003 unmodified lines10410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014127 unmodified lines16917017117217317417542 unmodified lines218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258import osimport cv2import jsonimport argparsefrom utils import Doubao, encode_image, image_maskDEFAULT_IMAGE_PATH = "data/input/test1.png"13 unmodified lines# PROMPT_navigation = "框出网页中的navigation的位置,请你只返回对应的bounding box。"# PROMPT_main_content = "框出网页中的main content的位置,请你只返回对应的bounding box。"def get_args():parser = argparse.ArgumentParser(description="Parses bounding boxes from an image using a vision model.")parser.add_argument('--run_id', type=str, required=True, help='A unique identifier for the processing run.')return parser.parse_args()def parse_bboxes(bbox_input: str) -> dict[str, tuple[int, int, int, int]]:"""Parse bounding box string to a dictionary of normalized (0-1000) coordinate tuples."""bboxes = {}try:components = bbox_input.strip().split('\n')for component in components:component = component.strip()if not component:continueif ':' in component:name, bbox_str = component.split(':', 1)else:bbox_str = componentif 'sidebar' in component.lower(): name = 'sidebar'elif 'header' in component.lower(): name = 'header'elif 'navigation' in component.lower(): name = 'navigation'elif 'main content' in component.lower(): name = 'main content'else: name = 'unknown'name = name.strip().lower()bbox_str = bbox_str.strip()if BBOX_TAG_START in bbox_str and BBOX_TAG_END in bbox_str:start_idx = bbox_str.find(BBOX_TAG_START) + len(BBOX_TAG_START)end_idx = bbox_str.find(BBOX_TAG_END)2 unmodified linestry:norm_coords = list(map(int, coords_str.split()))if len(norm_coords) == 4:bboxes[name] = tuple(norm_coords) # Directly store normalized coordinatesprint(f"Successfully parsed {name}: {bboxes[name]}")except ValueError as e:print(f"Failed to parse coordinates for {name}: {e}")except Exception as e:print(f"Coordinate parsing failed: {str(e)}")print("Final parsed bboxes:", bboxes)return bboxesdef draw_bboxes(image_path: str, bboxes: dict[str, tuple[int, int, int, int]], output_path: str) -> str:"""Draws normalized (0-1000) bboxes on an image for visualization."""image = cv2.imread(image_path)if image is None: return ""h, w = image.shape[:2]colors = {'sidebar': (0, 0, 255), 'header': (0, 255, 0), 'navigation': (255, 0, 0), 'main content': (255, 255, 0), 'unknown': (0, 0, 0)}output_image = image.copy()for component, norm_bbox in bboxes.items():x_min = int(norm_bbox[0] * w / 1000)y_min = int(norm_bbox[1] * h / 1000)x_max = int(norm_bbox[2] * w / 1000)y_max = int(norm_bbox[3] * h / 1000)color = colors.get(component.lower(), (0, 0, 255))cv2.rectangle(output_image, (x_min, y_min), (x_max, y_max), color, 3)cv2.putText(output_image, component, (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)if cv2.imwrite(output_path, output_image):print(f"Successfully saved annotated image: {output_path}")return output_pathreturn ""def save_bboxes_to_json(bboxes: dict[str, tuple[int, int, int, int]], json_path: str) -> str:"""Saves the normalized bboxes to a JSON file."""# This is the unified format: a dictionary of lists.bboxes_dict = {k: list(v) for k, v in bboxes.items()}try:with open(json_path, 'w', encoding='utf-8') as f:json.dump(bboxes_dict, f, indent=4, ensure_ascii=False)3 unmodified linesprint(f"Error saving JSON file: {str(e)}")return ""def resolve_containment(bboxes: dict[str, tuple[int, int, int, int]]) -> dict[str, tuple[int, int, int, int]]:"""Resolves containment issues among bounding boxes.If a box is found to be fully contained within another, it is removed.This is based on the assumption that major layout components should not contain each other."""def contains(box_a, box_b):"""Checks if box_a completely contains box_b."""xa1, ya1, xa2, ya2 = box_axb1, yb1, xb2, yb2 = box_breturn xa1 <= xb1 and ya1 <= yb1 and xa2 >= xb2 and ya2 >= yb2names = list(bboxes.keys())removed = set()for i in range(len(names)):for j in range(len(names)):if i == j or names[i] in removed or names[j] in removed:continuename1, box1 = names[i], bboxes[names[i]]name2, box2 = names[j], bboxes[names[j]]if contains(box1, box2) or contains(box2, box1):print(f"Containment found: '{name1}' contains '{name2}'. Removing '{name2}'.")removed.add(name2)return {name: bbox for name, bbox in bboxes.items() if name not in removed}# sequential version of bbox parsing: Using recursive detection with maskdef sequential_component_detection(image_path: str, api_path: str, temp_dir: str) -> dict[str, tuple[int, int, int, int]]:"""Sequential processing flow: detect each component in turn, mask the image after each detection"""27 unmodified linesmasked_image = image_mask(current_image_path, norm_bbox)temp_image_path = os.path.join(temp_dir, f"temp_{component_name}_masked.png")masked_image.save(temp_image_path)current_image_path = temp_image_path42 unmodified linesint(bbox[3] * h / 1000))def main():args = get_args()run_id = args.run_id# --- Dynamic Path Construction ---base_dir = os.path.dirname(os.path.abspath(__file__))tmp_dir = os.path.join(base_dir, 'data', 'tmp', run_id)image_path = os.path.join(tmp_dir, f"{run_id}.png")api_path = os.path.join(base_dir, "doubao_api.txt")json_output_path = os.path.join(tmp_dir, f"{run_id}_bboxes.json")annotated_image_output_path = os.path.join(tmp_dir, f"{run_id}_with_bboxes.png")if not os.path.exists(image_path) or not os.path.exists(api_path):print(f"Error: Input image or API key file not found.")exit(1)print(f"--- Starting BBox Parsing for run_id: {run_id} ---")client = Doubao(api_path)bbox_content = client.ask(PROMPT_MERGE, encode_image(image_path))bboxes = parse_bboxes(bbox_content)if bboxes:print("\n--- Resolving containment issues ---")bboxes = resolve_containment(bboxes)print("--- Containment resolved ---")print(f"\n--- Detection Complete for run_id: {run_id} ---")save_bboxes_to_json(bboxes, json_output_path)draw_bboxes(image_path, bboxes, annotated_image_output_path)else:print(f"\nNo valid bounding box coordinates found for run_id: {run_id}")# Still create an empty json file so the pipeline doesn't breaksave_bboxes_to_json({}, json_output_path)if __name__ == "__main__":main()
2 unmodified lines34567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111 unmodified line11311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717836 unmodified lines2152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503512 unmodified linesimport bs4from threading import Threadimport time# This dictionary can now be dynamically updated by an external script.user_instruction = {"sidebar": "Make all icons look better; fill in relevant English text; beautify the layout.","header": "Make the Google logo look better; change the avatar color to be more appealing.","navigation": "Please beautify the layout.","main content": "Based on the layout, please fill in appropriate English text and beautify the image blocks."}# Prompt for each componentPROMPT_DICT = {"sidebar": f"""This is a screenshot of a container. Please fill in a complete HTML and tail-wind CSS code to accurately reproduce the given container. Please note that the layout, icon style, size, and text information of all blocks need to be basically consistent with the original screenshot based on the user's additional conditions. The following is the code for filling in:<div>your code here</div>,only return the code within the <div> and </div> tags""","header": f"""This is a screenshot of a container. Please fill in a complete HTML and tail-wind CSS code to accurately reproduce the given container. Please note that the relative position, layout, text information, and color of all blocks in the boundary box need to be basically consistent with the original screenshot based on the user's additional conditions. The following is the code for filling in:<div>your code here</div>,only return the code within the <div> and </div> tags""","navigation": f"""This is a screenshot of a container. Please fill in a complete HTML and tail-wind CSS code to accurately reproduce the given container. Please note that the relative position, layout, text information, and color of all blocks in the boundary box need to be basically consistent with the original screenshot based on the user's additional conditions. Please use the same icons as in the original screenshot. The following is the code for filling in:<div>your code here</div>,only return the code within the <div> and </div> tags""","main content": f"""This is a screenshot of a container. Please fill in a complete HTML and tail-wind CSS code to accurately reproduce the given container. Please note that all images displayed in the screenshot must be replaced with pure gray-400 image blocks of the same size as the corresponding images in the original screenshot, and the text information in the images does not need to be recognized. The relative position, layout, text information, and color of all blocks in the boundary box need to be basically consistent with the original screenshot based on the user's additional conditions. The following is the code for filling in:<div>your code here</div>,only return the code within the <div> and </div> tags""",}# PROMPT_sidebar = f"""这是一个container的截图。请填写一段完整的HTML和tail-wind CSS代码以准确再现给定的容器。请注意所有组块的排版、图标样式、大小、文字信息需要在用户额外条件的基础上与原始截图基本保持一致。以下是供填写的代码:# <div># your code here# </div># 只需返回<div>和</div>标签内的代码"""# PROMPT_header = f"""这是一个container的截图。请填写一段完整的HTML和tail-wind CSS代码以准确再现给定的容器。请注意所有组块在boundary box中的相对位置、排版、文字信息、颜色需要在用户额外条件的基础上与原始截图基本保持一致。以下是供填写的代码:# <div># your code here# </div># 只需返回<div>和</div>标签内的代码"""# PROMPT_navigation = f"""这是一个container的截图。请填写一段完整的HTML和tail-wind CSS代码以准确再现给定的容器。请注意所有组块的在boundary box中的相对位置、文字排版、颜色需要在用户额外条件的基础上与原始截图基本保持一致。请你直接使用原始截图中一致的图标。以下是供填写的代码:# <div># your code here# </div># 只需返回<div>和</div>标签内的代码"""# PROMPT_main_content = f"""这是一个container的截图。请填写一段完整的HTML和tail-wind CSS代码以准确再现给定的容器。截图中显示的图像务必全部用与原始截图中对应图像同样大小的纯灰色图像块替换,不需要识别图像中的文字信息。请注意所有组块在boundary box中的相对位置、排版、文字信息、颜色需要在用户额外条件的基础上与原始截图基本保持一致。以下是供填写的代码:# <div># your code here# </div># 只需返回<div>和</div>标签内的代码"""# Generate code for each componentdef generate_code(bbox_tree, img_path, bot):"""generate code for all the leaf nodes in the bounding box tree, return a dictionary: {'id': 'code'}"""img = Image.open(img_path)code_dict = {}def _generate_code(node):if node["children"] == []:bbox = node["bbox"]# bbox is already in pixel coordinates [x1, y1, x2, y2]cropped_img = img.crop(bbox)# Select prompt based on node typeif "type" in node:if node["type"] == "sidebar":prompt = PROMPT_DICT["sidebar"]elif node["type"] == "header":prompt = PROMPT_DICT["header"]elif node["type"] == "navigation":prompt = PROMPT_DICT["navigation"]elif node["type"] == "main content":prompt = PROMPT_DICT["main content"]else:print(f"Unknown component type: {node['type']}")returnelse:print("Node type not found")returntry:code = bot.ask(prompt, encode_image(cropped_img))code_dict[node["id"]] = codeexcept Exception as e:print(f"Error generating code for {node.get('type', 'unknown')}: {str(e)}")code_dict[node["id"]] = f"<!-- Error: {str(e)} -->"else:for child in node["children"]:_generate_code(child)1 unmodified line_generate_code(bbox_tree)return code_dict# Generate code for each component in parallel# def generate_code_parallel(bbox_tree, img_path, prompt, bot):"""generate code for all the leaf nodes in the bounding box tree, return a dictionary: {'id': 'code'}"""code_dict = {}t_list = []def _generate_code_with_retry(node, max_retries=3, retry_delay=2):"""Generate code with retry mechanism for rate limit errors"""try:# Create a new image instance for each threadwith Image.open(img_path) as img:bbox = node["bbox"]cropped_img = img.crop(bbox)for attempt in range(max_retries):try:code = bot.ask(prompt, encode_image(cropped_img))code_dict[node["id"]] = codereturnexcept Exception as e:if "rate_limit" in str(e).lower() and attempt < max_retries - 1:print(f"Rate limit hit, retrying in {retry_delay} seconds... (Attempt {attempt + 1}/{max_retries})")time.sleep(retry_delay)retry_delay *= 2 # Exponential backoffelse:print(f"Error generating code for node {node['id']}: {str(e)}")code_dict[node["id"]] = f"<!-- Error: {str(e)} -->"returnexcept Exception as e:print(f"Error processing image for node {node['id']}: {str(e)}")code_dict[node["id"]] = f"<!-- Error: {str(e)} -->"def _generate_code(node):if node["children"] == []:t = Thread(target=lambda: _generate_code_with_retry(node))t.start()t_list.append(t)else:for child in node["children"]:_generate_code(child)_generate_code(bbox_tree)# Wait for all threads to completefor t in t_list:t.join()return code_dict# Generate HTML from the bounding box treedef generate_html(bbox_tree, output_file="output.html", img_path="data/test1.png"):"""Generates an HTML file with nested containers based on the bounding box tree.:param bbox_tree: Dictionary representing the bounding box tree.:param output_file: The name of the output HTML file."""# HTML and CSS templates# the container class is used to create grid and position the boxes# include the tailwind css in the head taghtml_template_start = """<!DOCTYPE html><html lang="en">36 unmodified lines</html>"""# Function to recursively generate HTMLdef process_bbox(node, parent_width, parent_height, parent_left, parent_top, img):bbox = node['bbox']children = node.get('children', [])id = node['id']# Calculate relative positions and sizesleft = (bbox[0] - parent_left) / parent_width * 100top = (bbox[1] - parent_top) / parent_height * 100width = (bbox[2] - bbox[0]) / parent_width * 100height = (bbox[3] - bbox[1]) / parent_height * 100# Start the box divhtml = f'''<div id="{id}" class="box" style="left: {left}%; top: {top}%; width: {width}%; height: {height}%;">'''if children:# If there are children, add a nested containerhtml += '''<div class="container">'''# Get the current box's width and height in pixels for child calculationscurrent_width = bbox[2] - bbox[0]current_height = bbox[3] - bbox[1]for child in children:html += process_bbox(child, current_width, current_height, bbox[0], bbox[1], img)html += '''</div>'''# Close the box divhtml += '''</div>'''return htmlroot_bbox = bbox_tree['bbox']root_children = bbox_tree.get('children', [])root_width = root_bbox[2]root_height = root_bbox[3]root_x = root_bbox[0]root_y = root_bbox[1]html_content = html_template_startfor child in root_children:html_content += process_bbox(child, root_width, root_height, root_x, root_y, img)html_content += html_template_endsoup = bs4.BeautifulSoup(html_content, 'html.parser')html_content = soup.prettify()with open(output_file, 'w') as f:f.write(html_content)# Substitute the code in the html filedef code_substitution(html_file, code_dict):"""substitute the code in the html file"""with open(html_file, "r") as f:html = f.read()soup = bs4.BeautifulSoup(html, 'html.parser')for id, code in code_dict.items():code = code.replace("```html", "").replace("```", "")div = soup.find(id=id)# replace the inner html of the divif div:div.append(bs4.BeautifulSoup(code, 'html.parser'))with open(html_file, "w") as f:f.write(soup.prettify())# Mainif __name__ == "__main__":import jsonimport timefrom PIL import Image# Load bboxes from block_parsing.py outputboxes_data = json.load(open("data/tmp/test1_bboxes.json"))img_path = "data/input/test1.png"with Image.open(img_path) as img:width, height = img.size# Create root node with actual image dimensionsroot = {"bbox": [0, 0, width, height], # Use actual image dimensions"children": []}# Map region IDs to component typesregion_type_mapping = {"1": "sidebar","2": "header","3": "navigation","4": "main content"}# Add each region as a child with its typefor region in boxes_data.get("regions", []):# Convert normalized coordinates to pixel coordinatesx = region["x"] * widthy = region["y"] * heightw = region["w"] * widthh = region["h"] * heightchild = {"bbox": [x, y, x + w, y + h], # Convert to [x1, y1, x2, y2] format"children": [],"type": region_type_mapping.get(region["id"], "unknown")}root["children"].append(child)# Assign IDs to all nodesdef assign_id(node, id):node["id"] = idfor child in node.get("children", []):id = assign_id(child, id+1)return idassign_id(root, 0)# print(root)# Generate initial HTML layoutgenerate_html(root, 'data/output/test1_layout.html')# Initialize the botbot = Doubao("doubao_api.txt", model = "doubao-1.5-thinking-vision-pro-250428")# bot = Qwen_2_5_VL("qwen_api.txt", model="qwen2.5-vl-72b-instruct")# Generate code for each componentcode_dict = generate_code(root, img_path, bot)# Substitute the generated code into the HTMLcode_substitution('data/output/test1_layout.html', code_dict)2 unmodified lines3456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566671 unmodified line697071727374757636 unmodified lines1131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222 unmodified linesimport bs4from threading import Threadimport timeimport argparseimport jsonimport os# This dictionary holds the user's instructions for the current run.user_instruction = {"sidebar": "", "header": "", "navigation": "", "main content": ""}def get_args():parser = argparse.ArgumentParser(description="Generates an HTML layout from bounding box data.")parser.add_argument('--run_id', type=str, required=True, help='A unique identifier for the processing run.')parser.add_argument('--instructions', type=str, help='A JSON string of instructions for different components.')return parser.parse_args()def get_prompt_dict(instructions):"""Dynamically creates the prompt dictionary with the user's instructions."""return {"sidebar": f"""This is a screenshot of a container. Please fill in a complete HTML and tail-wind CSS code to accurately reproduce the given container. Please note that the layout, icon style, size, and text information of all blocks need to be basically consistent with the original screenshot based on the user's additional conditions. User instruction: {instructions["sidebar"]}. The following is the code for filling in:<div>your code here</div>,only return the code within the <div> and </div> tags""","header": f"""This is a screenshot of a container. Please fill in a complete HTML and tail-wind CSS code to accurately reproduce the given container. Please note that the relative position, layout, text information, and color of all blocks in the boundary box need to be basically consistent with the original screenshot based on the user's additional conditions. User instruction: {instructions["header"]}. The following is the code for filling in:<div>your code here</div>,only return the code within the <div> and </div> tags""","navigation": f"""This is a screenshot of a container. Please fill in a complete HTML and tail-wind CSS code to accurately reproduce the given container. Please note that the relative position, layout, text information, and color of all blocks in the boundary box need to be basically consistent with the original screenshot based on the user's additional conditions. Please use the same icons as in the original screenshot. User instruction: {instructions["navigation"]}. The following is the code for filling in:<div>your code here</div>,only return the code within the <div> and </div> tags""","main content": f"""This is a screenshot of a container. Please fill in a complete HTML and tail-wind CSS code to accurately reproduce the given container. Please note that all images displayed in the screenshot must be replaced with pure gray-400 image blocks of the same size as the corresponding images in the original screenshot, and the text information in the images does not need to be recognized. The relative position, layout, text information, and color of all blocks in the boundary box need to be basically consistent with the original screenshot based on the user's additional conditions. User instruction: {instructions["main content"]}. The following is the code for filling in:<div>your code here</div>,only return the code within the <div> and </div> tags""",}def generate_code(bbox_tree, img_path, bot, instructions):"""Generates code for each leaf node in the bounding box tree."""img = Image.open(img_path)code_dict = {}prompt_dict = get_prompt_dict(instructions)def _generate_code(node):if not node.get("children"): # It's a leaf nodebbox = node["bbox"]cropped_img = img.crop(bbox)node_type = node.get("type")if node_type and node_type in prompt_dict:prompt = prompt_dict[node_type]try:code = bot.ask(prompt, encode_image(cropped_img))code_dict[node["id"]] = codeexcept Exception as e:print(f"Error generating code for {node_type}: {e}")else:print(f"Node type '{node_type}' not found or invalid.")else:for child in node["children"]:_generate_code(child)1 unmodified line_generate_code(bbox_tree)return code_dictdef generate_html(bbox_tree, output_file):"""Generates an HTML file with nested containers based on the bounding box tree."""html_template_start = """<!DOCTYPE html><html lang="en">36 unmodified lines</html>"""def process_bbox(node, parent_width, parent_height, parent_left, parent_top):bbox = node['bbox']children = node.get('children', [])node_id = node['id']left = (bbox[0] - parent_left) / parent_width * 100top = (bbox[1] - parent_top) / parent_height * 100width = (bbox[2] - bbox[0]) / parent_width * 100height = (bbox[3] - bbox[1]) / parent_height * 100html = f'<div id="{node_id}" class="box" style="left: {left}%; top: {top}%; width: {width}%; height: {height}%;">'if children:html += '<div class="container">'current_width = bbox[2] - bbox[0]current_height = bbox[3] - bbox[1]for child in children:html += process_bbox(child, current_width, current_height, bbox[0], bbox[1])html += '</div>'html += '</div>'return htmlroot_bbox = bbox_tree['bbox']root_children = bbox_tree.get('children', [])root_width = root_bbox[2] - root_bbox[0]root_height = root_bbox[3] - root_bbox[1]html_content = html_template_startfor child in root_children:html_content += process_bbox(child, root_width, root_height, root_bbox[0], root_bbox[1])html_content += html_template_endwith open(output_file, 'w') as f:f.write(bs4.BeautifulSoup(html_content, 'html.parser').prettify())def code_substitution(html_file, code_dict):"""Substitutes the generated code into the HTML file."""with open(html_file, "r") as f:soup = bs4.BeautifulSoup(f.read(), 'html.parser')for node_id, code in code_dict.items():div = soup.find(id=node_id)if div:div.append(bs4.BeautifulSoup(code.replace("```html", "").replace("```", ""), 'html.parser'))with open(html_file, "w") as f:f.write(soup.prettify())def main():args = get_args()if args.instructions:try:user_instruction.update(json.loads(args.instructions))except json.JSONDecodeError:print("Error: Could not decode instructions JSON.")# --- Dynamic Path Construction ---base_dir = os.path.dirname(os.path.abspath(__file__))tmp_dir = os.path.join(base_dir, 'data', 'tmp', args.run_id)output_dir = os.path.join(base_dir, 'data', 'output', args.run_id)os.makedirs(output_dir, exist_ok=True)input_json_path = os.path.join(tmp_dir, f"{args.run_id}_bboxes.json")img_path = os.path.join(tmp_dir, f"{args.run_id}.png")output_html_path = os.path.join(output_dir, f"{args.run_id}_layout.html")if not os.path.exists(input_json_path) or not os.path.exists(img_path):print("Error: Input bbox JSON or image file not found.")exit(1)print(f"--- Starting HTML Generation for run_id: {args.run_id} ---")with open(input_json_path, 'r') as f:boxes_data = json.load(f)with Image.open(img_path) as img:width, height = img.sizeroot = {"bbox": [0, 0, width, height], "children": [], "id": 0}# Convert normalized bboxes to pixel coordinatesfor name, norm_bbox in boxes_data.items():x1 = int(norm_bbox[0] * width / 1000)y1 = int(norm_bbox[1] * height / 1000)x2 = int(norm_bbox[2] * width / 1000)y2 = int(norm_bbox[3] * height / 1000)root["children"].append({"bbox": [x1, y1, x2, y2], "type": name, "children": []})# Assign unique IDs to all nodes for code substitutionnext_id = 1for child in root["children"]:child["id"] = next_idnext_id += 1generate_html(root, output_html_path)api_path = os.path.join(base_dir, "doubao_api.txt")if not os.path.exists(api_path):print(f"Error: API key not found at {api_path}")exit(1)bot = Doubao(api_path, model="doubao-1.5-thinking-vision-pro-250428")code_dict = generate_code(root, img_path, bot, user_instruction)code_substitution(output_html_path, code_dict)print(f"HTML layout with generated content saved to {os.path.basename(output_html_path)}")print(f"--- HTML Generation Complete for run_id: {args.run_id} ---")if __name__ == "__main__":main()
114 unmodified lines1151161171181191201211221231241251 unmodified line12712812913013113213313413513626 unmodified lines16316416516616716816917017117222 unmodified lines195196197198199200201202203204205206207208209210211212213214215216217218114 unmodified linesreturn boxeddef main(args):# Read original screenshotimg = cv2.imread(str(args.screenshot))if img is None:sys.exit(f"Error: Cannot read image {args.screenshot}")if img.std() < 5:print("Warning: The screenshot is almost pure color, it may not be the original screenshot with real thumbnails.")1 unmodified line# Parse HTML → Get bboxesregion_bboxes, placeholder_bboxes, layout_width, layout_height = asyncio.run(extract_bboxes_from_html(args.html))if not placeholder_bboxes:sys.exit("Error: No gray placeholder blocks found!")# Calculate separate scale factors for X and Y to handle aspect ratio differencesscale_x = W / layout_width if layout_width > 0 else 126 unmodified linesoverlay = draw_bboxes_on_image(img, scaled_regions, scaled_placeholders)# Save debug imageout_png = args.out / "debug_gray_bboxes_test1.png"out_png.parent.mkdir(parents=True, exist_ok=True)cv2.imwrite(str(out_png), overlay)print(f"Success: BBox overlay saved to {out_png}")# Convert absolute pixel coordinates to proportions for the final JSON output22 unmodified linesoutput_json = json.dumps(output_data, indent=2, ensure_ascii=False)print(output_json)if args.json:args.json.parent.mkdir(parents=True, exist_ok=True)args.json.write_text(output_json)print(f"Success: BBox list saved to {args.json}")# ---------- CLI ----------if __name__ == "__main__":parser = argparse.ArgumentParser(description="Draw BBoxes parsed from HTML on the original screenshot")parser.add_argument("--html", required=False, type=Path, default=Path("data/output/test1_layout.html"),help="Generated HTML file (with gray placeholder)")parser.add_argument("--screenshot", required=False, type=Path, default=Path("data/input/test1.png"),help="Original UI screenshot (with real thumbnails)")parser.add_argument("--out", default=Path("data/tmp"), type=Path,help="Output directory (save debug_gray_bboxes_test1.png)")parser.add_argument("--json", type=Path, default=Path("data/tmp/test1_bboxes.json"),help="If provided, write BBox list to JSON file")args = parser.parse_args()main(args)114 unmodified lines1151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471 unmodified line14915015115215315415515615715815926 unmodified lines18618718818919019119219319422 unmodified lines217218219220221222223224225226227228229230231232233234235114 unmodified linesreturn boxeddef main():args = get_args()run_id = args.run_id# --- Dynamic Path Construction ---base_dir = Path(__file__).parent.resolve()# Go up one level to the project root to find the data directoryproject_root = base_dir.parenttmp_dir = project_root / 'screencoder' / 'data' / 'tmp' / run_idoutput_dir = project_root / 'screencoder' / 'data' / 'output' / run_idhtml_path = output_dir / f"{run_id}_layout.html"screenshot_path = tmp_dir / f"{run_id}.png"output_json_path = tmp_dir / f"{run_id}_bboxes.json"debug_image_path = tmp_dir / f"debug_gray_bboxes_{run_id}.png"if not html_path.exists():sys.exit(f"Error: HTML file not found at {html_path}")if not screenshot_path.exists():sys.exit(f"Error: Screenshot not found at {screenshot_path}")print(f"--- Starting Image Box Detection for run_id: {run_id} ---")# Read original screenshotimg = cv2.imread(str(screenshot_path))if img is None:sys.exit(f"Error: Cannot read image {screenshot_path}")if img.std() < 5:print("Warning: The screenshot is almost pure color, it may not be the original screenshot with real thumbnails.")1 unmodified line# Parse HTML → Get bboxesregion_bboxes, placeholder_bboxes, layout_width, layout_height = asyncio.run(extract_bboxes_from_html(html_path))if not placeholder_bboxes:# This is not necessarily an error; some UIs might not have placeholders.print("Info: No gray placeholder blocks found.")# Calculate separate scale factors for X and Y to handle aspect ratio differencesscale_x = W / layout_width if layout_width > 0 else 126 unmodified linesoverlay = draw_bboxes_on_image(img, scaled_regions, scaled_placeholders)# Save debug imagedebug_image_path.parent.mkdir(parents=True, exist_ok=True)cv2.imwrite(str(debug_image_path), overlay)print(f"Success: BBox overlay saved to {debug_image_path}")# Convert absolute pixel coordinates to proportions for the final JSON output22 unmodified linesoutput_json = json.dumps(output_data, indent=2, ensure_ascii=False)print(output_json)output_json_path.parent.mkdir(parents=True, exist_ok=True)output_json_path.write_text(output_json)print(f"Success: BBox list saved to {output_json_path}")print(f"--- Image Box Detection Complete for run_id: {run_id} ---")def get_args():parser = argparse.ArgumentParser(description="Extracts placeholder bounding boxes from an HTML file and maps them to a screenshot.")parser.add_argument('--run_id', required=True, type=str,help="A unique identifier for the processing run.")return parser.parse_args()# ---------- CLI ----------if __name__ == "__main__":main()
3 unmodified lines45678910111213141516171819208 unmodified lines2930313233343531 unmodified lines6768697071727341 unmodified lines1151161171181191201211221231241251261271281291301311321331341351361371381391401413 unmodified linesfrom bs4 import BeautifulSoupimport cv2import redef main(args):# --- Phase 1: Crop and Save All Images First ---# 1. Load datamapping_data = json.loads(args.mapping.read_text())uied_data = json.loads(args.uied.read_text())original_image = cv2.imread(str(args.original_image))if original_image is None:raise ValueError(f"Could not load the original image from {args.original_image}")# Get image shapes to calculate a simple, global scaling factorH_proc, W_proc, _ = uied_data['img_shape']8 unmodified lines}# 2. Create a directory for cropped imagescrop_dir = args.output_html.parent / "cropped_images"crop_dir.mkdir(exist_ok=True)print(f"Saving cropped images to: {crop_dir.resolve()}")31 unmodified lines# --- Phase 2: Use BeautifulSoup to Replace Placeholders by Order ---print("\nStarting offline HTML processing with BeautifulSoup...")html_content = args.gray_html.read_text()soup = BeautifulSoup(html_content, 'html.parser')# 1. Find all placeholder elements by their class, in document order.41 unmodified linesph_element.replace_with(img_tag)# Save the modified HTMLargs.output_html.write_text(str(soup))print(f"\nSuccessfully replaced {min(len(placeholder_elements), len(ordered_placeholder_ids))} placeholders.")print(f"Final HTML generated at {args.output_html.resolve()}")if __name__ == "__main__":parser = argparse.ArgumentParser(description="Replace placeholder divs in an HTML file with cropped images based on UIED mappings.")parser.add_argument("--mapping", type=Path, required=False, help="Path to the mapping JSON file from mapping.py.")parser.add_argument("--uied", type=Path, required=False, help="Path to the UIED JSON file.")parser.add_argument("--original-image", type=Path, required=False, help="Path to the original screenshot image.")parser.add_argument("--gray-html", type=Path, required=False, help="Path to the input HTML file with gray placeholders.")parser.add_argument("--output-html", type=Path, required=False, help="Path to save the final, modified HTML file.")parser.set_defaults(mapping=Path('data/tmp/mapping_full_test1.json'),uied=Path('data/tmp/ip/test1.json'),original_image=Path('data/input/test1.png'),gray_html=Path('data/output/test1_layout.html'),output_html=Path('data/output/test1_layout_final.html'))args = parser.parse_args()main(args)3 unmodified lines4567891011121314151617181920212223242526272829303132333435363738394041424344454647488 unmodified lines5758596061626331 unmodified lines959697989910010141 unmodified lines1431441451461471481491501511521531541551561571583 unmodified linesfrom bs4 import BeautifulSoupimport cv2import reimport sysdef main():args = get_args()run_id = args.run_id# --- Dynamic Path Construction ---base_dir = Path(__file__).parent.resolve()tmp_dir = base_dir / 'data' / 'tmp' / run_idoutput_dir = base_dir / 'data' / 'output' / run_idmapping_path = tmp_dir / f"mapping_full_{run_id}.json"uied_path = tmp_dir / "ip" / f"{run_id}.json"original_image_path = tmp_dir / f"{run_id}.png"# This is the input HTML with placeholdersgray_html_path = output_dir / f"{run_id}_layout.html"# This will be the final output of the entire pipelinefinal_html_path = output_dir / f"{run_id}_layout_final.html"# --- Input Validation ---if not all([p.exists() for p in [mapping_path, uied_path, original_image_path, gray_html_path]]):print("Error: One or more required input files are missing.", file=sys.stderr)if not mapping_path.exists(): print(f"- Missing: {mapping_path}", file=sys.stderr)if not uied_path.exists(): print(f"- Missing: {uied_path}", file=sys.stderr)if not original_image_path.exists(): print(f"- Missing: {original_image_path}", file=sys.stderr)if not gray_html_path.exists(): print(f"- Missing: {gray_html_path}", file=sys.stderr)sys.exit(1)print(f"--- Starting Image Replacement for run_id: {run_id} ---")# --- Phase 1: Crop and Save All Images First ---# 1. Load datamapping_data = json.loads(mapping_path.read_text())uied_data = json.loads(uied_path.read_text())original_image = cv2.imread(str(original_image_path))if original_image is None:raise ValueError(f"Could not load the original image from {original_image_path}")# Get image shapes to calculate a simple, global scaling factorH_proc, W_proc, _ = uied_data['img_shape']8 unmodified lines}# 2. Create a directory for cropped imagescrop_dir = final_html_path.parent / f"cropped_images_{run_id}"crop_dir.mkdir(exist_ok=True)print(f"Saving cropped images to: {crop_dir.resolve()}")31 unmodified lines# --- Phase 2: Use BeautifulSoup to Replace Placeholders by Order ---print("\nStarting offline HTML processing with BeautifulSoup...")html_content = gray_html_path.read_text()soup = BeautifulSoup(html_content, 'html.parser')# 1. Find all placeholder elements by their class, in document order.41 unmodified linesph_element.replace_with(img_tag)# Save the modified HTMLfinal_html_path.write_text(str(soup))print(f"\nSuccessfully replaced {min(len(placeholder_elements), len(ordered_placeholder_ids))} placeholders.")print(f"Final HTML generated at {final_html_path.resolve()}")print(f"--- Image Replacement Complete for run_id: {run_id} ---")def get_args():parser = argparse.ArgumentParser(description="Replace placeholder divs in an HTML file with cropped images based on UIED mappings.")parser.add_argument("--run_id", type=str, required=True, help="A unique identifier for the processing run.")return parser.parse_args()if __name__ == "__main__":main()
1 unmodified line2345678910111213141516171 unmodified line1920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411 unmodified lineimport sysimport osimport json# A simple placeholder for prompt injection# In a real scenario, this should be a more robust mechanismdef inject_prompt_to_generator(prompt_text):if not prompt_text:return# In this example, we assume the prompt is a simple string for the "main content"# A more complex implementation would parse a structured promptuser_instruction = {"sidebar": "Make all icons look better; fill in relevant English text; beautify the layout.","header": "Make the Google logo look better; change the avatar color to be more appealing.",1 unmodified line"main content": prompt_text}generator_path = os.path.join(os.path.dirname(__file__), 'html_generator.py')with open(generator_path, 'r', encoding='utf-8') as f:lines = f.readlines()# Find the user_instruction dictionary and replace itnew_lines = []in_dict = Falsefor line in lines:if 'user_instruction = {' in line:in_dict = Truenew_lines.append(f"user_instruction = {json.dumps(user_instruction, indent=4)}\n")elif in_dict and '}' in line:in_dict = Falsecontinue # Skip the closing brace of the old dictelif not in_dict:new_lines.append(line)with open(generator_path, 'w', encoding='utf-8') as f:f.writelines(new_lines)def run_script(script_path):script_path = os.path.normpath(script_path)print(f"\n{'='*20}")print(f"Executing: python {script_path}")print(f"{'='*20}")try:result = subprocess.run([sys.executable, script_path],check=True,capture_output=True,text=True)print("Success!")print("Output:")print(result.stdout)if result.stderr:print("Stderr:")print(result.stderr)except FileNotFoundError:print(f"ERROR: Script not found at '{script_path}'")sys.exit(1)except subprocess.CalledProcessError as e:print(f"ERROR: Script '{script_path}' failed with exit code {e.returncode}")print("Stdout:")print(e.stdout)print("Stderr:")print(e.stderr)sys.exit(1)except Exception as e:print(f"An unexpected error occurred while running '{script_path}': {e}")sys.exit(1)def generate_html_for_demo(image_path, prompt, output_dir="screencoder/data/output"):"""A modified workflow for the Gradio demo.It takes an image path and a prompt, and returns the path to the final HTML file."""print("Starting the Screencoder demo workflow...")# Setup pathsproject_root = os.path.dirname(__file__)# The block_parsor script expects a specific input file name, so we must place our image there.# IMPORTANT: This assumes a single-user-at-a-time workflow.# For multi-user, you'd need isolated temp directories.target_input_path = os.path.join(project_root, "data/input/test1.png")# Ensure the input directory existsos.makedirs(os.path.dirname(target_input_path), exist_ok=True)# Copy the user-uploaded image to the location the script expectsimport shutilshutil.copy(image_path, target_input_path)# --- Part 1: Initial Generation with Placeholders ---print("\n--- Part 1: Initial Generation with Placeholders ---")inject_prompt_to_generator(prompt)run_script(os.path.join(project_root, "block_parsor.py"))run_script(os.path.join(project_root, "html_generator.py"))# --- Part 2: Final HTML Code Generation ---print("\n--- Part 2: Final HTML Code Generation ---")run_script(os.path.join(project_root, "image_box_detection.py"))run_script(os.path.join(project_root, "UIED/run_single.py"))run_script(os.path.join(project_root, "mapping.py"))run_script(os.path.join(project_root, "image_replacer.py"))final_html_path = os.path.join(output_dir, "test1_layout_final.html")print(f"\nScreencoder demo workflow completed! Final HTML at: {final_html_path}")# Check if the final file existsif os.path.exists(final_html_path):with open(final_html_path, 'r', encoding='utf-8') as f:return f.read()else:return "<html><body><h1>Error: Final HTML not generated.</h1></body></html>"def main():"""Main function to run the entire Screencoder workflow."""print("Starting the Screencoder full workflow...")# --- Part 1: Initial Generation with Placeholders ---print("\n--- Part 1: Initial Generation with Placeholders ---")run_script("block_parsor.py")run_script("html_generator.py")# --- Part 2: Final HTML Code Generation ---print("\n--- Part 2: Final HTML Code Generation ---")run_script("image_box_detection.py")run_script("UIED/run_single.py")run_script("mapping.py")run_script("image_replacer.py")print("\nScreencoder workflow completed successfully!")if __name__ == "__main__":1 unmodified line23456789101112131415161 unmodified line181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321 unmodified lineimport sysimport osimport jsonimport uuidimport shutilfrom PIL import Image# This function is now more robust, injecting the prompt into a temporary copy of the generator.def inject_prompt_to_generator(prompt_text, temp_generator_path):if not prompt_text:returnuser_instruction = {"sidebar": "Make all icons look better; fill in relevant English text; beautify the layout.","header": "Make the Google logo look better; change the avatar color to be more appealing.",1 unmodified line"main content": prompt_text}with open(temp_generator_path, 'r', encoding='utf-8') as f:content = f.read()start_marker = "user_instruction = {"end_marker = "}"start_index = content.find(start_marker)end_index = content.find(end_marker, start_index)if start_index != -1 and end_index != -1:dict_str = f"user_instruction = {json.dumps(user_instruction, indent=4)}"content = content[:start_index] + dict_str + content[end_index+1:]with open(temp_generator_path, 'w', encoding='utf-8') as f:f.write(content)def run_script_with_run_id(script_name, run_id, instructions=None):"""Executes a script with a specific run_id and optional instructions."""screencoder_dir = os.path.dirname(os.path.abspath(__file__))script_path = os.path.join(screencoder_dir, script_name)if not os.path.exists(script_path):# Handle scripts inside subdirectories like UIED/script_path = os.path.join(screencoder_dir, "UIED", script_name)command = ["python", script_path, "--run_id", run_id]# Add instructions to the command if providedif instructions and script_name == "html_generator.py":instructions_json = json.dumps(instructions)command.extend(["--instructions", instructions_json])print(f"\n--- Running script: {script_name} ---")try:# Pass the current environment variables to the subprocessresult = subprocess.run(command, check=True, capture_output=True, text=True, env=os.environ)print(result.stdout)if result.stderr:print("Error:")print(result.stderr)except subprocess.CalledProcessError as e:print(f"Error executing {script_name}:")print(e.stdout)print(e.stderr)raise # Re-raise the exception to stop the workflow if a script failsdef generate_html_for_demo(image_path, instructions):"""A refactored main function for Gradio demo integration.It orchestrates the script executions for a single image processing run.- Creates a unique run_id for each call.- Sets up temporary directories for input and output.- Cleans up temporary directories after execution."""run_id = str(uuid.uuid4())print(f"--- Starting Screencoder workflow for run_id: {run_id} ---")base_dir = os.path.dirname(os.path.abspath(__file__))tmp_dir = os.path.join(base_dir, 'data', 'tmp', run_id)output_dir = os.path.join(base_dir, 'data', 'output', run_id)os.makedirs(tmp_dir, exist_ok=True)os.makedirs(output_dir, exist_ok=True)try:# 1. Copy user-uploaded image to the temp input directorynew_image_path = os.path.join(tmp_dir, f"{run_id}.png")img = Image.open(image_path)img.save(new_image_path, "PNG")# 2. Run the processing scripts in sequencerun_script_with_run_id("UIED/run_single.py", run_id)run_script_with_run_id("block_parsor.py", run_id)run_script_with_run_id("html_generator.py", run_id, instructions)run_script_with_run_id("image_box_detection.py", run_id)run_script_with_run_id("mapping.py", run_id)run_script_with_run_id("image_replacer.py", run_id)# 3. Read the final generated HTMLfinal_html_path = os.path.join(output_dir, f"{run_id}_layout_final.html")if os.path.exists(final_html_path):with open(final_html_path, 'r', encoding='utf-8') as f:html_content = f.read()print(f"Successfully generated HTML for run_id: {run_id}")return html_contentelse:return f"Error: Final HTML file not found for run_id: {run_id}"except Exception as e:print(f"An error occurred during the workflow for run_id {run_id}: {e}")return f"An error occurred: {e}"finally:# 4. Cleanup: Remove temporary directoriestry:# shutil.rmtree(tmp_dir)# shutil.rmtree(output_dir)print(f"Cleaned up temporary files for run_id: {run_id}")except OSError as e:print(f"Error cleaning up temporary files for run_id {run_id}: {e}")def main():"""Main function to run the entire Screencoder workflow (legacy)."""print("Starting the Screencoder full workflow (legacy)...")# This main function is now considered legacy and does not use dynamic run_ids.# It will continue to use the hardcoded paths.run_id = "test1" # Hardcoded for legacy mainrun_script_with_run_id("UIED/run_single.py", run_id)run_script_with_run_id("block_parsor.py", run_id)run_script_with_run_id("html_generator.py", run_id)run_script_with_run_id("image_box_detection.py", run_id)run_script_with_run_id("mapping.py", run_id)run_script_with_run_id("image_replacer.py", run_id)print("\nScreencoder workflow completed successfully!")if __name__ == "__main__":
226 unmodified lines22722822923023123223323423523623723823924024124224324424524624746 unmodified lines294295296297298299300301302303304305306307308309310311312313314226 unmodified linescv2.imwrite(str(out_png), canvas)def main(args):# 1. Load the original screenshot to get its absolute dimensionsif not args.debug_src or not args.debug_src.exists():sys.exit("Error: A valid --debug-src image path must be provided for coordinate conversion.")orig_img = cv2.imread(str(args.debug_src))if orig_img is None:sys.exit(f"Error: Could not read debug source image at {args.debug_src}.")H_orig, W_orig, _ = orig_img.shape# 2. Load proportional data and convert to absolute pixel coordinatespixel_regions, pixel_placeholders = load_regions_and_placeholders(args.gray, W_orig, H_orig)# 3. Load UIED dataall_uied_boxes, uied_shape = load_uied_boxes(args.uied)if not pixel_placeholders or not all_uied_boxes:print("Error: Could not proceed without placeholder and UIED data.")46 unmodified lines# 6. Report and save resultsprint(f"Successfully created {total_mappings_count} one-to-one mappings out of {total_placeholders_count} placeholders.")args.out.write_text(json.dumps(final_results, indent=2, ensure_ascii=False))print(f"Mapping data written to {args.out}")if args.debug:if not args.debug_src or not args.debug_src.exists():print("Error: A valid --debug-src image path must be provided when using --debug.")returngenerate_debug_overlay(args.debug_src, all_uied_boxes, final_results, uied_shape, args.debug)print(f"Debug image written to {args.debug}")if __name__ == "__main__":ap = argparse.ArgumentParser()ap.add_argument("--gray", type=Path, default=Path("data/tmp/test1_bboxes.json"), help="Path to the JSON file with gray placeholder boxes.")ap.add_argument("--uied", type=Path, default=Path("data/tmp/ip/test1.json"), help="Path to the JSON file with UIED detected boxes.")ap.add_argument("--out", default=Path("data/tmp/mapping_full_test1.json"), type=Path, help="Output path for the mapping JSON file.")ap.add_argument("--debug", type=Path, default=Path("data/tmp/overlay_test_test1.png"), help="Output path for the debug overlay PNG.")ap.add_argument("--debug-src", type=Path, default=Path("data/input/test1.png"), help="Path to the original screenshot for the debug overlay background.")main(ap.parse_args())226 unmodified lines22722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626746 unmodified lines314315316317318319320321322323324325326327328329330331332226 unmodified linescv2.imwrite(str(out_png), canvas)def main():args = get_args()run_id = args.run_id# --- Dynamic Path Construction ---base_dir = Path(__file__).parent.resolve()tmp_dir = base_dir / 'data' / 'tmp' / run_idgray_json_path = tmp_dir / f"{run_id}_bboxes.json"uied_json_path = tmp_dir / "ip" / f"{run_id}.json"mapping_output_path = tmp_dir / f"mapping_full_{run_id}.json"debug_src_path = tmp_dir / f"{run_id}.png"debug_overlay_path = tmp_dir / f"overlay_test_{run_id}.png"# --- Input Validation ---if not gray_json_path.exists():sys.exit(f"Error: Placeholder JSON not found at {gray_json_path}")if not uied_json_path.exists():sys.exit(f"Error: UIED JSON not found at {uied_json_path}")if not debug_src_path.exists():sys.exit(f"Error: Source image for coordinate conversion not found at {debug_src_path}")print(f"--- Starting Mapping for run_id: {run_id} ---")# 1. Load the original screenshot to get its absolute dimensionsorig_img = cv2.imread(str(debug_src_path))if orig_img is None:sys.exit(f"Error: Could not read debug source image at {debug_src_path}.")H_orig, W_orig, _ = orig_img.shape# 2. Load proportional data and convert to absolute pixel coordinatespixel_regions, pixel_placeholders = load_regions_and_placeholders(gray_json_path, W_orig, H_orig)# 3. Load UIED dataall_uied_boxes, uied_shape = load_uied_boxes(uied_json_path)if not pixel_placeholders or not all_uied_boxes:print("Error: Could not proceed without placeholder and UIED data.")46 unmodified lines# 6. Report and save resultsprint(f"Successfully created {total_mappings_count} one-to-one mappings out of {total_placeholders_count} placeholders.")mapping_output_path.write_text(json.dumps(final_results, indent=2, ensure_ascii=False))print(f"Mapping data written to {mapping_output_path}")# Always generate the debug image if the source existsgenerate_debug_overlay(debug_src_path, all_uied_boxes, final_results, uied_shape, debug_overlay_path)print(f"Debug image written to {debug_overlay_path}")print(f"--- Mapping Complete for run_id: {run_id} ---")def get_args():ap = argparse.ArgumentParser(description="Map UIED components to placeholder boxes.")ap.add_argument('--run_id', required=True, type=str, help="A unique identifier for the processing run.")return ap.parse_args()if __name__ == "__main__":main()
app.py+53-30
screencoder/UIED/detect_compo/ip_region_proposal.py+36
screencoder/UIED/detect_compo/lib_ip/ip_detection.py+4-4
screencoder/UIED/run_single.py+35-5
screencoder/block_parsor.py+90-103
screencoder/html_generator.py+105-234
screencoder/image_box_detection.py+43-26
screencoder/image_replacer.py+43-26
screencoder/main.py+96-105
screencoder/mapping.py+41-23