Search code examples
pythonopencvcomputer-visionocr

How to check if a image has any text or not?


I'm looking for a simple solution that would return a boolean if ANY kind of English text is present in an image file. I wish to use this to detect memes. For example, the following file should be detected as an image with text.

https://i.pinimg.com/originals/d5/13/4b/d5134b891d3903d0f272f6430014f089.gif

I've come across elaborate machine learning techniques using OpenCV but I haven't been able to fully implement it. Is there any quicker, simpler, and just as effective solution for this?

I look forward to your valuable feedback!


Solution

  • There is indeed simple way with opencv and pytessaract after installing you will only need to use a few lines in order to get the text

    pip install opencv-python

    pip install pytesseract

    import cv2
    import pytesseract
    
    img = cv2.imread('yourimage.jpeg')   
    
    text = pytesseract.image_to_string(img)
    

    Read Text from Image with One Line of Python Code

    Also if you don't like the first way you can use Google vision, keep in mind it will return Json and you will extract what you need.

    https://cloud.google.com/vision/docs/ocr

    Python Client for Google Cloud Vision