Fix Live API video streaming in Get_started_LiveAPI quickstart#1165
Fix Live API video streaming in Get_started_LiveAPI quickstart#1165Kenxpx wants to merge 1 commit intogoogle-gemini:mainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses an issue in the Get_started_LiveAPI quickstart where the model would hallucinate visual content or fail to accurately describe screen shares. The changes improve the reliability of video streaming by updating the Live API input path to use the explicit "video" field, enhancing the real-time activity configuration for mixed audio/video sessions, and optimizing screen capture resolution. These adjustments aim to provide the model with more accurate and appropriately processed multimodal input, preventing misinterpretations. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request addresses an issue with video streaming in the Get_started_LiveAPI.py quickstart. The changes, which include updating the API call for sending video frames, adding a more forgiving realtime activity configuration, and resizing screen captures, are well-aligned with the goal of fixing visual content hallucination. The code is clearer and more correct. I've added one suggestion to improve the efficiency of the screen capture implementation by avoiding object re-initialization in a loop.
| image_io = io.BytesIO() | ||
| img.save(image_io, format="jpeg") | ||
| def _capture_screen(self): | ||
| sct = mss.mss() |
There was a problem hiding this comment.
Creating the mss.mss() object inside _capture_screen is inefficient as this method is called in a loop. It's recommended to initialize mss.mss() once and reuse the object.
A good approach would be to initialize it in the __init__ method of the AudioVideoLoop class and store it as an instance variable (e.g., self.sct). You could then use self.sct here.
Example of changes:
# In AudioVideoLoop.__init__
...
self.sct = None
if self.video_mode == "screen":
self.sct = mss.mss()
...
# In _capture_screen
def _capture_screen(self):
monitor = self.sct.monitors[0]
i = self.sct.grab(monitor)
...This would require modifying __init__ as well, but it would improve performance by avoiding repeated object creation.
Summary
This updates
quickstarts/Get_started_LiveAPI.pyto use the explicit Live API video realtime input path and adds a more forgiving realtime activity configuration for mixed audio/video sessions.Changes
session.send_realtime_input(video=msg)instead ofmedia=msgrealtime_input_configwith:activity_handling=NO_INTERRUPTIONsilence_duration_ms=800prefix_padding_ms=3001024x1024before JPEG encodingWhy
Issue #1108 reports that the quickstart can hallucinate visual content and fail to describe the shared screen or camera feed correctly. The current Live API docs describe explicit realtime
videoinput fields, and this patch aligns the sample with that API shape while also giving the session more time to process multimodal input before responding.Verification
python -m py_compile quickstarts/Get_started_LiveAPI.py