In this video,
we will look at processing files beyond simple conversion and examine some use
cases for extracting useful information from the raw data in different web
media file formats that we've acquired. When we take in web media, in general,
we're taking in data in it's raw format.
The next step is to be able to process the media to extract information
from the data and this is the way that we add value.
One of the most common things we'll come across with images is
the need to extract text‑based information.
Image‑based web media can have text embedded in the
image for a variety of reasons.
Regardless,
we need an efficient way to extract that data so we
can do something useful about it.
Text recognition from images is widely known as OCR, which stands for
optical character recognition. And while the world has moved on since
basic OCR started and there are some very sophisticated tool sets for
text based information extraction like those developed with various
deep learning frameworks,
you'll find that a lot of the time you can achieve the majority of
what you want with the Python Tesseract library.
We first need to install the Tesseract library and then
the Python wrapper library pytesseract.
The instructions of how to do this accompany this course.
After installing both libraries,
you can see again in a few lines how we can achieve our goal.
First,
we import the base Python image library, as well as Tesseract,
and then we set up the file we want to process.
In this case,
it's a simple PNG image with some example text that I put in for us to extract.
Making the magic happen is a simple, yet again, two lines of code.
First, we load the image using the standard image library.
Then we passed this image into Tesseract for our text
extraction by calling image_to_string.
When we run the code, we can see it works as we expect by putting the
previously embedded image based data as text information that we can then work
with and use. Just as it's common to want to extract text‑based data from
image files, the same is true for audio files.
There are many use cases for wanting to convert spoken audio to text,
among these would be creating subtitles and transcription.
We will use the Python library speech recognition to manage the process for us.
This library was released in 2017 and is a wrapper for a number of
different both on and offline speech recognition engines,
including those from IBM, Microsoft, and Google.
We install the library as usual using pip 3.
I'm going to use a simple file I recorded earlier.
Here is what it sounds like.
The quick brown dog jumped over the lazy fox. To start,
reference the library in the Import section,
then set up your input file.
We set up the speech recognizer instance and load the audio
from the file using the record function.
Once we have that, we can then send it into the speech
recognition engine of our choice.
I'm going to use the Google engine for this example.
When we run the code, we can see that the wave file loads, the
sound is sent to the engine, and finally,
the speech is sent back to us as text output, exactly what we want
and just ready for the next stage in our process.
There are many things we can do to process video web media,
but the most common is to separate out the audio so
we can process that. For this,
we will use the same library that we used for
converting the video format moviepy.
As before, we can set up the path to our input file, in this case,
a video file and MP4 format.
We can then reference the audio part of the video. And finally,
we write the audio out to an MP3 file. We can then play the audio and
compare it to the video to confirm it was extracted correctly.