Skip to content

Commit 3bf3910

Browse files
committed
🍱 Commit message: MSetup.
1 parent a78a1bb commit 3bf3910

File tree

2 files changed

+96
-3
lines changed

2 files changed

+96
-3
lines changed

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ install_requires =
5151
importlib-metadata; python_version<"3.8"
5252
ollama
5353
opencv-python
54+
beautifulsoup4
5455

5556
[options.packages.find]
5657
where = src

src/imagecoderx/core.py

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import subprocess
44
import cv2
55
import numpy as np
6+
from bs4 import BeautifulSoup
67
from imagecoderx import ocr, llm
78
from imagecoderx.algorithms import algorithms
89
from imagecoderx.config import load_config
@@ -50,14 +51,105 @@ def detect_text_regions(image_path: str) -> list[tuple[float, float, float, floa
5051

5152
return text_regions
5253

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+
5383
def convert_image_to_code(image_path: str, output_format: str) -> str:
5484
"""
5585
Converts an image to code accurately using Tesseract, Ollama, and custom algorithms.
5686
"""
87+
# Detect text regions
5788
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
61153

62154
def detect_objects_and_remove_background(image_path: str, output_dir: str):
63155
"""

0 commit comments

Comments
 (0)