This repository contains a Web App and an API which extract QR codes from images and scans the content.
PLEASE: read everything
--
--
This repo only works locally with Windows WSL2 and Linux.
You need to install some dependencies (if you are in Windows):
- Install Windows terminal. This makes easier to switch between Windows and WSL (Windows Substystem in Linux) terminal
- Install Ubuntu with WSL
With Windows terminal you must open a new terminal inside WSL (Ubuntu):
- Clone this repository
git clone git@github.com:alejlatorre/QRCodeScanner.git
- Inside the
scripts
folder run the config file
cd QRCodeScanner/
cd scripts/
chmod +x setup.sh
bash setup.sh
- After you run the
setup.sh
config file, you must locate on the code folder
cd ~/truora/QRCodeScanner
- Activate enviroment
source .venv/bin/activate
- Kill ports if they are in use by another program.
sudo lsof -t -i tcp:5000 | xargs kill -9
- Run the program with poetry
poetry run python web_app.py
- Usage:
- Kill ports if they are in use by another program.
sudo lsof -t -i tcp:8000 | xargs kill -9
- Run the program with uvicorn
uvicorn api:app --reload
-
Enter into FastAPI UI
-
Usage:
-
With the program running, you need to open a new tab in Windows terminal
-
Run
api_request_example.py
script with an image stored indata/
foldercd ~/truora/QRCodeScanner source .venv/bin/activate poetry run python api_request_example.py 'qrcode_sproutqr.jpg'
-
With the program running, you need to open a new tab in Windows terminal
-
Run the script with an image stored in
data/
foldercd ~/truora/QRCodeScanner source .venv/bin/activate curl -X 'POST' 'http://127.0.0.1:8000/get_text' -H 'accept: application/json' -H 'Content-Type: multipart/form-data' -F 'img=@data/qrcode_sproutqr.jpg;type=image/jpeg'
First, we use a customized module called engine
where is a class called QRCodeScanner
.
This class have two methods:
- read_image: It is used to read files with cv2 library when data is stored locally.
- extract_info: It is used to extract three elements from decoded image (rectangle, polygons and text decoded)
- Rectangle: Detects the perimeter where the QR code is in 2D
- Polygons: Detects the shape of the QR code regardless of the rotation angle
- Text: Extracts the decoded text from the QR code
Note: The extract_info
returns a list of strings, so if we have an image with multiple QR codes, it will returns a list of decoded texts.
In the Web App version, we use Flask and POST method to send an input file through a form element. Then it validates allowed extensions (jpg, jpeg and png) and save the image in application/static/
folder in order to use it with QRCodeScanner
class. Finally, the extracted text is iteratively shown through flask messages (with flash method) because we can have a list of decoded texts.
In the API version, we use FastAPI to create an app that contains a POST request url called http://127.0.0.1:8000/get_text
and basically uploads a file that is open with Pillow and then the QRCodeScanner class is used to extract the list of decoded texts.