Building a Simple OCR App with Python | Automating Text Search

Automata Learning Lab · Beginner ·🛠️ AI Tools & Apps ·2y ago
Skills: CV Basics90%

Key Takeaways

This video teaches how to build a simple OCR app with Python using OCR tools to automate text search in local images.

Full Transcript

that just put this right here there yep what's up guys welcome back to the channel in this video we're gonna learn how to do OCR search over text inside images in your local files in a local folder so I already have my Jupiter notebook setup over here and essentially what you're going to need is OCR search ask your foot python equal 3.10 and you're gonna You're Gonna Wanna install Ezio CR and torch and that's what you need to get set up I'm already set up so I'm already ready to go I'm gonna import OS typing I'm gonna import a list from the type checking so I will do type checking on the functions and p0shar to do to have a lifting of the user then we're going to set up two functions one to scan the um images for text and the second one to Loop over the image inside of a folder so and before we do that of course we gotta set up the redial CR so we're gonna set up reader equal to easusar.reader we're going to give it the English language and here I'm setting up GPU to true because I have a GPU machine but if you don't just set this to post and now I'm going to run this and now what we need is those are those two functions so we're gonna need the OCR scan function that runs the OCR in an image and another one that Loops over images in a folder so we're gonna set up Dual Star scan it's gonna take in the image path as the keyword argument as a as an argument that's going to be a string and we're going to return a string and the string is going to be obviously the text that was detected inside the image and now we're gonna set up the function so essentially here we're calling the reader and the read text method inside the reader that we instantiated over there the reader object and we're going to give it the image pass and now we're gonna say the recognized text is just a join of all the element texts inside the image that are stored in the result variable and now that we've done that we're going to return the regular guys text and we can do from here is we're going to set up right now just a toy just a template boilerplate of the search images so that we have it on our the skeleton over app and we're going to go back to this function later to finish it off but now I want to test the OCR scan function that we just read that we just wrote so I'm gonna come here I'm gonna set up the image path and I have an image path I have an image inside as a test there you guys can check out here it's just just a copy paste of some text that I have in my machine and I'm gonna run the OCR scan on that image for you guys to have an idea I'm going to just run it here and there you go super quick because I'm using the GPU and it detected all the text inside of that image so that's perfect and now I'm gonna move this search images function over here and now we can start setting this image up setting this function up so search images essentially is going to take in a directory okay and that's going to be a strain and a keyword which is going to be the thing that we're searching for when we are doing our OCR search because remember the goal of this is not just to do Char over the uh text inside images in local files in the local folder but to find a specific keyword that we're looking for within the text that we find in those images so that's that can be a really useful tool and it's really simple to set up with python and easier so this keyword is going to also be a string and we're going to Output the output of this function is going to be a list of image paths that contain the keyword that we're looking for okay so that's that's essentially that's essentially our search images function so now we're going to say okay so we're going to define a matching images list right and let's get a store all the images that match our requirements meaning contains the keyword that we're looking for and we're going to say update so forth root and then directory and then files in OST I'll walk directory okay so inside of the stain so for file in files what we're gonna do is we're gonna say if the file ends with uh that ends with and now here I'm going to give a tuple with all the possible image extensions so wpng.jpg and Dot J back with the knee and that's good for now and we're going to set up image best so join gluten file perfect that's Gelco pilot already recognize that that's what I want then we're going to give the text so let's say detect the text detective text is equal to a shark scan image path and then if the keyword dot lower in record in the detected text exactly detected text the lower so now I want to make everything lower so it's easier to search for we don't want to have issues where there's some Capital um some some letter characters that are capitalized and then from that reason we don't find keyword that we're looking for and now I can say all right so if that happens if we find our keyword we're gonna pin that path to our matching images list finally at the end we're going to return the matching images and that's going to be what we're looking for so now to test the search images what we're going to do is I'm gonna give the current folder as uh the reference and the keyword I'm going to set up keyword let's take a look at this one so this one image test if we came here we'll see no one for Windows please the official instructions here so this is some random text that I just copy pasted from my screen somewhere I don't even know where so now let's look for the word note okay so that's the one that's written inside this thing here just to see if it works so I'm going to say keyword equal note and what I expect to happen is it should return a list containing the name image test.png as the response so that's if that works if we run it and no it doesn't it says type list cannot be instantiated use list instead and that's where where where list string that should be correct ah yeah because yeah oh sorry that's my mistake I always my mistake I should have done this and there we go we found it and we return it so this thing is working so what we're going to do is we're going to transfer all of this code through an application that we can call from the terminal so I'm going to open up another file here on JS code I'm going to say Lucia search dot pi and now I'm going to put that on my right and I'm going to come back here and I'm going to come here to my notebook I'm going to copy these two functions to my right uh yeah perfect we're going to come here we're also going to copy this one yep and we're going to copy the setting up so this thing and now we're going to copy the Imports obviously and all we need now let's remove this and let's move that and all we need now is a main function that's going to contain the CLI tool that we're going to use so that we can call all this from the terminal okay so that's what we're going to be doing so this one is defined a CLI tool that allows for OCR search for HC word over uh images in a local folder or a single image that's what our main function is going to be doing let's set up this thing here I'm going to set up this thing here so now with our main function we're going to be doing I already have it here so I'm just going to copy One Lie by line so that you guys can take a look and see what what was going to be happening so now we're going to need arc bars so that we set up our cli2 and let's write it up so we don't need this part of the Jupiter notebook anymore okay so our main function is going to be so we're going to set up our parser okay so we're gonna set up uh ARG parse dot argument parser and then that's we're going to give it a description the description is going to be uh OCR search over local images add an argument and then we're gonna say this is going to be our directory so where we set up the directory so it's going to be called directory and the type obviously is going to be a string and this is the directory the images perfect and now we're going to say parser.add argument the first one here is going to be the keyword so KW and then here we're gonna say minus keyword and we're gonna say type equals string as well and we're gonna say help equal to [Music] uh this is the three word text we'll look for perfect and now that we have that we can add uh the option of a single image so we can say parser dot add argument minus I so let me put it like this minus I and then we're gonna say image and then we're gonna say type equal Str and then we're gonna say help equal d image to scan the single image case to Scag not the single image the scan is better yeah perfect now that we have these things we can just get our arguments so we can say R is equal to parser dot uh parse args right so now we can you know leverage RMS and then if the directory is given then we're going to say well matching images is equal to search homages uh and we're going to fit in the directory so we're going to say uh args.directory R and rs.q word and we're gonna print so we're gonna say images that console the keyword and we're gonna Loop over the matching images and we're gonna print that's called instead of calling image we're going to call it image name image path because those are not actual loaded images but they're paths two images now in the case that we don't Define the directory that means that we want to scan a single image so we say okay and what is the detected text for that single image that's perfect and then that's correct our image contains the image exactly so we say okay uh if that is inside we say detected image in the image and then we're gonna say this this this and we're going to give d image and we're gonna say all right scan the image and then we're not going to have a Passover the image so we're gonna say just the detected text so see where the Titan the image so we're going to say yes and then we're gonna print the tactic texts detected texts and I'm gonna just here and I'm gonna say this and this else keyword not attacking the image and that is it okay now that we have that all we need to do is we're going to set up our main function here and we're going to call it so now I can open near my terminal and I can come here and say okay uh I'm going to run this function so it's going to crawl let's try search we're going to do it over one image so let's say minus I it's gonna be the image that we just tested over here and the keyword is going to be nopes and now we run it let's see if it works keyword not detected in the image that is kind of weird so let's see English test the PNG because it's note in our notes so that was my mistake so I'm going to run that again with Note so now we run it keyword detected and then it prints the detected text as expected perfect so now just to finish things off instead of the single image we're gonna give the directory the director is going to be the current directory so that you know it's easier for us so just we just test a simple example and instead of using the word node let's look for the word windows so I'm just gonna I'm gonna say windows and let's see what happens we should return a list with the path to image test.png and that's exactly what happens images that contain the keyword and it gives me the path so all right guys so that's how you set up a simple OCR search app to look for text over images in your local folder if you guys like this video don't forget to like And subscribe and see you next time cheers

