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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -58
app.py CHANGED
@@ -1,73 +1,64 @@
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
4
- from PIL import Image, ImageOps
5
  import subprocess
6
 
7
- # --- 1. CNC BRAIN V5.5 (Solid + Detail Preserved) ---
8
- def process_vector(image, invert_input, detail_level, solidify_strength, smooth_factor, remove_noise):
9
  if image is None: return None, None
10
 
11
- # --- STEP 1: HD UPSCALE (V5 ka magic - Quality ke liye) ---
12
  w, h = image.size
13
- # Photo ko 2 guna bada karo taaki curves smooth aayein
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
21
  img_np = np.array(image.convert("RGB"))
22
  gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
23
-
24
- # --- STEP 2: SMART CONTRAST (Blur ki jagah ye use karenge) ---
25
- # Ye barik details ko ubhaar dega bina dhundla kiye
26
- clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
27
- gray = clahe.apply(gray)
28
-
29
- # --- STEP 3: ADAPTIVE DETAIL THRESHOLD ---
30
- # Ye slider decide karega ki kitni barik cheez pakadni hai
31
- # Slider value (11-99) ko hum Block Size banayenge
32
- block_size = int(detail_level)
33
- if block_size % 2 == 0: block_size += 1 # Odd number zaroori hai
34
-
35
- # Adaptive Threshold: Ye local area dekhta hai (Barik details ke liye best)
36
- binary = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
37
- cv2.THRESH_BINARY_INV, block_size, 5)
38
 
39
- # --- STEP 4: SOLIDIFY (Tera favorite feature) ---
40
- # Ye lines ko jodne aur solid banane ke liye hai
 
 
 
 
 
 
 
41
  kernel = np.ones((3,3), np.uint8)
42
-
43
- if solidify_strength > 0:
44
- # Mota karo (Dilate)
45
- binary = cv2.dilate(binary, kernel, iterations=int(solidify_strength))
46
- # Halka sa Erode taaki shape wapas sharp ho jaye
47
- binary = cv2.erode(binary, kernel, iterations=1)
48
- elif solidify_strength < 0:
49
- # Agar bahut chipak raha hai to alag karo
50
- binary = cv2.erode(binary, kernel, iterations=abs(int(solidify_strength)))
51
-
52
- # Step 5: Noise Removal
53
  if remove_noise:
54
  binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=1)
55
 
56
- # Step 6: Invert for Potrace
57
  final_binary = cv2.bitwise_not(binary)
58
 
59
  # Save Temp
60
  temp_bmp = "temp_trace.bmp"
61
  cv2.imwrite(temp_bmp, final_binary)
62
 
63
- # Step 7: Vectorize
64
- output_svg = "ts_vector_solid_detail.svg"
65
 
66
  cmd = [
67
  "potrace", temp_bmp,
68
  "-s", "-o", output_svg,
69
- "-t", "2",
70
- "-a", str(smooth_factor),
71
  "--opaque"
72
  ]
73
 
@@ -79,43 +70,43 @@ def process_vector(image, invert_input, detail_level, solidify_strength, smooth_
79
 
80
  return output_svg, Image.fromarray(final_binary)
81
 
82
- # --- 2. Interface ---
83
  custom_css = """
84
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;900&display=swap');
85
- body, .gradio-container { font-family: 'Inter', sans-serif !important; background: #000000 !important; color: white !important; }
86
- #main_card { border: 1px solid #333; border-radius: 15px; padding: 20px; background: #111; }
87
- .primary-btn { background: #00ff88 !important; color: black !important; font-weight: bold !important; }
88
  """
89
 
90
  with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as app:
91
  with gr.Column(elem_id="main_card"):
92
- gr.Markdown("# 🏆 TS VECTOR V5.5", elem_id="logo_text")
93
- gr.Markdown("### Solid Shapes + Fine Detail Control")
94
 
95
  with gr.Row():
96
  with gr.Column():
97
- inp_img = gr.Image(type="pil", label="Upload Photo", height=300)
98
 
99
- gr.Markdown("### 🎨 Image Type")
100
  inv_chk = gr.Checkbox(label="Is Design WHITE on BLACK?", value=False)
101
 
102
- gr.Markdown("### 🎛️ Settings (Adjust for Details)")
103
-
104
- # Ye hai naya slider - Isse barik detail aayegi
105
- detail_sld = gr.Slider(11, 151, value=45, step=2, label="1. Detail Sensitivity (Kam = Barik Detail, Zyada = Mota Design)")
 
106
 
107
- # Tera favorite Solidify slider wapas aa gaya
108
- solid_sld = gr.Slider(-2, 5, value=1, step=1, label="2. Solidify (Gap Bharne ke liye)")
109
 
110
- noise_chk = gr.Checkbox(label="3. Clean Noise", value=True)
111
- smooth_sld = gr.Slider(0, 1.3, value=1.0, label="4. Smoothing")
112
 
113
- btn = gr.Button("🔥 CREATE SOLID VECTOR", variant="primary", elem_classes=["primary-btn"])
114
 
115
  with gr.Column():
116
- preview_img = gr.Image(label="Computer Vision Preview (Black = Solid Material)", interactive=False)
117
  out_file = gr.File(label="Download SVG")
118
 
119
- btn.click(process_vector, inputs=[inp_img, inv_chk, detail_sld, solid_sld, smooth_sld, noise_chk], outputs=[out_file, preview_img])
120
 
121
  app.launch()
 
1
  import gradio as gr
2
  import cv2
3
  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)
49
 
50
  # Save Temp
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
 
 
70
 
71
  return output_svg, Image.fromarray(final_binary)
72
 
73
+ # --- Interface ---
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()