Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,59 +4,45 @@ import numpy as np
|
|
| 4 |
from PIL import Image, ImageOps
|
| 5 |
import subprocess
|
| 6 |
|
| 7 |
-
# --- 1. CNC BRAIN
|
| 8 |
-
def process_vector(image,
|
| 9 |
if image is None: return None, None
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
img_np = np.array(image.convert("RGB"))
|
| 13 |
gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
|
| 14 |
-
|
| 15 |
-
# --- INTELLIGENT INVERSION LOGIC ---
|
| 16 |
-
# Agar user ne kaha "Light Design" (White on Black), toh hum pehle hi photo ko ulta (Invert) kar denge.
|
| 17 |
-
# Isse White Design -> Black ban jayega aur Black BG -> White ban jayega.
|
| 18 |
-
# Phir humara purana V5 engine uspe perfect kaam karega!
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
# B
|
| 24 |
-
|
| 25 |
-
enhanced_gray = clahe.apply(gray)
|
| 26 |
-
|
| 27 |
-
# C. Adaptive Scanning (V5 Logic - Jo Golden Jali par perfect tha)
|
| 28 |
-
block_size = int(scan_sensitivity)
|
| 29 |
-
if block_size % 2 == 0: block_size += 1
|
| 30 |
-
|
| 31 |
-
# Adaptive Threshold
|
| 32 |
-
binary = cv2.adaptiveThreshold(enhanced_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
| 33 |
-
cv2.THRESH_BINARY_INV, block_size, 5)
|
| 34 |
|
| 35 |
-
#
|
| 36 |
kernel = np.ones((3,3), np.uint8)
|
| 37 |
-
|
| 38 |
-
# Clean noisy dots
|
| 39 |
-
binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=1)
|
| 40 |
-
|
| 41 |
-
# E. SOLIDIFY (Double Line Fixer)
|
| 42 |
if solidify_strength > 0:
|
| 43 |
binary = cv2.dilate(binary, kernel, iterations=int(solidify_strength))
|
| 44 |
binary = cv2.erode(binary, kernel, iterations=1)
|
| 45 |
|
| 46 |
-
#
|
| 47 |
if remove_noise:
|
| 48 |
binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=1)
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
# Potrace ko hamesha "Black Design on White BG" chahiye hota hai.
|
| 52 |
final_binary = cv2.bitwise_not(binary)
|
| 53 |
|
| 54 |
# Save Temp
|
| 55 |
temp_bmp = "temp_trace.bmp"
|
| 56 |
cv2.imwrite(temp_bmp, final_binary)
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
output_svg = "
|
| 60 |
|
| 61 |
cmd = [
|
| 62 |
"potrace", temp_bmp,
|
|
@@ -79,38 +65,33 @@ custom_css = """
|
|
| 79 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;900&display=swap');
|
| 80 |
body, .gradio-container { font-family: 'Inter', sans-serif !important; background: #000000 !important; color: white !important; }
|
| 81 |
#main_card { border: 1px solid #333; border-radius: 15px; padding: 20px; background: #111; }
|
| 82 |
-
.primary-btn { background:
|
| 83 |
"""
|
| 84 |
|
| 85 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as app:
|
| 86 |
with gr.Column(elem_id="main_card"):
|
| 87 |
-
gr.Markdown("#
|
| 88 |
-
gr.Markdown("### Handles Both: Golden Jali & White Vectors")
|
| 89 |
|
| 90 |
with gr.Row():
|
| 91 |
with gr.Column():
|
| 92 |
inp_img = gr.Image(type="pil", label="Upload Photo", height=300)
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
["Dark Design (Golden/Sketch)", "Light Design (White on Black)"],
|
| 98 |
-
value="Dark Design (Golden/Sketch)",
|
| 99 |
-
label="Tumhari photo kaisi hai?"
|
| 100 |
-
)
|
| 101 |
|
| 102 |
-
gr.Markdown("### ποΈ
|
| 103 |
-
|
| 104 |
-
solid_sld = gr.Slider(0, 5, value=2, step=1, label="Solidify (
|
| 105 |
noise_chk = gr.Checkbox(label="Clean Noise", value=True)
|
| 106 |
smooth_sld = gr.Slider(0, 1.3, value=1.0, label="Smoothing")
|
| 107 |
|
| 108 |
-
btn = gr.Button("
|
| 109 |
|
| 110 |
with gr.Column():
|
| 111 |
preview_img = gr.Image(label="Computer Vision Preview", interactive=False)
|
| 112 |
out_file = gr.File(label="Download SVG")
|
| 113 |
|
| 114 |
-
btn.click(process_vector, inputs=[inp_img,
|
| 115 |
|
| 116 |
app.launch()
|
|
|
|
| 4 |
from PIL import Image, ImageOps
|
| 5 |
import subprocess
|
| 6 |
|
| 7 |
+
# --- 1. CNC BRAIN V4.5 (Solid Shape + Invert 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 |
+
# --- NEW FIX: INVERT LOGIC ---
|
| 12 |
+
# Agar design White hai aur Background Black, toh hum pehle hi usse palat denge
|
| 13 |
+
if invert_input:
|
| 14 |
+
image = ImageOps.invert(image.convert("RGB"))
|
| 15 |
+
|
| 16 |
+
# Step A: Convert to Grayscale & Blur
|
| 17 |
img_np = np.array(image.convert("RGB"))
|
| 18 |
gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Blur to mix highlights
|
| 21 |
+
gray_blurred = cv2.GaussianBlur(gray, (5, 5), 0)
|
| 22 |
|
| 23 |
+
# Step B: Thresholding (Solid Black logic)
|
| 24 |
+
_, binary = cv2.threshold(gray_blurred, threshold_val, 255, cv2.THRESH_BINARY_INV)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Step C: SOLIDIFY (Double Line Fixer)
|
| 27 |
kernel = np.ones((3,3), np.uint8)
|
| 28 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
if solidify_strength > 0:
|
| 30 |
binary = cv2.dilate(binary, kernel, iterations=int(solidify_strength))
|
| 31 |
binary = cv2.erode(binary, kernel, iterations=1)
|
| 32 |
|
| 33 |
+
# Step D: Noise Removal
|
| 34 |
if remove_noise:
|
| 35 |
binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=1)
|
| 36 |
|
| 37 |
+
# Step E: Invert for Potrace
|
|
|
|
| 38 |
final_binary = cv2.bitwise_not(binary)
|
| 39 |
|
| 40 |
# Save Temp
|
| 41 |
temp_bmp = "temp_trace.bmp"
|
| 42 |
cv2.imwrite(temp_bmp, final_binary)
|
| 43 |
|
| 44 |
+
# Step F: Vectorize
|
| 45 |
+
output_svg = "ts_vector_solid.svg"
|
| 46 |
|
| 47 |
cmd = [
|
| 48 |
"potrace", temp_bmp,
|
|
|
|
| 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: #FFD700 !important; color: black !important; font-weight: bold !important; }
|
| 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 SOLID TRACER V4.5", elem_id="logo_text")
|
|
|
|
| 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 (Ye naya hai)")
|
| 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=2, step=1, label="Solidify (Double Line Fixer)")
|
| 86 |
noise_chk = gr.Checkbox(label="Clean Noise", value=True)
|
| 87 |
smooth_sld = gr.Slider(0, 1.3, value=1.0, label="Smoothing")
|
| 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)
|
| 93 |
out_file = gr.File(label="Download SVG")
|
| 94 |
|
| 95 |
+
btn.click(process_vector, inputs=[inp_img, inv_chk, thresh_sld, solid_sld, smooth_sld, noise_chk], outputs=[out_file, preview_img])
|
| 96 |
|
| 97 |
app.launch()
|