Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,45 +4,36 @@ import numpy as np
|
|
| 4 |
from PIL import Image, ImageOps, ImageEnhance
|
| 5 |
import subprocess
|
| 6 |
|
| 7 |
-
# --- CNC BRAIN:
|
| 8 |
-
def process_vector(image, invert_input, contrast_boost, detail_threshold, solid_force,
|
| 9 |
if image is None: return None, None
|
| 10 |
|
| 11 |
-
# 1. HD UPSCALE (Resolution
|
| 12 |
w, h = image.size
|
| 13 |
-
# 2x बड़ा कर रहे हैं ताकि बारीक पिक्सेल अलग हो सकें
|
| 14 |
image = image.resize((w * 2, h * 2), Image.LANCZOS)
|
| 15 |
|
| 16 |
-
# 2. PRE-PROCESSING
|
| 17 |
-
# OpenCV से पहले Pillow से कंट्रास्ट बढ़ाएंगे ताकि लाइनें उभर कर आएं
|
| 18 |
enhancer = ImageEnhance.Contrast(image)
|
| 19 |
-
image = enhancer.enhance(contrast_boost)
|
| 20 |
|
| 21 |
# --- INVERT LOGIC ---
|
| 22 |
if invert_input:
|
| 23 |
image = ImageOps.invert(image.convert("RGB"))
|
| 24 |
|
| 25 |
-
# Grayscale conversion
|
| 26 |
img_np = np.array(image.convert("RGB"))
|
| 27 |
gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
|
| 28 |
|
| 29 |
-
# ---
|
| 30 |
-
# कोई ब्लर नहीं, कोई ऑटोमैटिक दिमाग नहीं। सीधा थ्रेशोल्ड।
|
| 31 |
-
# यह स्लाइडर तय करेगा कि किस शेड को काला मानना है।
|
| 32 |
-
# बारीक डिज़ाइन के लिए यह सबसे सटीक तरीका है।
|
| 33 |
_, binary = cv2.threshold(gray, detail_threshold, 255, cv2.THRESH_BINARY_INV)
|
| 34 |
|
| 35 |
-
# --- SOLID FORCE
|
| 36 |
-
# सिर्फ तब इस्तेमाल करें जब लाइनें टूट रही हों।
|
| 37 |
-
# बारीक डिज़ाइन में इसे 0 रखना ही बेहतर है।
|
| 38 |
kernel = np.ones((3,3), np.uint8)
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
if remove_noise:
|
| 45 |
-
binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=1)
|
| 46 |
|
| 47 |
# Final Invert for Potrace
|
| 48 |
final_binary = cv2.bitwise_not(binary)
|
|
@@ -51,14 +42,16 @@ def process_vector(image, invert_input, contrast_boost, detail_threshold, solid_
|
|
| 51 |
temp_bmp = "temp_trace.bmp"
|
| 52 |
cv2.imwrite(temp_bmp, final_binary)
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
output_svg = "
|
| 56 |
|
|
|
|
| 57 |
cmd = [
|
| 58 |
"potrace", temp_bmp,
|
| 59 |
"-s", "-o", output_svg,
|
| 60 |
-
"-t", "
|
| 61 |
-
"-a",
|
|
|
|
| 62 |
"--opaque"
|
| 63 |
]
|
| 64 |
|
|
@@ -74,39 +67,37 @@ def process_vector(image, invert_input, contrast_boost, detail_threshold, solid_
|
|
| 74 |
custom_css = """
|
| 75 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;900&display=swap');
|
| 76 |
body, .gradio-container { font-family: 'Inter', sans-serif !important; background: #0a0a0a !important; color: white !important; }
|
| 77 |
-
#main_card { border: 2px solid #
|
| 78 |
-
.primary-btn { background: linear-gradient(45deg, #
|
| 79 |
"""
|
| 80 |
|
| 81 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as app:
|
| 82 |
with gr.Column(elem_id="main_card"):
|
| 83 |
-
gr.Markdown("#
|
| 84 |
-
gr.Markdown("###
|
| 85 |
|
| 86 |
with gr.Row():
|
| 87 |
with gr.Column():
|
| 88 |
-
inp_img = gr.Image(type="pil", label="Upload
|
| 89 |
|
| 90 |
-
gr.Markdown("### ⚙️
|
| 91 |
inv_chk = gr.Checkbox(label="Is Design WHITE on BLACK?", value=False)
|
| 92 |
|
| 93 |
-
#
|
| 94 |
-
cont_sld = gr.Slider(1.0,
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
thresh_sld = gr.Slider(0, 255, value=180, step=1, label="2. Detail Cutoff (इसे घुमाकर प्रीव्यू में चेक करें)")
|
| 98 |
-
|
| 99 |
-
# 3. Solid Force
|
| 100 |
-
solid_sld = gr.Slider(0, 3, value=0, step=1, label="3. Solid Force (बारीक डिज़ाइन के लिए 0 रखें)")
|
| 101 |
|
| 102 |
-
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
btn = gr.Button("⚡
|
| 105 |
|
| 106 |
with gr.Column():
|
| 107 |
-
preview_img = gr.Image(label="Computer Vision Preview (
|
| 108 |
out_file = gr.File(label="Download SVG")
|
| 109 |
|
| 110 |
-
btn.click(process_vector, inputs=[inp_img, inv_chk, cont_sld, thresh_sld, solid_sld,
|
| 111 |
|
| 112 |
app.launch()
|
|
|
|
| 4 |
from PIL import Image, ImageOps, ImageEnhance
|
| 5 |
import subprocess
|
| 6 |
|
| 7 |
+
# --- CNC BRAIN V8: SMOOTH MASTER (Detail + Curves Balanced) ---
|
| 8 |
+
def process_vector(image, invert_input, contrast_boost, detail_threshold, solid_force, smooth_factor):
|
| 9 |
if image is None: return None, None
|
| 10 |
|
| 11 |
+
# 1. HD UPSCALE (Resolution zaroori hai smooth curves ke liye)
|
| 12 |
w, h = image.size
|
|
|
|
| 13 |
image = image.resize((w * 2, h * 2), Image.LANCZOS)
|
| 14 |
|
| 15 |
+
# 2. PRE-PROCESSING
|
|
|
|
| 16 |
enhancer = ImageEnhance.Contrast(image)
|
| 17 |
+
image = enhancer.enhance(contrast_boost)
|
| 18 |
|
| 19 |
# --- INVERT LOGIC ---
|
| 20 |
if invert_input:
|
| 21 |
image = ImageOps.invert(image.convert("RGB"))
|
| 22 |
|
|
|
|
| 23 |
img_np = np.array(image.convert("RGB"))
|
| 24 |
gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
|
| 25 |
|
| 26 |
+
# --- THRESHOLDING ---
|
|
|
|
|
|
|
|
|
|
| 27 |
_, binary = cv2.threshold(gray, detail_threshold, 255, cv2.THRESH_BINARY_INV)
|
| 28 |
|
| 29 |
+
# --- SOLID FORCE & CLEANING ---
|
|
|
|
|
|
|
| 30 |
kernel = np.ones((3,3), np.uint8)
|
| 31 |
+
|
| 32 |
+
# Noise cleaning (Thoda zaroori hai smooth vector ke liye)
|
| 33 |
+
binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=1)
|
| 34 |
|
| 35 |
+
if solid_force > 0:
|
| 36 |
+
binary = cv2.dilate(binary, kernel, iterations=int(solidify_strength))
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# Final Invert for Potrace
|
| 39 |
final_binary = cv2.bitwise_not(binary)
|
|
|
|
| 42 |
temp_bmp = "temp_trace.bmp"
|
| 43 |
cv2.imwrite(temp_bmp, final_binary)
|
| 44 |
|
| 45 |
+
# --- VECTORIZE WITH SMOOTHING ENGINE ---
|
| 46 |
+
output_svg = "ts_vector_smooth.svg"
|
| 47 |
|
| 48 |
+
# Potrace Commands (Ye hai magic settings)
|
| 49 |
cmd = [
|
| 50 |
"potrace", temp_bmp,
|
| 51 |
"-s", "-o", output_svg,
|
| 52 |
+
"-t", "2", # Despeckle: Chote daane hatayega taaki curve kharab na ho
|
| 53 |
+
"-a", str(smooth_factor), # Alphamax: YE HAI GOLAI KA SLIDER (0=Sharp, 1.3=Very Round)
|
| 54 |
+
"--longcurve", # CNC ke liye lambi smooth lines banane ki koshish karega
|
| 55 |
"--opaque"
|
| 56 |
]
|
| 57 |
|
|
|
|
| 67 |
custom_css = """
|
| 68 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;900&display=swap');
|
| 69 |
body, .gradio-container { font-family: 'Inter', sans-serif !important; background: #0a0a0a !important; color: white !important; }
|
| 70 |
+
#main_card { border: 2px solid #00b8ff; border-radius: 15px; padding: 25px; background: #111; }
|
| 71 |
+
.primary-btn { background: linear-gradient(45deg, #00b8ff, #00ff88) !important; color: black !important; font-weight: 900 !important; letter-spacing: 1px; }
|
| 72 |
"""
|
| 73 |
|
| 74 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as app:
|
| 75 |
with gr.Column(elem_id="main_card"):
|
| 76 |
+
gr.Markdown("# 🌀 TS VECTOR V8 (Smooth)", elem_id="logo_text")
|
| 77 |
+
gr.Markdown("### Perfect Curves for CNC Cutting")
|
| 78 |
|
| 79 |
with gr.Row():
|
| 80 |
with gr.Column():
|
| 81 |
+
inp_img = gr.Image(type="pil", label="Upload Photo", height=300)
|
| 82 |
|
| 83 |
+
gr.Markdown("### ⚙️ Controls")
|
| 84 |
inv_chk = gr.Checkbox(label="Is Design WHITE on BLACK?", value=False)
|
| 85 |
|
| 86 |
+
# Basic Controls
|
| 87 |
+
cont_sld = gr.Slider(1.0, 2.5, value=1.5, step=0.1, label="1. Contrast (Detail Ubharo)")
|
| 88 |
+
thresh_sld = gr.Slider(0, 255, value=170, step=1, label="2. Detail Cutoff (Preview dekho)")
|
| 89 |
+
solid_sld = gr.Slider(0, 3, value=0, step=1, label="3. Solid Force (Mota karne ke liye)")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
# --- NEW SMOOTH SLIDER ---
|
| 92 |
+
gr.Markdown("### 🌀 Curve Control (Golai)")
|
| 93 |
+
smooth_sld = gr.Slider(0.0, 1.34, value=1.0, step=0.1, label="4. Smoothing Factor (0=Kata hua, 1.0=Mast Golai)")
|
| 94 |
|
| 95 |
+
btn = gr.Button("⚡ CREATE SMOOTH VECTOR", variant="primary", elem_classes=["primary-btn"])
|
| 96 |
|
| 97 |
with gr.Column():
|
| 98 |
+
preview_img = gr.Image(label="Computer Vision Preview (Black = Cut)", interactive=False)
|
| 99 |
out_file = gr.File(label="Download SVG")
|
| 100 |
|
| 101 |
+
btn.click(process_vector, inputs=[inp_img, inv_chk, cont_sld, thresh_sld, solid_sld, smooth_sld], outputs=[out_file, preview_img])
|
| 102 |
|
| 103 |
app.launch()
|