Interface編集部
10-3CameraQRReader.py
000: import cv2
001: import webbrowser
002: import numpy as np
003:
004: def __main():
005: width = 1280.
006: height = 720.
007: cap = cv2.VideoCapture(0, cv2.CAP_V4L)
008: cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
009: cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
010:
011: if not cap.isOpened(): # ビデオキャプチャー可能か判断
012: print(“Not Opened Video Camera”)
013: exit()
014:
015: margin = 100
016: pt1 = np.array([(width / 2) – margin, (height / 2) – margin], dtype=np.int)
017: pt2 = np.array([(width / 2) + margin, (height / 2) + margin], dtype=np.int)
018: color = (255, 255, 255)
019: while True:
020: ret, img = cap.read()
021: org = img.copy()
022: img = cv2.flip(img, 1) # 画像を左右反転
023: img = cv2.rectangle(img=img, pt1=(pt1[0], pt1[1]), pt2=(pt2[0], pt2[1]), color=color, thickness=2) # thickness=5で線の太さ
024: qrArea = org[pt1[1]:pt2[1], pt1[0]:pt2[0]]
025: qrData = getQRCode(qrArea)
026: if len(qrData) > 0:
027: webbrowser.open(url=qrData, new=1, autoraise=True)
028: cv2.waitKey(0)
029: break
030:
031: cv2.imshow(‘QRCode Reader’, img)
032: if cv2.waitKey(10) > -1:
033: break
034:
035: cap.release()
036: cv2.destroyAllWindows()
037:
038: def getQRCode(img):
039: global code
040: try:
041: data, point, qrcode = code.detectAndDecode(img=img)
042: except Exception as e:
043: print(e)
044: data = “”
045: print(“data = {0}”.format(data))
046:
047: return data
048:
049: if __name__ == ‘__main__’:
050: print(cv2.__version__)
051: code = cv2.QRCodeDetector()
052: __main()