Skip to content

Commit

Permalink
Improvements following code review: remove references to GROUNDLIGHT_…
Browse files Browse the repository at this point in the history
…API_TOKEN in the code sample; add instructions on how to get video_id; rename script to get most recent frame
  • Loading branch information
paulina-positronix committed Oct 24, 2023
1 parent 775b6e8 commit ce7d634
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions docs/docs/getting-started/5-streaming.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ pip install groundlight pillow ffmpeg yt-dlp typer

## Creating the Application

1. Save this command as a shell script `get_last_frame.sh`:
1. Save this command as a shell script `get_latest_frame.sh`:

```
#!/bin/bash
ffmpeg -i "$(yt-dlp -g $1 | head -n 1)" -vframes 1 last.jpg -y
```

This will download the last frame from a YouTube live stream and save it to a local file `last.jpg`.
This will download the most recent frame from a YouTube live stream and save it to a local file `last.jpg`.

2. Log in to the [Groundlight application](https://app.groundlight.ai) and get an [API Token](api-tokens).

Expand All @@ -38,21 +38,19 @@ import typer
from groundlight import Groundlight
from PIL import Image

GROUNDLIGHT_API_TOKEN=os.getenv("GROUNDLIGHT_API_TOKEN")


def main(*, video_id: str = None, detector_name: str = None, query: str = None, confidence: float = 0.75, wait: int = 60):
"""
Run the script to get the stream's last frame as a subprocess, and submit result as an image query to a Groundlight detector
:param video_id: Video ID of the YouTube live stream
:param video_id: Video ID of the YouTube live stream (the URLs have the form https://www.youtube.com/watch?v=<VIDEO_ID>)
:param detector_name: Name for your Groundlight detector
:param query: Question you want to ask of the stream (we will alert on the answer of NO)
"""
gl = Groundlight(api_token=GROUNDLIGHT_API_TOKEN)
gl = Groundlight()
detector = gl.create_detector(name=detector_name, query=query, confidence_threshold=confidence)

while True:
p = subprocess.run(["./get_last_frame.sh", video_id])
p = subprocess.run(["./get_latest_frame.sh", video_id])
if p.returncode != 0:
raise RuntimeError(f"Could not get image from video ID: {video_id}. Process exited with return code {p.returncode}.")

Expand All @@ -68,7 +66,7 @@ if __name__ == "__main__":

```

4. Save the script as `streaming_alert.py` and run it:
4. Save the script as `streaming_alert.py` in the same directory as `get_latest_frame.sh` above and run it:

```bash
python streaming_alert.py <VIDEO_ID> --detector_name <DETECTOR_NAME> --query <QUERY IN QUOTATION MARKS>
Expand Down

0 comments on commit ce7d634

Please sign in to comment.