TS447 commited on
Commit
e6867d2
Β·
verified Β·
1 Parent(s): b43ed8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -41
app.py CHANGED
@@ -4,12 +4,11 @@ import numpy as np
4
  from PIL import Image, ImageOps
5
  import subprocess
6
 
7
- # --- 1. CNC BRAIN V6 (Micro-Detail Logic) ---
8
- def process_vector(image, invert_input, detail_sens, thickness, remove_noise):
9
  if image is None: return None, None
10
 
11
- # --- STEP 0: HD UPSCALE ---
12
- # Detail nikalne ke liye photo bada hona zaroori hai
13
  w, h = image.size
14
  image = image.resize((w * 2, h * 2), Image.LANCZOS)
15
 
@@ -17,35 +16,35 @@ def process_vector(image, invert_input, detail_sens, thickness, remove_noise):
17
  if invert_input:
18
  image = ImageOps.invert(image.convert("RGB"))
19
 
20
- # Step A: Grayscale & Contrast Boost (No Blur!)
21
  img_np = np.array(image.convert("RGB"))
22
  gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
23
 
24
- # CLAHE (Smart Contrast): Ye Gold color ko background se alag karega
25
- clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
26
- gray = clahe.apply(gray)
27
-
28
- # Step B: Adaptive Thresholding (Detail Hunter)
29
- # Block size jitna chota (odd number), utni barik detail pakdega
30
- block_size = int(detail_sens)
31
- if block_size % 2 == 0: block_size += 1 # Must be odd
32
-
33
- binary = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
34
- cv2.THRESH_BINARY_INV, block_size, 5)
35
 
36
- # Step C: THICKNESS CONTROL (Negative allows opening gaps)
37
  kernel = np.ones((3,3), np.uint8)
38
 
39
- if thickness > 0:
40
- # Positive: Mota karo (Dilate)
41
- binary = cv2.dilate(binary, kernel, iterations=int(thickness))
42
- elif thickness < 0:
43
- # Negative: Patla karo (Erode) - Ye 'chipki hui' lines ko kholega
44
- binary = cv2.erode(binary, kernel, iterations=abs(int(thickness)))
45
-
46
- # Step D: Noise Removal (Optional - for very noisy photos)
 
 
 
47
  if remove_noise:
48
- # Bilkul halka sa clean, zyada nahi
49
  binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=1)
50
 
51
  # Step E: Invert for Potrace
@@ -56,13 +55,13 @@ def process_vector(image, invert_input, detail_sens, thickness, remove_noise):
56
  cv2.imwrite(temp_bmp, final_binary)
57
 
58
  # Step F: Vectorize
59
- output_svg = "ts_vector_detail.svg"
60
 
61
  cmd = [
62
  "potrace", temp_bmp,
63
  "-s", "-o", output_svg,
64
- "-t", "2", # Turdsize kam kiya taaki chote dots na udein
65
- "-a", "1.0",
66
  "--opaque"
67
  ]
68
 
@@ -79,13 +78,13 @@ 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: #00ff88 !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 PRO (V6)", elem_id="logo_text")
88
- gr.Markdown("### Optimized for High-Detail & Mandala Designs")
89
 
90
  with gr.Row():
91
  with gr.Column():
@@ -94,22 +93,23 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as app:
94
  gr.Markdown("### 🎨 Image Type")
95
  inv_chk = gr.Checkbox(label="Is Design WHITE on BLACK?", value=False)
96
 
97
- gr.Markdown("### πŸŽ›οΈ Detail Settings")
98
 
99
- # Naya Slider: Block Size
100
- sens_sld = gr.Slider(11, 99, value=45, step=2, label="1. Detail Sensitivity (Kam = Barik, Zyada = Mota Area)")
101
 
102
- # Naya Slider: Negative value allowed
103
- thick_sld = gr.Slider(-3, 3, value=0, step=1, label="2. Line Thickness (Minus me le jao gap kholne ke liye)")
104
 
105
- noise_chk = gr.Checkbox(label="3. Clean Noise (Off rakho agar detail udd rahi hai)", value=False)
 
106
 
107
- btn = gr.Button("πŸ”₯ CAPTURE DETAILS", variant="primary", elem_classes=["primary-btn"])
108
 
109
  with gr.Column():
110
- preview_img = gr.Image(label="Computer Vision Preview", interactive=False)
111
  out_file = gr.File(label="Download SVG")
112
 
113
- btn.click(process_vector, inputs=[inp_img, inv_chk, sens_sld, thick_sld, noise_chk], outputs=[out_file, preview_img])
114
 
115
  app.launch()
 
4
  from PIL import Image, ImageOps
5
  import subprocess
6
 
7
+ # --- 1. CNC BRAIN V7 (Solid Shape Master) ---
8
+ def process_vector(image, invert_input, threshold_val, solidify_strength, smooth_factor, remove_noise):
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
 
 
16
  if invert_input:
17
  image = ImageOps.invert(image.convert("RGB"))
18
 
19
+ # Step A: Grayscale
20
  img_np = np.array(image.convert("RGB"))
21
  gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
22
 
23
+ # --- STEP 2: MEDIAN BLUR (The Line-Art Killer) ---
24
+ # Ye texture/lines ko gayab karke shape ko 'Flat' banata hai.
25
+ # Value 5 ya 7 best hai 3D renders ke liye.
26
+ gray_flat = cv2.medianBlur(gray, 7)
27
+
28
+ # Step B: GLOBAL THRESHOLD (Wapas purana reliable logic)
29
+ # Adaptive hata diya kyunki wo sketch bana raha tha.
30
+ # Ab hum user ke hath mein power denge ki wo decide kare kya black hai kya white.
31
+ _, binary = cv2.threshold(gray_flat, threshold_val, 255, cv2.THRESH_BINARY_INV)
 
 
32
 
33
+ # Step C: SOLIDIFY (Filling the gaps)
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 D: Noise Removal
47
  if remove_noise:
 
48
  binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=1)
49
 
50
  # Step E: Invert for Potrace
 
55
  cv2.imwrite(temp_bmp, final_binary)
56
 
57
  # Step F: Vectorize
58
+ output_svg = "ts_vector_solid.svg"
59
 
60
  cmd = [
61
  "potrace", temp_bmp,
62
  "-s", "-o", output_svg,
63
+ "-t", "2",
64
+ "-a", str(smooth_factor),
65
  "--opaque"
66
  ]
67
 
 
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: #FFD700 !important; color: black !important; font-weight: bold !important; }
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 SOLID TRACER V7", elem_id="logo_text")
87
+ gr.Markdown("### Converts 3D Images into SOLID Cut Files")
88
 
89
  with gr.Row():
90
  with gr.Column():
 
93
  gr.Markdown("### 🎨 Image Type")
94
  inv_chk = gr.Checkbox(label="Is Design WHITE on BLACK?", value=False)
95
 
96
+ gr.Markdown("### πŸŽ›οΈ Solid Settings")
97
 
98
+ # Global Threshold wapas aa gaya hai - Sabse reliable
99
+ thresh_sld = gr.Slider(0, 255, value=128, step=1, label="1. Solid Threshold (Adjust until design is full Black)")
100
 
101
+ # Solidify
102
+ solid_sld = gr.Slider(-2, 5, value=1, step=1, label="2. Solidify (Mota/Patla)")
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")
106
 
107
+ btn = gr.Button("πŸ”₯ CREATE SOLID VECTOR", variant="primary", elem_classes=["primary-btn"])
108
 
109
  with gr.Column():
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, thresh_sld, solid_sld, smooth_sld, noise_chk], outputs=[out_file, preview_img])
114
 
115
  app.launch()