Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,18 @@ import numpy as np
|
|
| 4 |
from PIL import Image, ImageOps
|
| 5 |
import subprocess
|
| 6 |
|
| 7 |
-
# --- 1. CNC BRAIN
|
| 8 |
def process_vector(image, invert_input, threshold_val, solidify_strength, smooth_factor, remove_noise):
|
| 9 |
if image is None: return None, None
|
| 10 |
|
| 11 |
-
# ---
|
| 12 |
-
# Agar
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
if invert_input:
|
| 14 |
image = ImageOps.invert(image.convert("RGB"))
|
| 15 |
|
|
@@ -17,10 +23,10 @@ def process_vector(image, invert_input, threshold_val, solidify_strength, smooth
|
|
| 17 |
img_np = np.array(image.convert("RGB"))
|
| 18 |
gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
|
| 19 |
|
| 20 |
-
# Blur
|
| 21 |
-
gray_blurred = cv2.GaussianBlur(gray, (
|
| 22 |
|
| 23 |
-
# Step B: Thresholding
|
| 24 |
_, binary = cv2.threshold(gray_blurred, threshold_val, 255, cv2.THRESH_BINARY_INV)
|
| 25 |
|
| 26 |
# Step C: SOLIDIFY (Double Line Fixer)
|
|
@@ -42,13 +48,14 @@ def process_vector(image, invert_input, threshold_val, solidify_strength, smooth
|
|
| 42 |
cv2.imwrite(temp_bmp, final_binary)
|
| 43 |
|
| 44 |
# Step F: Vectorize
|
| 45 |
-
output_svg = "
|
| 46 |
|
| 47 |
cmd = [
|
| 48 |
"potrace", temp_bmp,
|
| 49 |
"-s", "-o", output_svg,
|
| 50 |
"-t", "10",
|
| 51 |
-
"-a", str(smooth_factor),
|
|
|
|
| 52 |
"--opaque"
|
| 53 |
]
|
| 54 |
|
|
@@ -65,28 +72,28 @@ custom_css = """
|
|
| 65 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;900&display=swap');
|
| 66 |
body, .gradio-container { font-family: 'Inter', sans-serif !important; background: #000000 !important; color: white !important; }
|
| 67 |
#main_card { border: 1px solid #333; border-radius: 15px; padding: 20px; background: #111; }
|
| 68 |
-
.primary-btn { background: #
|
| 69 |
"""
|
| 70 |
|
| 71 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as app:
|
| 72 |
with gr.Column(elem_id="main_card"):
|
| 73 |
-
gr.Markdown("# π TS
|
|
|
|
| 74 |
|
| 75 |
with gr.Row():
|
| 76 |
with gr.Column():
|
| 77 |
inp_img = gr.Image(type="pil", label="Upload Photo", height=300)
|
| 78 |
|
| 79 |
-
gr.Markdown("### π¨ Image Type
|
| 80 |
-
# Yahi wo magic button hai jo White Design ko fix karega
|
| 81 |
inv_chk = gr.Checkbox(label="Is Design WHITE on BLACK Background?", value=False)
|
| 82 |
|
| 83 |
gr.Markdown("### ποΈ Settings")
|
| 84 |
thresh_sld = gr.Slider(0, 255, value=140, step=1, label="Darkness Threshold")
|
| 85 |
-
solid_sld = gr.Slider(0, 5, value=
|
| 86 |
noise_chk = gr.Checkbox(label="Clean Noise", value=True)
|
| 87 |
-
smooth_sld = gr.Slider(0, 1.
|
| 88 |
|
| 89 |
-
btn = gr.Button("π₯ CREATE VECTOR", variant="primary", elem_classes=["primary-btn"])
|
| 90 |
|
| 91 |
with gr.Column():
|
| 92 |
preview_img = gr.Image(label="Computer Vision Preview", interactive=False)
|
|
|
|
| 4 |
from PIL import Image, ImageOps
|
| 5 |
import subprocess
|
| 6 |
|
| 7 |
+
# --- 1. CNC BRAIN V5 (HD Upscale + Smooth Logic) ---
|
| 8 |
def process_vector(image, invert_input, threshold_val, solidify_strength, smooth_factor, remove_noise):
|
| 9 |
if image is None: return None, None
|
| 10 |
|
| 11 |
+
# --- STEP 0: HD UPSCALE (The Magic Fix) ---
|
| 12 |
+
# Agar photo choti hai ya sharp hai, to usse 2x bada kar do.
|
| 13 |
+
# Isse curves "Blocky" nahi honge, bilkul smooth aayenge.
|
| 14 |
+
w, h = image.size
|
| 15 |
+
# High Quality Resizing (Lanczos Filter)
|
| 16 |
+
image = image.resize((w * 2, h * 2), Image.LANCZOS)
|
| 17 |
+
|
| 18 |
+
# --- INVERT LOGIC ---
|
| 19 |
if invert_input:
|
| 20 |
image = ImageOps.invert(image.convert("RGB"))
|
| 21 |
|
|
|
|
| 23 |
img_np = np.array(image.convert("RGB"))
|
| 24 |
gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
|
| 25 |
|
| 26 |
+
# Blur thoda badhaya hai taaki pixelate na ho
|
| 27 |
+
gray_blurred = cv2.GaussianBlur(gray, (9, 9), 0)
|
| 28 |
|
| 29 |
+
# Step B: Thresholding
|
| 30 |
_, binary = cv2.threshold(gray_blurred, threshold_val, 255, cv2.THRESH_BINARY_INV)
|
| 31 |
|
| 32 |
# Step C: SOLIDIFY (Double Line Fixer)
|
|
|
|
| 48 |
cv2.imwrite(temp_bmp, final_binary)
|
| 49 |
|
| 50 |
# Step F: Vectorize
|
| 51 |
+
output_svg = "ts_vector_hd.svg"
|
| 52 |
|
| 53 |
cmd = [
|
| 54 |
"potrace", temp_bmp,
|
| 55 |
"-s", "-o", output_svg,
|
| 56 |
"-t", "10",
|
| 57 |
+
"-a", str(smooth_factor), # Curve smoothness controlled here
|
| 58 |
+
"--alphamax", "1.34", # Maximize curves
|
| 59 |
"--opaque"
|
| 60 |
]
|
| 61 |
|
|
|
|
| 72 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;900&display=swap');
|
| 73 |
body, .gradio-container { font-family: 'Inter', sans-serif !important; background: #000000 !important; color: white !important; }
|
| 74 |
#main_card { border: 1px solid #333; border-radius: 15px; padding: 20px; background: #111; }
|
| 75 |
+
.primary-btn { background: #00ff88 !important; color: black !important; font-weight: bold !important; }
|
| 76 |
"""
|
| 77 |
|
| 78 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as app:
|
| 79 |
with gr.Column(elem_id="main_card"):
|
| 80 |
+
gr.Markdown("# π TS VECTOR HD (V5)", elem_id="logo_text")
|
| 81 |
+
gr.Markdown("### Auto-Upscale for Smooth Curves")
|
| 82 |
|
| 83 |
with gr.Row():
|
| 84 |
with gr.Column():
|
| 85 |
inp_img = gr.Image(type="pil", label="Upload Photo", height=300)
|
| 86 |
|
| 87 |
+
gr.Markdown("### π¨ Image Type")
|
|
|
|
| 88 |
inv_chk = gr.Checkbox(label="Is Design WHITE on BLACK Background?", value=False)
|
| 89 |
|
| 90 |
gr.Markdown("### ποΈ Settings")
|
| 91 |
thresh_sld = gr.Slider(0, 255, value=140, step=1, label="Darkness Threshold")
|
| 92 |
+
solid_sld = gr.Slider(0, 5, value=0, step=1, label="Solidify (Keep 0 for Graphics)")
|
| 93 |
noise_chk = gr.Checkbox(label="Clean Noise", value=True)
|
| 94 |
+
smooth_sld = gr.Slider(0, 1.5, value=1.2, label="Smoothing (High for Round shapes)")
|
| 95 |
|
| 96 |
+
btn = gr.Button("π₯ CREATE HD VECTOR", variant="primary", elem_classes=["primary-btn"])
|
| 97 |
|
| 98 |
with gr.Column():
|
| 99 |
preview_img = gr.Image(label="Computer Vision Preview", interactive=False)
|