Twitter Image Recognition

About this project


This small app takes the link to an image of a tweet as an input and attempts to find the corresponding link. It uses Tesseract OCR to extract the text & metadata from the tweet.

Technical details


Implementing this type of image recognition was surprisingly easy, thanks to the Tesseract OCR application. After that, it became a matter of extracting the data from the tweet, for which I used RegEx, and then finding the link, for which I used a simple Google search.

Getting the raw string is a one-liner:

Python
raw_tweet_text = pytesseract.image_to_string(image)

I then wrote a function to extract the data piece by piece:

Python
user, text, date = extract_tweet_data(raw_tweet_text)

which looks as follows:

Python
def extract_tweet_data(raw_tweet_text: str) -> Tuple[str, str, str]:
    user = re.search(r"@\S\S.+", raw_tweet_text)
    date = re.findall(r"(?<= - ).*(?= - )", raw_tweet_text)
    end = raw_tweet_text.find(date) - 11
    ...
    return user, text, date

The remaining code then just sends a request to Google and parses the response to find the link. We can then also create a simple embed for the tweet, and we're done!