green channel
Browse files
app.py
CHANGED
|
@@ -226,7 +226,6 @@
|
|
| 226 |
# theme=gr.themes.Soft(),
|
| 227 |
# ssr_mode=False
|
| 228 |
# )
|
| 229 |
-
|
| 230 |
import gradio as gr
|
| 231 |
import cv2
|
| 232 |
import numpy as np
|
|
@@ -316,7 +315,6 @@ def scan_edges(image):
|
|
| 316 |
"""
|
| 317 |
Edge detection with CLAHE preprocessing to recover edges lost in
|
| 318 |
shadowed regions (e.g. bearing saddle arcs on engine blocks).
|
| 319 |
-
|
| 320 |
Pipeline:
|
| 321 |
RGB → Grayscale → Gaussian Blur → CLAHE → Canny
|
| 322 |
"""
|
|
@@ -349,6 +347,57 @@ def scan_edges(image):
|
|
| 349 |
return edges_rgb
|
| 350 |
|
| 351 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
def process_image(image, mode):
|
| 353 |
"""
|
| 354 |
Process image based on selected mode
|
|
@@ -358,16 +407,42 @@ def process_image(image, mode):
|
|
| 358 |
|
| 359 |
if mode == "Object Detection":
|
| 360 |
return detect_objects(image)
|
| 361 |
-
|
| 362 |
edges = scan_edges(image)
|
| 363 |
-
return edges, "Edge detection completed"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
|
| 365 |
# Create Gradio interface
|
| 366 |
with gr.Blocks(title="Object Scanner") as demo:
|
| 367 |
-
gr.Markdown("
|
| 368 |
|
| 369 |
with gr.Tabs():
|
| 370 |
-
with gr.TabItem(" Image Scanner"):
|
| 371 |
with gr.Row():
|
| 372 |
with gr.Column():
|
| 373 |
input_image = gr.Image(
|
|
@@ -376,11 +451,16 @@ with gr.Blocks(title="Object Scanner") as demo:
|
|
| 376 |
label="Upload or Capture Image"
|
| 377 |
)
|
| 378 |
mode = gr.Radio(
|
| 379 |
-
choices=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
value="Object Detection",
|
| 381 |
label="Scanning Mode"
|
| 382 |
)
|
| 383 |
-
scan_btn = gr.Button(" Process Image", variant="primary")
|
| 384 |
|
| 385 |
with gr.Column():
|
| 386 |
output_image = gr.Image(type="numpy", label="Processed Result")
|
|
@@ -392,6 +472,8 @@ with gr.Blocks(title="Object Scanner") as demo:
|
|
| 392 |
examples=[
|
| 393 |
["examples/sample1.jpg", "Object Detection"],
|
| 394 |
["examples/sample2.jpg", "Edge Detection"],
|
|
|
|
|
|
|
| 395 |
],
|
| 396 |
inputs=[input_image, mode],
|
| 397 |
outputs=[output_image, output_text],
|
|
@@ -399,8 +481,8 @@ with gr.Blocks(title="Object Scanner") as demo:
|
|
| 399 |
cache_examples=False,
|
| 400 |
)
|
| 401 |
|
| 402 |
-
with gr.TabItem("🎥 Live
|
| 403 |
-
gr.Markdown("### Real-time
|
| 404 |
with gr.Row():
|
| 405 |
with gr.Column():
|
| 406 |
camera_input = gr.Image(
|
|
@@ -409,15 +491,24 @@ with gr.Blocks(title="Object Scanner") as demo:
|
|
| 409 |
type="numpy",
|
| 410 |
label="Live Feed"
|
| 411 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 412 |
with gr.Column():
|
| 413 |
camera_output = gr.Image(
|
| 414 |
-
label="
|
| 415 |
)
|
| 416 |
|
| 417 |
-
# Live stream logic
|
| 418 |
camera_input.stream(
|
| 419 |
-
fn=
|
| 420 |
-
inputs=camera_input,
|
| 421 |
outputs=camera_output
|
| 422 |
)
|
| 423 |
|
|
@@ -427,6 +518,21 @@ with gr.Blocks(title="Object Scanner") as demo:
|
|
| 427 |
inputs=[input_image, mode],
|
| 428 |
outputs=[output_image, output_text]
|
| 429 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 430 |
|
| 431 |
if __name__ == "__main__":
|
| 432 |
demo.launch(
|
|
|
|
| 226 |
# theme=gr.themes.Soft(),
|
| 227 |
# ssr_mode=False
|
| 228 |
# )
|
|
|
|
| 229 |
import gradio as gr
|
| 230 |
import cv2
|
| 231 |
import numpy as np
|
|
|
|
| 315 |
"""
|
| 316 |
Edge detection with CLAHE preprocessing to recover edges lost in
|
| 317 |
shadowed regions (e.g. bearing saddle arcs on engine blocks).
|
|
|
|
| 318 |
Pipeline:
|
| 319 |
RGB → Grayscale → Gaussian Blur → CLAHE → Canny
|
| 320 |
"""
|
|
|
|
| 347 |
return edges_rgb
|
| 348 |
|
| 349 |
|
| 350 |
+
def extract_green_channel(image):
|
| 351 |
+
"""
|
| 352 |
+
Extract the green channel from an RGB image.
|
| 353 |
+
Green channel often provides good contrast for vegetation and certain materials.
|
| 354 |
+
"""
|
| 355 |
+
# 1. Convert PIL image to numpy array if needed
|
| 356 |
+
if isinstance(image, Image.Image):
|
| 357 |
+
image = np.array(image)
|
| 358 |
+
|
| 359 |
+
# 2. Extract green channel (index 1 in RGB)
|
| 360 |
+
green_channel = image[:, :, 1]
|
| 361 |
+
|
| 362 |
+
# 3. Convert to RGB for display (all channels = green)
|
| 363 |
+
green_rgb = cv2.cvtColor(green_channel, cv2.COLOR_GRAY2RGB)
|
| 364 |
+
|
| 365 |
+
return green_rgb
|
| 366 |
+
|
| 367 |
+
|
| 368 |
+
def green_bilateral_edges(image):
|
| 369 |
+
"""
|
| 370 |
+
Edge detection using green channel with bilateral filtering.
|
| 371 |
+
Pipeline:
|
| 372 |
+
RGB → Green Channel → Bilateral Filter → Canny Edge Detection
|
| 373 |
+
|
| 374 |
+
Bilateral filtering preserves edges while reducing noise, making it ideal
|
| 375 |
+
for edge detection on noisy or textured surfaces.
|
| 376 |
+
"""
|
| 377 |
+
# 1. Convert PIL image to numpy array if needed
|
| 378 |
+
if isinstance(image, Image.Image):
|
| 379 |
+
image = np.array(image)
|
| 380 |
+
|
| 381 |
+
# 2. Extract green channel
|
| 382 |
+
green_channel = image[:, :, 1]
|
| 383 |
+
|
| 384 |
+
# 3. Apply bilateral filter
|
| 385 |
+
# d=9 : diameter of pixel neighborhood
|
| 386 |
+
# sigmaColor=75 : filter sigma in the color space (larger = more colors mixed)
|
| 387 |
+
# sigmaSpace=75 : filter sigma in the coordinate space (larger = farther pixels influence)
|
| 388 |
+
# Bilateral filtering smooths flat regions while preserving sharp edges
|
| 389 |
+
bilateral = cv2.bilateralFilter(green_channel, d=9, sigmaColor=75, sigmaSpace=75)
|
| 390 |
+
|
| 391 |
+
# 4. Apply Canny edge detection
|
| 392 |
+
# Using moderate thresholds for balanced edge detection
|
| 393 |
+
edges = cv2.Canny(bilateral, 50, 150)
|
| 394 |
+
|
| 395 |
+
# 5. Convert back to RGB for Gradio display
|
| 396 |
+
edges_rgb = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)
|
| 397 |
+
|
| 398 |
+
return edges_rgb
|
| 399 |
+
|
| 400 |
+
|
| 401 |
def process_image(image, mode):
|
| 402 |
"""
|
| 403 |
Process image based on selected mode
|
|
|
|
| 407 |
|
| 408 |
if mode == "Object Detection":
|
| 409 |
return detect_objects(image)
|
| 410 |
+
elif mode == "Edge Detection":
|
| 411 |
edges = scan_edges(image)
|
| 412 |
+
return edges, "Edge detection completed (CLAHE + Canny)"
|
| 413 |
+
elif mode == "Green Channel":
|
| 414 |
+
green = extract_green_channel(image)
|
| 415 |
+
return green, "Green channel extracted"
|
| 416 |
+
elif mode == "Green + Bilateral Edges":
|
| 417 |
+
edges = green_bilateral_edges(image)
|
| 418 |
+
return edges, "Edge detection completed (Green Channel + Bilateral Filter + Canny)"
|
| 419 |
+
else:
|
| 420 |
+
return image, "Unknown mode selected"
|
| 421 |
+
|
| 422 |
+
|
| 423 |
+
def process_live_stream(image, mode):
|
| 424 |
+
"""
|
| 425 |
+
Process live stream based on selected mode
|
| 426 |
+
"""
|
| 427 |
+
if image is None:
|
| 428 |
+
return None
|
| 429 |
+
|
| 430 |
+
if mode == "Edge Detection":
|
| 431 |
+
return scan_edges(image)
|
| 432 |
+
elif mode == "Green Channel":
|
| 433 |
+
return extract_green_channel(image)
|
| 434 |
+
elif mode == "Green + Bilateral Edges":
|
| 435 |
+
return green_bilateral_edges(image)
|
| 436 |
+
else:
|
| 437 |
+
return scan_edges(image) # Default to edge detection
|
| 438 |
+
|
| 439 |
|
| 440 |
# Create Gradio interface
|
| 441 |
with gr.Blocks(title="Object Scanner") as demo:
|
| 442 |
+
gr.Markdown("# 🔍 Object Scanner\nDetect objects, scan edges, or extract green channel using your camera or uploaded images")
|
| 443 |
|
| 444 |
with gr.Tabs():
|
| 445 |
+
with gr.TabItem("📷 Image Scanner"):
|
| 446 |
with gr.Row():
|
| 447 |
with gr.Column():
|
| 448 |
input_image = gr.Image(
|
|
|
|
| 451 |
label="Upload or Capture Image"
|
| 452 |
)
|
| 453 |
mode = gr.Radio(
|
| 454 |
+
choices=[
|
| 455 |
+
"Object Detection",
|
| 456 |
+
"Edge Detection",
|
| 457 |
+
"Green Channel",
|
| 458 |
+
"Green + Bilateral Edges"
|
| 459 |
+
],
|
| 460 |
value="Object Detection",
|
| 461 |
label="Scanning Mode"
|
| 462 |
)
|
| 463 |
+
scan_btn = gr.Button("🔍 Process Image", variant="primary")
|
| 464 |
|
| 465 |
with gr.Column():
|
| 466 |
output_image = gr.Image(type="numpy", label="Processed Result")
|
|
|
|
| 472 |
examples=[
|
| 473 |
["examples/sample1.jpg", "Object Detection"],
|
| 474 |
["examples/sample2.jpg", "Edge Detection"],
|
| 475 |
+
["examples/sample1.jpg", "Green Channel"],
|
| 476 |
+
["examples/sample2.jpg", "Green + Bilateral Edges"],
|
| 477 |
],
|
| 478 |
inputs=[input_image, mode],
|
| 479 |
outputs=[output_image, output_text],
|
|
|
|
| 481 |
cache_examples=False,
|
| 482 |
)
|
| 483 |
|
| 484 |
+
with gr.TabItem("🎥 Live Processing"):
|
| 485 |
+
gr.Markdown("### Real-time Image Processing")
|
| 486 |
with gr.Row():
|
| 487 |
with gr.Column():
|
| 488 |
camera_input = gr.Image(
|
|
|
|
| 491 |
type="numpy",
|
| 492 |
label="Live Feed"
|
| 493 |
)
|
| 494 |
+
live_mode = gr.Radio(
|
| 495 |
+
choices=[
|
| 496 |
+
"Edge Detection",
|
| 497 |
+
"Green Channel",
|
| 498 |
+
"Green + Bilateral Edges"
|
| 499 |
+
],
|
| 500 |
+
value="Edge Detection",
|
| 501 |
+
label="Processing Mode"
|
| 502 |
+
)
|
| 503 |
with gr.Column():
|
| 504 |
camera_output = gr.Image(
|
| 505 |
+
label="Processed Stream"
|
| 506 |
)
|
| 507 |
|
| 508 |
+
# Live stream logic
|
| 509 |
camera_input.stream(
|
| 510 |
+
fn=lambda img, mode: process_live_stream(img, mode),
|
| 511 |
+
inputs=[camera_input, live_mode],
|
| 512 |
outputs=camera_output
|
| 513 |
)
|
| 514 |
|
|
|
|
| 518 |
inputs=[input_image, mode],
|
| 519 |
outputs=[output_image, output_text]
|
| 520 |
)
|
| 521 |
+
|
| 522 |
+
# Info section
|
| 523 |
+
with gr.Accordion("ℹ️ Mode Information", open=False):
|
| 524 |
+
gr.Markdown("""
|
| 525 |
+
### Available Modes:
|
| 526 |
+
|
| 527 |
+
**Object Detection** - Uses DETR model to detect and label objects with bounding boxes
|
| 528 |
+
|
| 529 |
+
**Edge Detection** - CLAHE-enhanced Canny edge detection for shadowed regions
|
| 530 |
+
|
| 531 |
+
**Green Channel** - Extracts the green channel, useful for vegetation and certain materials
|
| 532 |
+
|
| 533 |
+
**Green + Bilateral Edges** - Combines green channel extraction with bilateral filtering before edge detection.
|
| 534 |
+
Bilateral filtering preserves edges while reducing noise, ideal for textured surfaces.
|
| 535 |
+
""")
|
| 536 |
|
| 537 |
if __name__ == "__main__":
|
| 538 |
demo.launch(
|