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

2D code library based on OpenCV and python

編輯:Python

Write their own two-dimensional code generation and recognition of the library
The test function of each function is under the function , Test yourself

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Read the list of backup files 
def read_backupsname(filePath):
import os
name = os.listdir(filePath)# obtain filepath List of files 
print(name)
return name
# print(len(read_backupsname('./backups/')))
# Create without logo QR code 
def qrcode_make(information):
import qrcode
# information = input(" Enter the information contained in the QR code :\n")
# call qrcode Of make() Methods the incoming url Or what you want to show 
img = qrcode.make(str(information))
# print(img.save)
# Write display file 
with open('show.png', 'wb') as f1:
img.save(f1)
print(len(read_backupsname('./backups/')))
# Write backup file 
with open('backups/backups'+str(len(read_backupsname('./backups/')))+'.png', 'wb') as f2:
img.save(f2)
# qrcode_make('www.baidu.com')
# Create a logo QR code 
def qrcode_make_logo(information,logo,versions):
import qrcode
from PIL import Image
qr = qrcode.QRCode(
version=versions, #1~5 
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
) # establish QRCode object 
if information=='':
print(' Please enter valid information ')
return False
else:
qr.add_data(data=str(information)) # Add data 
qr.make(fit=True)# Change the color of QR code , The default is black 
img = qr.make_image(fill_color="#000000", back_color="white")#make_image(fill_color="#ff3300", back_color="white")
# print(img.size)#(290,290)
# Get the width and height of the QR code 
img_size=img.size
img_w,img_h=img_size
print(img_w,img_h)#410 410
icon = Image.open(logo)# open icons 
# print(icon)
# Set the size of the small icon , The size must be an integer when scaling , Otherwise, the report will be wrong 
icon_w,icon_h = int(img_w/6) ,int(img_h/6)
print(icon_w,icon_h)#68 68
icon_small = icon.resize((icon_w,icon_h),Image.ANTIALIAS) # Image.ANTIALIAS Smooth zoom of the picture 
# Get the coordinates of the pasted small icon , Must be an integer , Otherwise, when pasting , Report errors 
icon_x, icon_y = int((img_w-icon_w)/2 ), int((img_h-icon_h)/2)
print(icon_x,icon_y)#171 171
# Paste the icon in the middle of the QR code .
img.paste(icon_small,(icon_x,icon_y))
# img.show()
# Write display file 
with open('show.png', 'wb') as f1:
img.save(f1)
print(len(read_backupsname('./backups/')))
# Write backup file 
with open('backups/backups'+str(len(read_backupsname('./backups/')))+'.png', 'wb') as f2:
img.save(f2)
return True
# qrcode_make_logo(286153101803429915,'logo.jpg',4)
# Identify QR code chart 
def qrcode_recognition_pic(pic):
import cv2
import numpy as np
# print(pic)
if pic!='':
# Read QR code 
# src = cv2.imread(pic)# Chinese path generation bug
src = cv2.imdecode(np.fromfile(pic,dtype = np.uint8),-1)# Solve the Chinese path problem 
# gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
# Set the detector 
qrcoder = cv2.QRCodeDetector()
# Detect and identify QR code 
codeinfo, points, straight_qrcode = qrcoder.detectAndDecode(src)
# print(points,straight_qrcode)
print(codeinfo, points, straight_qrcode)
result = np.copy(src)
# cv2.drawContours(result, [np.int32(points)], 0, (0, 0, 255), 2)
# Output the information identifying the QR code 
print("qrcode information is : \n%s"% codeinfo)
# display picture 
# cv2.imshow("result", result)
# cv2.imshow("qrcode roi", np.uint8(straight_qrcode))
cv2.waitKey(0)
cv2.destroyAllWindows()
return codeinfo
else:
return -1
# a='E:/AI/ QR code recognition /logo.jpg'
# ret = qrcode_recognition_pic(str(a))
# if ret!='':
# print(ret,'good')
# Identify QR code Camera or video Enter the video name or camera symbol Such as 0 or 1
def qrcode_recognition_video(inputs):
import cv2
import time
cap = cv2.VideoCapture(inputs)
print(cap,cap.isOpened())
if cap.isOpened() == False:
codeinfo=False
t_start = time.time()
# print(t_start) 
while cap.isOpened() == True:
ret,frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Set the detector 
qrcoder = cv2.QRCodeDetector()
# print(qrcoder.rect)
# Detect and identify QR code 
# print(qrcoder.detectAndDecode(gray))
codeinfo, points, straight_qrcode = qrcoder.detectAndDecode(gray)
# print(codeinfo, points, straight_qrcode)
# cv2.drawContours(frame, contours, 0, (0, 0, 255), 2)
# Output the information identifying the QR code 
# print(len(codeinfo))
t_stop = time.time()
t = t_stop-t_start
# print(t)
if len(codeinfo) == 0:
print('not qrcode')
else:
t_start = time.time()
print("qrcode information is : \n%s"%codeinfo)
break
if int(t) > 15 and len(codeinfo) == 0:
break
cv2.imshow("cap", frame)
if cv2.waitKey(10)&0xff == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
return codeinfo
# information = qrcode_recognition_video(1)
# print(information)

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