TS447 commited on
Commit
f463e03
·
verified ·
1 Parent(s): bdfa7db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -43
app.py CHANGED
@@ -4,45 +4,36 @@ 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)
@@ -51,14 +42,16 @@ def process_vector(image, invert_input, contrast_boost, detail_threshold, solid_
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
 
@@ -74,39 +67,37 @@ def process_vector(image, invert_input, contrast_boost, detail_threshold, solid_
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()
 
4
  from PIL import Image, ImageOps, ImageEnhance
5
  import subprocess
6
 
7
+ # --- CNC BRAIN V8: SMOOTH MASTER (Detail + Curves Balanced) ---
8
+ def process_vector(image, invert_input, contrast_boost, detail_threshold, solid_force, smooth_factor):
9
  if image is None: return None, None
10
 
11
+ # 1. HD UPSCALE (Resolution zaroori hai smooth curves ke liye)
12
  w, h = image.size
 
13
  image = image.resize((w * 2, h * 2), Image.LANCZOS)
14
 
15
+ # 2. PRE-PROCESSING
 
16
  enhancer = ImageEnhance.Contrast(image)
17
+ image = enhancer.enhance(contrast_boost)
18
 
19
  # --- INVERT LOGIC ---
20
  if invert_input:
21
  image = ImageOps.invert(image.convert("RGB"))
22
 
 
23
  img_np = np.array(image.convert("RGB"))
24
  gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
25
 
26
+ # --- THRESHOLDING ---
 
 
 
27
  _, binary = cv2.threshold(gray, detail_threshold, 255, cv2.THRESH_BINARY_INV)
28
 
29
+ # --- SOLID FORCE & CLEANING ---
 
 
30
  kernel = np.ones((3,3), np.uint8)
31
+
32
+ # Noise cleaning (Thoda zaroori hai smooth vector ke liye)
33
+ binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=1)
34
 
35
+ if solid_force > 0:
36
+ binary = cv2.dilate(binary, kernel, iterations=int(solidify_strength))
 
 
37
 
38
  # Final Invert for Potrace
39
  final_binary = cv2.bitwise_not(binary)
 
42
  temp_bmp = "temp_trace.bmp"
43
  cv2.imwrite(temp_bmp, final_binary)
44
 
45
+ # --- VECTORIZE WITH SMOOTHING ENGINE ---
46
+ output_svg = "ts_vector_smooth.svg"
47
 
48
+ # Potrace Commands (Ye hai magic settings)
49
  cmd = [
50
  "potrace", temp_bmp,
51
  "-s", "-o", output_svg,
52
+ "-t", "2", # Despeckle: Chote daane hatayega taaki curve kharab na ho
53
+ "-a", str(smooth_factor), # Alphamax: YE HAI GOLAI KA SLIDER (0=Sharp, 1.3=Very Round)
54
+ "--longcurve", # CNC ke liye lambi smooth lines banane ki koshish karega
55
  "--opaque"
56
  ]
57
 
 
67
  custom_css = """
68
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;900&display=swap');
69
  body, .gradio-container { font-family: 'Inter', sans-serif !important; background: #0a0a0a !important; color: white !important; }
70
+ #main_card { border: 2px solid #00b8ff; border-radius: 15px; padding: 25px; background: #111; }
71
+ .primary-btn { background: linear-gradient(45deg, #00b8ff, #00ff88) !important; color: black !important; font-weight: 900 !important; letter-spacing: 1px; }
72
  """
73
 
74
  with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as app:
75
  with gr.Column(elem_id="main_card"):
76
+ gr.Markdown("# 🌀 TS VECTOR V8 (Smooth)", elem_id="logo_text")
77
+ gr.Markdown("### Perfect Curves for CNC Cutting")
78
 
79
  with gr.Row():
80
  with gr.Column():
81
+ inp_img = gr.Image(type="pil", label="Upload Photo", height=300)
82
 
83
+ gr.Markdown("### ⚙️ Controls")
84
  inv_chk = gr.Checkbox(label="Is Design WHITE on BLACK?", value=False)
85
 
86
+ # Basic Controls
87
+ cont_sld = gr.Slider(1.0, 2.5, value=1.5, step=0.1, label="1. Contrast (Detail Ubharo)")
88
+ thresh_sld = gr.Slider(0, 255, value=170, step=1, label="2. Detail Cutoff (Preview dekho)")
89
+ solid_sld = gr.Slider(0, 3, value=0, step=1, label="3. Solid Force (Mota karne ke liye)")
 
 
 
 
90
 
91
+ # --- NEW SMOOTH SLIDER ---
92
+ gr.Markdown("### 🌀 Curve Control (Golai)")
93
+ smooth_sld = gr.Slider(0.0, 1.34, value=1.0, step=0.1, label="4. Smoothing Factor (0=Kata hua, 1.0=Mast Golai)")
94
 
95
+ btn = gr.Button("⚡ CREATE SMOOTH VECTOR", variant="primary", elem_classes=["primary-btn"])
96
 
97
  with gr.Column():
98
+ preview_img = gr.Image(label="Computer Vision Preview (Black = Cut)", interactive=False)
99
  out_file = gr.File(label="Download SVG")
100
 
101
+ btn.click(process_vector, inputs=[inp_img, inv_chk, cont_sld, thresh_sld, solid_sld, smooth_sld], outputs=[out_file, preview_img])
102
 
103
  app.launch()