Skip to content

Basic Web Application and an API made with Python + Flask + FastAPI

Notifications You must be signed in to change notification settings

alejlatorre/QRCodeScanner

Repository files navigation

QR Code Scanner

This repository contains a Web App and an API which extract QR codes from images and scans the content.

PLEASE: read everything

--

--

Dev enviroment setup

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

Inside WSL terminal

With Windows terminal you must open a new terminal inside WSL (Ubuntu):

image

Setup script

  • 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

Web app

QRCodeScanner-web

Deploy

  1. Kill ports if they are in use by another program.
sudo lsof -t -i tcp:5000 | xargs kill -9
  1. Run the program with poetry
poetry run python web_app.py
  1. Open http://127.0.0.1:5000
  • Usage:
    • Select a file stored in data/ folder web_app_file_selection
    • Click on submit web_app_submit
    • The input/output images and the extracted text will be shown
    • Test with more files. It also manages rotated QRs and images with multiple QR codes

API call

  1. Kill ports if they are in use by another program.
sudo lsof -t -i tcp:8000 | xargs kill -9
  1. Run the program with uvicorn
uvicorn api:app --reload

Test in FastAPI UI

  • Enter into FastAPI UI api_call

  • Usage:

    • Click on Try it out
    • Select a file
    • Click on Execute
    • The response body will be shown api_call_gui_example

Test with a python script in WSL CLI

  • With the program running, you need to open a new tab in Windows terminal api_call_new_tab

  • Run api_request_example.py script with an image stored in data/ folder

    cd ~/truora/QRCodeScanner
    source .venv/bin/activate
    poetry run python api_request_example.py 'qrcode_sproutqr.jpg'
    

    api_call_gui_example

Test with a cURL script in WSL CLI

  • With the program running, you need to open a new tab in Windows terminal api_call_new_tab

  • Run the script with an image stored in data/ folder

    cd ~/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'
    

    api_call_gui_example

Reasoning behind

First, we use a customized module called engine where is a class called QRCodeScanner.

This class have two methods:

  1. read_image: It is used to read files with cv2 library when data is stored locally.
  2. 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.

About

Basic Web Application and an API made with Python + Flask + FastAPI

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published