File size: 28,725 Bytes
f3929e1 ea3cae7 f3929e1 ea3cae7 8e2116d 1cb0feb 518eb83 0fa40c7 518eb83 08237b0 518eb83 08237b0 518eb83 08237b0 518eb83 08237b0 518eb83 08237b0 518eb83 08237b0 8e2116d 08237b0 518eb83 08237b0 518eb83 08237b0 518eb83 8e2116d 08237b0 518eb83 8e2116d 518eb83 08237b0 518eb83 08237b0 518eb83 08237b0 518eb83 ea3cae7 b224bbe f3929e1 1cb0feb 84d48c8 25f3686 84d48c8 1ca2a2d 25f3686 1cb0feb 25f3686 1ca2a2d 25f3686 84d48c8 1cb0feb 1ca2a2d 1cb0feb 25f3686 1ca2a2d 25f3686 1ca2a2d 1cb0feb 25f3686 1cb0feb 84d48c8 1cb0feb 1ca2a2d 1cb0feb 1ca2a2d 1cb0feb 25f3686 1ca2a2d 25f3686 1ca2a2d 1cb0feb 1ca2a2d 1cb0feb 1ca2a2d 1cb0feb 1ca2a2d 1cb0feb 84d48c8 1cb0feb 84d48c8 08237b0 1cb0feb d10946b 1cb0feb 08237b0 1cb0feb d10946b 1cb0feb 08237b0 1cb0feb d10946b 1cb0feb 08237b0 1cb0feb d10946b 1cb0feb 08237b0 25f3686 1cb0feb 08237b0 4ac7149 08237b0 8e2116d 08237b0 8e2116d 08237b0 4ac7149 08237b0 1cb0feb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 |
import cv2
import numpy as np
import os
import gradio as gr
from PIL import Image
import tempfile
from typing import Union, Tuple
import json
import datetime
import pathlib
# Custom CSS for styling the interface
custom_css = """
.container {
max-width: 1200px;
margin: 0 auto;
}
/* Main styling */
.gradio-container {
font-family: 'Roboto', 'Segoe UI', sans-serif;
color: white;
}
/* Card styling */
.app-card {
border-radius: 12px;
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.15);
padding: 20px;
background: linear-gradient(135deg, #2a3a4a 0%, #1e2a3a 100%);
margin-bottom: 20px;
}
/* Header styling */
h1, h2, h3 {
font-weight: 700 !important;
color: white !important;
}
/* Labels styling */
label, .label {
font-size: 1rem !important;
font-weight: 600 !important;
color: white !important;
margin-bottom: 6px !important;
}
/* Input and slider styling */
.slider-label {
font-weight: 600 !important;
color: white !important;
font-size: 0.95rem !important;
}
/* Button styling */
button.primary {
background: linear-gradient(135deg, #3498db, #2980b9) !important;
color: white !important;
font-weight: 600 !important;
border-radius: 8px !important;
padding: 12px 24px !important;
font-size: 1.1rem !important;
transition: all 0.3s ease !important;
border: none !important;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1), 0 0 10px rgba(52, 152, 219, 0.4) !important;
}
button.primary:hover {
background: linear-gradient(135deg, #2980b9, #2573a7) !important;
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 0 15px rgba(52, 152, 219, 0.6) !important;
transform: translateY(-2px) !important;
}
/* Radio buttons */
.radio-group label {
font-weight: 600 !important;
color: white !important;
}
/* Tab styling */
.tab-nav {
font-weight: 600 !important;
font-size: 1.05rem !important;
}
/* Responsive adjustments */
@media (max-width: 768px) {
.gradio-container {
padding: 10px !important;
}
label, .label {
font-size: 0.95rem !important;
}
button.primary {
padding: 10px 18px !important;
font-size: 1rem !important;
}
}
"""
# Enable OpenCL for better performance
cv2.ocl.setUseOpenCL(True)
# ------------------- Logger Class ------------------- #
class UsageLogger:
"""Simple logger to record app usage timestamps using temp directory in Hugging Face Spaces"""
def __init__(self, log_filename="usage_logs.json"):
# Use the temporary directory in Hugging Face Spaces
self.logs_dir = tempfile.gettempdir()
self.log_file = os.path.join(self.logs_dir, log_filename)
print(f"Log file location: {self.log_file}")
self.ensure_log_file_exists()
def ensure_log_file_exists(self):
"""Create log file with empty array if it doesn't exist"""
try:
if not os.path.exists(self.log_file):
with open(self.log_file, 'w') as f:
json.dump({"visits": [], "features": []}, f)
# Test if file is readable/writable
with open(self.log_file, 'r+') as f:
pass
except Exception as e:
print(f"Error accessing log file: {str(e)}")
# Create another temporary file as fallback
temp_log = tempfile.NamedTemporaryFile(mode='w+', suffix='.json', delete=False)
self.log_file = temp_log.name
print(f"Using fallback temp log file: {self.log_file}")
with open(self.log_file, 'w') as f:
json.dump({"visits": [], "features": []}, f)
def log_visit(self):
"""Log a timestamp when the app is visited/used"""
current_time = datetime.datetime.now().isoformat()
try:
# Read existing logs or create new if file doesn't exist or is corrupt
try:
if os.path.exists(self.log_file) and os.path.getsize(self.log_file) > 0:
with open(self.log_file, 'r') as f:
logs = json.load(f)
else:
logs = {"visits": [], "features": []}
except (json.JSONDecodeError, FileNotFoundError):
# If file is corrupt or doesn't exist, start fresh
logs = {"visits": [], "features": []}
# Append new timestamp
logs["visits"].append({"timestamp": current_time})
# Write updated logs
with open(self.log_file, 'w') as f:
json.dump(logs, f, indent=2)
print(f"Visit logged at {current_time}")
return True
except Exception as e:
print(f"Error logging visit: {str(e)}")
# Try creating a new temporary file if there was an error
try:
temp_log = tempfile.NamedTemporaryFile(mode='w+', suffix='.json', delete=False)
self.log_file = temp_log.name
print(f"Created new temp log file: {self.log_file}")
logs = {"visits": [{"timestamp": current_time}], "features": []}
json.dump(logs, temp_log, indent=2)
temp_log.close()
return True
except Exception as backup_error:
print(f"Error creating backup log file: {str(backup_error)}")
return False
def log_usage(self, feature_type, media_type=None):
"""Log when a specific feature is used
Args:
feature_type: The feature used (e.g., 'black_white_image', 'sketch_video')
media_type: The type of media processed (e.g., 'image', 'video')
"""
current_time = datetime.datetime.now().isoformat()
# Extract media type from feature name if not provided
if media_type is None:
if "image" in feature_type:
media_type = "image"
elif "video" in feature_type:
media_type = "video"
else:
media_type = "unknown"
# Extract service type
if "black_white" in feature_type:
service_type = "black_and_white"
elif "sketch" in feature_type:
service_type = "pencil_sketch"
else:
service_type = "unknown"
try:
# Read existing logs or create new if file doesn't exist or is corrupt
try:
if os.path.exists(self.log_file) and os.path.getsize(self.log_file) > 0:
with open(self.log_file, 'r') as f:
logs = json.load(f)
else:
logs = {"visits": [], "features": []}
except (json.JSONDecodeError, FileNotFoundError):
# If file is corrupt or doesn't exist, start fresh
logs = {"visits": [], "features": []}
# Make sure features key exists
if "features" not in logs:
logs["features"] = []
# Append new usage record
logs["features"].append({
"timestamp": current_time,
"feature": feature_type,
"service": service_type,
"media_type": media_type
})
# Write updated logs
with open(self.log_file, 'w') as f:
json.dump(logs, f, indent=2)
print(f"Feature usage logged: {feature_type} ({media_type}) at {current_time}")
return True
except Exception as e:
print(f"Error logging usage: {str(e)}")
# Try creating a new temporary file if there was an error
try:
temp_log = tempfile.NamedTemporaryFile(mode='w+', suffix='.json', delete=False)
self.log_file = temp_log.name
print(f"Created new temp log file for usage: {self.log_file}")
logs = {"visits": [], "features": [{
"timestamp": current_time,
"feature": feature_type,
"service": service_type,
"media_type": media_type
}]}
json.dump(logs, temp_log, indent=2)
temp_log.close()
return True
except Exception as backup_error:
print(f"Error creating backup log file: {str(backup_error)}")
return False
# Create a global logger instance
logger = UsageLogger()
# Rest of the code remains unchanged...
# ------------------- Theme Setup ------------------- #
def create_custom_theme():
"""Create a custom dark theme for the interface"""
return gr.themes.Base().set(
body_background_fill="linear-gradient(to bottom right, #1a1f2c, #121620)",
body_background_fill_dark="linear-gradient(to bottom right, #1a1f2c, #121620)",
body_text_color="white",
body_text_color_dark="white",
button_primary_background_fill="linear-gradient(135deg, #3498db, #2980b9)",
button_primary_background_fill_hover="linear-gradient(135deg, #2980b9, #2573a7)",
button_primary_text_color="white",
button_primary_text_color_dark="white",
button_primary_border_color="transparent",
button_primary_border_color_dark="transparent",
button_secondary_background_fill="#34495e",
button_secondary_background_fill_hover="#2c3e50",
button_secondary_text_color="white",
button_secondary_text_color_dark="white",
block_title_text_color="white",
block_title_text_color_dark="white",
block_label_text_color="white",
block_label_text_color_dark="white",
slider_color="#3498db",
slider_color_dark="#3498db",
border_color_primary="#3498db",
border_color_primary_dark="#3498db",
background_fill_primary="#2a3a4a",
background_fill_primary_dark="#2a3a4a",
background_fill_secondary="#1e2a3a",
background_fill_secondary_dark="#1e2a3a",
border_radius_size="12px",
spacing_md="12px",
spacing_lg="16px",
text_size="16px",
text_md="18px",
text_lg="20px",
text_xl="24px",
font=["Roboto", "ui-sans-serif", "system-ui", "sans-serif"],
)
# ------------------- Black & White Converter Functions ------------------- #
def convert_to_black_white(image, threshold_value=127, method="otsu"):
"""Convert image to black and white using specified thresholding method"""
if isinstance(image, str):
image = cv2.imread(image)
# Convert to grayscale if not already
if len(image.shape) == 3:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
else:
gray = image
if method == "adaptive":
binary = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 11, 2)
elif method == "otsu":
_, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
else:
_, binary = cv2.threshold(gray, threshold_value, 255, cv2.THRESH_BINARY)
return binary
def process_image_bw(image, threshold_value, method):
"""Process image with black and white thresholding"""
if image is None:
return None
# Convert to numpy array if PIL Image
if isinstance(image, Image.Image):
image_np = np.array(image)
# Convert RGB to BGR for OpenCV
if len(image_np.shape) == 3:
image_np = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
else:
image_np = image
result = convert_to_black_white(image_np, threshold_value, method)
return result
def process_video_bw(video_path, threshold_value, method):
"""Process video with black and white thresholding"""
if not os.path.exists(video_path):
return "Video file not found", None
try:
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
return "Could not open video file", None
# Get video properties
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
# Create temporary output file
temp_output = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False)
output_path = temp_output.name
temp_output.close()
# Create video writer
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height), isColor=False)
# Process each frame
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
bw_frame = convert_to_black_white(frame, threshold_value, method)
out.write(bw_frame)
cap.release()
out.release()
return "Video processed successfully", output_path
except Exception as e:
return f"Error processing video: {str(e)}", None
# ------------------- Pencil Sketch Converter Functions ------------------- #
def process_image_sketch(image, intensity=255, blur_ksize=21, sigma=0):
"""Convert image to pencil sketch effect"""
if image is None:
return None
# Convert to numpy array if PIL Image
if isinstance(image, Image.Image):
image_np = np.array(image)
# Convert RGB to BGR for OpenCV
if len(image_np.shape) == 3:
image_np = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
else:
image_np = image
# Convert to grayscale
gray = cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY) if len(image_np.shape) == 3 else image_np
# Create sketch effect
inverted = cv2.bitwise_not(gray)
blur_ksize = blur_ksize if blur_ksize % 2 == 1 else blur_ksize + 1 # Ensure kernel size is odd
blurred = cv2.GaussianBlur(inverted, (blur_ksize, blur_ksize), sigma)
sketch = cv2.divide(gray, cv2.bitwise_not(blurred), scale=intensity)
return sketch
def process_video_sketch(video_path, intensity=255, blur_ksize=21, sigma=0):
"""Process video with pencil sketch effect"""
if not os.path.exists(video_path):
return "Video file not found", None
try:
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
return "Could not open video file", None
# Get video properties
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
# Create temporary output file
temp_output = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False)
output_path = temp_output.name
temp_output.close()
# Create video writer
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height), isColor=True)
# Process each frame
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
sketch_frame = process_image_sketch(frame, intensity, blur_ksize, sigma)
# Convert grayscale to BGR for video output
sketch_bgr = cv2.cvtColor(sketch_frame, cv2.COLOR_GRAY2BGR)
out.write(sketch_bgr)
cap.release()
out.release()
return "Video processed successfully", output_path
except Exception as e:
return f"Error processing video: {str(e)}", None
# ------------------- Gradio Interface Functions ------------------- #
def black_white_image(image, threshold_method, threshold_value):
"""Process image with black and white filter for Gradio"""
# Log the usage of this feature
logger.log_usage("black_white_image", "image")
if threshold_method != "manual":
threshold_value = 0 # Not used for adaptive or Otsu
result = process_image_bw(image, threshold_value, threshold_method)
return Image.fromarray(result)
def black_white_video(video, threshold_method, threshold_value):
"""Process video with black and white filter for Gradio"""
# Log the usage of this feature
logger.log_usage("black_white_video", "video")
if threshold_method != "manual":
threshold_value = 0 # Not used for adaptive or Otsu
message, output_path = process_video_bw(video, threshold_value, threshold_method)
if output_path:
return output_path
else:
raise gr.Error(message)
def sketch_image(image, intensity, blur_ksize, sigma):
"""Process image with pencil sketch filter for Gradio"""
# Log the usage of this feature
logger.log_usage("sketch_image", "image")
result = process_image_sketch(image, intensity, blur_ksize, sigma)
return Image.fromarray(result)
def sketch_video(video, intensity, blur_ksize, sigma):
"""Process video with pencil sketch filter for Gradio"""
# Log the usage of this feature
logger.log_usage("sketch_video", "video")
message, output_path = process_video_sketch(video, intensity, blur_ksize, sigma)
if output_path:
return output_path
else:
raise gr.Error(message)
# ------------------- Create Gradio Interface ------------------- #
def create_interface():
# Tooltip content
otsu_tooltip = "Otsu automatically determines the optimal threshold value by analyzing the image histogram."
adaptive_tooltip = "Adaptive thresholding calculates different thresholds for different areas of the image, useful for images with varying lighting conditions."
manual_tooltip = "Manual threshold lets you set a specific brightness cutoff point between black and white pixels."
intensity_tooltip = "Controls the strength of the pencil sketch effect. Higher values create more contrast."
blur_tooltip = "Controls how much the image is blurred. Higher values create a softer sketch effect."
sigma_tooltip = "Controls the standard deviation of the Gaussian blur. Higher values increase the blurring effect."
# Black and White Image Interface
with gr.Blocks(title="Image Processor", css=custom_css, theme=gr.themes.Base()) as app:
# Log app visit at launch
def log_application_visit():
print("Application loaded - logging visit")
success = logger.log_visit()
if success:
print("Visit logged successfully")
else:
print("Failed to log visit")
return None
app.load(fn=log_application_visit, inputs=None, outputs=None)
with gr.Row(elem_classes="container"):
gr.Markdown("""
# Image and Video Processor
Transform your media with professional black & white conversion and pencil sketch effects
""")
with gr.Tabs() as tabs:
with gr.TabItem("Pencil Sketch Converter", elem_classes="app-card"):
with gr.Tabs() as sketch_tabs:
with gr.TabItem("Image Processing"):
with gr.Row(equal_height=True):
with gr.Column(scale=1):
sketch_image_input = gr.Image(label="Input Image")
with gr.Group():
sketch_intensity = gr.Slider(
minimum=1,
maximum=255,
value=255,
step=1,
label="Intensity",
info=intensity_tooltip,
elem_classes="slider-label"
)
sketch_blur = gr.Slider(
minimum=1,
maximum=99,
value=21,
step=2,
label="Blur Kernel Size",
info=blur_tooltip,
elem_classes="slider-label"
)
sketch_sigma = gr.Slider(
minimum=0,
maximum=50,
value=0,
step=0.1,
label="Standard Deviation",
info=sigma_tooltip,
elem_classes="slider-label"
)
sketch_image_btn = gr.Button("Convert", elem_classes="primary")
with gr.Column(scale=1):
sketch_image_output = gr.Image(label="Processed Image")
with gr.TabItem("Video Processing"):
with gr.Row(equal_height=True):
with gr.Column(scale=1):
sketch_video_input = gr.Video(label="Input Video")
with gr.Group():
sketch_video_intensity = gr.Slider(
minimum=1,
maximum=255,
value=255,
step=1,
label="Intensity",
info=intensity_tooltip,
elem_classes="slider-label"
)
sketch_video_blur = gr.Slider(
minimum=1,
maximum=99,
value=21,
step=2,
label="Blur Kernel Size",
info=blur_tooltip,
elem_classes="slider-label"
)
sketch_video_sigma = gr.Slider(
minimum=0,
maximum=50,
value=0,
step=0.1,
label="Standard Deviation",
info=sigma_tooltip,
elem_classes="slider-label"
)
sketch_video_btn = gr.Button("Convert", elem_classes="primary")
with gr.Column(scale=1):
sketch_video_output = gr.Video(label="Processed Video")
with gr.TabItem("Black & White Converter", elem_classes="app-card"):
with gr.Tabs() as bw_tabs:
with gr.TabItem("Image Processing"):
with gr.Row(equal_height=True):
with gr.Column(scale=1):
bw_image_input = gr.Image(label="Input Image", elem_classes="input-image")
with gr.Group():
bw_method = gr.Radio(
choices=["otsu", "adaptive", "manual"],
value="otsu",
label="Thresholding Method",
info=otsu_tooltip,
elem_classes="radio-group"
)
bw_threshold = gr.Slider(
minimum=0,
maximum=255,
value=127,
step=1,
label="Manual Threshold Value",
info=manual_tooltip,
interactive=True,
elem_classes="slider-label"
)
bw_image_btn = gr.Button("Convert", elem_classes="primary")
with gr.Column(scale=1):
bw_image_output = gr.Image(label="Processed Image")
with gr.TabItem("Video Processing"):
with gr.Row(equal_height=True):
with gr.Column(scale=1):
bw_video_input = gr.Video(label="Input Video")
with gr.Group():
bw_video_method = gr.Radio(
choices=["otsu", "adaptive", "manual"],
value="otsu",
label="Thresholding Method",
info=otsu_tooltip,
elem_classes="radio-group"
)
bw_video_threshold = gr.Slider(
minimum=0,
maximum=255,
value=127,
step=1,
label="Manual Threshold Value",
info=manual_tooltip,
interactive=True,
elem_classes="slider-label"
)
bw_video_btn = gr.Button("Convert", elem_classes="primary")
with gr.Column(scale=1):
bw_video_output = gr.Video(label="Processed Video")
with gr.Row(elem_classes="container"):
gr.Markdown("""
### How to use:
1. Upload an image or video
2. Adjust the settings as needed
3. Click the Convert button to process your media
""")
# Set up event listeners
bw_image_btn.click(
fn=black_white_image,
inputs=[bw_image_input, bw_method, bw_threshold],
outputs=bw_image_output
)
bw_video_btn.click(
fn=black_white_video,
inputs=[bw_video_input, bw_video_method, bw_video_threshold],
outputs=bw_video_output
)
sketch_image_btn.click(
fn=sketch_image,
inputs=[sketch_image_input, sketch_intensity, sketch_blur, sketch_sigma],
outputs=sketch_image_output
)
sketch_video_btn.click(
fn=sketch_video,
inputs=[sketch_video_input, sketch_video_intensity, sketch_video_blur, sketch_video_sigma],
outputs=sketch_video_output
)
# Make blur slider always odd
def update_blur(value):
return value if value % 2 == 1 else value + 1
sketch_blur.change(update_blur, sketch_blur, sketch_blur)
sketch_video_blur.change(update_blur, sketch_video_blur, sketch_video_blur)
# Add visibility toggle based on method selection
def update_threshold_visibility(method):
return gr.update(visible=(method == "manual"))
bw_method.change(update_threshold_visibility, bw_method, bw_threshold)
bw_video_method.change(update_threshold_visibility, bw_video_method, bw_video_threshold)
return app
# ------------------- Launch App ------------------- #
if __name__ == "__main__":
app = create_interface()
app.launch() |