TS447 commited on
Commit
33a10ec
Β·
verified Β·
1 Parent(s): 4e6321c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -47
app.py CHANGED
@@ -4,59 +4,45 @@ import numpy as np
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,
@@ -79,38 +65,33 @@ 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()
 
4
  from PIL import Image, ImageOps
5
  import subprocess
6
 
7
+ # --- 1. CNC BRAIN V4.5 (Solid Shape + Invert 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
+ # --- NEW FIX: INVERT LOGIC ---
12
+ # Agar design White hai aur Background Black, toh hum pehle hi usse palat denge
13
+ if invert_input:
14
+ image = ImageOps.invert(image.convert("RGB"))
15
+
16
+ # Step A: Convert to Grayscale & Blur
17
  img_np = np.array(image.convert("RGB"))
18
  gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
 
 
 
 
 
19
 
20
+ # Blur to mix highlights
21
+ gray_blurred = cv2.GaussianBlur(gray, (5, 5), 0)
22
 
23
+ # Step B: Thresholding (Solid Black logic)
24
+ _, binary = cv2.threshold(gray_blurred, threshold_val, 255, cv2.THRESH_BINARY_INV)
 
 
 
 
 
 
 
 
 
25
 
26
+ # Step C: SOLIDIFY (Double Line Fixer)
27
  kernel = np.ones((3,3), np.uint8)
28
+
 
 
 
 
29
  if solidify_strength > 0:
30
  binary = cv2.dilate(binary, kernel, iterations=int(solidify_strength))
31
  binary = cv2.erode(binary, kernel, iterations=1)
32
 
33
+ # Step D: Noise Removal
34
  if remove_noise:
35
  binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=1)
36
 
37
+ # Step E: Invert for Potrace
 
38
  final_binary = cv2.bitwise_not(binary)
39
 
40
  # Save Temp
41
  temp_bmp = "temp_trace.bmp"
42
  cv2.imwrite(temp_bmp, final_binary)
43
 
44
+ # Step F: Vectorize
45
+ output_svg = "ts_vector_solid.svg"
46
 
47
  cmd = [
48
  "potrace", temp_bmp,
 
65
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;900&display=swap');
66
  body, .gradio-container { font-family: 'Inter', sans-serif !important; background: #000000 !important; color: white !important; }
67
  #main_card { border: 1px solid #333; border-radius: 15px; padding: 20px; background: #111; }
68
+ .primary-btn { background: #FFD700 !important; color: black !important; font-weight: bold !important; }
69
  """
70
 
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 V4.5", elem_id="logo_text")
 
74
 
75
  with gr.Row():
76
  with gr.Column():
77
  inp_img = gr.Image(type="pil", label="Upload Photo", height=300)
78
 
79
+ gr.Markdown("### 🎨 Image Type (Ye naya hai)")
80
+ # Yahi wo magic button hai jo White Design ko fix karega
81
+ inv_chk = gr.Checkbox(label="Is Design WHITE on BLACK Background?", value=False)
 
 
 
 
82
 
83
+ gr.Markdown("### πŸŽ›οΈ Settings")
84
+ thresh_sld = gr.Slider(0, 255, value=140, step=1, label="Darkness Threshold")
85
+ solid_sld = gr.Slider(0, 5, value=2, step=1, label="Solidify (Double Line Fixer)")
86
  noise_chk = gr.Checkbox(label="Clean Noise", value=True)
87
  smooth_sld = gr.Slider(0, 1.3, value=1.0, label="Smoothing")
88
 
89
+ btn = gr.Button("πŸ”₯ CREATE VECTOR", variant="primary", elem_classes=["primary-btn"])
90
 
91
  with gr.Column():
92
  preview_img = gr.Image(label="Computer Vision Preview", interactive=False)
93
  out_file = gr.File(label="Download SVG")
94
 
95
+ btn.click(process_vector, inputs=[inp_img, inv_chk, thresh_sld, solid_sld, smooth_sld, noise_chk], outputs=[out_file, preview_img])
96
 
97
  app.launch()