### Install CPU version of PyTorch Source: https://github.com/eric-canas/qreader/blob/main/README.md Install the CPU-only version of PyTorch before installing QReader. This is recommended for servers with limited resources. ```bash pip install torch --no-cache-dir ``` -------------------------------- ### Install QReader and Dependencies Source: https://github.com/eric-canas/qreader/blob/main/example.ipynb Installs the QReader library and the necessary zbar dependency for QR code scanning. ```bash !pip install qreader !sudo apt-get install libzbar0 ``` -------------------------------- ### Install Test Dependencies Source: https://github.com/eric-canas/qreader/blob/main/README.md Install the test version of the package using pip. This command installs the package in editable mode with its test dependencies. ```bash python -m pip install --editable ".[test"] ``` -------------------------------- ### Install QReader locally Source: https://github.com/eric-canas/qreader/blob/main/README.md Install the QReader package locally in editable mode using pip. This is useful for development. ```bash python -m pip install --editable . ``` -------------------------------- ### Install zbar on Linux Source: https://github.com/eric-canas/qreader/blob/main/README.md Install the zbar library on Linux systems using apt-get. This is a dependency for Pyzbar. ```bash sudo apt-get install libzbar0 ``` -------------------------------- ### Install QReader using pip Source: https://github.com/eric-canas/qreader/blob/main/README.md Install the QReader package using pip. This is the standard method for adding the library to your Python environment. ```bash pip install qreader ``` -------------------------------- ### Run Tests with Pytest Source: https://github.com/eric-canas/qreader/blob/main/README.md Execute the project's tests using pytest. Ensure you have installed the test dependencies first. ```bash python -m pytest tests/ ``` -------------------------------- ### Install zbar on Mac OS X Source: https://github.com/eric-canas/qreader/blob/main/README.md Install the zbar library on Mac OS X using Homebrew. This is a dependency for Pyzbar. ```bash brew install zbar ``` -------------------------------- ### Basic QR Code Detection and Decoding Source: https://github.com/eric-canas/qreader/blob/main/README.md Use this snippet for the most common use case of detecting and decoding QR codes in an image. Ensure you have qreader and cv2 installed. The image should be in RGB or BGR format. ```python from qreader import QReader import cv2 # Create a QReader instance qreader = QReader() # Get the image that contains the QR code image = cv2.cvtColor(cv2.imread("path/to/image.png"), cv2.COLOR_BGR2RGB) # Use the detect_and_decode function to get the decoded QR data decoded_text = qreader.detect_and_decode(image=image) ``` -------------------------------- ### Decode QR Codes with QReader, OpenCV, and pyzbar Source: https://github.com/eric-canas/qreader/blob/main/README.md This snippet initializes and uses QReader, OpenCV's QRCodeDetector, and pyzbar to decode QR codes from image files. It compares the output of all three libraries for each image. Ensure you have these libraries installed and the image files ('test_mobile.jpeg', 'test_draw_64x64.jpeg') are in the same directory. ```python from qreader import QReader from cv2 import QRCodeDetector, imread from pyzbar.pyzbar import decode # Initialize the three tested readers (QRReader, OpenCV and pyzbar) qreader_reader, cv2_reader, pyzbar_reader = QReader(), QRCodeDetector(), decode for img_path in ('test_mobile.jpeg', 'test_draw_64x64.jpeg'): # Read the image img = imread(img_path) # Try to decode the QR code with the three readers qreader_out = qreader_reader.detect_and_decode(image=img) cv2_out = cv2_reader.detectAndDecode(img=img)[0] pyzbar_out = pyzbar_reader(image=img) # Read the content of the pyzbar output (double decoding will save you from a lot of wrongly decoded characters) pyzbar_out = tuple(out.data.data.decode('utf-8').encode('shift-jis').decode('utf-8') for out in pyzbar_out) # Print the results print(f"Image: {img_path} -> QReader: {qreader_out}. OpenCV: {cv2_out}. pyzbar: {pyzbar_out}.") ``` -------------------------------- ### QReader Class Initialization Source: https://github.com/eric-canas/qreader/blob/main/README.md Initializes the QReader object. It's recommended to instantiate this class only once to optimize model loading. ```APIDOC ## QReader(model_size = 's', min_confidence = 0.5, reencode_to = 'shift-jis', weights_folder = None) ### Description This is the main class of the library. Please, try to instantiate it just once to avoid loading the model every time you need to detect a QR code. ### Parameters #### Initialization Parameters - **model_size** (str) - Optional - The size of the model to use. It can be 'n' (nano), 's' (small), 'm' (medium) or 'l' (large). Larger models could be more accurate but slower. Recommended: 's'. Default: 's'. - **min_confidence** (float) - Optional - The minimum confidence of the QR detection to be considered valid. Values closer to 0.0 can get more False Positives, while values closer to 1.0 can lose difficult QRs. Default (and recommended): 0.5. - **reencode_to** (str | None) - Optional - The encoding to reencode the `utf-8` decoded QR string. If None, it won't re-encode. If you find some characters being decoded incorrectly, try to set a Code Page that matches your specific charset. Recommendations: 'shift-jis' for Germanic languages, 'cp65001' for Asian languages. Default: 'shift-jis'. - **weights_folder** (str|None) - Optional - Folder where the detection model will be downloaded. If None, it will be downloaded to the default qrdet package internal folder. Default: `None`. ``` -------------------------------- ### Import Necessary Libraries Source: https://github.com/eric-canas/qreader/blob/main/example.ipynb Imports the required libraries for QReader functionality, image manipulation, and network requests. ```python from qreader import QReader import requests import cv2 import numpy as np import requests ``` -------------------------------- ### QReader.detect Source: https://github.com/eric-canas/qreader/blob/main/README.md Detects QR codes within an image and returns detailed information about each detection. ```APIDOC ## QReader.detect(image, is_bgr) ### Description This method detects the QR codes in the image and returns a tuple of dictionaries with all the detection information. ### Method Not specified (likely a Python method call) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **image** (np.ndarray) - Required - The image to be read. It is expected to be RGB or BGR (uint8). Format (HxWx3). - **is_bgr** (boolean) - Optional - If True, the received image is expected to be BGR instead of RGB. ### Returns - **tuple[dict[str, np.ndarray|float|tuple[float|int, float|int]]]** - A tuple of dictionaries containing all the information of every detection. Contains the following keys: - `confidence` (float) - Detection confidence. - `bbox_xyxy` (np.ndarray) - Bounding box in [x1, y1, x2, y2] format. - `cxcy` (tuple[float, float]) - Center of bounding box in (x, y) format. - `wh` (tuple[float, float]) - Bounding box width and height in (w, h) format. - `polygon_xy` (np.ndarray) - Precise polygon that segments the QR. - `quad_xy` (np.ndarray) - Four corners polygon that segments the QR. - `padded_quad_xy` (np.ndarray) - `quad_xy` padded to fully cover `polygon_xy`. - `image_shape` (tuple[int, int]) - Shape of the input image in (h, w) format. ### Request Example ```python # Example usage (assuming image is loaded as a numpy array) detector.detect(image) ``` ### Response #### Success Response - **tuple[dict]** - A tuple of dictionaries, each containing detection information. ``` -------------------------------- ### Detect and Decode QR Codes from URL Source: https://github.com/eric-canas/qreader/blob/main/example.ipynb Loads an image from a URL, detects QR codes within it, and decodes their content. It also returns the location of the detected QR codes. ```python # Read image directly from URL as np.array img = np.asarray(bytearray(requests.get('https://raw.githubusercontent.com/Eric-Canas/QReader/main/documentation/resources/logo.png').content), dtype=np.uint8) img = cv2.imdecode(img, cv2.IMREAD_COLOR) detector = QReader() # Detect and decode the QRs within the image decodedQRs, QRlocations = detector.detect_and_decode(image=img, return_detections=True) # Print the results for i, (decodedQR, QRlocation) in enumerate(zip(decodedQRs, QRlocations)): print(f"QR {i+1}: {decodedQR}") print(f"QR {i+1} position: x: {QRlocation['cxcyn'][0]}, y: {QRlocation['cxcyn'][1]}") #print(f"Full detection info: {QRlocation}") ``` -------------------------------- ### QReader.detect_and_decode Source: https://github.com/eric-canas/qreader/blob/main/README.md Detects and decodes QR codes within an image. It can return just the decoded text or detailed detection information. ```APIDOC ## QReader.detect_and_decode(image, return_detections = False) ### Description This method will decode the QR codes in the given image and return the decoded strings (or None, if any of them was detected but not decoded). ### Parameters #### Path Parameters - **image** (np.ndarray) - Required - The image to be read. It is expected to be RGB or BGR (uint8). Format (HxWx3). - **return_detections** (bool) - Optional - If True, it will return the full detection results together with the decoded QRs. If False, it will return only the decoded content of the QR codes. Default: False. - **is_bgr** (boolean) - Optional - If True, the received image is expected to be BGR instead of RGB. ### Returns - **tuple[str | None] | tuple[tuple[dict[str, np.ndarray | float | tuple[float | int, float | int]]], str | None]]**: A tuple with all detected QR codes decodified. If `return_detections` is `False`, the output will look like: `('Decoded QR 1', 'Decoded QR 2', None, 'Decoded QR 4', ...)`. If `return_detections` is `True` it will look like: `(('Decoded QR 1', {'bbox_xyxy': (x1_1, y1_1, x2_1, y2_1), 'confidence': conf_1}), ('Decoded QR 2', {'bbox_xyxy': (x1_2, y1_2, x2_2, y2_2), 'confidence': conf_2, ...}), ...)`. ``` -------------------------------- ### QReader.decode Source: https://github.com/eric-canas/qreader/blob/main/README.md Decodes a single QR code from an image using provided detection results. ```APIDOC ## QReader.decode(image, detection_result) ### Description This method decodes a single QR code on the given image, described by a detection result. Internally, this method will run the pyzbar decoder, using the information of the `detection_result`, to apply different image preprocessing techniques that heavily increase the decoding rate. ### Method Not specified (likely a Python method call) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **image** (np.ndarray) - Required - NumPy Array with the image that contains the QR to decode. The image is expected to be in uint8 format [HxWxC], RGB. - **detection_result** (dict) - Required - One of the detection dicts returned by the detect method. Note that QReader.detect() returns a tuple of these dict. This method expects just one of them. ### Returns - **str | None** - The decoded content of the QR code or None if it couldn't be read. ### Request Example ```python # Assuming detection_results is a tuple of dicts from QReader.detect() detector.decode(image, detection_results[0]) ``` ### Response #### Success Response - **str** - The decoded content of the QR code. #### Error Response - **None** - Returned if the QR code could not be read. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.