Original Description

Let's write a simple Python script to automate search over text in your local images with OCR using Python. Let's get started! :) 📚 Chapters: 00:00 - Introduction and setting up the OCR search project 00:18 - Initial project setup requirements and Python package installations 00:32 - Importing necessary libraries 00:45 - Setting up the functions to scan images for text and loop over images in a folder 01:00 - Setting up EasyOCR reader for text detection 01:16 - Coding the OCR scan function 01:57 - Setting up the function to process recognized text 02:20 - Introduction of a template for the search images function 02:31 - Initial testing of the OCR scan function 03:00 - Execution of the OCR scan on an image for demonstration 03:16 - Starting the development of the search images function 04:12 - Explanation of how the search images function works 06:10 - Testing the search images function 07:28 - Transitioning the code to an application callable from terminal 08:20 - Developing a CLI tool to enable OCR search from terminal 10:26 - Parsing the arguments for the CLI tool 11:34 - Coding the logic to handle directory search and keyword recognition 12:30 - Handling single image OCR search in the CLI tool 14:31 - Running and testing the application in the terminal 15:02 - Conclusion and review of the project. 🔗 Links: - Medium Article with source code: https://medium.com/@lucas-soares/how-to-build-a-simple-ocr-search-app-for-your-local-files-1a6357c0a1d0 - Subscribe!: https://www.youtube.com/channel/UCu8WF59Scx9f3H1N_FgZUwQ - Join Medium: https://lucas-soares.medium.com/membership - Tiktok: https://www.tiktok.com/@enkrateialucca?lang=en - Twitter: https://twitter.com/LucasEnkrateia - LinkedIn: https://www.linkedin.com/in/lucas-soares-969044167/ #automation #python #productivity #datascience #artificialintelligence
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Automata Learning Lab · Automata Learning Lab · 0 of 60

