-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoice_operated_detect_object.py
More file actions
56 lines (46 loc) · 1.83 KB
/
Copy pathvoice_operated_detect_object.py
File metadata and controls
56 lines (46 loc) · 1.83 KB
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
import os
from fastrtc import (ReplyOnPause, Stream, get_stt_model, get_tts_model)
from fastrtc.tracks import StreamHandlerBase
import cv2
import gradio as gr
from openai import OpenAI
import numpy as np
openrouter_client = OpenAI(api_key=os.getenv("OPENROUTER_API_KEY"), base_url="https://openrouter.ai/api/v1")
model="google/gemini-2.0-flash-thinking-exp"
stt_model = get_stt_model()
# Define a class that inherits from StreamHandlerBase
class MyStreamHandler(StreamHandlerBase):
def __init__(self, stt_model):
super().__init__()
self.stt_model = stt_model
def process_stream(self, audio=None, video_frame=None):
if audio:
# Process voice commands
prompt = self.stt_model.stt(audio)
if "detect" in prompt.lower():
# Activate detection mode
return self.process_detection(video_frame)
elif "track" in prompt.lower():
# Activate tracking mode
return self.process_tracking(video_frame)
return video_frame
def process_detection(self, frame):
# Convert to grayscale and detect edges
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150)
return cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
def process_tracking(self, frame):
# Placeholder for tracking logic
return frame # Replace with actual tracking implementation
# Instantiate the handler
stream_handler = MyStreamHandler(stt_model)
stream = Stream(
handler=stream_handler, # Pass the stream_handler object
modality="audio-video",
mode="send-receive",
additional_inputs=[
gr.Slider(minimum=50, maximum=150, value=100, label="Detection Sensitivity"),
gr.Checkbox(label="Enable Voice Commands", value=True)
]
)
stream.ui.launch()