Skip to content

Commit 15ea366

Browse files
committed
update readme
1 parent 7c75167 commit 15ea366

File tree

1 file changed

+50
-49
lines changed

1 file changed

+50
-49
lines changed

README.md

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -52,62 +52,63 @@ make build
5252

5353
## Quick Start
5454

55-
PyTrickle is designed for simplicity. You can create a powerful real-time video processor by providing a few async functions to the `StreamProcessor`. This handles the server, state management, and model loading for you.
55+
PyTrickle uses the FrameProcessor pattern for building video processing applications. See the complete example in `examples/async_processor_example.py`.
5656

57-
See a complete, runnable example in `examples/model_loading_example.py`.
58-
59-
### Simple Video Processor
60-
61-
Here's how to create a basic video processor that applies a green tint to video frames:
57+
### Basic FrameProcessor
6258

6359
```python
64-
import asyncio
65-
import logging
66-
from pytrickle import StreamProcessor
67-
from pytrickle.frames import VideoFrame
68-
69-
logging.basicConfig(level=logging.INFO)
70-
71-
# 1. Define your model loading logic
72-
async def load_my_model(**kwargs):
73-
"""Simulate loading a model. Runs in the background without blocking the server."""
74-
logging.info("🔄 Model loading started...")
75-
await asyncio.sleep(5) # Simulate a 5-second model load time
76-
logging.info("✅ Model loaded successfully!")
77-
78-
# 2. Define your video processing function
79-
async def apply_green_tint(frame: VideoFrame) -> VideoFrame:
80-
"""Apply a simple green tint effect to the video."""
81-
tensor = frame.tensor.clone()
82-
tensor[1] = tensor[1] * 2.0 # Increase green channel
83-
return frame.replace_tensor(tensor)
84-
85-
# 3. Define a function to handle real-time parameter updates
86-
async def handle_parameter_updates(params: dict):
87-
"""Handle incoming parameters to adjust processing in real-time."""
88-
logging.info(f"Parameters updated: {params}")
89-
90-
# 4. Create and run the StreamProcessor
91-
if __name__ == "__main__":
92-
processor = StreamProcessor(
93-
video_processor=apply_green_tint,
94-
model_loader=load_my_model,
95-
param_updater=handle_parameter_updates,
60+
from pytrickle import FrameProcessor, StreamServer
61+
from pytrickle.frames import VideoFrame, AudioFrame
62+
from typing import Optional, List
63+
64+
class MyProcessor(FrameProcessor):
65+
"""Custom video processor with real-time parameter updates."""
66+
67+
def __init__(self, intensity: float = 0.5, **kwargs):
68+
super().__init__(**kwargs)
69+
self.intensity = intensity
70+
self.ready = False
71+
72+
async def initialize(self):
73+
"""Initialize and warm up the processor."""
74+
# Load your AI model or initialize processing here
75+
self.ready = True
76+
77+
async def process_video_async(self, frame: VideoFrame) -> Optional[VideoFrame]:
78+
"""Process video frame asynchronously."""
79+
if not self.ready:
80+
return frame
81+
82+
# Your processing logic here
83+
tensor = frame.tensor.clone()
84+
# Apply effects, AI models, filters, etc.
85+
86+
return frame.replace_tensor(tensor)
87+
88+
async def process_audio_async(self, frame: AudioFrame) -> Optional[List[AudioFrame]]:
89+
"""Process audio frame asynchronously."""
90+
return [frame] # Pass through or process
91+
92+
def update_params(self, params: dict):
93+
"""Update processing parameters in real-time."""
94+
if "intensity" in params:
95+
self.intensity = float(params["intensity"])
96+
97+
async def main():
98+
# Create and initialize processor
99+
processor = MyProcessor(intensity=0.5)
100+
await processor.start()
101+
102+
# Create app with processor
103+
app = StreamServer(
104+
frame_processor=processor,
96105
port=8000,
97-
name="green-tint-processor"
106+
capability_name="my-video-processor"
98107
)
99-
processor.run()
108+
await app.run_forever()
100109
```
101110

102-
## Model Loading
103-
104-
PyTrickle features a non-blocking model loading mechanism that allows the server to start immediately and be available for health checks while your model loads in the background.
105-
106-
- **Non-Blocking Startup**: The server starts instantly and responds to `/health` requests with a `LOADING` status.
107-
- **Automatic Loading**: Your `model_loader` function is triggered automatically after startup.
108-
- **State Transition**: Once your `model_loader` completes, the server's health status transitions from `LOADING` to `IDLE`, indicating it's ready to process streams.
109-
110-
This robust pattern is ideal for managed environments (like Docker or Kubernetes) where immediate health checks are critical for service orchestration. It is the recommended way to handle model initialization.
111+
For a complete working example with green tint processing, see [`examples/process_video_example.py`](examples/process_video_example.py) and [`examples/model_loading_example.py`](examples/model_loading_example.py).
111112

112113
## HTTP API
113114

0 commit comments

Comments
 (0)