Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,26 +16,24 @@ def process_vector(image, threshold_val, solidify_strength, smooth_factor, remov
|
|
| 16 |
gray_blurred = cv2.GaussianBlur(gray, (5, 5), 0)
|
| 17 |
|
| 18 |
# Step B: Thresholding (Solid Black logic)
|
| 19 |
-
# Binary Inverse: Jali banegi WHITE, Background banega BLACK
|
| 20 |
_, binary = cv2.threshold(gray_blurred, threshold_val, 255, cv2.THRESH_BINARY_INV)
|
| 21 |
|
| 22 |
-
# Step C: SOLIDIFY (
|
| 23 |
-
# Agar line double aa rahi hai, to hum usse "Dilate" (failayenge) taaki wo jud jaye
|
| 24 |
kernel = np.ones((3,3), np.uint8)
|
| 25 |
|
| 26 |
if solidify_strength > 0:
|
| 27 |
# Dilate: White hissa (Jali) mota hoga aur beech ke holes bharenge
|
| 28 |
binary = cv2.dilate(binary, kernel, iterations=int(solidify_strength))
|
| 29 |
-
# Wapas thoda shape mein laane ke liye halka erode
|
| 30 |
binary = cv2.erode(binary, kernel, iterations=1)
|
| 31 |
|
| 32 |
-
# Step D: Noise Removal (Watermark
|
| 33 |
if remove_noise:
|
| 34 |
# Morph Open: Chote white dots (noise) gayab karega
|
| 35 |
binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=1)
|
| 36 |
|
| 37 |
# Step E: Invert for Potrace (Potrace needs Black Shape on White)
|
| 38 |
-
# Ab Jali Black ho jayegi, Background White
|
| 39 |
final_binary = cv2.bitwise_not(binary)
|
| 40 |
|
| 41 |
# Save Temp
|
|
@@ -48,7 +46,7 @@ def process_vector(image, threshold_val, solidify_strength, smooth_factor, remov
|
|
| 48 |
cmd = [
|
| 49 |
"potrace", temp_bmp,
|
| 50 |
"-s", "-o", output_svg,
|
| 51 |
-
"-t", "10",
|
| 52 |
"-a", str(smooth_factor),
|
| 53 |
"--opaque"
|
| 54 |
]
|
|
@@ -59,10 +57,9 @@ def process_vector(image, threshold_val, solidify_strength, smooth_factor, remov
|
|
| 59 |
print(f"Error: {e}")
|
| 60 |
return None, None
|
| 61 |
|
| 62 |
-
# Preview Image (Hum user ko wahi dikhayenge jo Potrace ne dekha)
|
| 63 |
return output_svg, Image.fromarray(final_binary)
|
| 64 |
|
| 65 |
-
# --- 2. Interface ---
|
| 66 |
custom_css = """
|
| 67 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;900&display=swap');
|
| 68 |
body, .gradio-container { font-family: 'Inter', sans-serif !important; background: #000000 !important; color: white !important; }
|
|
@@ -70,7 +67,8 @@ body, .gradio-container { font-family: 'Inter', sans-serif !important; backgroun
|
|
| 70 |
.primary-btn { background: #FFD700 !important; color: black !important; font-weight: bold !important; }
|
| 71 |
"""
|
| 72 |
|
| 73 |
-
|
|
|
|
| 74 |
with gr.Column(elem_id="main_card"):
|
| 75 |
gr.Markdown("# π TS SOLID TRACER", elem_id="logo_text")
|
| 76 |
|
|
@@ -78,19 +76,17 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Base(mode='dark')) as app:
|
|
| 78 |
with gr.Column():
|
| 79 |
inp_img = gr.Image(type="pil", label="Upload Photo", height=300)
|
| 80 |
|
| 81 |
-
gr.Markdown("### ποΈ Settings
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
noise_chk = gr.Checkbox(label="3. Clean Noise (Watermark dots hataye)", value=True)
|
| 87 |
smooth_sld = gr.Slider(0, 1.3, value=1.0, label="4. Smoothing")
|
| 88 |
|
| 89 |
btn = gr.Button("π₯ CREATE SOLID VECTOR", variant="primary", elem_classes=["primary-btn"])
|
| 90 |
|
| 91 |
with gr.Column():
|
| 92 |
-
|
| 93 |
-
preview_img = gr.Image(label="Computer Vision (Isse check karo)", interactive=False)
|
| 94 |
out_file = gr.File(label="Download SVG")
|
| 95 |
|
| 96 |
btn.click(process_vector, inputs=[inp_img, thresh_sld, solid_sld, smooth_sld, noise_chk], outputs=[out_file, preview_img])
|
|
|
|
| 16 |
gray_blurred = cv2.GaussianBlur(gray, (5, 5), 0)
|
| 17 |
|
| 18 |
# Step B: Thresholding (Solid Black logic)
|
| 19 |
+
# Binary Inverse: Jali banegi WHITE, Background banega BLACK
|
| 20 |
_, binary = cv2.threshold(gray_blurred, threshold_val, 255, cv2.THRESH_BINARY_INV)
|
| 21 |
|
| 22 |
+
# Step C: SOLIDIFY (Magic Fix for Double Lines)
|
|
|
|
| 23 |
kernel = np.ones((3,3), np.uint8)
|
| 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 |
+
# Step D: Noise Removal (Watermark cleaning)
|
| 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 |
# Step E: Invert for Potrace (Potrace needs Black Shape on White)
|
|
|
|
| 37 |
final_binary = cv2.bitwise_not(binary)
|
| 38 |
|
| 39 |
# Save Temp
|
|
|
|
| 46 |
cmd = [
|
| 47 |
"potrace", temp_bmp,
|
| 48 |
"-s", "-o", output_svg,
|
| 49 |
+
"-t", "10",
|
| 50 |
"-a", str(smooth_factor),
|
| 51 |
"--opaque"
|
| 52 |
]
|
|
|
|
| 57 |
print(f"Error: {e}")
|
| 58 |
return None, None
|
| 59 |
|
|
|
|
| 60 |
return output_svg, Image.fromarray(final_binary)
|
| 61 |
|
| 62 |
+
# --- 2. Interface (FIXED THEME ERROR) ---
|
| 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; }
|
|
|
|
| 67 |
.primary-btn { background: #FFD700 !important; color: black !important; font-weight: bold !important; }
|
| 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("# π TS SOLID TRACER", elem_id="logo_text")
|
| 74 |
|
|
|
|
| 76 |
with gr.Column():
|
| 77 |
inp_img = gr.Image(type="pil", label="Upload Photo", height=300)
|
| 78 |
|
| 79 |
+
gr.Markdown("### ποΈ Settings")
|
| 80 |
|
| 81 |
+
thresh_sld = gr.Slider(0, 255, value=140, step=1, label="1. Darkness Threshold")
|
| 82 |
+
solid_sld = gr.Slider(0, 5, value=2, step=1, label="2. Solidify (Double Line Fixer)")
|
| 83 |
+
noise_chk = gr.Checkbox(label="3. Clean Noise", value=True)
|
|
|
|
| 84 |
smooth_sld = gr.Slider(0, 1.3, value=1.0, label="4. Smoothing")
|
| 85 |
|
| 86 |
btn = gr.Button("π₯ CREATE SOLID VECTOR", variant="primary", elem_classes=["primary-btn"])
|
| 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, thresh_sld, solid_sld, smooth_sld, noise_chk], outputs=[out_file, preview_img])
|