这是我参与更文挑战的第19天,活动详情查看: 更文挑战
OpenCV
是一个C++
库,目前流行的计算机视觉编程库,用于实时处理计算机视觉方面的问题,它涵盖了很多计算机视觉领域的模块。在Python
中常使用OpenCV
库实现图像处理。
本文将介绍如何在Python3
中使用OpenCV
实现摄像头对二维码的识别与解析
本文需要利用电脑自带的摄像头,实现对二维码的实时检测与解析。
我使用的二维码是将我的掘金首页
利用草料二维码
制作成的二维码。扫码后就可以打开我的掘金首页
,我们看看如何利用代码实现摄像头扫码
、二维码检测与解析
、网页打开
等几个功能。
准备工作
本文所使用的模块有:
import cv2
import webbrowser
from pyzbar import pyzbar
cv2
:就是Opencv
;webbrowser
:二维码识别与解析出来的网站可以通过webbrowser
库,调用谷歌浏览器进行显示;pyzbar
:Python3
中用来识别条形码和二维码的库;
代码解读
摄像头打开、二维码动态识别
camera = cv2.VideoCapture(0)
camera.set(3, 1280) # 设置分辨率
camera.set(4, 768)
代码主函数
while True:
(grabbed, frame) = camera.read()
# 获取画面中心点
h1, w1 = frame.shape[0], frame.shape[1]
# 纠正畸变
dst = frame
# 扫描二维码
text = pyzbar.decode(dst)
for texts in text:
textdate = texts.data.decode('utf-8')
print(textdate)
(x, y, w, h) = texts.rect # 获取二维码的外接矩形顶点坐标
print('识别内容:' + textdate)
# 二维码中心坐标
cx = int(x + w / 2)
cy = int(y + h / 2)
cv2.circle(dst, (cx, cy), 2, (0, 255, 0), 8) # 做出中心坐标
print('中间点坐标:', cx, cy)
coordinate = (cx, cy)
# 在画面左上角写出二维码中心位置
cv2.putText(dst, 'QRcode_location' + str(coordinate), (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# 画出画面中心与二维码中心的连接线
cv2.line(dst, (cx, cy), (int(w1 / 2), int(h1 / 2)), (255, 0, 0), 2)
# cv2.rectangle(dst, (x, y), (x + w, y + h), (0, 255, 255), 2) # 做出外接矩形
# 二维码最小矩形
cv2.line(dst, texts.polygon[0], texts.polygon[1], (255, 0, 0), 2)
cv2.line(dst, texts.polygon[1], texts.polygon[2], (255, 0, 0), 2)
cv2.line(dst, texts.polygon[2], texts.polygon[3], (255, 0, 0), 2)
cv2.line(dst, texts.polygon[3], texts.polygon[0], (255, 0, 0), 2)
# 写出扫描内容
txt = '(' + texts.type + ') ' + textdate
cv2.putText(dst, txt, (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 50, 255), 2)
# 使用Google浏览器,打开扫码得到的网站
chromePath = r'C:\Users\37218\AppData\Local\Google\Chrome\Application\chrome.exe '
webbrowser.register('chrome', None, webbrowser.BackgroundBrowser(chromePath))
webbrowser.get('chrome').open(textdate, new=1, autoraise=True)
cv2.imshow('dst', dst)
if cv2.waitKey(1) & 0xFF == ord('S'): # 按S保存一张图片
cv2.imwrite("./frame.jpg", frame)
break
完整代码:
import cv2
import webbrowser
from pyzbar import pyzbar
# 二维码动态识别
camera = cv2.VideoCapture(0)
camera.set(3, 1280) # 设置分辨率
camera.set(4, 768)
while True:
(grabbed, frame) = camera.read()
# 获取画面中心点
h1, w1 = frame.shape[0], frame.shape[1]
# 纠正畸变
dst = frame
# 扫描二维码
text = pyzbar.decode(dst)
for texts in text:
textdate = texts.data.decode('utf-8')
print(textdate)
(x, y, w, h) = texts.rect # 获取二维码的外接矩形顶点坐标
print('识别内容:' + textdate)
# 二维码中心坐标
cx = int(x + w / 2)
cy = int(y + h / 2)
cv2.circle(dst, (cx, cy), 2, (0, 255, 0), 8) # 做出中心坐标
print('中间点坐标:', cx, cy)
coordinate = (cx, cy)
# 在画面左上角写出二维码中心位置
cv2.putText(dst, 'QRcode_location' + str(coordinate), (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# 画出画面中心与二维码中心的连接线
cv2.line(dst, (cx, cy), (int(w1 / 2), int(h1 / 2)), (255, 0, 0), 2)
# cv2.rectangle(dst, (x, y), (x + w, y + h), (0, 255, 255), 2) # 做出外接矩形
# 二维码最小矩形
cv2.line(dst, texts.polygon[0], texts.polygon[1], (255, 0, 0), 2)
cv2.line(dst, texts.polygon[1], texts.polygon[2], (255, 0, 0), 2)
cv2.line(dst, texts.polygon[2], texts.polygon[3], (255, 0, 0), 2)
cv2.line(dst, texts.polygon[3], texts.polygon[0], (255, 0, 0), 2)
# 写出扫描内容
txt = '(' + texts.type + ') ' + textdate
cv2.putText(dst, txt, (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 50, 255), 2)
# 使用Google浏览器,打开扫码得到的网站
chromePath = r'C:\Users\37218\AppData\Local\Google\Chrome\Application\chrome.exe '
webbrowser.register('chrome', None, webbrowser.BackgroundBrowser(chromePath))
webbrowser.get('chrome').open(textdate, new=1, autoraise=True)
cv2.imshow('dst', dst)
if cv2.waitKey(1) & 0xFF == ord('S'): # 按S保存一张图片
cv2.imwrite("./frame.jpg", frame)
break
camera.release()
cv2.destroyAllWindows()
输出为:
本月将陆续推出相关系列文章,
篇篇精彩,尽请关注。