Spaces:
Running
Running
| 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() | |