AWS Textract tutorial, Extract Forms, Tables from Image using Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone uh today tutorial all about AWS text track the text track is one of the AI service provided by AWS and what it extract can do so texture can be used to extract the text content from the document such as PDF and jpg or any other text document with this tutorial I'm gonna show how to extract the content from the JPG file and step by step through the Lambda so stay tuned uh as as usual if you like my content uh please like And subscribe I really appreciate it so let's get in I'm just going to be in console the first thing what we're going to do we create a new Lambda so search your Lambda in the search bar and uh so I do have a couple of axis in one what what I'm going to do I am creating a little Lambda give it a name and uh just a runtime Master python 3.9 and keep the other a setting as a default so click on function right the function got created what we can do we need to add a bit of permission uh because we are accessing the S3 bucket so the scenario is we we put the image file into S3 bucket the S3 bucket trigger the Lambda Lambda process the image file and we can see the actual X content in Cloud watch that's a plan so what we can do we need a give a permission so basically if I go to configuration and the permission and open the role the default role and what I am going to do I am giving a history full formation and text Track full formation policies S3 full permission of course and uh the other one is text correct extract for permission and attach policies right the role has a full permission to S3 and the text track and we don't close in this one and the Lambda is ready to go so what we gonna do uh already I do have a S3 bucket created with the standard SD bucket nothing nothing major so what we're going to do we I'll copy some code into here and let's go through the code after that so as you can see I paste some code into Lambda and before go through the Discord whatever we need to do we need to add the S3 event as I mentioned we put the file into S3 S3 trigger the Lambda Lambda process the image file and extract the content that's a plant so for example the one of the file we are supposed to process at the initially this is one of the image file we're gonna process this I got from this from the Google so this is quite a form so this is a image file uh image we're gonna try so what we're going to do I'm going to add a triggers uh select the S3 and select our demo bucket text track that's our bucket name and keep the everything is default this says Windows whenever any object comes to the SD S3 bucket it's trigger the Lambda right so the S3 trigger is added the code is there what I'm going to do I'm copy this code vs code and then we can go through the uh some work through on the call right so this is a chord uh hopefully you can see I think it's big enough and uh so it comes with a few method it's used the bottle uh the AWS SDK let's start with the um the Lambda function the initial entry point so as you can see this is nothing much basically what we do it's get the history record and get the bucket and the file name whatever the the file we pushed to the S3 and we just print in them and we call the uh one of the method called get KB map so what does it do the query map if I go into the KV map uh as you can see it's a get the bottom client for the text track and the call the the analyze document method on this client so we are passing the object uh the for example in this case S3 and you can pass of course uh byte stream sort of thing and we pass in the feature type as a form so feature type because in this case what I am interested with the phone this is the image is like a phone that's why I'm uh as a phone so if I go to uh the documentation as you can see you can pass in tables forms queries and signature like whatever the whatever the the the supported type is mentioned here in my case I'm interested the phones but this is the array you can you can pass any number of time and it's written the data and I'll put this bottle 3 document station you can refer but otherwise you can you can Google it and find it right I'm going back to the Lambda function what this KV map function does basically we get the response the analyze document return the response and in the response we do have blocks at the property called blocks and the first thing what I'm doing I am printing this block and I'll show you the what is blocks look like then that's maybe then it's easy to go through this code and it's more makes sense but before before go is showing this what is this block I'll quick go through this what does it do it's block gets like as array and I'm going with the this going with each block and finding as you can see it's a block type is key value set as I mentioned these forms are complete the key value the date is a key February 1st of 2019 is the value right so what this trying to do basically if any type is key it's pushed to the key map if the the edit the key is not entity type the edit type is not key that's mean value that's pushed to the value map so if I go to the block so we can see this this is very clearly and what has to do it's adding and it's passed to the the follow-up method so once you get the uh the KL key map value map and the block map what we pass in to get the relationship for because we have to have another uh method if we quickly go through method what is it there what is code what does it do let's go with each key map and find in the uh the relevant value and based on the general value we get the actual text so you can see we get we get the text and it's happened with the the array and uh that's all it does if you go to the key value here you can see this kind of uh properties in the uh this block uh block 11. I'll show you uh and that block and then we will come back again so let's go with the uh everything is set up Lambda is ready and what we're going to do we we get this file we upload one of the file file the same file I I suppose to process the this is the file I'm going to upload right this is a file I just uploaded all right so we will close it and let's go to our Lambda and go to Monitor and open the our Cloud watch loads let's see what does it look like [Music] um right so the file is yeah this is a log file it's got generated and let's see as you can see the Lambda has a I'll go through here what is it we do have a print block and this is a print blocks and actually then we have printed all the the key and values like if I quickly go through the qn values before that so you can see the Callback callback is in the this and it's come with the it says it's selected and if I go to the fax number let's say for example fax number you can see uh fax number is 762 3.568 blah blah and two and the from all those the message for example in this message is got clearly got as a key value pair so that's something really good like what we're gonna do we will look uh how does it look like this block element what I'm going to do I'm copy this whole block it's a it's a big nice [Music] Block it's hard to go even let me copy that it's very big and we will go through some of the item copy copy up to the blocks okay let me copy again it's not really hard it's a big blocks oh right here okay this is the one actually uh copy if I come to my list card and a new file and paste it and change into it's a Json Json file and do a bit of formatting all right so as you can see this is a very big response so what I'm going to do I'm [Music] uh like it's a build one it's it's never ending what we're gonna do we will find something like uh as per the K value key value set what I am going to do and finding key value set so this is extract with only and this is the file right so you can see uh if you see the the block type so for example if I find the block type usually this let's find the block Drive first uh as you can see the block types of pages lines and the word and there should be something like selection element and the key values in so in our case what we're going to we are finding the key value set for example if we got the very first one this is a key value set right so if I come back here right as you see in the code what we are interesting we are checking the block type is key value that's the one we are we are bringing if I go back to here key value set is this this is a new object as you can see this one say if you call it bit down uh and a little bit make it smaller key value set and the type entity type is key that's mean uh this is a kind of key value blocks block type is key value and within this object you can find a key right and the other thing is you can see a relationship in the relationship you can see type is value right so this object contains the key and if you want to find the value you need to go to the uh this ID basically it's referring everything is uh back to like a link list it's back to the uh the IDS they are matching a relationship using the IDS like if I go if I want to see this ID this this is basically key as you can see key if I'm going to copy uh this is the key and this is the value so if you want to see the value of this key and copy this ID and if I find this you can see three instance and this is ID right you can see this is says call right so if I open the image file again if I see the first one is a call right but as you can see this is only for the call it doesn't have back so what I'm going to do if I go back to my uh K value set right so key value set is has a two child this is a this is the working K value as a two value basically key if I go to the K which is called call and as you can this is a two word the call is a one child the back is the other child if I go back to here if I find this other other um one as you will see this uh this is ID you need to find ID and you can see it's a back the second child is back right that's how they are mapping each other right so now I'm back to here so the key we figure out the key is a callback Now using this ID if you want to see the value what we're going to do we go repeat this one and let's go to the copy right this says it's a type of K value and the entity type is value that's mean okay this is the the value of the key value set right so if you want to see the actual value uh so you need to go this if you copy this and if you find that you can you can see it's a selection element selection element so as you can see if I open the our image file call back callback is a tick is it selection that's why it's a sales selector this is how the the relation between the the key and the value uh it's built up in this response right so if you go with another one you're pretty much same like if you go to the date yeah it's pretty much same it date is in the key value the key of the key value object and the February this date is a value in the key value object so this same thing uh uh going uh side by side if you don't see the the actual debugs you can do do kind of some like a print in this block element and then go and have a look and you can query and you can find the actual content what you are interesting so same thing as uh the all this method does the uh kind of just mapping with the key and value and get the text right so now let's go with the actual actual output right let's say if I go back again as you can see uh everything is perfectly matched and uh uh it's ready so let's see I'm quickly add another file for example um there's another file called let me check file from and this one for example this file so I'm gonna upload this file form two this again I got from the Google I don't know what is it but it's just a kind of hard lazy resolution is very low right if I if I upload this file uh the second file right it's good let's let it to upload right all right it's got uploaded uh if I go back to my Lambda now if I show you this the item let's see okay it's still processing timeout or three seconds okay uh because my Lambda has a timeout as a three second I didn't change what I'm going to do I'm gonna change the [Music] um General configuration and edit I'm I'm going to increase to 15 minutes so this is good like people people get this kind of error and maybe sometimes get confused just read the actual error message then you can figure out what's the actual error so for example you can see the Lambda is Timeout after 3.06 second um usually the default Lambda it's a three seconds uh timeout you can go up to 15 minutes maximum you can go with the Lambda right so what I'm doing I'm uploading the same file again because the first time it didn't go through it's a time out of the Lambda and let's check the file hopefully yeah it's create another log file let's have a look and I'll get more spaces and if I open the file side by side um right let's say this is really hard to see right for example if I open the let's say uh position for which you are applying as advertisers for example let's see uh if I can't find something positioned for uh oh yeah this one position for which you are applying as a as you can see it's it's a red admin clock it's really hard to see but uh text extracts enabled really uh intelligent to grab this kind of information for example if you've got the education department or something Department of Education was advertise education as they think like this is a space but you can see the education it's nicely good same thing for everything like if the identity identity number for example ID number it's perfectly to 900 101 and ND with 87 right it's perfectly read thread the file and same way you can read the the for example tables and most of most of the the content right hopefully you enjoy this video so if you guys have any question while it's running this kind of uh demo and please comment below I'm happy to have and uh as I mentioned uh the code will be I'll push you to GitHub and the link will be in the description right so thanks everyone and uh of hope everyone enjoy and if you like my content uh please like And subscribe really appreciated thank you so much see you on next video
Info
Channel: LoveToCode
Views: 13,862
Rating: undefined out of 5
Keywords:
Id: xlUSJEsFQdk
Channel Id: undefined
Length: 20min 5sec (1205 seconds)
Published: Thu Nov 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.