""" scan_QR_code.py
Recognize QR code using camera.
Press Q to exit the program.
Prerequisites:
pip3 install pyzbar numpy opencv-python
"""
__DATE__ = "2019/10/22"
import pyzbar.pyzbar as pyzbar
import numpy as np
import cv2
CAMERA_INDEX = 1 # which camera to use
def process(img):
""" Detect QR code and barcode in the image.
@param img (numpy.ndarray): the image to be processed.
@return (list): a list containing all the features detected.
"""
# Find barcodes and QR codes
objects = pyzbar.decode(img)
for obj in objects:
print("="*24)
print(obj.rect)
print("Type:", obj.type)
print("Data:", obj.data, "\n")
return objects
# Display barcode and QR code location
def display(img, objects):
""" Display barcode and QR code location.
@param img (numpy.ndarray): the original image
@param objects (list): detected features
"""
for code in objects:
points = code.polygon
# If the points do not form a quad, find convex hull
if len(points) > 4:
hull = cv2.convexHull(np.array(points, dtype=np.float32))
hull = list(map(tuple, np.squeeze(hull)))
else:
hull = points
# Number of points in the convex hull
n = len(hull)
# Draw the convext hull
for j in range(0, n):
cv2.line(img, hull[j], hull[(j+1) % n], (255, 0, 0), 3)
# Display results
cv2.imshow("Result", img)
if __name__ == "__main__":
cap = cv2.VideoCapture(CAMERA_INDEX) # Start camera
while True:
img = cap.read()[1]
objects = process(img)
display(img, objects)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Python | 检测二维码
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 缘起 需要检测发票中二维码的位置,以确定图像该怎么旋转,同时也可以为提取二维码信息创造先觉条件!(万恶的需求!) ...
- github 下载地址: https://github.com/ZenKingLen/KLAboutQRCodeDemo
- 为确保学生安全,宣传防溺水的知识,我校于4月3日组织学生制作以“珍爱生命,预防溺水”为主题的板报。 本次制作板报的...