Hello, and this video, you will learn how to scan a QR code with Python.
So we're going to build a program which starts the computer webcam and then in front of the webcam,
you can place a barcodes which could be on your phone or in a piece of paper.
And the program will detect that QR codes and it will open automatically the browser pointing to the
URL associated with that QR code.
But let's take things step by step before we detect a QR code from the webcam.
First, let's detect a barcode from a static image file.
So I have a picture file here.
You can find this in the lecture resources attached and place it in your project directory.
Then I'm going to create a new script.
Maine, though, its P Y and import CV to so CV to IS the library of vets makes it possible to scan
QR codes and read them and open the URL associated with them.
So for this, make sure that you have installed open CV using PIP Install Open CV Dash Python was obviously
it already, so what I'll do now is are will create an image object which is key to I am reads.
So image reads.
And here you plays the path of your image.
You are the energy in my case.
Then we create a detector objects, which is key to that cue or the coat detector, so that's the class
that we need to.
Initialize that will create this detector object and then.
The courts pixels.
A single detector does detect and decode.
Image so we detect and decode the image detect means we find to where that bar called is in the image
is it's here is it's here.
So we're going to get some coordinates relative to the origin point of the image.
So maybe this points here has sort of coordinates of this point that sort of coordinates.
And so every points of these four corners here and then prints.
You know, cords and pixels, let's see what we get.
So run as the ultimate.
So what did we get here?
Well, the URL is this one in here.
So that's QR code that is inside this image is connected to Google Dot Com, who generated that QR code
anyway.
What did I find?
It's.
I generated.
It's with Python.
So actually, in the next video, I'm going to show you how to create a Python script, which generates
QR codes, and it's associated with them with the URLs, so you can create a multiple QR codes and associates
each of them with a particular URL.
By the way, I only generated the bar codes, not the codes and the mountains here, so I manually placed
them the barcode image on top of this image to make it harder for this current script to detect the
barcode instead of having just a white image with the barcode on top.
So now this script is detecting the QR codes on top of this image, and that's the URL that we get from
the U r el variable.
So this expression here returns a total of three items, and so you able will get the first item of
that vegetable and course we'll get the second item.
So the second item is this one in here.
These are the coordinates of the barcode in the image.
So as I told you this, that's on that.
And then you have the last item.
This is a nine month array as well.
And it contains the pixel values of the barcode and you see that we have all these zeros and 250 fives.
So a combination of zeros means black and 255 means white.
So actually, when we combine three 255 values, we get two white.
Anyway, all we're interested about is the URL.
No, that's the URL, so if we run again, we only get the URL.
But what can we do this?
Well, we can open its own the web browser for that you can import.
The Web browser.
Standard Library, you don't need to install it, and then a web browser does open and provide the URL.
Now if I run the script again.
It will open Google Dot Com.
All my default web browser.
So that's what this script does.
It reads an image.
But what we want to do is to read to the Web Cam V2.
So let's do that now.
So what I'll do is I'll have another script in here.
Let's say camera, and I do have some codes that we developed previously in the course we worked with
over CV how to capture a webcam VIDEO So vesicles.
And basically we import over civvy, we start the camera.
You might have to replace this one with zero.
So zero means the first camera of your computer, one means the second camera and so on.
So either zero or one might work in your case, maybe three.
Then we capture these two variables from video that reads This will be either true or false, and this
will have the current frame of the webcam.
VIDEO So a video is made of frames.
By default, OpenSea V works with three frames per second, so three images are captured in one second.
That creates a video.
We check if this is true.
So while true, while the video was captured successfully, then we show that frame on the computer
screen.
So let me run this to see what we have so far and try to switch among your programs and you see this
icon Python.
So open that and then a window should open.
So here is the window and this is life.
So I'm speaking here.
And if I press cue on my keyboard.
The video window will close because in here we are having this condition.
So if the cue keyboard's keys pressed, we break the loop.
So that brief statement and we break the loop, there will be no more iterations or no more frames will
be captured and Python will execute this, so it will release the video.
It will destroy the window, so it will close that window.
Otherwise, if nobody presses cue, then we go to this step of the wire loop.
So we capture the next frame and then we go at the beginning of a loop.
So we show that current frame that we captured.
And here again, we wait for the cue key.
We captured a second, the third frame, we go again to the loop and so on and so forth.
So now how do we combine this with our script?
Well, I'm just going to get this code, but not the import statement and place it, maybe here on top.
And then we see what we can do.
So we capture that video success and frame.
So frame now is the image.
Therefore, we don't is an image anymore.
So I'm going to delete this PNG image from there and I'm going to cut the detector and place it in here
outside the wire loop.
So we saw the camera, we read the first frame, we started the detector and then we saw the loop.
Now we have the first frame.
So what do we do with that?
Well, I'll get this code switch the text.
The barcodes and place it just under the wall loop, so I will detect the URL, fold the frame variable.
So that's what we have.
And then I'm going to show the frame on the computer screen and then press Q and then if the user wants
to press Q, then they close the video and so on.
Now at what point do we start the browser?
So this should be taken from here and placed?
Well, maybe just under the URL in here.
So let's run this.
For this, before you run it out suggests you to just to open this image and place it on your phone
or just take a picture of this image.
So open the computer's camera, take a picture of this and that's quality should be enough.
So just open your phone.
Where do you have this picture?
And then show on camera and try this out.
So maybe that's why I'm going to write.
And bring the window.
And here so here I am, if I show, you know, this picture in here, but probably unique to zooming
in because this is too small.
So I'm going to zoom it.
Show it again and the brochure opens, but you see, it's opening multiple URLs, so we don't want that.
So let's fix it.
Press Q to close this.
In this case, it's not closing because we have compromised program programs, I'm going to close it
from my charm.
So what's happening here is that the loop keeps running, so it keeps detecting the URL from Vets QR
codes over and over again.
So what do you want to do is we want to find out through a conditional a way to only detective a QR
code.
Well, so if you know.
Then open the Web browser and then break.
So when there is no QR codes on the front of the camera, the you are a variable will be none.
But if a URL is detected, then it will be equal to a string to the URL.
So that means if you URL will be true if you are at least equal to a string, right?
So we have this scenario if you URL is none and we say if you are to.
Print, yes.
We're not going to get to, yes, print out.
But if you're old is equal to something to a shrink and then we run, that's conditional again.
We get, yes, print it out.
So when you are a sequel to the string, we open the browser and we break the entire loop.
So now I'm going to run this again.
Bring the video here and try the barcodes again.
So it was quite quick.
It opened the browser and it's closed.
The camera window, so that is the final code, I'm going to remove this print function and here you
go.
So that is how to detect QR codes from the webcam.
In the next video, we're going to create a program which generates QR codes out of a URL.
So you give the program a URL and it will create a QR codes associated with that URL.
So I'll see you in the next video.