程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python recognition barcode

編輯:Python

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

install pyzbar

pip install pyzbar

Read the QR code

step 1:

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)

step 2:

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

step 3:

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 [ ].

step 4

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)

step 5

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

step 6

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)

step 7

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()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved