Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,64 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
from PIL import Image, ImageOps
|
| 5 |
import subprocess
|
| 6 |
|
| 7 |
-
# ---
|
| 8 |
-
def process_vector(image, invert_input,
|
| 9 |
if image is None: return None, None
|
| 10 |
|
| 11 |
-
#
|
| 12 |
w, h = image.size
|
| 13 |
-
#
|
| 14 |
image = image.resize((w * 2, h * 2), Image.LANCZOS)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# --- INVERT LOGIC ---
|
| 17 |
if invert_input:
|
| 18 |
image = ImageOps.invert(image.convert("RGB"))
|
| 19 |
|
| 20 |
-
#
|
| 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 |
-
# ---
|
| 40 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
kernel = np.ones((3,3), np.uint8)
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 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 |
-
#
|
| 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 |
-
#
|
| 64 |
-
output_svg = "
|
| 65 |
|
| 66 |
cmd = [
|
| 67 |
"potrace", temp_bmp,
|
| 68 |
"-s", "-o", output_svg,
|
| 69 |
-
"-t", "
|
| 70 |
-
"-a",
|
| 71 |
"--opaque"
|
| 72 |
]
|
| 73 |
|
|
@@ -79,43 +70,43 @@ def process_vector(image, invert_input, detail_level, solidify_strength, smooth_
|
|
| 79 |
|
| 80 |
return output_svg, Image.fromarray(final_binary)
|
| 81 |
|
| 82 |
-
# ---
|
| 83 |
custom_css = """
|
| 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: #
|
| 86 |
-
#main_card { border:
|
| 87 |
-
.primary-btn { background: #
|
| 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("#
|
| 93 |
-
gr.Markdown("###
|
| 94 |
|
| 95 |
with gr.Row():
|
| 96 |
with gr.Column():
|
| 97 |
-
inp_img = gr.Image(type="pil", label="Upload Photo", height=300)
|
| 98 |
|
| 99 |
-
gr.Markdown("###
|
| 100 |
inv_chk = gr.Checkbox(label="Is Design WHITE on BLACK?", value=False)
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
| 106 |
|
| 107 |
-
#
|
| 108 |
-
solid_sld = gr.Slider(
|
| 109 |
|
| 110 |
-
noise_chk = gr.Checkbox(label="
|
| 111 |
-
smooth_sld = gr.Slider(0, 1.3, value=1.0, label="4. Smoothing")
|
| 112 |
|
| 113 |
-
btn = gr.Button("
|
| 114 |
|
| 115 |
with gr.Column():
|
| 116 |
-
preview_img = gr.Image(label="Computer Vision Preview (
|
| 117 |
out_file = gr.File(label="Download SVG")
|
| 118 |
|
| 119 |
-
btn.click(process_vector, inputs=[inp_img, inv_chk,
|
| 120 |
|
| 121 |
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
+
from PIL import Image, ImageOps, ImageEnhance
|
| 5 |
import subprocess
|
| 6 |
|
| 7 |
+
# --- CNC BRAIN: THE ULTIMATE (Zero Blur, Pure Detail) ---
|
| 8 |
+
def process_vector(image, invert_input, contrast_boost, detail_threshold, solid_force, remove_noise):
|
| 9 |
if image is None: return None, None
|
| 10 |
|
| 11 |
+
# 1. HD UPSCALE (Resolution is king for details)
|
| 12 |
w, h = image.size
|
| 13 |
+
# 2x बड़ा कर रहे हैं ताकि बारीक पिक्सेल अलग हो सकें
|
| 14 |
image = image.resize((w * 2, h * 2), Image.LANCZOS)
|
| 15 |
|
| 16 |
+
# 2. PRE-PROCESSING (Contrast Boost)
|
| 17 |
+
# OpenCV से पहले Pillow से कंट्रास्ट बढ़ाएंगे ताकि लाइनें उभर कर आएं
|
| 18 |
+
enhancer = ImageEnhance.Contrast(image)
|
| 19 |
+
image = enhancer.enhance(contrast_boost) # User controlled boost
|
| 20 |
+
|
| 21 |
# --- INVERT LOGIC ---
|
| 22 |
if invert_input:
|
| 23 |
image = ImageOps.invert(image.convert("RGB"))
|
| 24 |
|
| 25 |
+
# Grayscale conversion
|
| 26 |
img_np = np.array(image.convert("RGB"))
|
| 27 |
gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# --- THE SURGICAL THRESHOLD ---
|
| 30 |
+
# कोई ब्लर नहीं, कोई ऑटोमैटिक दिमाग नहीं। सीधा थ्रेशोल्ड।
|
| 31 |
+
# यह स्लाइडर तय करेगा कि किस शेड को काला मानना है।
|
| 32 |
+
# बारीक डिज़ाइन के लिए यह सबसे सटीक तरीका है।
|
| 33 |
+
_, binary = cv2.threshold(gray, detail_threshold, 255, cv2.THRESH_BINARY_INV)
|
| 34 |
+
|
| 35 |
+
# --- SOLID FORCE (Optional) ---
|
| 36 |
+
# सिर्फ तब इस्तेमाल करें जब लाइनें टूट रही हों।
|
| 37 |
+
# बारीक डिज़ाइन में इसे 0 रखना ही बेहतर है।
|
| 38 |
kernel = np.ones((3,3), np.uint8)
|
| 39 |
+
if solid_force > 0:
|
| 40 |
+
binary = cv2.dilate(binary, kernel, iterations=int(solid_force))
|
| 41 |
+
|
| 42 |
+
# --- NOISE REMOVAL (Dangerous for fine detail) ---
|
| 43 |
+
# इसे बहुत सोच समझ कर ऑन करना, यह बारीक बिन्दुओं को उड़ा सकता है।
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
if remove_noise:
|
| 45 |
binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=1)
|
| 46 |
|
| 47 |
+
# Final Invert for Potrace
|
| 48 |
final_binary = cv2.bitwise_not(binary)
|
| 49 |
|
| 50 |
# Save Temp
|
| 51 |
temp_bmp = "temp_trace.bmp"
|
| 52 |
cv2.imwrite(temp_bmp, final_binary)
|
| 53 |
|
| 54 |
+
# Vectorize with precision settings
|
| 55 |
+
output_svg = "ts_vector_ultimate.svg"
|
| 56 |
|
| 57 |
cmd = [
|
| 58 |
"potrace", temp_bmp,
|
| 59 |
"-s", "-o", output_svg,
|
| 60 |
+
"-t", "0", # Turdsize 0: मतलब एक पिक्सेल भी नहीं छोड़ना है। सब पकड़ो।
|
| 61 |
+
"-a", "0", # Alphamax 0: कोई अपनी मर्जी से स्मूथिंग नहीं। जैसा है वैसा ट्रेस करो।
|
| 62 |
"--opaque"
|
| 63 |
]
|
| 64 |
|
|
|
|
| 70 |
|
| 71 |
return output_svg, Image.fromarray(final_binary)
|
| 72 |
|
| 73 |
+
# --- Interface ---
|
| 74 |
custom_css = """
|
| 75 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;900&display=swap');
|
| 76 |
+
body, .gradio-container { font-family: 'Inter', sans-serif !important; background: #0a0a0a !important; color: white !important; }
|
| 77 |
+
#main_card { border: 2px solid #FFD700; border-radius: 15px; padding: 25px; background: #111; }
|
| 78 |
+
.primary-btn { background: linear-gradient(45deg, #FFD700, #FF8C00) !important; color: black !important; font-weight: 900 !important; letter-spacing: 1px; }
|
| 79 |
"""
|
| 80 |
|
| 81 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as app:
|
| 82 |
with gr.Column(elem_id="main_card"):
|
| 83 |
+
gr.Markdown("# 💀 TS VECTOR ULTIMATE", elem_id="logo_text")
|
| 84 |
+
gr.Markdown("### The Final Attempt for Extreme Detail (Zero Compromise)")
|
| 85 |
|
| 86 |
with gr.Row():
|
| 87 |
with gr.Column():
|
| 88 |
+
inp_img = gr.Image(type="pil", label="Upload The Hardest Photo", height=300)
|
| 89 |
|
| 90 |
+
gr.Markdown("### ⚙️ Critical Controls (ध्यान से सेट करें)")
|
| 91 |
inv_chk = gr.Checkbox(label="Is Design WHITE on BLACK?", value=False)
|
| 92 |
|
| 93 |
+
# 1. Contrast Boost
|
| 94 |
+
cont_sld = gr.Slider(1.0, 3.0, value=1.5, step=0.1, label="1. Contrast Booster (लाइनों को उभारने के लिए)")
|
| 95 |
+
|
| 96 |
+
# 2. The Main Threshold
|
| 97 |
+
thresh_sld = gr.Slider(0, 255, value=180, step=1, label="2. Detail Cutoff (इसे घुमाकर प्रीव्यू में चेक करें)")
|
| 98 |
|
| 99 |
+
# 3. Solid Force
|
| 100 |
+
solid_sld = gr.Slider(0, 3, value=0, step=1, label="3. Solid Force (बारीक डिज़ाइन के लिए 0 रखें)")
|
| 101 |
|
| 102 |
+
noise_chk = gr.Checkbox(label="4. Clean Noise (बारीक डिज़ाइन में OFF रखें)", value=False)
|
|
|
|
| 103 |
|
| 104 |
+
btn = gr.Button("⚡ TRACE EXACT PIXELS", variant="primary", elem_classes=["primary-btn"])
|
| 105 |
|
| 106 |
with gr.Column():
|
| 107 |
+
preview_img = gr.Image(label="Computer Vision Preview (What you see is what you get)", interactive=False)
|
| 108 |
out_file = gr.File(label="Download SVG")
|
| 109 |
|
| 110 |
+
btn.click(process_vector, inputs=[inp_img, inv_chk, cont_sld, thresh_sld, solid_sld, noise_chk], outputs=[out_file, preview_img])
|
| 111 |
|
| 112 |
app.launch()
|