File size: 4,205 Bytes
f1aee34
 
 
bdfa7db
f1aee34
 
f463e03
 
509d126
f1aee34
f463e03
6176d79
 
 
f463e03
bdfa7db
f463e03
bdfa7db
6176d79
33a10ec
 
 
509d126
 
4e6321c
f463e03
bdfa7db
 
f463e03
4e6321c
f463e03
 
 
bdfa7db
f463e03
 
ead23ff
bdfa7db
12c2b5f
509d126
12c2b5f
f1aee34
12c2b5f
f1aee34
f463e03
 
f1aee34
f463e03
f1aee34
 
ead23ff
f463e03
 
 
ead23ff
f1aee34
 
509d126
 
 
 
 
f1aee34
12c2b5f
f1aee34
bdfa7db
f1aee34
 
bdfa7db
f463e03
 
f1aee34
 
8ceb97e
f1aee34
f463e03
 
f1aee34
 
509d126
f463e03
509d126
f463e03
b43ed8e
 
f463e03
 
 
 
f1aee34
f463e03
 
 
f1aee34
f463e03
f1aee34
509d126
f463e03
12c2b5f
f1aee34
f463e03
f1aee34
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import gradio as gr
import cv2
import numpy as np
from PIL import Image, ImageOps, ImageEnhance
import subprocess

# --- CNC BRAIN V8: SMOOTH MASTER (Detail + Curves Balanced) ---
def process_vector(image, invert_input, contrast_boost, detail_threshold, solid_force, smooth_factor):
    if image is None: return None, None

    # 1. HD UPSCALE (Resolution zaroori hai smooth curves ke liye)
    w, h = image.size
    image = image.resize((w * 2, h * 2), Image.LANCZOS)

    # 2. PRE-PROCESSING
    enhancer = ImageEnhance.Contrast(image)
    image = enhancer.enhance(contrast_boost)

    # --- INVERT LOGIC ---
    if invert_input:
        image = ImageOps.invert(image.convert("RGB"))

    img_np = np.array(image.convert("RGB"))
    gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)

    # --- THRESHOLDING ---
    _, binary = cv2.threshold(gray, detail_threshold, 255, cv2.THRESH_BINARY_INV)

    # --- SOLID FORCE & CLEANING ---
    kernel = np.ones((3,3), np.uint8)
    
    # Noise cleaning (Thoda zaroori hai smooth vector ke liye)
    binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=1)

    if solid_force > 0:
        binary = cv2.dilate(binary, kernel, iterations=int(solidify_strength))

    # Final Invert for Potrace
    final_binary = cv2.bitwise_not(binary)

    # Save Temp
    temp_bmp = "temp_trace.bmp"
    cv2.imwrite(temp_bmp, final_binary)

    # --- VECTORIZE WITH SMOOTHING ENGINE ---
    output_svg = "ts_vector_smooth.svg"
    
    # Potrace Commands (Ye hai magic settings)
    cmd = [
        "potrace", temp_bmp,
        "-s", "-o", output_svg,
        "-t", "2",   # Despeckle: Chote daane hatayega taaki curve kharab na ho
        "-a", str(smooth_factor), # Alphamax: YE HAI GOLAI KA SLIDER (0=Sharp, 1.3=Very Round)
        "--longcurve", # CNC ke liye lambi smooth lines banane ki koshish karega
        "--opaque"
    ]
    
    try:
        subprocess.run(cmd, check=True)
    except Exception as e:
        print(f"Error: {e}")
        return None, None

    return output_svg, Image.fromarray(final_binary)

# --- Interface ---
custom_css = """
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;900&display=swap');
body, .gradio-container { font-family: 'Inter', sans-serif !important; background: #0a0a0a !important; color: white !important; }
#main_card { border: 2px solid #00b8ff; border-radius: 15px; padding: 25px; background: #111; }
.primary-btn { background: linear-gradient(45deg, #00b8ff, #00ff88) !important; color: black !important; font-weight: 900 !important; letter-spacing: 1px; }
"""

with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as app:
    with gr.Column(elem_id="main_card"):
        gr.Markdown("# ๐ŸŒ€ TS VECTOR V8 (Smooth)", elem_id="logo_text")
        gr.Markdown("### Perfect Curves for CNC Cutting")
        
        with gr.Row():
            with gr.Column():
                inp_img = gr.Image(type="pil", label="Upload Photo", height=300)
                
                gr.Markdown("### โš™๏ธ Controls")
                inv_chk = gr.Checkbox(label="Is Design WHITE on BLACK?", value=False)
                
                # Basic Controls
                cont_sld = gr.Slider(1.0, 2.5, value=1.5, step=0.1, label="1. Contrast (Detail Ubharo)")
                thresh_sld = gr.Slider(0, 255, value=170, step=1, label="2. Detail Cutoff (Preview dekho)")
                solid_sld = gr.Slider(0, 3, value=0, step=1, label="3. Solid Force (Mota karne ke liye)")
                
                # --- NEW SMOOTH SLIDER ---
                gr.Markdown("### ๐ŸŒ€ Curve Control (Golai)")
                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)")
                
                btn = gr.Button("โšก CREATE SMOOTH VECTOR", variant="primary", elem_classes=["primary-btn"])

            with gr.Column():
                preview_img = gr.Image(label="Computer Vision Preview (Black = Cut)", interactive=False)
                out_file = gr.File(label="Download SVG")

    btn.click(process_vector, inputs=[inp_img, inv_chk, cont_sld, thresh_sld, solid_sld, smooth_sld], outputs=[out_file, preview_img])

app.launch()