File size: 3,116 Bytes
2e036a2 f4586bd 2e036a2 f4586bd 2e036a2 f4586bd 619639a 2e036a2 f4586bd 2e036a2 | 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 | """
Image input és controls komponensek.
"""
import gradio as gr
import numpy as np
from typing import Optional
from ui.utils import render
class InputSection:
def __init__(self):
self.stream_image = None
self.upload_image = None
self.mode_selector = None
self.current_image: Optional[np.ndarray] = None
self.image_history = []
def create(self):
with gr.Column(scale=1):
with gr.Group(elem_classes="input-controls-section"):
gr.HTML(render('section_label.html', icon="📤", title="Image Input"))
self.mode_selector = gr.Radio(
choices=["Live Webcam", "Upload/Snapshot"],
value="Upload/Snapshot",
label="Input Mode",
container=False
)
# Streaming webcam (for real-time)
self.stream_image = gr.Image(
label="",
type="numpy",
sources=["webcam"],
height=200,
container=False,
streaming=True,
visible=False
)
# Upload or snapshot mode
self.upload_image = gr.Image(
label="",
type="numpy",
sources=["upload", "webcam"],
container=False,
visible=True,
height=70
)
return self.stream_image, self.upload_image, self.mode_selector
def switch_mode(self, mode: str):
"""Switch between streaming and upload mode."""
if mode == "Live Webcam":
return gr.update(visible=True), gr.update(visible=False)
else:
return gr.update(visible=False), gr.update(visible=True)
def set_image(self, image: Optional[np.ndarray]):
if image is not None:
self.current_image = image
self.image_history.append(image)
if len(self.image_history) > 10:
self.image_history.pop(0)
def get_image(self) -> Optional[np.ndarray]:
return self.current_image
def clear(self):
self.current_image = None
return None
def get_history(self):
return self.image_history.copy()
def clear_history(self):
self.image_history = []
class ControlsSection:
def __init__(self):
self.button_component = None
self.is_processing = False
def create(self):
with gr.Column(scale=2):
with gr.Group(elem_classes="input-controls-section"):
gr.HTML(render('section_label.html', icon="🎛️", title="Controls"))
self.button_component = gr.Button(
"▶ Process Image",
size="lg",
elem_classes="process-btn"
)
return self.button_component
def set_processing(self, state: bool):
self.is_processing = state
def is_busy(self) -> bool:
return self.is_processing
|