← Previous Next →
1 A Quick Tutorial on NLP Basics
A Quick Tutorial on NLP Basics
Automata Learning Lab
2 Automating your Digital Morning Routine with Python
Automating your Digital Morning Routine with Python
Automata Learning Lab
3 Exploring Problem Solving with Python and Jupyter Notebook #1
Exploring Problem Solving with Python and Jupyter Notebook #1
Automata Learning Lab
4 Summarize Papers with Python and GPT-3
Summarize Papers with Python and GPT-3
Automata Learning Lab
5 An Experiment Tracking Tutorial with Mlflow and Keras
An Experiment Tracking Tutorial with Mlflow and Keras
Automata Learning Lab
6 Automating Google Forms Submissions with Python
Automating Google Forms Submissions with Python
Automata Learning Lab
7 Productivity Tracking With Python and the Notion API
Productivity Tracking With Python and the Notion API
Automata Learning Lab
8 When your Machine Learning Model Fails Do This ;p
When your Machine Learning Model Fails Do This ;p
Automata Learning Lab
9 Machine Learning Tip#1 Practical Deep Learning Course
Machine Learning Tip#1 Practical Deep Learning Course
Automata Learning Lab
10 Machine Learning Tips: Deep Learning Monitor
Machine Learning Tips: Deep Learning Monitor
Automata Learning Lab
11 Machine Learning Tips#5 MLOPs specialization in Coursera #machinelearning
Machine Learning Tips#5 MLOPs specialization in Coursera #machinelearning
Automata Learning Lab
12 Automatically Changing Desktop Wallpaper with Python and the Nasa Image API
Automatically Changing Desktop Wallpaper with Python and the Nasa Image API
Automata Learning Lab
13 Building an Image Classifier to Filter Out Unused Images From Your Photo Album with Machine Learning
Building an Image Classifier to Filter Out Unused Images From Your Photo Album with Machine Learning
Automata Learning Lab
14 Automating VS Code Snippets with Python
Automating VS Code Snippets with Python
Automata Learning Lab
15 How to Set Up a Machine Learning Environment with Conda and Pip-Tools
How to Set Up a Machine Learning Environment with Conda and Pip-Tools
Automata Learning Lab
16 9 Google Search Tips for Machine Learning
9 Google Search Tips for Machine Learning
Automata Learning Lab
17 Thinking Tools
Thinking Tools
Automata Learning Lab
18 Automating Car Search with Python and Data Science
Automating Car Search with Python and Data Science
Automata Learning Lab
19 Generating Images from Text with Stable Diffusion and Hugging Face
Generating Images from Text with Stable Diffusion and Hugging Face
Automata Learning Lab
20 A Practical Introduction to Data Science using the Spaceship Titanic Dataset from Kaggle
A Practical Introduction to Data Science using the Spaceship Titanic Dataset from Kaggle
Automata Learning Lab
21 Jiu Jitsu App with Python and Streamlit
Jiu Jitsu App with Python and Streamlit
Automata Learning Lab
22 2 Apps for Coding In The Ipad Pro
2 Apps for Coding In The Ipad Pro
Automata Learning Lab
23 From Tensorflow to Pytorch?
From Tensorflow to Pytorch?
Automata Learning Lab
24 Building an Audio Transcription App with OpenAI Whisper and Streamlit
Building an Audio Transcription App with OpenAI Whisper and Streamlit
Automata Learning Lab
25 Productivity Tracking with Python Short Summary
Productivity Tracking with Python Short Summary
Automata Learning Lab
26 Automating Expense Reports with Python
Automating Expense Reports with Python
Automata Learning Lab
27 ChatGPT, Angry Pandas and AI Code
ChatGPT, Angry Pandas and AI Code
Automata Learning Lab
28 7 Strategies To Learn Anything Using ChatGPT
7 Strategies To Learn Anything Using ChatGPT
Automata Learning Lab
29 Building a Thought Summarization App with Whisper and GPT3
Building a Thought Summarization App with Whisper and GPT3
Automata Learning Lab
30 Visualize a Neural Net Learning Polynomial Functions
Visualize a Neural Net Learning Polynomial Functions
Automata Learning Lab
31 Automating Notion with Python
Automating Notion with Python
Automata Learning Lab
32 Pose Tracking for Jiu Jitsu - Update #jiujitsu #machinelearning
Pose Tracking for Jiu Jitsu - Update #jiujitsu #machinelearning
Automata Learning Lab
33 Update to my Pose Tracking for Jiu Jitsu Project #machinelearning #jiujitsu #ai #deeplearning
Update to my Pose Tracking for Jiu Jitsu Project #machinelearning #jiujitsu #ai #deeplearning
Automata Learning Lab
34 ChatGPT API Released by OpenAI
ChatGPT API Released by OpenAI
Automata Learning Lab
35 ChatGPT API Response Format #machinelearning #ai #datascience
ChatGPT API Response Format #machinelearning #ai #datascience
Automata Learning Lab
36 Beyond Stable Diffusion with Composer | Automata Learning Lab Paper Series #1
Beyond Stable Diffusion with Composer | Automata Learning Lab Paper Series #1
Automata Learning Lab
37 Beyond Diffusion Models with Composer #machinelearning #ai
Beyond Diffusion Models with Composer #machinelearning #ai
Automata Learning Lab
38 Machine Learning for Jiu Jitsu
Machine Learning for Jiu Jitsu
Automata Learning Lab
39 Prompt Engineering Basics #machinelearning #gpt4 #chatgpt
Prompt Engineering Basics #machinelearning #gpt4 #chatgpt
Automata Learning Lab
40 Visual ChatGPT: Integrating Images with ChatGPT Paper Series#2
Visual ChatGPT: Integrating Images with ChatGPT Paper Series#2
Automata Learning Lab
41 Visual ChatGPT #machinelearning #ai #artificialintelligence
Visual ChatGPT #machinelearning #ai #artificialintelligence
Automata Learning Lab
42 LERF - Language Embeddings + NERF for Querying 3D Spaces #machinelearning #ai
LERF - Language Embeddings + NERF for Querying 3D Spaces #machinelearning #ai
Automata Learning Lab
43 Summarize Papers with Python and ChatGPT
Summarize Papers with Python and ChatGPT
Automata Learning Lab
44 Large Language Models can use Tools Now! #artificialintelligence #machinelearning #ai
Large Language Models can use Tools Now! #artificialintelligence #machinelearning #ai
Automata Learning Lab
45 Sparks of AGI in GPT4? #machinelearning #ai #agi #artificialintelligence
Sparks of AGI in GPT4? #machinelearning #ai #agi #artificialintelligence
Automata Learning Lab
46 Toolformer: LLMs can use Tools! #chatgpt #llms #gpt4 #gpt3 #artificialintelligence
Toolformer: LLMs can use Tools! #chatgpt #llms #gpt4 #gpt3 #artificialintelligence
Automata Learning Lab
47 Talking to Your Notes with LangChain #artificialintelligence #llms #gpt4 #chatgpt
Talking to Your Notes with LangChain #artificialintelligence #llms #gpt4 #chatgpt
Automata Learning Lab
48 How to Talk to a PDF using LangChain and ChatGPT
How to Talk to a PDF using LangChain and ChatGPT
Automata Learning Lab
49 Query Your Own Notes With LangChain
Query Your Own Notes With LangChain
Automata Learning Lab
50 HuggingGPT #machinelearning #artificialintelligence #huggingface #gpt4 #chatgpt
HuggingGPT #machinelearning #artificialintelligence #huggingface #gpt4 #chatgpt
Automata Learning Lab
51 Do as I Can Not as I Say Paper #artificialintelligence #llms #reinforcementlearning
Do as I Can Not as I Say Paper #artificialintelligence #llms #reinforcementlearning
Automata Learning Lab
52 Automating Anki Flashcards with OpenAI and GPT-4
Automating Anki Flashcards with OpenAI and GPT-4
Automata Learning Lab
53 Building A PDF Summarization App with  Gradio and LangChain
Building A PDF Summarization App with Gradio and LangChain
Automata Learning Lab
54 Auto-GPT #artificialintelligence #gpt4 #llms #autogpt
Auto-GPT #artificialintelligence #gpt4 #llms #autogpt
Automata Learning Lab
55 DocGPT - Chat with Github #artificialintelligence #gpt4 #chatgpt
DocGPT - Chat with Github #artificialintelligence #gpt4 #chatgpt
Automata Learning Lab
56 LLMs for Research and Planning #artificialintelligence #gpt4 #llms
LLMs for Research and Planning #artificialintelligence #gpt4 #llms
Automata Learning Lab
57 How I Use ChatGPT for Interactive Language Learning
How I Use ChatGPT for Interactive Language Learning
Automata Learning Lab
58 Building an Audio Transcription App with Gradio and Whisper
Building an Audio Transcription App with Gradio and Whisper
Automata Learning Lab
59 Summarizing and Querying Multiple Papers with LangChain
Summarizing and Querying Multiple Papers with LangChain
Automata Learning Lab
60 Mojo - The New AI Programming Language?
Mojo - The New AI Programming Language?
Automata Learning Lab

