Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,13 @@ import numpy as np
|
|
| 4 |
from PIL import Image, ImageOps
|
| 5 |
import subprocess
|
| 6 |
|
| 7 |
-
# --- 1. CNC BRAIN
|
| 8 |
-
def process_vector(image, invert_input,
|
| 9 |
if image is None: return None, None
|
| 10 |
|
| 11 |
-
# --- STEP 1: HD UPSCALE (Quality ke liye) ---
|
| 12 |
w, h = image.size
|
|
|
|
| 13 |
image = image.resize((w * 2, h * 2), Image.LANCZOS)
|
| 14 |
|
| 15 |
# --- INVERT LOGIC ---
|
|
@@ -20,42 +21,47 @@ def process_vector(image, invert_input, threshold_val, solidify_strength, smooth
|
|
| 20 |
img_np = np.array(image.convert("RGB"))
|
| 21 |
gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
|
| 22 |
|
| 23 |
-
# --- STEP 2:
|
| 24 |
-
# Ye
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
-
#
|
| 30 |
-
#
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
#
|
|
|
|
| 34 |
kernel = np.ones((3,3), np.uint8)
|
| 35 |
|
| 36 |
-
# Morph Close: Ye chote chote gaps ko forcefully band karta hai
|
| 37 |
-
# Taki wo 'Line Art' na rahe, 'Solid Block' ban jaye.
|
| 38 |
-
binary = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel, iterations=2)
|
| 39 |
-
|
| 40 |
-
# Extra Thickness Control
|
| 41 |
if solidify_strength > 0:
|
|
|
|
| 42 |
binary = cv2.dilate(binary, kernel, iterations=int(solidify_strength))
|
|
|
|
|
|
|
| 43 |
elif solidify_strength < 0:
|
|
|
|
| 44 |
binary = cv2.erode(binary, kernel, iterations=abs(int(solidify_strength)))
|
| 45 |
|
| 46 |
-
# Step
|
| 47 |
if remove_noise:
|
| 48 |
binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=1)
|
| 49 |
|
| 50 |
-
# Step
|
| 51 |
final_binary = cv2.bitwise_not(binary)
|
| 52 |
|
| 53 |
# Save Temp
|
| 54 |
temp_bmp = "temp_trace.bmp"
|
| 55 |
cv2.imwrite(temp_bmp, final_binary)
|
| 56 |
|
| 57 |
-
# Step
|
| 58 |
-
output_svg = "
|
| 59 |
|
| 60 |
cmd = [
|
| 61 |
"potrace", temp_bmp,
|
|
@@ -78,13 +84,13 @@ custom_css = """
|
|
| 78 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;900&display=swap');
|
| 79 |
body, .gradio-container { font-family: 'Inter', sans-serif !important; background: #000000 !important; color: white !important; }
|
| 80 |
#main_card { border: 1px solid #333; border-radius: 15px; padding: 20px; background: #111; }
|
| 81 |
-
.primary-btn { background: #
|
| 82 |
"""
|
| 83 |
|
| 84 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as app:
|
| 85 |
with gr.Column(elem_id="main_card"):
|
| 86 |
-
gr.Markdown("# π TS
|
| 87 |
-
gr.Markdown("###
|
| 88 |
|
| 89 |
with gr.Row():
|
| 90 |
with gr.Column():
|
|
@@ -93,13 +99,13 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as app:
|
|
| 93 |
gr.Markdown("### π¨ Image Type")
|
| 94 |
inv_chk = gr.Checkbox(label="Is Design WHITE on BLACK?", value=False)
|
| 95 |
|
| 96 |
-
gr.Markdown("### ποΈ
|
| 97 |
|
| 98 |
-
#
|
| 99 |
-
|
| 100 |
|
| 101 |
-
# Solidify
|
| 102 |
-
solid_sld = gr.Slider(-2, 5, value=1, step=1, label="2. Solidify (
|
| 103 |
|
| 104 |
noise_chk = gr.Checkbox(label="3. Clean Noise", value=True)
|
| 105 |
smooth_sld = gr.Slider(0, 1.3, value=1.0, label="4. Smoothing")
|
|
@@ -110,6 +116,6 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as app:
|
|
| 110 |
preview_img = gr.Image(label="Computer Vision Preview (Black = Solid Material)", interactive=False)
|
| 111 |
out_file = gr.File(label="Download SVG")
|
| 112 |
|
| 113 |
-
btn.click(process_vector, inputs=[inp_img, inv_chk,
|
| 114 |
|
| 115 |
app.launch()
|
|
|
|
| 4 |
from PIL import Image, ImageOps
|
| 5 |
import subprocess
|
| 6 |
|
| 7 |
+
# --- 1. CNC BRAIN V5.5 (Solid + Detail Preserved) ---
|
| 8 |
+
def process_vector(image, invert_input, detail_level, solidify_strength, smooth_factor, remove_noise):
|
| 9 |
if image is None: return None, None
|
| 10 |
|
| 11 |
+
# --- STEP 1: HD UPSCALE (V5 ka magic - Quality ke liye) ---
|
| 12 |
w, h = image.size
|
| 13 |
+
# Photo ko 2 guna bada karo taaki curves smooth aayein
|
| 14 |
image = image.resize((w * 2, h * 2), Image.LANCZOS)
|
| 15 |
|
| 16 |
# --- INVERT LOGIC ---
|
|
|
|
| 21 |
img_np = np.array(image.convert("RGB"))
|
| 22 |
gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
|
| 23 |
|
| 24 |
+
# --- STEP 2: SMART CONTRAST (Blur ki jagah ye use karenge) ---
|
| 25 |
+
# Ye barik details ko ubhaar dega bina dhundla kiye
|
| 26 |
+
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
|
| 27 |
+
gray = clahe.apply(gray)
|
| 28 |
+
|
| 29 |
+
# --- STEP 3: ADAPTIVE DETAIL THRESHOLD ---
|
| 30 |
+
# Ye slider decide karega ki kitni barik cheez pakadni hai
|
| 31 |
+
# Slider value (11-99) ko hum Block Size banayenge
|
| 32 |
+
block_size = int(detail_level)
|
| 33 |
+
if block_size % 2 == 0: block_size += 1 # Odd number zaroori hai
|
| 34 |
+
|
| 35 |
+
# Adaptive Threshold: Ye local area dekhta hai (Barik details ke liye best)
|
| 36 |
+
binary = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
| 37 |
+
cv2.THRESH_BINARY_INV, block_size, 5)
|
| 38 |
|
| 39 |
+
# --- STEP 4: SOLIDIFY (Tera favorite feature) ---
|
| 40 |
+
# Ye lines ko jodne aur solid banane ke liye hai
|
| 41 |
kernel = np.ones((3,3), np.uint8)
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
if solidify_strength > 0:
|
| 44 |
+
# Mota karo (Dilate)
|
| 45 |
binary = cv2.dilate(binary, kernel, iterations=int(solidify_strength))
|
| 46 |
+
# Halka sa Erode taaki shape wapas sharp ho jaye
|
| 47 |
+
binary = cv2.erode(binary, kernel, iterations=1)
|
| 48 |
elif solidify_strength < 0:
|
| 49 |
+
# Agar bahut chipak raha hai to alag karo
|
| 50 |
binary = cv2.erode(binary, kernel, iterations=abs(int(solidify_strength)))
|
| 51 |
|
| 52 |
+
# Step 5: Noise Removal
|
| 53 |
if remove_noise:
|
| 54 |
binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=1)
|
| 55 |
|
| 56 |
+
# Step 6: Invert for Potrace
|
| 57 |
final_binary = cv2.bitwise_not(binary)
|
| 58 |
|
| 59 |
# Save Temp
|
| 60 |
temp_bmp = "temp_trace.bmp"
|
| 61 |
cv2.imwrite(temp_bmp, final_binary)
|
| 62 |
|
| 63 |
+
# Step 7: Vectorize
|
| 64 |
+
output_svg = "ts_vector_solid_detail.svg"
|
| 65 |
|
| 66 |
cmd = [
|
| 67 |
"potrace", temp_bmp,
|
|
|
|
| 84 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;900&display=swap');
|
| 85 |
body, .gradio-container { font-family: 'Inter', sans-serif !important; background: #000000 !important; color: white !important; }
|
| 86 |
#main_card { border: 1px solid #333; border-radius: 15px; padding: 20px; background: #111; }
|
| 87 |
+
.primary-btn { background: #00ff88 !important; color: black !important; font-weight: bold !important; }
|
| 88 |
"""
|
| 89 |
|
| 90 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as app:
|
| 91 |
with gr.Column(elem_id="main_card"):
|
| 92 |
+
gr.Markdown("# π TS VECTOR V5.5", elem_id="logo_text")
|
| 93 |
+
gr.Markdown("### Solid Shapes + Fine Detail Control")
|
| 94 |
|
| 95 |
with gr.Row():
|
| 96 |
with gr.Column():
|
|
|
|
| 99 |
gr.Markdown("### π¨ Image Type")
|
| 100 |
inv_chk = gr.Checkbox(label="Is Design WHITE on BLACK?", value=False)
|
| 101 |
|
| 102 |
+
gr.Markdown("### ποΈ Settings (Adjust for Details)")
|
| 103 |
|
| 104 |
+
# Ye hai naya slider - Isse barik detail aayegi
|
| 105 |
+
detail_sld = gr.Slider(11, 151, value=45, step=2, label="1. Detail Sensitivity (Kam = Barik Detail, Zyada = Mota Design)")
|
| 106 |
|
| 107 |
+
# Tera favorite Solidify slider wapas aa gaya
|
| 108 |
+
solid_sld = gr.Slider(-2, 5, value=1, step=1, label="2. Solidify (Gap Bharne ke liye)")
|
| 109 |
|
| 110 |
noise_chk = gr.Checkbox(label="3. Clean Noise", value=True)
|
| 111 |
smooth_sld = gr.Slider(0, 1.3, value=1.0, label="4. Smoothing")
|
|
|
|
| 116 |
preview_img = gr.Image(label="Computer Vision Preview (Black = Solid Material)", interactive=False)
|
| 117 |
out_file = gr.File(label="Download SVG")
|
| 118 |
|
| 119 |
+
btn.click(process_vector, inputs=[inp_img, inv_chk, detail_sld, solid_sld, smooth_sld, noise_chk], outputs=[out_file, preview_img])
|
| 120 |
|
| 121 |
app.launch()
|