diff --git a/docs/features/camera-integration.mdx b/docs/features/camera-integration.mdx new file mode 100644 index 000000000..b63f1dc4f --- /dev/null +++ b/docs/features/camera-integration.mdx @@ -0,0 +1,423 @@ +--- +title: Camera Integration +description: How to integrate camera feeds with PraisonAI multimodal agents for real-time visual analysis +--- + +# Camera Integration + +PraisonAI supports camera integration for real-time visual analysis through multimodal agents. While there's no built-in camera capture, you can easily integrate camera feeds by capturing frames or videos and passing them to vision agents. + +## Overview + +Camera integration works by: +1. **Capturing frames/videos** from camera using OpenCV +2. **Saving temporarily** to disk +3. **Passing file paths** to agents via the `images` parameter +4. **Cleaning up** temporary files after analysis + +## Quick Start + +### Prerequisites + +```bash +pip install praisonaiagents opencv-python +export OPENAI_API_KEY=your_openai_api_key +``` + +### Basic Camera Capture + +```python +import cv2 +from praisonaiagents import Agent, Task, PraisonAIAgents + +def capture_and_analyze(): + # Create vision agent + vision_agent = Agent( + name="CameraAnalyst", + role="Camera Feed Analyzer", + goal="Analyze camera captures in real-time", + backstory="Expert in real-time visual analysis", + llm="gpt-4o-mini" + ) + + # Capture from camera + cap = cv2.VideoCapture(0) # 0 for default camera + ret, frame = cap.read() + + if ret: + # Save frame temporarily + cv2.imwrite("temp_capture.jpg", frame) + cap.release() + + # Create analysis task + task = Task( + description="Analyze what you see in this camera feed", + expected_output="Detailed analysis of camera content", + agent=vision_agent, + images=["temp_capture.jpg"] + ) + + # Run analysis + agents = PraisonAIAgents( + agents=[vision_agent], + tasks=[task] + ) + + return agents.start() + +# Run analysis +result = capture_and_analyze() +``` + +## Integration Patterns + +### 1. Single Frame Analysis + +Perfect for quick snapshots and one-time analysis: + +```python +def single_frame_analysis(): + cap = cv2.VideoCapture(0) + ret, frame = cap.read() + + if ret: + image_path = "snapshot.jpg" + cv2.imwrite(image_path, frame) + + # Analyze with your agent + # ... (agent setup and task creation) + + cap.release() +``` + +### 2. Continuous Monitoring + +Ideal for security systems and real-time monitoring: + +```python +def continuous_monitoring(interval=10): + vision_agent = Agent( + name="SecurityMonitor", + role="Security Camera Analyst", + goal="Monitor for security events", + backstory="Expert security analyst", + llm="gpt-4o-mini" + ) + + cap = cv2.VideoCapture(0) + + while True: + ret, frame = cap.read() + if ret: + timestamp = int(time.time()) + filename = f"capture_{timestamp}.jpg" + cv2.imwrite(filename, frame) + + # Analyze frame + task = Task( + description="Monitor for unusual activities", + agent=vision_agent, + images=[filename] + ) + + agents = PraisonAIAgents( + agents=[vision_agent], + tasks=[task] + ) + + result = agents.start() + print(f"Analysis: {result}") + + time.sleep(interval) +``` + +### 3. Multi-Agent Analysis + +Use multiple specialized agents for comprehensive analysis: + +```python +def multi_agent_camera_analysis(): + # Create specialized agents + security_agent = Agent( + name="SecurityExpert", + role="Security Specialist", + goal="Identify security threats", + backstory="Expert in surveillance and threat detection", + llm="gpt-4o-mini" + ) + + object_detector = Agent( + name="ObjectDetector", + role="Object Recognition Specialist", + goal="Identify and catalog objects", + backstory="Computer vision expert", + llm="gpt-4o-mini" + ) + + scene_analyst = Agent( + name="SceneAnalyst", + role="Scene Understanding Expert", + goal="Provide comprehensive scene analysis", + backstory="Environmental analysis expert", + llm="gpt-4o-mini" + ) + + # Capture frame + cap = cv2.VideoCapture(0) + ret, frame = cap.read() + cap.release() + + if ret: + cv2.imwrite("analysis_frame.jpg", frame) + + # Create specialized tasks + security_task = Task( + description="Analyze for security threats", + agent=security_agent, + images=["analysis_frame.jpg"] + ) + + object_task = Task( + description="Identify all objects in scene", + agent=object_detector, + images=["analysis_frame.jpg"] + ) + + scene_task = Task( + description="Provide comprehensive scene analysis", + agent=scene_analyst, + images=["analysis_frame.jpg"] + ) + + # Run parallel analysis + agents = PraisonAIAgents( + agents=[security_agent, object_detector, scene_analyst], + tasks=[security_task, object_task, scene_task], + process="parallel" + ) + + return agents.start() +``` + +### 4. Video Recording & Analysis + +Analyze temporal events and activities: + +```python +def record_and_analyze_video(duration=10): + cap = cv2.VideoCapture(0) + + # Setup video writer + fps = int(cap.get(cv2.CAP_PROP_FPS)) or 30 + width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) + height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) + + fourcc = cv2.VideoWriter_fourcc(*'mp4v') + out = cv2.VideoWriter('recording.mp4', fourcc, fps, (width, height)) + + # Record video + start_time = time.time() + while (time.time() - start_time) < duration: + ret, frame = cap.read() + if ret: + out.write(frame) + + cap.release() + out.release() + + # Analyze video + video_agent = Agent( + name="VideoAnalyst", + role="Video Content Analyzer", + goal="Analyze video content for activities", + backstory="Expert in video analysis", + llm="gpt-4o-mini" + ) + + task = Task( + description="Analyze this video for activities and events", + agent=video_agent, + images=["recording.mp4"] # Video files work too! + ) + + agents = PraisonAIAgents( + agents=[video_agent], + tasks=[task] + ) + + return agents.start() +``` + +## Supported Input Types + +PraisonAI accepts various visual input formats: + +- āœ… **Local Images**: `"camera_shot.jpg"`, `"webcam_capture.png"` +- āœ… **Local Videos**: `"security_feed.mp4"`, `"recording.avi"` +- āœ… **Image URLs**: `"https://example.com/live_feed.jpg"` +- āœ… **Multiple Sources**: `["cam1.jpg", "cam2.jpg", "video.mp4"]` + +## Configuration Options + +### Camera Selection + +```python +# Different camera sources +camera_id = 0 # Default camera +camera_id = 1 # External USB camera +camera_id = "rtsp://camera-ip/stream" # IP camera +``` + +### Analysis Intervals + +```python +# For continuous monitoring +analysis_interval = 5 # Every 5 seconds (high frequency) +analysis_interval = 30 # Every 30 seconds (moderate) +analysis_interval = 300 # Every 5 minutes (low frequency) +``` + +### Video Recording + +```python +# Recording duration +duration = 10 # 10 seconds +duration = 60 # 1 minute +duration = 300 # 5 minutes +``` + +## Use Cases + +### Security Monitoring +- Real-time threat detection +- Unauthorized access alerts +- Suspicious activity identification +- Perimeter monitoring + +### Retail Analytics +- Customer behavior analysis +- Inventory monitoring +- Queue management +- Theft prevention + +### Industrial Automation +- Quality control inspection +- Safety compliance monitoring +- Equipment status verification +- Process optimization + +### Smart Home +- Activity recognition +- Elderly care monitoring +- Pet monitoring +- Energy usage optimization + +## Best Practices + +### Performance Optimization + +1. **Frame Rate Management** + ```python + # Reduce analysis frequency for better performance + time.sleep(5) # Analyze every 5 seconds instead of continuously + ``` + +2. **Image Size Optimization** + ```python + # Resize frames for faster processing + frame = cv2.resize(frame, (640, 480)) + ``` + +3. **Parallel Processing** + ```python + # Use parallel process for multiple agents + process="parallel" + ``` + +### Memory Management + +1. **Clean Up Temporary Files** + ```python + import os + if os.path.exists(image_path): + os.remove(image_path) + ``` + +2. **Limit Video Duration** + ```python + # Keep recordings short to avoid large files + max_duration = 30 # seconds + ``` + +### Security Considerations + +1. **Camera Permissions** + - Ensure application has camera access + - Handle permission errors gracefully + +2. **Privacy Protection** + - Implement data retention policies + - Secure transmission of camera data + - User consent for recording + +3. **Access Control** + - Authenticate camera access + - Implement role-based permissions + +## Troubleshooting + +### Camera Not Found + +```python +# Check available cameras +for i in range(4): + cap = cv2.VideoCapture(i) + if cap.isOpened(): + print(f"Camera {i} is available") + cap.release() + else: + print(f"Camera {i} not available") +``` + +### Permission Issues + +**Linux:** +```bash +# Add user to video group +sudo usermod -a -G video $USER +``` + +**macOS:** +- Grant camera permissions in System Preferences > Security & Privacy + +**Windows:** +- Check camera privacy settings in Windows Settings + +### Performance Issues + +```python +# Reduce frame size +frame = cv2.resize(frame, (320, 240)) + +# Lower analysis frequency +time.sleep(10) # Analyze every 10 seconds + +# Use threading for non-blocking capture +import threading +``` + +## Examples + +Complete working examples are available in the repository: + +- [`camera-basic.py`](../../examples/python/camera/camera-basic.py) - Basic single frame capture +- [`camera-continuous.py`](../../examples/python/camera/camera-continuous.py) - Continuous monitoring +- [`camera-multi-agent.py`](../../examples/python/camera/camera-multi-agent.py) - Multi-agent analysis +- [`camera-video-analysis.py`](../../examples/python/camera/camera-video-analysis.py) - Video recording & analysis + +## Related Features + +- [Multimodal Agents](./multimodal) - Core multimodal capabilities +- [Image Generation](./image-generation) - Creating images with AI +- [Agent Memory](../concepts/memory) - Persistent agent memory +- [Task Management](../concepts/tasks) - Task configuration and execution \ No newline at end of file diff --git a/docs/mint.json b/docs/mint.json index 0c86c2d9f..5bfb22eb6 100644 --- a/docs/mint.json +++ b/docs/mint.json @@ -143,7 +143,8 @@ "features/mathagent", "features/structured", "features/callbacks", - "features/chat-with-pdf" + "features/chat-with-pdf", + "features/camera-integration" ] }, { diff --git a/examples/python/camera/README.md b/examples/python/camera/README.md new file mode 100644 index 000000000..adcfe4a64 --- /dev/null +++ b/examples/python/camera/README.md @@ -0,0 +1,173 @@ +# Camera Integration Examples + +This directory contains examples demonstrating how to integrate camera feeds with PraisonAI multimodal agents for real-time visual analysis. + +## šŸ“‹ Overview + +PraisonAI supports visual input through the `images` parameter in Tasks. While there's no built-in camera capture, you can integrate camera feeds by capturing frames/videos and passing them to vision agents. + +## šŸ› ļø Setup + +### Requirements +```bash +pip install praisonaiagents opencv-python +``` + +### Environment Variables +```bash +export OPENAI_API_KEY=your_openai_api_key +``` + +## šŸ“ Examples + +### 1. `camera-basic.py` - Basic Camera Capture +- **Purpose**: Capture a single frame and analyze it +- **Use Case**: Quick analysis, testing camera setup +- **Features**: + - Single frame capture + - Basic object and scene analysis + - Automatic cleanup + +**Usage:** +```bash +python camera-basic.py +``` + +### 2. `camera-continuous.py` - Continuous Monitoring +- **Purpose**: Continuous camera monitoring with periodic analysis +- **Use Case**: Security monitoring, surveillance systems +- **Features**: + - Configurable analysis intervals + - Real-time monitoring + - Security-focused analysis + - Graceful shutdown with Ctrl+C + +**Usage:** +```bash +python camera-continuous.py +``` + +### 3. `camera-multi-agent.py` - Multi-Agent Analysis +- **Purpose**: Multiple specialized agents analyzing the same camera feed +- **Use Case**: Comprehensive analysis from different perspectives +- **Features**: + - Security analysis agent + - Object detection agent + - Scene analysis agent + - Parallel processing + +**Usage:** +```bash +python camera-multi-agent.py +``` + +### 4. `camera-video-analysis.py` - Video Recording & Analysis +- **Purpose**: Record video segments and analyze temporal events +- **Use Case**: Activity analysis, event detection +- **Features**: + - Video recording with configurable duration + - Temporal event analysis + - Timeline extraction + - Automatic cleanup + +**Usage:** +```bash +python camera-video-analysis.py +``` + +## šŸŽÆ Supported Input Types + +- āœ… **Local Images**: `"camera_shot.jpg"`, `"webcam_capture.png"` +- āœ… **Local Videos**: `"security_feed.mp4"`, `"recording.avi"` +- āœ… **Image URLs**: `"https://example.com/live_feed.jpg"` +- āœ… **Multiple Sources**: `["cam1.jpg", "cam2.jpg", "video.mp4"]` + +## šŸ”§ Configuration Options + +### Camera Settings +```python +camera_id = 0 # Default camera (change to 1, 2, etc. for other cameras) +``` + +### Analysis Intervals +```python +analysis_interval = 10 # Seconds between analyses for continuous monitoring +``` + +### Recording Duration +```python +recording_duration = 15 # Seconds for video recording +``` + +## šŸ’” Integration Patterns + +### 1. Real-time Processing +```python +# Capture frames periodically and process them as separate tasks +def capture_and_analyze(): + cap = cv2.VideoCapture(0) + ret, frame = cap.read() + if ret: + cv2.imwrite("temp_capture.jpg", frame) + # Process with PraisonAI agent + cap.release() +``` + +### 2. Batch Analysis +```python +# Process multiple camera angles simultaneously +task = Task( + description="Analyze multiple camera feeds", + agent=vision_agent, + images=["cam1.jpg", "cam2.jpg", "cam3.jpg"] +) +``` + +### 3. Video Analysis +```python +# Save video segments and analyze them +task = Task( + description="Analyze this video for activities", + agent=vision_agent, + images=["security_footage.mp4"] # Video files work too +) +``` + +## šŸ”’ Security Considerations + +- **Camera Permissions**: Ensure your application has camera access +- **Privacy**: Be mindful of privacy when recording/analyzing +- **Storage**: Clean up temporary files to avoid storage issues +- **Access Control**: Implement proper access controls for camera systems + +## šŸ› Troubleshooting + +### Camera Not Found +```python +# Check available cameras +for i in range(4): # Check first 4 camera indices + cap = cv2.VideoCapture(i) + if cap.isOpened(): + print(f"Camera {i} is available") + cap.release() +``` + +### Permission Issues +- On Linux: Add user to `video` group +- On macOS: Grant camera permissions in System Preferences +- On Windows: Check camera privacy settings + +### Performance Optimization +- Reduce frame size for faster processing +- Adjust analysis intervals based on requirements +- Use parallel processing for multiple agents + +## šŸ“š Related Documentation + +- [Multimodal Features](../../docs/features/multimodal.mdx) +- [Agent Documentation](../../docs/concepts/agents.mdx) +- [Task Documentation](../../docs/concepts/tasks.mdx) + +## šŸ¤ Contributing + +Feel free to contribute additional camera integration examples or improvements to existing ones! \ No newline at end of file diff --git a/examples/python/camera/camera-basic.py b/examples/python/camera/camera-basic.py new file mode 100644 index 000000000..f6f2e50d3 --- /dev/null +++ b/examples/python/camera/camera-basic.py @@ -0,0 +1,102 @@ +""" +Basic Camera Integration Example + +This example demonstrates how to capture a single frame from a camera +and analyze it using a PraisonAI vision agent. + +Requirements: + pip install praisonaiagents opencv-python + +Usage: + python camera-basic.py + +Environment Variables: + OPENAI_API_KEY=your_openai_api_key +""" + +import cv2 +import os +from praisonaiagents import Agent, Task, PraisonAIAgents + + +def capture_and_analyze(): + """Capture a single frame from camera and analyze it""" + + # Create vision agent + vision_agent = Agent( + name="CameraAnalyst", + role="Camera Feed Analyzer", + goal="Analyze camera captures in real-time", + backstory="Expert in real-time visual analysis with computer vision skills", + llm="gpt-4o-mini" + ) + + # Initialize camera (0 for default camera) + cap = cv2.VideoCapture(0) + + if not cap.isOpened(): + print("Error: Could not open camera") + return None + + try: + # Capture frame + ret, frame = cap.read() + + if ret: + # Save frame temporarily + image_path = "temp_camera_capture.jpg" + cv2.imwrite(image_path, frame) + print(f"Frame captured and saved as {image_path}") + + # Create analysis task + task = Task( + name="analyze_camera_feed", + description="Analyze what you see in this camera capture. Identify objects, people, and activities.", + expected_output="Detailed analysis of the camera content including objects, people, activities, and overall scene description", + agent=vision_agent, + images=[image_path] + ) + + # Run analysis + agents = PraisonAIAgents( + agents=[vision_agent], + tasks=[task], + process="sequential", + verbose=1 + ) + + result = agents.start() + + # Clean up temporary file + if os.path.exists(image_path): + os.remove(image_path) + + return result + + else: + print("Error: Could not capture frame") + return None + + finally: + cap.release() + cv2.destroyAllWindows() + + +def main(): + """Main function""" + print("šŸ“ø Starting basic camera analysis...") + print("Make sure your camera is connected and accessible") + + result = capture_and_analyze() + + if result: + print("\nšŸ” Analysis Results:") + for task_id, task_result in result["task_results"].items(): + print(f"\nTask {task_id}:") + print(task_result.raw) + else: + print("āŒ Failed to capture and analyze camera feed") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/examples/python/camera/camera-continuous.py b/examples/python/camera/camera-continuous.py new file mode 100644 index 000000000..5dcf929d7 --- /dev/null +++ b/examples/python/camera/camera-continuous.py @@ -0,0 +1,142 @@ +""" +Continuous Camera Monitoring Example + +This example demonstrates continuous camera monitoring with periodic analysis +using PraisonAI vision agents. Perfect for security monitoring or real-time surveillance. + +Requirements: + pip install praisonaiagents opencv-python + +Usage: + python camera-continuous.py + +Environment Variables: + OPENAI_API_KEY=your_openai_api_key +""" + +import cv2 +import time +import os +from datetime import datetime +from praisonaiagents import Agent, Task, PraisonAIAgents + + +class ContinuousCameraMonitor: + """Continuous camera monitoring system""" + + def __init__(self, camera_id=0, analysis_interval=10): + """ + Initialize camera monitor + + Args: + camera_id (int): Camera ID (0 for default) + analysis_interval (int): Seconds between analyses + """ + self.camera_id = camera_id + self.analysis_interval = analysis_interval + self.running = False + + # Create vision agent + self.vision_agent = Agent( + name="SecurityMonitor", + role="Security Camera Analyst", + goal="Monitor camera feeds for activities and security events", + backstory="Expert security analyst with computer vision skills specialized in real-time monitoring", + llm="gpt-4o-mini" + ) + + def capture_frame(self, cap): + """Capture a single frame from camera""" + ret, frame = cap.read() + if ret: + timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") + filename = f"camera_capture_{timestamp}.jpg" + cv2.imwrite(filename, frame) + return filename + return None + + def analyze_frame(self, image_path): + """Analyze captured frame""" + task = Task( + name="monitor_camera_feed", + description="""Monitor this camera feed for: + 1. Any people or unusual activities + 2. Objects that seem out of place + 3. Movement or changes in the scene + 4. Security-relevant events + + Provide a brief but detailed security report.""", + expected_output="Security analysis report with timestamp and observations", + agent=self.vision_agent, + images=[image_path] + ) + + agents = PraisonAIAgents( + agents=[self.vision_agent], + tasks=[task], + process="sequential", + verbose=0 # Reduced verbosity for continuous monitoring + ) + + return agents.start() + + def start_monitoring(self): + """Start continuous monitoring""" + print(f"šŸŽ„ Starting continuous camera monitoring...") + print(f"šŸ“Š Analysis interval: {self.analysis_interval} seconds") + print("šŸ›‘ Press Ctrl+C to stop") + + cap = cv2.VideoCapture(self.camera_id) + + if not cap.isOpened(): + print(f"āŒ Error: Could not open camera {self.camera_id}") + return + + self.running = True + + try: + while self.running: + # Capture frame + image_path = self.capture_frame(cap) + + if image_path: + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + print(f"\nā° {timestamp} - Analyzing camera feed...") + + # Analyze frame + result = self.analyze_frame(image_path) + + # Display results + if result and "task_results" in result: + for task_id, task_result in result["task_results"].items(): + print(f"šŸ“‹ Security Report: {task_result.raw}") + + # Clean up temporary file + if os.path.exists(image_path): + os.remove(image_path) + + # Wait for next analysis + time.sleep(self.analysis_interval) + + except KeyboardInterrupt: + print("\nšŸ›‘ Stopping camera monitoring...") + finally: + self.running = False + cap.release() + cv2.destroyAllWindows() + print("āœ… Camera monitoring stopped") + + +def main(): + """Main function""" + # Configuration + camera_id = 0 # Default camera + analysis_interval = 15 # Analyze every 15 seconds + + # Create and start monitor + monitor = ContinuousCameraMonitor(camera_id, analysis_interval) + monitor.start_monitoring() + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/examples/python/camera/camera-multi-agent.py b/examples/python/camera/camera-multi-agent.py new file mode 100644 index 000000000..e8b4a4891 --- /dev/null +++ b/examples/python/camera/camera-multi-agent.py @@ -0,0 +1,164 @@ +""" +Multi-Agent Camera Analysis Example + +This example demonstrates using multiple specialized agents to analyze +camera feeds from different perspectives (security, object detection, etc.). + +Requirements: + pip install praisonaiagents opencv-python + +Usage: + python camera-multi-agent.py + +Environment Variables: + OPENAI_API_KEY=your_openai_api_key +""" + +import cv2 +import os +from datetime import datetime +from praisonaiagents import Agent, Task, PraisonAIAgents + + +def capture_camera_frame(camera_id=0): + """Capture a frame from camera""" + cap = cv2.VideoCapture(camera_id) + + if not cap.isOpened(): + print(f"Error: Could not open camera {camera_id}") + return None + + ret, frame = cap.read() + cap.release() + + if ret: + timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") + image_path = f"camera_frame_{timestamp}.jpg" + cv2.imwrite(image_path, frame) + return image_path + + return None + + +def analyze_with_multiple_agents(): + """Analyze camera feed using multiple specialized agents""" + + # Capture frame + image_path = capture_camera_frame() + if not image_path: + print("Failed to capture camera frame") + return None + + print(f"šŸ“ø Captured frame: {image_path}") + + # Create specialized agents + security_agent = Agent( + name="SecurityExpert", + role="Security Specialist", + goal="Identify security threats and suspicious activities", + backstory="Expert security analyst with years of experience in surveillance and threat detection", + llm="gpt-4o-mini" + ) + + object_detector = Agent( + name="ObjectDetector", + role="Object Recognition Specialist", + goal="Identify and catalog all objects in the scene", + backstory="Computer vision expert specializing in object detection and classification", + llm="gpt-4o-mini" + ) + + scene_analyst = Agent( + name="SceneAnalyst", + role="Scene Understanding Expert", + goal="Provide comprehensive scene analysis and context", + backstory="Environmental analyst expert in understanding spatial relationships and scene context", + llm="gpt-4o-mini" + ) + + # Create specialized tasks + security_task = Task( + name="security_analysis", + description="""Analyze this camera feed for security concerns: + 1. Identify any people and their activities + 2. Look for suspicious behavior or unauthorized access + 3. Check for potential security threats + 4. Assess overall security status of the area""", + expected_output="Detailed security assessment with threat level and recommendations", + agent=security_agent, + images=[image_path] + ) + + object_task = Task( + name="object_detection", + description="""Identify and catalog all objects in this image: + 1. List all visible objects with their approximate locations + 2. Identify any vehicles, electronics, or valuable items + 3. Note any objects that seem out of place + 4. Provide a comprehensive inventory""", + expected_output="Detailed object inventory with locations and descriptions", + agent=object_detector, + images=[image_path] + ) + + scene_task = Task( + name="scene_analysis", + description="""Provide comprehensive scene analysis: + 1. Describe the overall environment and setting + 2. Analyze lighting conditions and time of day + 3. Assess the general activity level and atmosphere + 4. Provide context about what type of location this appears to be""", + expected_output="Complete scene description with environmental context", + agent=scene_analyst, + images=[image_path] + ) + + # Run multi-agent analysis + agents = PraisonAIAgents( + agents=[security_agent, object_detector, scene_analyst], + tasks=[security_task, object_task, scene_task], + process="parallel", # Run all analyses in parallel + verbose=1 + ) + + result = agents.start() + + # Clean up temporary file + if os.path.exists(image_path): + os.remove(image_path) + + return result + + +def main(): + """Main function""" + print("šŸŽÆ Starting multi-agent camera analysis...") + print("šŸ“‹ This will analyze the camera feed from multiple perspectives") + + result = analyze_with_multiple_agents() + + if result: + print("\n" + "="*60) + print("šŸ“Š MULTI-AGENT ANALYSIS RESULTS") + print("="*60) + + # Display results by agent + agent_names = { + "security_analysis": "šŸ›”ļø SECURITY ANALYSIS", + "object_detection": "šŸ” OBJECT DETECTION", + "scene_analysis": "šŸŒ SCENE ANALYSIS" + } + + for task_id, task_result in result["task_results"].items(): + print(f"\n{agent_names.get(task_id, f'Task {task_id}')}:") + print("-" * 40) + print(task_result.raw) + + print("\n" + "="*60) + print("āœ… Multi-agent analysis complete!") + else: + print("āŒ Failed to complete camera analysis") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/examples/python/camera/camera-video-analysis.py b/examples/python/camera/camera-video-analysis.py new file mode 100644 index 000000000..2742d3e5c --- /dev/null +++ b/examples/python/camera/camera-video-analysis.py @@ -0,0 +1,206 @@ +""" +Camera Video Recording and Analysis Example + +This example demonstrates recording video segments from camera +and analyzing them using PraisonAI vision agents. + +Requirements: + pip install praisonaiagents opencv-python + +Usage: + python camera-video-analysis.py + +Environment Variables: + OPENAI_API_KEY=your_openai_api_key +""" + +import cv2 +import os +import time +from datetime import datetime +from praisonaiagents import Agent, Task, PraisonAIAgents + + +class CameraVideoAnalyzer: + """Camera video recording and analysis system""" + + def __init__(self, camera_id=0): + """ + Initialize video analyzer + + Args: + camera_id (int): Camera ID (0 for default) + """ + self.camera_id = camera_id + + # Create video analysis agent + self.video_agent = Agent( + name="VideoAnalyst", + role="Video Content Analyzer", + goal="Analyze video content for activities, objects, and events", + backstory="Expert in video analysis and temporal event detection with computer vision expertise", + llm="gpt-4o-mini" + ) + + def record_video_segment(self, duration_seconds=10): + """ + Record a video segment from camera + + Args: + duration_seconds (int): Length of video to record + + Returns: + str: Path to recorded video file or None if failed + """ + cap = cv2.VideoCapture(self.camera_id) + + if not cap.isOpened(): + print(f"Error: Could not open camera {self.camera_id}") + return None + + # Get video properties + fps = int(cap.get(cv2.CAP_PROP_FPS)) or 30 + width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) + height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) + + # Setup video writer + timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") + video_path = f"camera_recording_{timestamp}.mp4" + + fourcc = cv2.VideoWriter_fourcc(*'mp4v') + out = cv2.VideoWriter(video_path, fourcc, fps, (width, height)) + + print(f"šŸŽ¬ Recording video for {duration_seconds} seconds...") + print(f"šŸ“¹ Resolution: {width}x{height}, FPS: {fps}") + + start_time = time.time() + frame_count = 0 + + try: + while (time.time() - start_time) < duration_seconds: + ret, frame = cap.read() + + if ret: + out.write(frame) + frame_count += 1 + + # Show recording indicator + if frame_count % fps == 0: # Update every second + elapsed = int(time.time() - start_time) + remaining = duration_seconds - elapsed + print(f"ā±ļø Recording... {remaining}s remaining") + else: + break + + finally: + cap.release() + out.release() + cv2.destroyAllWindows() + + if frame_count > 0: + print(f"āœ… Video recorded: {video_path} ({frame_count} frames)") + return video_path + else: + print("āŒ Failed to record video") + if os.path.exists(video_path): + os.remove(video_path) + return None + + def analyze_video(self, video_path): + """ + Analyze recorded video + + Args: + video_path (str): Path to video file + + Returns: + dict: Analysis results + """ + task = Task( + name="analyze_video_content", + description="""Analyze this video recording and provide: + 1. Summary of main activities and events observed + 2. Timeline of key moments or changes + 3. Identification of people, objects, and movements + 4. Any notable interactions or behaviors + 5. Overall assessment of what happened in the video + + Focus on providing a comprehensive temporal analysis.""", + expected_output="Detailed video analysis with timeline and event description", + agent=self.video_agent, + images=[video_path] # PraisonAI supports video files in images parameter + ) + + agents = PraisonAIAgents( + agents=[self.video_agent], + tasks=[task], + process="sequential", + verbose=1 + ) + + return agents.start() + + def record_and_analyze(self, duration=10): + """ + Record video and analyze it + + Args: + duration (int): Recording duration in seconds + """ + # Record video + video_path = self.record_video_segment(duration) + + if not video_path: + return None + + print(f"\nšŸ” Analyzing recorded video...") + + # Analyze video + result = self.analyze_video(video_path) + + # Clean up video file + if os.path.exists(video_path): + os.remove(video_path) + print(f"šŸ—‘ļø Cleaned up temporary video file") + + return result + + +def main(): + """Main function""" + print("šŸŽ„ Camera Video Recording and Analysis") + print("=" * 50) + + # Configuration + camera_id = 0 + recording_duration = 15 # seconds + + print(f"šŸ“¹ Camera: {camera_id}") + print(f"ā±ļø Recording Duration: {recording_duration} seconds") + print("\nMake sure your camera is connected and accessible") + print("The system will record a video segment and then analyze it") + + input("\nPress Enter to start recording...") + + # Create analyzer and run + analyzer = CameraVideoAnalyzer(camera_id) + result = analyzer.record_and_analyze(recording_duration) + + if result: + print("\n" + "="*60) + print("šŸ“Š VIDEO ANALYSIS RESULTS") + print("="*60) + + for task_id, task_result in result["task_results"].items(): + print(f"\nTask: {task_id}") + print("-" * 40) + print(task_result.raw) + + print("\n" + "="*60) + print("āœ… Video analysis complete!") + else: + print("āŒ Failed to record and analyze video") + + +if __name__ == "__main__": + main() \ No newline at end of file