Related Reads

📰
Beyond Connectivity: How Wi-Fi Is Becoming One of the World’s Largest Passive Sensor Networks
Wi-Fi is transforming into a massive passive sensor network, enabling innovative applications and use cases, and it's crucial to understand its potential and implications
Medium · AI
📰
Automating Form I-130 Drafting – Spouse, Parent, Spouse, Parent, Sibling, and Child Petitions Made Simple
Automate Form I-130 drafting using AI tools like the AI Validation Engine to streamline the petition process for spouses, parents, siblings, and children
Dev.to AI
📰
Every digital creator, developer, and marketer knows the silent frustration of the modern workflow…
Streamline your workflow by leveraging AI tools to automate tasks and increase productivity
Medium · AI
📰
Every digital creator, developer, and marketer knows the silent frustration of the modern workflow…
Streamline your workflow by integrating AI tools to reduce switching between apps and increase productivity
Medium · Startup

Chapters (20)

Introduction and setting up the OCR search project
0:18 Initial project setup requirements and Python package installations
0:32 Importing necessary libraries
0:45 Setting up the functions to scan images for text and loop over images in a fol
1:00 Setting up EasyOCR reader for text detection
1:16 Coding the OCR scan function
1:57 Setting up the function to process recognized text
2:20 Introduction of a template for the search images function
2:31 Initial testing of the OCR scan function
3:00 Execution of the OCR scan on an image for demonstration
3:16 Starting the development of the search images function
4:12 Explanation of how the search images function works
6:10 Testing the search images function
7:28 Transitioning the code to an application callable from terminal
8:20 Developing a CLI tool to enable OCR search from terminal
10:26 Parsing the arguments for the CLI tool
11:34 Coding the logic to handle directory search and keyword recognition
12:30 Handling single image OCR search in the CLI tool
14:31 Running and testing the application in the terminal
15:02 Conclusion and review of the project.
Up next
monday.com AI Review: What Works and What’s Just Hype? (Honest Take)
Mastering monday
Watch →