|
3 | 3 | import subprocess
|
4 | 4 | import cv2
|
5 | 5 | import numpy as np
|
| 6 | +from bs4 import BeautifulSoup |
6 | 7 | from imagecoderx import ocr, llm
|
7 | 8 | from imagecoderx.algorithms import algorithms
|
8 | 9 | from imagecoderx.config import load_config
|
@@ -50,14 +51,105 @@ def detect_text_regions(image_path: str) -> list[tuple[float, float, float, floa
|
50 | 51 |
|
51 | 52 | return text_regions
|
52 | 53 |
|
| 54 | +def analyze_background(image_path: str) -> str: |
| 55 | + """ |
| 56 | + Analyzes the background of an image to determine its type (background, logo, button, etc.). |
| 57 | + """ |
| 58 | + img = cv2.imread(image_path) |
| 59 | + if img is None: |
| 60 | + print(f"Error: Could not read image at {image_path}") |
| 61 | + return "unknown" |
| 62 | + |
| 63 | + # Convert the image to grayscale |
| 64 | + gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) |
| 65 | + |
| 66 | + # Calculate the average color of the background |
| 67 | + average_color = np.mean(gray) |
| 68 | + |
| 69 | + # Analyze the shape of the object |
| 70 | + contours, _ = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
| 71 | + if contours: |
| 72 | + # Approximate the contour with a simpler shape |
| 73 | + approx = cv2.approxPolyDP(contours[0], 0.01 * cv2.arcLength(contours[0], True), True) |
| 74 | + num_vertices = len(approx) |
| 75 | + |
| 76 | + if num_vertices <= 4: |
| 77 | + return "background" |
| 78 | + else: |
| 79 | + return "logo" |
| 80 | + else: |
| 81 | + return "background" |
| 82 | + |
53 | 83 | def convert_image_to_code(image_path: str, output_format: str) -> str:
|
54 | 84 | """
|
55 | 85 | Converts an image to code accurately using Tesseract, Ollama, and custom algorithms.
|
56 | 86 | """
|
| 87 | + # Detect text regions |
57 | 88 | text_regions = detect_text_regions(image_path)
|
58 |
| - text, boxes = ocr.extract_text_from_image(image_path) |
59 |
| - refined_code = llm.process_text_with_llm(image_path, text, boxes, output_format, text_regions) |
60 |
| - return algorithms.apply_custom_algorithms(refined_code, output_format) |
| 89 | + |
| 90 | + # Load the image |
| 91 | + img = cv2.imread(image_path) |
| 92 | + image_height, image_width = img.shape[:2] |
| 93 | + |
| 94 | + # Initialize HTML structure |
| 95 | + html_content = """<!DOCTYPE html> |
| 96 | +<html lang="en"> |
| 97 | +<head> |
| 98 | + <meta charset="UTF-8"> |
| 99 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 100 | + <title>Generated Code</title> |
| 101 | + <style> |
| 102 | + body { margin: 0; } |
| 103 | + .region { position: absolute; } |
| 104 | + </style> |
| 105 | +</head> |
| 106 | +<body>""" |
| 107 | + body_content = "" |
| 108 | + style_content = "" |
| 109 | + |
| 110 | + for i, (x, y, w, h) in enumerate(text_regions): |
| 111 | + # Calculate absolute coordinates |
| 112 | + x1 = int(x * image_width) |
| 113 | + y1 = int(y * image_height) |
| 114 | + x2 = int((x + w) * image_width) |
| 115 | + y2 = int((y + h) * image_height) |
| 116 | + |
| 117 | + # Crop the region from the image |
| 118 | + region_roi = img[y1:y2, x1:x2] |
| 119 | + |
| 120 | + # Save the region to a temporary file |
| 121 | + temp_file = os.path.join(os.path.dirname(image_path), f"temp_region_{i}.png") |
| 122 | + cv2.imwrite(temp_file, region_roi) |
| 123 | + |
| 124 | + # Extract text from the region |
| 125 | + text, boxes = ocr.extract_text_from_image(temp_file) |
| 126 | + |
| 127 | + # Get code from LLM |
| 128 | + refined_code = llm.process_text_with_llm(image_path, text, boxes, output_format, [(x, y, w, h)]) |
| 129 | + |
| 130 | + # Extract body and style from the code |
| 131 | + soup = BeautifulSoup(refined_code, 'html.parser') |
| 132 | + body = soup.find('body') |
| 133 | + style = soup.find('style') |
| 134 | + |
| 135 | + if body: |
| 136 | + body_content += str(body.contents[0]) if body.contents else "" |
| 137 | + if style: |
| 138 | + style_content += str(style.contents[0]) if style.contents else "" |
| 139 | + |
| 140 | + # Remove the temporary file |
| 141 | + os.remove(temp_file) |
| 142 | + |
| 143 | + # Combine the HTML structure |
| 144 | + html_content += f""" |
| 145 | + <style> |
| 146 | + {style_content} |
| 147 | + </style> |
| 148 | + {body_content} |
| 149 | +</body> |
| 150 | +</html>""" |
| 151 | + |
| 152 | + return html_content |
61 | 153 |
|
62 | 154 | def detect_objects_and_remove_background(image_path: str, output_dir: str):
|
63 | 155 | """
|
|
0 commit comments