Here is updated version of the code:
import cv2
import numpy as np
# Load the image
inputImage = 'qrkodsaracuna.png'
image = cv2.imread(inputImage)
# Define the border size (quiet zone). A typical quiet zone is 4 modules (or squares) wide,
# but you can adjust based on the size of your QR code.
quiet_zone_size = 10 # Change this size depending on the QR code size and scaling
# Add a white border (quiet zone) around the image
image_with_border = cv2.copyMakeBorder(
image,
top=quiet_zone_size,
bottom=quiet_zone_size,
left=quiet_zone_size,
right=quiet_zone_size,
borderType=cv2.BORDER_CONSTANT,
value=[255, 255, 255] # White color for the border
)
# QR code detection
detector = cv2.QRCodeDetector()
decodedText, points, _ = detector.detectAndDecode(image_with_border)
print('Regular scan: ', inputImage, decodedText, len(decodedText))
And it works!
However this only solves half of my problem.
The file which I provided was a perfect image of a QR code.
In reality, I will be scanning the codes from the paper and trying to read it.
Actually, real life code looks like this:
And I will be reading it from laptop or phone camera.
Here is my current code for this:
def detect_qr_code(self, frame):
quiet_zone_size = 10
frame = cv2.copyMakeBorder(
frame,
top=quiet_zone_size,
bottom=quiet_zone_size,
left=quiet_zone_size,
right=quiet_zone_size,
borderType=cv2.BORDER_CONSTANT,
value=[255, 255, 255] # White color for the border
)
# Initialize the QRCode detector
detector = cv2.QRCodeDetector()
# Detect and decode the QR code in the given frame
data, bbox, _ = detector.detectAndDecode(frame)
# Check if there is a QR code in the frame
if bbox is not None:
bbox = bbox.astype(int) # Ensure bbox is an integer type
# Display the image with lines
for i in range(len(bbox)):
# Draw all lines around the QR code
pt1 = tuple(bbox[i][0])
pt2 = tuple(bbox[(i+1) % len(bbox)][0])
cv2.line(frame, pt1, pt2, color=(255, 0, 0), thickness=2)
if data:
print(f"[+] QR Code detected, data: {data}")
#self.ids.qr_code_result.text = f"QR Code Data: {data}"
time.sleep(3)
else:
print(f"QR Code detected but no data found.")
#self.ids.qr_code_result.text = "QR Code detected but no data found."
else:
#self.ids.qr_code_result.text = "No QR code detected."
pass
Code above is still not able to properly read this QR.
I am not sure if I was supposed to apply the quiet zone in this code, as you said " do not do this to actual pictures of QR codes.".
I would kindly ask for help in reading this, as this is my real life usecase.
Thanks a lot!