Skip to content

Commit

Permalink
update remaining examples
Browse files Browse the repository at this point in the history
  • Loading branch information
davidzhao committed Nov 11, 2023
1 parent f117e18 commit c31199b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 35 deletions.
24 changes: 17 additions & 7 deletions examples/basic_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
from signal import SIGINT, SIGTERM
from typing import Union

from livekit import rtc
from livekit import api, rtc

URL = "ws://localhost:7880"
TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE5MDY2MTMyODgsImlzcyI6IkFQSVRzRWZpZFpqclFvWSIsIm5hbWUiOiJuYXRpdmUiLCJuYmYiOjE2NzI2MTMyODgsInN1YiI6Im5hdGl2ZSIsInZpZGVvIjp7InJvb20iOiJ0ZXN0Iiwicm9vbUFkbWluIjp0cnVlLCJyb29tQ3JlYXRlIjp0cnVlLCJyb29tSm9pbiI6dHJ1ZSwicm9vbUxpc3QiOnRydWV9fQ.uSNIangMRu8jZD5mnRYoCHjcsQWCrJXgHCs0aNIgBFY" # noqa
# ensure LIVEKIT_URL, LIVEKIT_API_KEY, and LIVEKIT_API_SECRET are set


async def main(room: rtc.Room) -> None:
Expand Down Expand Up @@ -105,7 +104,8 @@ def on_connection_quality_changed(
def on_track_subscription_failed(
participant: rtc.RemoteParticipant, track_sid: str, error: str
):
logging.info("track subscription failed: %s %s", participant.identity, error)
logging.info("track subscription failed: %s %s",
participant.identity, error)

@room.on("connection_state_changed")
def on_connection_state_changed(state: rtc.ConnectionState):
Expand All @@ -127,7 +127,15 @@ def on_reconnecting() -> None:
def on_reconnected() -> None:
logging.info("reconnected")

await room.connect(URL, TOKEN)
info = api.ConnectionInfo()
token = api.AccessToken(info.api_key, info.api_secret) \
.with_identity("python-bot") \
.with_name("Python Bot") \
.with_grants(api.VideoGrants(
room_join=True,
room="my-room",
)).to_jwt()
await room.connect(info.websocket_url(), token)
logging.info("connected to room %s", room.name)
logging.info("participants: %s", room.participants)

Expand All @@ -138,7 +146,8 @@ def on_reconnected() -> None:
if __name__ == "__main__":
logging.basicConfig(
level=logging.INFO,
handlers=[logging.FileHandler("basic_room.log"), logging.StreamHandler()],
handlers=[logging.FileHandler(
"basic_room.log"), logging.StreamHandler()],
)

loop = asyncio.get_event_loop()
Expand All @@ -150,7 +159,8 @@ async def cleanup():

asyncio.ensure_future(main(room))
for signal in [SIGINT, SIGTERM]:
loop.add_signal_handler(signal, lambda: asyncio.ensure_future(cleanup()))
loop.add_signal_handler(
signal, lambda: asyncio.ensure_future(cleanup()))

try:
loop.run_forever()
Expand Down
65 changes: 37 additions & 28 deletions examples/face_landmark/face_landmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
from mediapipe import solutions
from mediapipe.framework.formats import landmark_pb2

from livekit import rtc
from livekit import api, rtc

URL = "ws://localhost:7880"
TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE5MDY2MTMyODgsImlzcyI6IkFQSVRzRWZpZFpqclFvWSIsIm5hbWUiOiJuYXRpdmUiLCJuYmYiOjE2NzI2MTMyODgsInN1YiI6Im5hdGl2ZSIsInZpZGVvIjp7InJvb20iOiJ0ZXN0Iiwicm9vbUFkbWluIjp0cnVlLCJyb29tQ3JlYXRlIjp0cnVlLCJyb29tSm9pbiI6dHJ1ZSwicm9vbUxpc3QiOnRydWV9fQ.uSNIangMRu8jZD5mnRYoCHjcsQWCrJXgHCs0aNIgBFY" # noqa
# ensure LIVEKIT_URL, LIVEKIT_API_KEY, and LIVEKIT_API_SECRET are set

tasks = set()

Expand All @@ -30,10 +29,38 @@
running_mode=VisionRunningMode.VIDEO,
)

# from https://github.com/googlesamples/mediapipe/blob/main/examples/face_landmarker/python/%5BMediaPipe_Python_Tasks%5D_Face_Landmarker.ipynb

async def main(room: rtc.Room) -> None:
video_stream = None

@room.on("track_subscribed")
def on_track_subscribed(track: rtc.Track, *_):
if track.kind == rtc.TrackKind.KIND_VIDEO:
nonlocal video_stream
if video_stream is not None:
# only process the first stream received
return

print("subscribed to track: " + track.name)
video_stream = rtc.VideoStream(track)
task = asyncio.create_task(frame_loop(video_stream))
tasks.add(task)
task.add_done_callback(tasks.remove)

info = api.ConnectionInfo()
token = api.AccessToken(info.api_key, info.api_secret) \
.with_identity("python-bot") \
.with_name("Python Bot") \
.with_grants(api.VideoGrants(
room_join=True,
room="my-room",
)).to_jwt()
await room.connect(info.websocket_url(), token)
print("connected to room: " + room.name)


def draw_landmarks_on_image(rgb_image, detection_result):
# from https://github.com/googlesamples/mediapipe/blob/main/examples/face_landmarker/python/%5BMediaPipe_Python_Tasks%5D_Face_Landmarker.ipynb
face_landmarks_list = detection_result.face_landmarks

# Loop through the detected faces to visualize.
Expand Down Expand Up @@ -97,7 +124,8 @@ async def frame_loop(video_stream: rtc.VideoStream) -> None:

mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=arr)

detection_result = landmarker.detect_for_video(mp_image, frame.timestamp_us)
detection_result = landmarker.detect_for_video(
mp_image, frame.timestamp_us)

draw_landmarks_on_image(arr, detection_result)

Expand All @@ -111,31 +139,11 @@ async def frame_loop(video_stream: rtc.VideoStream) -> None:
cv2.destroyAllWindows()


async def main(room: rtc.Room) -> None:
video_stream = None

@room.on("track_subscribed")
def on_track_subscribed(track: rtc.Track, *_):
if track.kind == rtc.TrackKind.KIND_VIDEO:
nonlocal video_stream
if video_stream is not None:
# only process the first stream received
return

print("subscribed to track: " + track.name)
video_stream = rtc.VideoStream(track)
task = asyncio.create_task(frame_loop(video_stream))
tasks.add(task)
task.add_done_callback(tasks.remove)

await room.connect(URL, TOKEN)
print("connected to room: " + room.name)


if __name__ == "__main__":
logging.basicConfig(
level=logging.INFO,
handlers=[logging.FileHandler("face_landmark.log"), logging.StreamHandler()],
handlers=[logging.FileHandler(
"face_landmark.log"), logging.StreamHandler()],
)

loop = asyncio.get_event_loop()
Expand All @@ -147,7 +155,8 @@ async def cleanup():

asyncio.ensure_future(main(room))
for signal in [SIGINT, SIGTERM]:
loop.add_signal_handler(signal, lambda: asyncio.ensure_future(cleanup()))
loop.add_signal_handler(
signal, lambda: asyncio.ensure_future(cleanup()))

try:
loop.run_forever()
Expand Down

0 comments on commit c31199b

Please sign in to comment.