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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -32
app.py CHANGED
@@ -4,40 +4,48 @@ import numpy as np
4
  from PIL import Image, ImageOps
5
  import subprocess
6
 
7
- # --- 1. CNC BRAIN V5 (HD Upscale + Smooth Logic) ---
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 0: HD UPSCALE (The Magic Fix) ---
12
- # Agar photo choti hai ya sharp hai, to usse 2x bada kar do.
13
- # Isse curves "Blocky" nahi honge, bilkul smooth aayenge.
14
  w, h = image.size
15
- # High Quality Resizing (Lanczos Filter)
16
  image = image.resize((w * 2, h * 2), Image.LANCZOS)
17
 
18
  # --- INVERT LOGIC ---
19
  if invert_input:
20
  image = ImageOps.invert(image.convert("RGB"))
21
 
22
- # Step A: Convert to Grayscale & Blur
23
  img_np = np.array(image.convert("RGB"))
24
  gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
25
 
26
- # Blur thoda badhaya hai taaki pixelate na ho
27
- gray_blurred = cv2.GaussianBlur(gray, (9, 9), 0)
28
-
29
- # Step B: Thresholding
30
- _, binary = cv2.threshold(gray_blurred, threshold_val, 255, cv2.THRESH_BINARY_INV)
 
 
 
 
 
 
31
 
32
- # Step C: SOLIDIFY (Double Line Fixer)
33
  kernel = np.ones((3,3), np.uint8)
34
 
35
- if solidify_strength > 0:
36
- binary = cv2.dilate(binary, kernel, iterations=int(solidify_strength))
37
- binary = cv2.erode(binary, kernel, iterations=1)
38
-
39
- # Step D: Noise Removal
 
 
 
40
  if remove_noise:
 
41
  binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=1)
42
 
43
  # Step E: Invert for Potrace
@@ -48,14 +56,13 @@ def process_vector(image, invert_input, threshold_val, solidify_strength, smooth
48
  cv2.imwrite(temp_bmp, final_binary)
49
 
50
  # Step F: Vectorize
51
- output_svg = "ts_vector_hd.svg"
52
 
53
  cmd = [
54
  "potrace", temp_bmp,
55
  "-s", "-o", output_svg,
56
- "-t", "10",
57
- "-a", str(smooth_factor), # Curve smoothness controlled here
58
- "--alphamax", "1.34", # Maximize curves
59
  "--opaque"
60
  ]
61
 
@@ -77,28 +84,32 @@ body, .gradio-container { font-family: 'Inter', sans-serif !important; backgroun
77
 
78
  with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as app:
79
  with gr.Column(elem_id="main_card"):
80
- gr.Markdown("# πŸ† TS VECTOR HD (V5)", elem_id="logo_text")
81
- gr.Markdown("### Auto-Upscale for Smooth Curves")
82
 
83
  with gr.Row():
84
  with gr.Column():
85
  inp_img = gr.Image(type="pil", label="Upload Photo", height=300)
86
 
87
  gr.Markdown("### 🎨 Image Type")
88
- inv_chk = gr.Checkbox(label="Is Design WHITE on BLACK Background?", value=False)
 
 
 
 
 
 
 
 
89
 
90
- gr.Markdown("### πŸŽ›οΈ Settings")
91
- thresh_sld = gr.Slider(0, 255, value=140, step=1, label="Darkness Threshold")
92
- solid_sld = gr.Slider(0, 5, value=0, step=1, label="Solidify (Keep 0 for Graphics)")
93
- noise_chk = gr.Checkbox(label="Clean Noise", value=True)
94
- smooth_sld = gr.Slider(0, 1.5, value=1.2, label="Smoothing (High for Round shapes)")
95
 
96
- btn = gr.Button("πŸ”₯ CREATE HD VECTOR", variant="primary", elem_classes=["primary-btn"])
97
 
98
  with gr.Column():
99
  preview_img = gr.Image(label="Computer Vision Preview", interactive=False)
100
  out_file = gr.File(label="Download SVG")
101
 
102
- btn.click(process_vector, inputs=[inp_img, inv_chk, thresh_sld, solid_sld, smooth_sld, noise_chk], outputs=[out_file, preview_img])
103
 
104
  app.launch()
 
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
 
16
  # --- INVERT LOGIC ---
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
  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
 
 
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():
92
  inp_img = gr.Image(type="pil", label="Upload Photo", height=300)
93
 
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()