livekit / python-sdks

LiveKit real-time and server SDKs for Python

Home Page:https://docs.livekit.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Video Quality Appears Noticeably Blurry When Using Python SDK

chengch1016 opened this issue · comments

I successfully uploaded my video stream using the Python SDK on my PC.

I utilized https://meet.livekit.io/?tab=custom for frontend display. Simultaneously, locally, I displayed real-time images from my camera using OpenCV. The local image resolution is 720*1280. However, on https://meet.livekit.io/?tab=custom, the video appears noticeably blurry.

Could you please advise on potential reasons? Is it possible that I haven't configured the settings correctly in my code?

Below is my code (I've omitted the token due to security reasons):

import asyncio
import logging
from signal import SIGINT, SIGTERM

import cv2
import numpy as np
from livekit import rtc

URL = "ws://localhost"
TOKEN = ""


async def publish_camera_video_stream(source: rtc.VideoSource):
    argb_frame = None
    arr = None

    cap = cv2.VideoCapture(0)

    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

    while True:
        ret, frame = cap.read()

        if not ret:
            continue

        logging.info(f"frame type: {type(frame)}")
        logging.info(f"frame shape: {frame.shape}")

        if frame is not None and argb_frame is None and arr is None:
            height, width, _ = frame.shape
            logging.info(f"Height: {height}, Width: {width}")
            argb_frame = rtc.ArgbFrame.create(rtc.VideoFormatType.FORMAT_ARGB, width, height*2)
            arr = np.frombuffer(argb_frame.data, dtype=np.uint8)

        logging.info(f"argb_frame: {argb_frame.data.shape}")

        flipped_frame = cv2.flip(frame, 0)

        combined_frame = cv2.vconcat([frame, flipped_frame])

        cv2.imshow("Video Stream", combined_frame)

        arr.flat[::4] = combined_frame[:, :, 0].ravel()
        arr.flat[1::4] = combined_frame[:, :, 1].ravel()
        arr.flat[2::4] = combined_frame[:, :, 2].ravel()
        arr.flat[3::4] = 0

        video_frame = rtc.VideoFrame(
            0, rtc.VideoRotation.VIDEO_ROTATION_0, argb_frame.to_i420()
        )

        source.capture_frame(video_frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()


async def main(room: rtc.Room):
    logging.info("connecting to %s", URL)
    try:
        await room.connect(URL, TOKEN)
        logging.info("connected to room %s", room.name)
    except rtc.ConnectError as e:
        logging.error("failed to connect to the room: %s", e)
        return

    # publish a track
    source = rtc.VideoSource()
    track = rtc.LocalVideoTrack.create_video_track("hue", source)
    options = rtc.TrackPublishOptions()
    options.source = rtc.TrackSource.SOURCE_CAMERA
    publication = await room.local_participant.publish_track(track, options)
    logging.info("published track %s", publication.sid)

    asyncio.ensure_future(publish_camera_video_stream(source))


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

    loop = asyncio.get_event_loop()
    room = rtc.Room(loop=loop)

    async def cleanup():
        await room.disconnect()
        loop.stop()

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

    try:
        loop.run_forever()
    finally:
        loop.close()

I would greatly appreciate any insights or guidance to resolve this resolution issue during the video stream. Thank you for your assistance

Hey, I just reproduced the issue, thanks for the code example.
I'll release a fix today

Hey, this will be fixed by #88. It adds width and height arguments to the VideoSource