Python writes a small program for mobile phone scanning QR code service

import qrcode
import cv2
import pyautogui

# Open camera
cap = cv2.VideoCapture(0)

while True:
# Read camera image
ret, frame = cap.read()

# Convert to grayscale image
gray = cv2.cvtColor(frame, cv2. COLOR_BGR2GRAY)

# Binarized image
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

# Find contour
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# traverse all profiles
for cnt in contours:
# Calculate the contour rectangle
x, y, w, h = cv2.boundingRect(cnt)

# Determine whether it is a valid QR code
if w / h > 2 and w / h < 5:
# On the image Draw a rectangular frame
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) #

extract the QR code image
qr_img = gray[y:y + h, x:x + w]

# decode the QR code
data = qrcode.make(qr_img)
decoder = qrcode.Decoder()
result = decoder.decode(data)

# draw decoding on the image Result
cv2.putText(frame, result, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

# display image

Guess you like

Origin blog.csdn.net/ducanwang/article/details/131726357