User Tools

Site Tools


Star Trek "Red Alert" LED Strip


In August 2021, I wrote a script which detects when somebody says “red alert” in an episode of Star Trek and changes the colour of the lighting in the room to red. This project was just for fun as an excuse to play around with some Python libraries I hadn't used before.

This software works by performing OCR (optical character recognition) on the subtitles of whatever show you are currently watching. Specifically, a Python library called mss will take a screenshot and Tesseract will process it (with the help of Pillow (PIL fork))

Here's what it looks like when in use:

There is a delay of around 3 seconds between “red alert” being displayed on the screen and the LED strip changing colour. This is because the OCR is being performed on the entire screen which is a waste of resources and is slow; only the bottom 1/3 should be processed to improve performance. Lowering your screen resolution may also be an acceptable workaround to speed things up.

The code

Before this code will run, you need to install pillow (pip install pillow), pytesseract (pip install pytesseract), mss (pip install mss), requests (pip install requests) and the tesseract binary from this GitHub repo.

All of the hard work is being done by 3rd party libraries. My script hasn't even hit 50 lines:

from mss import mss
from PIL import Image
import pytesseract
import time
import requests

def handle_text(thetext):
    #Everything the OCR process "reads" is printed here for debugging.
    if "red alert" in thetext.lower():
            r = requests.get("")
            return False
        return r.status_code == 200
    return False

def main():
    #This path may need to be changed for your system. You'll get an error if that's the case.
    pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
    with mss() as sct:
        for num, monitor in enumerate(sct.monitors[1:], 1):
            raw = sct.grab(monitor)
            img = Image.frombytes("RGB", raw.size, raw.bgra, "raw", "BGRX")
            thetext = pytesseract.image_to_string(img)

            break #normally all monitors are checked, but this makes it stop after just 1.

while True:

When “red alert” is detected, a single HTTP GET request is made to - on my network, this is Node-Red which controls my home automation flows. You should replace this line with a HTTP request to your LED controller or home automation software. Any Python code can be put here to perform any custom action you want.

I am running tesseract 5.0.0 alpha, pytesseract 0.3.8, requests 2.25.1, Pillow 8.3.1, and mss 6.1.0 on Python 3.9.6 on Windows 10 Pro 20H2 19042.1110.

I wrote this just for fun and will not be maintaining it.

If you liked this project, please like/retweet this Tweet.


red_alert_led_strip.txt · Last modified: 2023/02/18 15:34 by