You are on page 1of 1

import cv2 thư viện opencv

import numpy as np thư viện munpy


from pyzbar.pyzbar import decode thư viện decode

#img = cv2.imread('1.png') mở ảnh


cap = cv2.VideoCapture(0) mở video
cap.set(3,640) tốc độ ghi khung hình
cap.set(4,480)

with open('mydatafile') as f:
myDataList = f.read().splitlines()
with open('mydatafile2') as f:
myDataList2 = f.read().splitlines()

while True:

success, img = cap.read()nếu giá trị true đưa ảnh chụp vào biến img
for barcode in decode(img): decode img
myData = barcode.data.decode('utf-8')mã hóa kí tự theo chuẩn unicode
utf8
print(myData) in data
if myData in myDataList :
myOutput = 'loai 1'
myColor = (0, 255, 0)
print(myOutput)
elif myData in myDataList2:
myOutput = 'loai 2'
myColor = (255, 0, 255)
print(myOutput)
else:
myOutput = 'loai 3'
myColor = (0, 0, 255)
print(myOutput)

pts = np.array([barcode.polygon],np.int32) tạo mảng chứa các tọa độ


các điểm thành 1 vùng khép kín trên qr
pts = pts.reshape((-1,1,2)) chỉnh lại ảnh cho vuông góc
cv2.polylines(img,[pts],True,(255,0,255),5) tạo chuỗi các đường thẳng
nối các điểm lại vs nhau ra hình vuông
pts2 = barcode.rect gán giá trị biến pts2 = hàm đọc barcode của cv2
cv2.putText(img,myData,(pts2[0],pts2[1]),cv2.FONT_HERSHEY_SIMPLEX,
0.9,(255,0,255),2) in ra màn hình

cv2.imshow('Result',img) show kết quả


cv2.waitKey(1)

You might also like