A one-dimensional 、 A two-dimensional 、 Three dimensional bar code
One dimension and two dimensions are common , It's the first time to hear about 3D barcode , I wonder if it's a quantum cloud code .
The introduction of three-dimensional bar code is excerpted on the Internet :
Three dimensional bar code has greater information capacity 、 Same recognition convenience and better security .
The main feature of three-dimensional code is the use of color or gray ( Or black density ) Represent and encode different data .
Actually Python There is a barcode scanning library ,Python2 There has always been a very famous , That's it zbar, But this library . Although cattle , But it has stopped maintenance , If it is python3, Out of commission zbar library .
For now Python3 The most commonly used ones are :pyzbar
pip install pyzbar
Use opencv2, Initialize camera information , Adjust the camera to recognize the image width and height.
import cv2
capture = cv2.VideoCapture(0)
# Camera settings ,
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1024)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1024)
capture.set(cv2.CAP_PROP_EXPOSURE, 0.1)
test cv2 Whether it can be or not? , Call the camera to recognize the image , Press esc sign out
while True:
# Reading images from the camera ,ok It is used to judge whether the read is successful or not
ret,img = capture.read()
cv2.imshow('frame', img)
k = cv2.waitKey(1)
if k == 27: # 'ESC' close
break
Read barcode
import cv2
import csv
import pyzbar.pyzbar as pyzbar # Barcode reading module
barcodeData1 = ''
found = set()
capture = cv2.VideoCapture(0)
# Camera settings
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1024)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1024)
capture.set(cv2.CAP_PROP_EXPOSURE, 0.1)
while True:
# Reading images from the camera ,ok It is used to judge whether the read is successful or not
ret, frame = capture.read()
# Turn to grayscale image
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
barcodes = pyzbar.decode(gray) # Read barcode
print(barcodes)
If the barcode information is recognized, it will be printed as follows :( Line breaks have been added for easy observation )
[
Decoded(
data=b’9787545152210’,
type=‘EAN13’,
rect=Rect(left=677, top=116, width=195, height=136),
polygon=[
Point(x=677, y=239),
Point(x=677, y=249),
Point(x=771, y=251),
Point(x=871, y=252),
Point(x=872, y=118),
Point(x=872, y=116),
Point(x=678, y=117)]
)]
The above information contains data That is, the value of the barcode , And that is Rect The rectangular area is the area of the identified barcode , And some recognized pixels .
If the barcode information is not recognized , Then an empty list will be printed [ ].
In the above code barcodes What is returned is a list information , We can traverse the list information . Get barcode value ,type Type and rectangular area . And use cv2 Draw the rectangle shown in the above figure .
for barcode in barcodes:
# Extract the position of the bounding box of the barcode
(x, y, w, h) = barcode.rect
# Draw the border box of the barcode in the image
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 0), 2)
data=b’9787545152210’ Is a byte object , At this point, we need to convert the byte object into a string .
# Barcode data is a byte object , So if we want to output the image
# Draw out , You need to convert it into a string first
barcodeData = barcode.data.decode("utf-8") # utf-8 code
barcodeType = barcode.type
Read the product information table , Judge the recognized barcode information , Whether it is in the product list , in order to Avoid scanning repeatedly to identify . Used Gather to repeat Barcode information .
code_set = set() # A collection for storing barcodes # Avoid duplicate reads
if barcodeData not in code_set:
with open('shopping.csv', 'r') as rs:
reader = csv.reader(rs) # Traverse the supermarket inventory file
for line in reader:
if barcodeData in line: # It means that the supermarket has this product
print(f' This product exists in our supermarket , name :{
line[1]}, Price :{
line[3]}')
break
else:
pass
else:
pass
code_set.add(barcodeData)
Turn on the camera , If the key is ESC The exit , Identification system
cv2.imshow('qrcode+barcode', frame)
k = cv2.waitKey(1)
if k == 27:
breakcv2.destroyAllWindows()
Complete code :
import cv2import csvimport pyzbar.pyzbar as pyzbarbarcodeData1 = ''code_set = set()capture = cv2.VideoCapture(0)# Camera settings capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1024)capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1024)capture.set(cv2.CAP_PROP_EXPOSURE, 0.1)while True: # Reading images from the camera ,ok It is used to judge whether the read is successful or not ret, frame = capture.read() # Turn to grayscale image gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) barcodes = pyzbar.decode(gray) print(barcodes) for barcode in barcodes: # Extract the position of the bounding box of the barcode # Draw the border box of the barcode in the image (x, y, w, h) = barcode.rect cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 0), 2) # Barcode data is a byte object , So if we want to output the image # Draw out , You need to convert it into a string first barcodeData = barcode.data.decode("utf-8") barcodeType = barcode.type # print(barcodeData) # Judge to scan a bar code multiple times , Print only once if barcodeData == '' or barcodeData != barcodeData1: barcodeData1 = barcodeData print("Recognize result>>> type: {0} content: {1}".format(barcodeType, barcodeData)) else: pass # Avoid duplicate reads if barcodeData not in code_set: with open('shopping.csv', 'r') as rs: reader = csv.reader(rs) # Traverse the supermarket inventory file for line in reader: if barcodeData in line: # It means that the supermarket has this product print(f' This product exists in our supermarket , name :{line[1]}, Price :{line[3]}') break else: pass else: pass code_set.add(barcodeData) cv2.imshow('qrcode+barcode', frame) k = cv2.waitKey(1) if k == 27: breakcv2.destroyAllWindows()