How to Generate and Read QR Code in Python
QR কোড এক ধরণের ম্যাট্রিক্স বারকোড যা মেশিন রিডেবল অপটিকাল লেবেলে এতে যে আইটেমটি সংযুক্ত থাকে সে সম্পর্কে তথ্য থাকে। অনুশীলনে, কিউআর কোডগুলিতে প্রায়শই কোনও লোকেটার, সনাক্তকারী বা ট্র্যাকারের ডেটা থাকে যা কোনও ওয়েবসাইট বা অ্যাপ্লিকেশনকে নির্দেশ করে।
QR কোড এক ধরণের ম্যাট্রিক্স বারকোড যা মেশিন রিডেবল অপটিকাল লেবেলে এতে যে আইটেমটি সংযুক্ত থাকে সে সম্পর্কে তথ্য থাকে। অনুশীলনে, কিউআর কোডগুলিতে প্রায়শই কোনও লোকেটার, সনাক্তকারী বা ট্র্যাকারের ডেটা থাকে যা কোনও ওয়েবসাইট বা অ্যাপ্লিকেশনকে নির্দেশ করে।
এই টিউটোরিয়ালে , আমরা শিখব কিভাবে একটি qr code তৈরী এবং ব্যবহার করা পাইথন মধ্যে qr কোড পড়তে কিভাবে শিখতে হবে qrcode এবং OpenCV লাইব্রেরি ব্যবহার করে।
প্রয়োজনীয় নির্ভরতা ইনস্টল করা:
pip3 install opencv-python qrcode
কিউআর কোড উত্পন্ন করুন
এটি মূলত qrcode লাইব্রেরি ব্যবহার করে কিউআর কোড উত্পন্ন করার জন্য সরাসরি এগিয়ে রয়েছে :
import qrcode
# example data
data = "https://www.thepythoncode.com"
# output file name
filename = "site.png"
# generate qr code
img = qrcode.make(data)
# save img to a file
img.save(filename)
This will generate a new file in the current directory with the name of "site.png", which contains a QR code image of the data specified, easy enough?
Read QR Code
There are many tools that reads QR code. However, we will be using OpenCV for that, as it is popular and easy to integrate with the webcam. Alright, open up a new Python file and follow along with me:
Let's read the image that is just generated:
import cv2
# read the QRCODE image
img = cv2.imread("site.png")
ভাগ্যক্রমে আমাদের জন্য, ওপেনসিভি ইতিমধ্যে অন্তর্নির্মিত কিউআর কোড সনাক্তকারী পেয়েছে:
# initialize the cv2 QRCode detector
detector = cv2.QRCodeDetector()
আমাদের কাছে চিত্র এবং সনাক্তকারী রয়েছে, আসুন সেই ডেটাটি সনাক্ত এবং ডিকোড করুন:
# detect and decode
data, bbox, straight_qrcode = detector.detectAndDecode(img)
ডিটেকএন্ডডেকোড () ফাংশনটি একটি ইনপুট হিসাবে একটি চিত্র নেয় এবং 3 টি মানের মান দেয়: কিউআর কোড থেকে ডিকোড করা ডেটা, পাওয়া কিউআর কোডের চতুর্ভুজটির উলম্বের আউটপুট অ্যারে এবং সংশোধিত এবং বাইনারিযুক্ত কিউআর কোডযুক্ত আউটপুট চিত্র।
আমাদের এখানে কেবল ডেটা এবং বক্স দরকার , বক্স আমাদের চিত্রের চতুর্ভুজ আঁকতে সহায়তা করবে এবং কনসোলে ডেটা মুদ্রিত হবে!
চল এটা করি:
# if there is a QR code
if bbox is not None:
print(f"QRCode data:\n{data}")
# display the image with lines
# length of bounding box
n_lines = len(bbox)
for i in range(n_lines):
# draw all lines
point1 = tuple(bbox[i][0])
point2 = tuple(bbox[(i+1) % n_lines][0])
cv2.line(img, point1, point2, color=(255, 0, 0), thickness=2)
cv2.line () ফাংশন দুটি পয়েন্টের সাথে সংযোগকারী একটি রেখাংশ অঙ্কন করে, আমরা এই পয়েন্টগুলি বাক্স অ্যারে থেকে পুনরুদ্ধার করি যা ডিটাকড এবং ডিডকোড () এর আগে ডিকোড করা হয়েছিল । আমরা একটি নীল রঙ নির্দিষ্ট করেছি ( (255, 0, 0) ওপেনসিভিতে বিজিআর রঙগুলি ব্যবহার করার কারণে এটি নীল ) এবং 2 এর বেধ ।
অবশেষে, আসুন চিত্রটি প্রদর্শিত হবে এবং একটি কী টিপে গেলে ছেড়ে দিন:
# display the result
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
একবার এটি চালানোর পরে, ডিকোড করা ডেটা মুদ্রিত হবে:
QRCode data:
https://www.thepythoncode.com
এবং নিম্নলিখিত চিত্রটি দেখানো হয়েছে:

