Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,44 +4,59 @@ 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 |
-
|
| 16 |
-
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
|
|
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
if solidify_strength > 0:
|
| 26 |
-
# Dilate: White hissa (Jali) mota hoga aur beech ke holes bharenge
|
| 27 |
binary = cv2.dilate(binary, kernel, iterations=int(solidify_strength))
|
| 28 |
-
# Wapas thoda shape mein laane ke liye halka erode
|
| 29 |
binary = cv2.erode(binary, kernel, iterations=1)
|
| 30 |
|
| 31 |
-
#
|
| 32 |
if remove_noise:
|
| 33 |
-
# Morph Open: Chote white dots (noise) gayab karega
|
| 34 |
binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=1)
|
| 35 |
|
| 36 |
-
#
|
|
|
|
| 37 |
final_binary = cv2.bitwise_not(binary)
|
| 38 |
|
| 39 |
# Save Temp
|
| 40 |
temp_bmp = "temp_trace.bmp"
|
| 41 |
cv2.imwrite(temp_bmp, final_binary)
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
output_svg = "
|
| 45 |
|
| 46 |
cmd = [
|
| 47 |
"potrace", temp_bmp,
|
|
@@ -59,36 +74,43 @@ def process_vector(image, threshold_val, solidify_strength, smooth_factor, remov
|
|
| 59 |
|
| 60 |
return output_svg, Image.fromarray(final_binary)
|
| 61 |
|
| 62 |
-
# --- 2. Interface
|
| 63 |
custom_css = """
|
| 64 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;900&display=swap');
|
| 65 |
body, .gradio-container { font-family: 'Inter', sans-serif !important; background: #000000 !important; color: white !important; }
|
| 66 |
#main_card { border: 1px solid #333; border-radius: 15px; padding: 20px; background: #111; }
|
| 67 |
-
.primary-btn { background: #
|
| 68 |
"""
|
| 69 |
|
| 70 |
-
# Maine yahan se mode='dark' hata diya hai, ab Soft theme use hoga jo safe hai
|
| 71 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as app:
|
| 72 |
with gr.Column(elem_id="main_card"):
|
| 73 |
-
gr.Markdown("#
|
|
|
|
| 74 |
|
| 75 |
with gr.Row():
|
| 76 |
with gr.Column():
|
| 77 |
inp_img = gr.Image(type="pil", label="Upload Photo", height=300)
|
| 78 |
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
| 85 |
|
| 86 |
-
btn = gr.Button("
|
| 87 |
|
| 88 |
with gr.Column():
|
| 89 |
preview_img = gr.Image(label="Computer Vision Preview", interactive=False)
|
| 90 |
out_file = gr.File(label="Download SVG")
|
| 91 |
|
| 92 |
-
btn.click(process_vector, inputs=[inp_img,
|
| 93 |
|
| 94 |
app.launch()
|
|
|
|
| 4 |
from PIL import Image, ImageOps
|
| 5 |
import subprocess
|
| 6 |
|
| 7 |
+
# --- 1. CNC BRAIN V6 (Color Aware Scanner) ---
|
| 8 |
+
def process_vector(image, color_mode, scan_sensitivity, solidify_strength, smooth_factor, remove_noise):
|
| 9 |
if image is None: return None, None
|
| 10 |
|
| 11 |
+
# A. Grayscale
|
| 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 |
+
if color_mode == "Light Design (White on Black)":
|
| 21 |
+
gray = cv2.bitwise_not(gray)
|
| 22 |
|
| 23 |
+
# B. Contrast Enhance (CLAHE) - Detail nikalne ke liye
|
| 24 |
+
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
|
| 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 |
+
# D. POST-PROCESSING
|
| 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 |
+
# F. Noise Removal
|
| 47 |
if remove_noise:
|
|
|
|
| 48 |
binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=1)
|
| 49 |
|
| 50 |
+
# G. Potrace Preparation
|
| 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 |
+
# H. Vectorize
|
| 59 |
+
output_svg = "ts_vector_pro.svg"
|
| 60 |
|
| 61 |
cmd = [
|
| 62 |
"potrace", temp_bmp,
|
|
|
|
| 74 |
|
| 75 |
return output_svg, Image.fromarray(final_binary)
|
| 76 |
|
| 77 |
+
# --- 2. Interface ---
|
| 78 |
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: linear-gradient(45deg, #00ff88, #00b8ff) !important; color: black !important; font-weight: bold !important; }
|
| 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("# π³ TS VECTOR V6", elem_id="logo_text")
|
| 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 |
+
# --- NEW OPTION HERE ---
|
| 95 |
+
gr.Markdown("### π¨ 1. Image Color Type (Very Important)")
|
| 96 |
+
color_mode = gr.Radio(
|
| 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("### ποΈ 2. Fine Tuning")
|
| 103 |
+
scan_sld = gr.Slider(11, 61, value=31, step=2, label="Scanner Sensitivity (Detail)")
|
| 104 |
+
solid_sld = gr.Slider(0, 5, value=2, step=1, label="Solidify (Mota karne ke liye)")
|
| 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("π CONVERT NOW", variant="primary", elem_classes=["primary-btn"])
|
| 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, color_mode, scan_sld, solid_sld, smooth_sld, noise_chk], outputs=[out_file, preview_img])
|
| 115 |
|
| 116 |
app.launch()
|