How to Detect QRCode and BarCode using OpenCV in Python + Project

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to my channel in this video we are going to learn how to detect QR code and bar code in an image using OpenCV we first look at the basics and then we will move on to create an authentication project in which we will check whether a person is authorized or unauthorized based on the QR code on their ID if you like such projects do subscribe and hit that like button if you found this video useful so let's get started so let's get started with decoding we will be needing three different libraries for this project the first one is open CV then we need numpy and the last one we will be needing is the buy side bar now buy side bar is the library that allows us to detect and localize barcodes and QR codes now the great thing about this is that it will decode the data the information about your barcode or QR code and it will also tell you the location of it so that is what I mean by localization it will tell you it is available for example it was found in x and y position and it is off the size of W and H which is your width and the height so we are here in Python and I have opened up a new project and a new file so the first thing we need to do is to import our libraries so we will go to file we will go to settings and here we will go to the interpreter and over here we are going to add open CV so where is its open CV then we need numpy and then we need bytes at our PI Z bar so here we can check the progress so now that the installation is done we are going to import our libraries and the first one we will import is the cv 2 then we have numpy so import numpy as NP and then we are going to import our by side part so regarding pi sidebar we just need to import one function and believe it or not this one function will give us all the things that we require which are the detection of the barcode and the QR code and their position as well and it will decode the message for us as well so that function so we are just going to import that function so we will say from PI Z VAR dot pi z bar we are going to import decoder and that is the function that will help us finding the code and get it finding the QR code and getting the message out of it and to find the location as well so let's look at the bare minimum of what we need to do so let me bring in an image so so let's import this image and we are going to decode this to find our barcode so let me show you this image first so this image is just the ID of an employee and then you have the QR code so this is what we are going to find so to import this image we are going to write C v2 dot I am Reed and we are going to define the name and the path so it's right in the directory so we will say 1 dot PNG and we are going to save this as image now all we need to do to get our information is to send this image to our decode function so we will write here decode and we will send our image and at the back here we are going to save it let's say in our code and then we can print it out we can write code and we can print this out and there you go so if you see that we are getting lots of information here so first of all we have the data which is inside which is the message of this QR code then it's telling us that which type of code it is is that QR or is it barcode then it is telling us where is the bounding rectangle around it where is the bounding box and then it is telling us also the polygon points so it might not be a perfect square or a rectangle so it might have some tilt around it so that's why it's giving us points so we can draw the polygon as well so now we might also have multiple barcodes or QR codes so in that case we need to access the values of each one of these so for that we can add a loop so we can simply say for let's say bar code in decode and we are going to send our image we are going to write for example bar code dots data and we are going to print so let me remove that and let me remove that so if we run this now and there you go this is the message that we are getting from our power cord we can also write correct that will tell us the position and the size so we have the left value and the top value and then we have the width and the height of our bounding box so this way we can get the information of what we require now the most important information is basically your message so if we write that down dot data you will see that we have a B in front of it that B just means that it is in bytes so we need to decode it we are going to write for example my data is equals to barcode dot data dot decode and we are going to specify a decoding method now you don't have to worry about this this is a very general method and this will convert this into string so if we print this out and we write my data now we are getting our actual data so this 1 1 1 1 is basically what was written when it was created now this message can be anything it can be text it can be a website it can be numbers or it can be anything next what we can do is we can add our webcam and then we can try different barcodes and we can put some bounding boxes around them so let's convert this code so that it can accept the webcam and then it can find the barcodes or the QR codes within our image so for that we will comment this out for the webcam we need to define our object for the video capture source so we will write cap is equals to say V 2 dot or video capture and we have to define the ID so in this case we will put 0 and I forgot the age I will write that SCAP okay so next we are going to define the width and the height so cap dot sets width ID is three and we will write it as 640 and cap dot sets the ID for height is four and we will write 480 okay so now we will put our while loop while while true we are going to do all of this so we are going to get our image so success and then our image is equals to cap dot read so now we will write our imshow function so we will write CB 2 dot i am show and then we are going to write here let's say result and then IMG so we also need to wait so CB 2 dot wait key and we will put a delay of 1 millisecond so if we run this now it should turn on my webcam and there you go so we have a few QR codes and oh actually we are detecting we are already detecting the messages so you can see different QR codes with different messages and what we will do now is we will put some bounding box around them and you can see that some of them they have a message this is a QR message and some of them that they are just playing text numbers and I think there was a website as well no website I think I put a website as well let me check again yeah there is a website here as well there you go so you have the website as well so we can remove this and now we are going to add our bounding box so for that we don't want to use the rectangle we want to use the polygon now for polygons we have to follow a pattern we have to convert it into an array and we have to reshape that array and then send it to our polygon lines function so this is just a formality that you have to go through so bear with it so we will define points is equals to numpy dot array and then we are going to put our polygon values inside here so we will write barcode dots polygon and then we will write numpy dots integer of 32 then we will write that our points we are going to reshape so PT s dot reshape not resize reshape and then we are going to reshape it to minus 1 1 & 2 now finally we can write our function for the poly polygon so we can write C V 2 dots poly poly lines and we have to define our image so this is our image and then we have to define the points so we have pts and then we are going to say that it is closed so it is true and we have to define a color let's put two five five zero and 255 which is I think purple and then we have the Tecna so we'll put it as five so let's run this and there you go so you are looking at the bounding polygon so if I rotate this it will still work fine now this is the difference between the rectangle and the polygon so if I had rotated while I'm using the points of the rectangle it might have been something like this so it will not show me the actual bounding area it would just show me the approximated bounding box so that's why we are using polygons so now that we have this we can print out the message on top of that so we understand what is being printed out so we can write here CV two dots for text and we have to put it on our image and we are going to put our data so that is my data and because we have already converted into string so it will give us these values we can technically put them in and then we have our points now the points the origin if we use polylines the problem with that will be that if if the angle if we have an angle on our image the text will rotate as well but we don't want a rotated text we want a text that stays constant so it's easier to read so for that we can use the points of our rectangle the top point of our rectangle so we can write here points to is equals to barcode dot rectangle and we can use this so we can say points to at zero and then points to at one and then we have to define our C v2 dot font so we can choose any let's choose simplex and then we have to define the scale and then the color so let's keep the color same two five five zero two five five and what happened there okay so then we have the thickness let's put it as two and you have that's pretty much it let's run this and there we are getting our message and you can see for each one of them we are getting the relevant message above so that's how we can detect a QR code and now let's look at an example of the bar bar code so the good thing is for bar code you don't have to do anything special it's just the way it is and we can run this and there you go so we are detecting and we are getting the numbers and there you go now you can see the the polygons are flickering at the points of the polygon but still our data is pretty consistent so it should work fine so next we are going to write our code for our authentication program and the idea behind it is that we have a list of approved IDs and whenever a person is coming in they will show their ID and the system will check whether they are authorized or not now this is a very simple example we could do a lot more things for example we could set up an online database that will generate these QR codes send them to people and then it will create a list and then we can check through that list whether a person is authorized or not we can for example check the attendance of people we can check what time did they enter there could be a lot of things that could be done but we are going to keep it simple today and we will just check we'll compare it with a text file and see whether this person trying to enter is authorized or not so let's start off with that we are going to copy our code that we have written so far and then we are going to create a new Python file and over here we are going to write our QR code project we are going to paste this and over here we are going to start off by creating a text file so we will write new and then we will write file so this file will be our my data file and we will write dot txt and yes we will say it is a text file I wrote the spelling wrong let me write that again okay so so let's open that and over here what we are going to do we are going to write the numbers or the IDs of all the people who are authorized so let's write it down so we will write 1 1 1 1 1 1 and this is just a random number I am writing down then 2 then 3 4 5 so these are the 5 people that are authorized for example and of course these are just random values you can generate your own values and you can write them out and what I've done is I have created the QR codes for each one of these so let's go back and the first thing we will do is we will read all of this data and we are going to store it in a list so we can write with open and then we will write our file name so it is my data file dot txt and then we are going to say as F and then my data list is equals to now this will be a list with all that information we are going to write F dot read and then we will write dots split lights so what this will do is it will read all the data and based on the lines it will add one item to the list so every line is a new item so we can print this out to tests or prints to my data list and we can turn this off for the sake of it so here we have a list and inside the list we have the items based on each of these what we call numbers so what we'll do next is to check whether this person is authorized or not the QR code that we are getting is it authorized or not so we are already getting our my data so what we need to do is right after we get the data we are going to write if my data in my data list then we are going to say that this person is authorized so we are going to print authorized else we can print on authorized so let's see if that works out so if we run this and so here we have the first ID so if I can just focus one is focus so bad anyways so it's telling us that it is authorized and then this is telling us that it is unauthorized so it is working one so what we can do next is we can change the color if it's authorized for example we can put green and we can also write down the text authorized and unauthorized instead of writing down the actual message so here instead of print we will say that my output is equals to authorized and over here let me copy that my outputs here will be unauthorized so I can just copy output and in the text I can just replace it with my data so if we run that we can see authorized and then unauthorized and regarding the color we can also change the color so we can say my color is equals to green so 0 255 and 0 and then we can write here for unauthorized we can write red so 255 and over here it will be 0 and we can change the color of the polygon and of the text as well where is the color so here is the color we can change the text so if we run that we have authorized and unauthorized so let me let me check it with the other barcodes so here you can see unauthorized authorized authorized unauthorized this one is authorized authorized unauthorized and there we go so this is it for today's video I hope you have learned something new and if you found this video useful give it a thumbs up and don't forget to subscribe and I will see you in the next video
Info
Channel: Murtaza's Workshop - Robotics and AI
Views: 57,628
Rating: 4.98212 out of 5
Keywords: qr code, opencv, python, tutorial, qr code reader, scanner, pyzbar, qrcode opencv, qr code opencv python, qrcode opencv python, python pyzbar, opencv pyzbar, barcode opencv, bar code opencv, barcode opencv python, How to Detect QRCode, How to Detect QRCode and BarCode, How to Detect QRCode OpenCV, qr code opencv cv how to, qr code scanner, qr code scanner opencv, python tutorial, python barcode, read qr code, python project
Id: SrZuwM705yE
Channel Id: undefined
Length: 23min 45sec (1425 seconds)
Published: Fri Apr 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.