Category: python

Getting player names from Twitch streams or an exercise in futility

I had an idea last month for a site based around identifying video game streamers while they are in game. If I can identify them in real time then I could have real time fantasy league style betting based around the stats... it was a fun experiment but my techniques ended up not being very practical.

The Idea

In the game League of Legends I could try to find the "yellow health bar" that only your character has. Once I have the coordinates for the yellow health bar I can look above it to see the character name! Boom, with the character name I can look up stats and track all of their games automatically.

The process

For example, watching Krepo's stream here's how I'd go about getting his character name.

Get the stream frame

from livestreamer import Livestreamer

while True:
    session = Livestreamer()
    streams = session.streams('http://www.twitch.tv/%s' % streamer)
    if streams:
        stream = streams['source']

        container = av.open(stream.url)
        video_stream = next(s for s in container.streams if s.type == b'video')

        image = None
        for packet in container.demux(video_stream):
            for frame in packet.decode():
                image = frame.to_image()
                features = process_image(image)

Original Stream Image

Find yellow health bar

def _find_our_champion(image, search_step=2, draw_on_image=False):
    # should find the yellow bar underneath our hero and then 
    # we can use the top left of that to center our search area
    data = np.asarray(image)
    # remove any alpha channel in the data so we just have (r, g, b)
    data = data[:, :, :3]
    width, height = data.shape[1], data.shape[0]
    character_name_coords = None

    # Cut off the bottom few hundred pixels, not needed
    height -= 200

    for y in xrange(0, height):
        hits_this_row = 0
        for x in xrange(0, width, search_step):
            r, g, b = data[y][x]
            if r > 200 and 160 < g < 230 and 30 < b < 70:
                # really yellow
                hits_this_row += 1
            if hits_this_row > 20:
                # Top left and bottom right
                character_name_coords = (x - 120, y - 35, x + 100, y - 8)
                if draw_on_image:
                    red = (255, 0, 0)
                    draw = ImageDraw.Draw(image)
                    draw.rectangle(character_name_coords, fill=red)
                break
        if character_name_coords:
            break
    return character_name_coords

Character name image

Process the image

def _ocr_name_box(name_box_image):
    # 0 means load in grayscale
    try:
        gray = cv2.cvtColor(np.array(name_box_image), 0)
    except TypeError:
        gray = cv2.cvtColor(name_box_image, 0)
    ret, gray = cv2.threshold(gray, 160, 255, cv2.THRESH_BINARY)

    # ocr code below

Character name image processed

OCR the final processed image

def _ocr_name_box(name_box_image):
    # processing code above

    gray_image = Image.fromarray(gray)
    return pytesseract.image_to_string(gray_image)

OCR results: Krepo

So, it worked in this particular case... this was one of the few that did work.

The results

NOT GOOD!

The idea was super fun to pursue but it doesn't perform very well for a few reasons:

  • Streamers can disable showing character name
  • The "yellow health bar" specific to your character can be disabled
  • What region are players on?
  • PyTesseract is pretty cool but Tesseract is pretty cumbersome to use. Hard to train, seems kind of outdated but definitely decent for being free!
  • The OCR results were pretty bad although I'm sure with mechanical turk + some training we could get it in a decent state, but still.. names could not be shown at all!

So... it was a fun experiment but not very practical!

You can find all the source here.

— 16 December 2015
…discuss this

Best Pycon 2014 Videos

I have been binge watching Pycon 2014 videos and I thought I might as well make that productive somehow, so here's a list of the best videos (in my very humble opinion)!

I haven't watched every single Pycon video so please be sure to recommend your favorites in the comments!

Python Macgyvering

David Beazley slays it. As an expert witness he writes his own versions of simple things and comes up with interesting ways to reverse engineering/research a huge codebase.

"Discovering Python"

Imposter Syndrome

Julie Pagano reminds us to maybe not be so hard on ourselves. This one is especially important to me, because I am starting a new job soon and there's a large part of me that feels like I may not fit in.

That is bull shit!

I don't know why we get these strange fears. Everyone has that awkward first day but I'm sure at my new job the people are looking out for my best interests. They want me to be happy and feel welcomed, so I just need to think positive and do my best—no reason to worry!

"It's Dangerous to Go Alone: Battling the Invisible Monsters in Tech - PyCon 2014"

Helicopter Badassery

Ned Jackson Lovely flies an awesome little helicopter around with python! Imagine flying this into your family and friends via iPython, what joy!

This also goes into quite a bit of arduino stuff which is something I am trying to get into, pretty interesting.

"Cheap Helicopters In My Living Room"

Memcached ins-and-outs

Guillaume Ardaud goes into some great specific about memcached, a lot of stuff I didn't know--like you pronounce memcached "memcache dee"!

I think this guy is a pretty good speaker to model yourself after. He was confident and obviously practiced a lot beforehand, really enjoyed this presentation.

"Cache me if you can: memcached, caching patterns and best practices"

Machine learning for personalized Hacker News

Ned Jackson Lovely did an awesome presentation on machine learning/sciki. It was very beginner friendly. My favorite part was probably the flow chart "if you have less than 50 samples, get more samples."

"Enough Machine Learning to Make Hacker News Readable Again"

Computer Science education in the US

Jessica McKellar talks about how we could get more kids involved with computer science. What I thought stood out about her presentation was concrete ideas/details, like:

  • Make Computer Science count as a Math/Sci credit instead of being an elective -- encourages teachers and students to take it more seriously
  • Switch from Java to Python—don't throw people into the deep end of OOP, etc.
  • Call your local school board/legislature and talk about the above simple steps.

"Keynote"

— 19 May 2014
…discuss this