আপনি দেখতে পাচ্ছেন, নীল রেখাগুলি সঠিক QR কোড সীমানায় আঁকা
আমরা এই স্ক্রিপ্টটি দিয়ে সম্পন্ন করেছি, এটি বিভিন্ন ডেটা দিয়ে চালানোর চেষ্টা করুন এবং নিজের ফলাফল দেখুন!
আপনি যদি নিজের ওয়েবক্যাম ব্যবহার করে কিউআর কোডগুলি লাইভ সনাক্ত করতে এবং ডিকোড করতে চান (এবং আমি নিশ্চিত যে আপনি করছেন), তার জন্য এখানে একটি কোড রয়েছে:
import cv2
# initalize the cam
cap = cv2.VideoCapture(0)
# initialize the cv2 QRCode detector
detector = cv2.QRCodeDetector()
while True:
_, img = cap.read()
# detect and decode
data, bbox, _ = detector.detectAndDecode(img)
# check if there is a QRCode in the image
if bbox is not None:
# display the image with lines
for i in range(len(bbox)):
# draw all lines
cv2.line(img, tuple(bbox[i][0]), tuple(bbox[(i+1) % len(bbox)][0]), color=(255, 0, 0), thickness=2)
if data:
print("[+] QR Code detected, data:", data)
# display the result
cv2.imshow("img", img)
if cv2.waitKey(1) == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
আমরা এই টিউটোরিয়ালটি দিয়ে শেষ করেছি, আপনি এখন এটি আপনার নিজস্ব অ্যাপ্লিকেশনগুলিতে সংহত করতে পারেন!
Code for How to Generate and Read QR Code in Python
You can also view the full code on github.
generate_qrcode.py
import qrcode
import sys
data = sys.argv[1]
filename = sys.argv[2]
# generate qr code
img = qrcode.make(data)
# save img to a file
img.save(filename)
read_qrcode.py
import cv2
import sys
filename = sys.argv[1]
# read the QRCODE image
img = cv2.imread(filename)
# initialize the cv2 QRCode detector
detector = cv2.QRCodeDetector()
# detect and decode
data, bbox, straight_qrcode = detector.detectAndDecode(img)
# if there is a QR code
if bbox is not None:
print(f"QRCode data:\n{data}")
# display the image with lines
# length of bounding box
n_lines = len(bbox)
for i in range(n_lines):
# draw all lines
point1 = tuple(bbox[i][0])
point2 = tuple(bbox[(i+1) % n_lines][0])
cv2.line(img, point1, point2, color=(255, 0, 0), thickness=2)
# display the result
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
read_qrcode_live.py
import cv2
# initalize the cam
cap = cv2.VideoCapture(0)
# initialize the cv2 QRCode detector
detector = cv2.QRCodeDetector()
while True:
_, img = cap.read()
# detect and decode
data, bbox, _ = detector.detectAndDecode(img)
# check if there is a QRCode in the image
if bbox is not None:
# display the image with lines
for i in range(len(bbox)):
# draw all lines
cv2.line(img, tuple(bbox[i][0]), tuple(bbox[(i+1) % len(bbox)][0]), color=(255, 0, 0), thickness=2)
if data:
print("[+] QR Code detected, data:", data)
# display the result
cv2.imshow("img", img)
if cv2.waitKey(1) == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
0 comments:
Post a Comment