Spaces:
Sleeping
Sleeping
Update app.py
Browse files3 unmodified lines456789101112131415161718192021222324252627282930313233343536373839404142434 unmodified lines484950515253545556575859606115 unmodified lines77787980818283848586878889909192939495969798991001011021031043 unmodified linesfrom PIL import Image, ImageOpsimport subprocess# --- 1. CNC BRAIN V5 (HD Upscale + Smooth Logic) ---def process_vector(image, invert_input, threshold_val, solidify_strength, smooth_factor, remove_noise):if image is None: return None, None# --- STEP 0: HD UPSCALE (The Magic Fix) ---# Agar photo choti hai ya sharp hai, to usse 2x bada kar do.# Isse curves "Blocky" nahi honge, bilkul smooth aayenge.w, h = image.size# High Quality Resizing (Lanczos Filter)image = image.resize((w * 2, h * 2), Image.LANCZOS)# --- INVERT LOGIC ---if invert_input:image = ImageOps.invert(image.convert("RGB"))# Step A: Convert to Grayscale & Blurimg_np = np.array(image.convert("RGB"))gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)# Blur thoda badhaya hai taaki pixelate na hogray_blurred = cv2.GaussianBlur(gray, (9, 9), 0)# Step B: Thresholding_, binary = cv2.threshold(gray_blurred, threshold_val, 255, cv2.THRESH_BINARY_INV)# Step C: SOLIDIFY (Double Line Fixer)kernel = np.ones((3,3), np.uint8)if solidify_strength > 0:binary = cv2.dilate(binary, kernel, iterations=int(solidify_strength))binary = cv2.erode(binary, kernel, iterations=1)# Step D: Noise Removalif remove_noise:binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=1)# Step E: Invert for Potrace4 unmodified linescv2.imwrite(temp_bmp, final_binary)# Step F: Vectorizeoutput_svg = "ts_vector_hd.svg"cmd = ["potrace", temp_bmp,"-s", "-o", output_svg,"-t", "10","-a", str(smooth_factor), # Curve smoothness controlled here"--alphamax", "1.34", # Maximize curves"--opaque"]15 unmodified lineswith gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as app:with gr.Column(elem_id="main_card"):gr.Markdown("# π TS VECTOR HD (V5)", elem_id="logo_text")gr.Markdown("### Auto-Upscale for Smooth Curves")with gr.Row():with gr.Column():inp_img = gr.Image(type="pil", label="Upload Photo", height=300)gr.Markdown("### π¨ Image Type")inv_chk = gr.Checkbox(label="Is Design WHITE on BLACK Background?", value=False)gr.Markdown("### ποΈ Settings")thresh_sld = gr.Slider(0, 255, value=140, step=1, label="Darkness Threshold")solid_sld = gr.Slider(0, 5, value=0, step=1, label="Solidify (Keep 0 for Graphics)")noise_chk = gr.Checkbox(label="Clean Noise", value=True)smooth_sld = gr.Slider(0, 1.5, value=1.2, label="Smoothing (High for Round shapes)")btn = gr.Button("π₯ CREATE HD VECTOR", variant="primary", elem_classes=["primary-btn"])with gr.Column():preview_img = gr.Image(label="Computer Vision Preview", interactive=False)out_file = gr.File(label="Download SVG")btn.click(process_vector, inputs=[inp_img, inv_chk, thresh_sld, solid_sld, smooth_sld, noise_chk], outputs=[out_file, preview_img])app.launch()3 unmodified lines4567891011121314151617181920212223242526272829303132333435363738394041424344454647484950514 unmodified lines5657585960616263646566676815 unmodified lines848586878889909192939495969798991001011021031041051061071081091101111121131141153 unmodified linesfrom PIL import Image, ImageOpsimport subprocess# --- 1. CNC BRAIN V6 (Micro-Detail Logic) ---def process_vector(image, invert_input, detail_sens, thickness, remove_noise):if image is None: return None, None# --- STEP 0: HD UPSCALE ---# Detail nikalne ke liye photo bada hona zaroori haiw, h = image.sizeimage = image.resize((w * 2, h * 2), Image.LANCZOS)# --- INVERT LOGIC ---if invert_input:image = ImageOps.invert(image.convert("RGB"))# Step A: Grayscale & Contrast Boost (No Blur!)img_np = np.array(image.convert("RGB"))gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)# CLAHE (Smart Contrast): Ye Gold color ko background se alag karegaclahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))gray = clahe.apply(gray)# Step B: Adaptive Thresholding (Detail Hunter)# Block size jitna chota (odd number), utni barik detail pakdegablock_size = int(detail_sens)if block_size % 2 == 0: block_size += 1 # Must be oddbinary = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV, block_size, 5)# Step C: THICKNESS CONTROL (Negative allows opening gaps)kernel = np.ones((3,3), np.uint8)if thickness > 0:# Positive: Mota karo (Dilate)binary = cv2.dilate(binary, kernel, iterations=int(thickness))elif thickness < 0:# Negative: Patla karo (Erode) - Ye 'chipki hui' lines ko kholegabinary = cv2.erode(binary, kernel, iterations=abs(int(thickness)))# Step D: Noise Removal (Optional - for very noisy photos)if remove_noise:# Bilkul halka sa clean, zyada nahibinary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=1)# Step E: Invert for Potrace4 unmodified linescv2.imwrite(temp_bmp, final_binary)# Step F: Vectorizeoutput_svg = "ts_vector_detail.svg"cmd = ["potrace", temp_bmp,"-s", "-o", output_svg,"-t", "2", # Turdsize kam kiya taaki chote dots na udein"-a", "1.0","--opaque"]15 unmodified lineswith gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as app:with gr.Column(elem_id="main_card"):gr.Markdown("# π¬ TS VECTOR PRO (V6)", elem_id="logo_text")gr.Markdown("### Optimized for High-Detail & Mandala Designs")with gr.Row():with gr.Column():inp_img = gr.Image(type="pil", label="Upload Photo", height=300)gr.Markdown("### π¨ Image Type")inv_chk = gr.Checkbox(label="Is Design WHITE on BLACK?", value=False)gr.Markdown("### ποΈ Detail Settings")# Naya Slider: Block Sizesens_sld = gr.Slider(11, 99, value=45, step=2, label="1. Detail Sensitivity (Kam = Barik, Zyada = Mota Area)")# Naya Slider: Negative value allowedthick_sld = gr.Slider(-3, 3, value=0, step=1, label="2. Line Thickness (Minus me le jao gap kholne ke liye)")noise_chk = gr.Checkbox(label="3. Clean Noise (Off rakho agar detail udd rahi hai)", value=False)btn = gr.Button("π₯ CAPTURE DETAILS", variant="primary", elem_classes=["primary-btn"])with gr.Column():preview_img = gr.Image(label="Computer Vision Preview", interactive=False)out_file = gr.File(label="Download SVG")btn.click(process_vector, inputs=[inp_img, inv_chk, sens_sld, thick_sld, noise_chk], outputs=[out_file, preview_img])app.launch()
app.py+43-32