搞完工程訓練搬運小車後,閒來無事,我就想利用opencv來搞一下識別二維碼:
自制掃碼器:
先看運行結果:
由於CSDN不能傳mp4,所以各位下載百度網盤打開,下載看視頻吧:
鏈接:https://pan.baidu.com/s/1dkkdfrA86mAS_Up2HjhQOQ 提取碼:qk3y
我們來看部分代碼:
引入庫:
import cv2
import random
#掃碼鏡頭
class Shot(object):
def __init__(self,frame,x,y):
self.x = x
self.y = y
def shot(self):
#第一象限
if self.x > 300 and self.y < 250:
cv2.line(frame,(self.x,self.y-d),(self.x,self.y),(0,255,0),3)
cv2.line(frame,(self.x+d,self.y),(self.x,self.y),(0,255,0),3)
#第四象限
if self.x > 300 and self.y > 250:
cv2.line(frame,(self.x,self.y+d),(self.x,self.y),(0,255,0),3)#豎
cv2.line(frame,(self.x+d,self.y),(self.x,self.y),(0,255,0),3)#橫
#第二象限
if self.x < 300 and self.y < 250:
cv2.line(frame,(self.x,self.y-d),(self.x,self.y),(0,255,0),3)
cv2.line(frame,(self.x-d,self.y),(self.x,self.y),(0,255,0),3)
#第三象限
if self.x < 300 and self.y > 250:
cv2.line(frame,(self.x,self.y+d),(self.x,self.y),(0,255,0),3)#豎
cv2.line(frame,(self.x-d,self.y),(self.x,self.y),(0,255,0),3)#橫
這一部分是我寫的【掃碼鏡頭】,即圖中箭頭所指的部分:
引用的時候:
#掃碼邊線
for m in (200,400):
for n in (180,400):
sho = Shot(frame,m,n)
sho.shot()
各位如果看到這裏,如果有更簡單的繪製瞄準鏡頭的方式,可千萬一定留言告訴我啊!!!
這一部分是掃描線,和掃描特效:
#掃描橫條
def saomiao(frame):
#每單個掃碼線間距爲5
for i in range(0,50):
# cv2.line(frame,(0,y-10),(800,y-10),(0,255,0),1)
# cv2.line(frame,(0,y-5),(800,y-5),(0,255,0),1)
cv2.line(frame,(0,y-i*5),(800,y-i*5),(r,255,0),1)
#隨機的 5*5 像素的彩色方塊
def texiao(frame,x_rand,y_rand):
cv2.rectangle(frame, (x_rand,y_rand), (x_rand+5,y_rand+5), (r,255,0),2) #12
如圖所示:
我們日常生活中掃碼的時候,我們發現,掃描線到一定位置後,顏色慢慢變淺,然後隨即消失,來來往復,矢志不渝,
這一段代碼,我是這樣實現的:
首先定義全局變量,r
r = 0 #漸變顏色初始值 即 => R,G,B 中的 r
然後:
if clicked:
if y > 250 and r < 255:
r += 5
if y > 300:
texiao(frame,x_rand,y_rand)#驗證碼拂過的特效
decodeDisplay(frame)#識別驗證碼
if y == 700:
y = 50
r = 0
else:
y += 5
saomiao(frame)
這樣可以達到顏色漸變的過程!
掃描線下降的實現是座標的變化,
我們用opencv中的cv2.line來畫直線
cv2.line(frame,(0,y-i5),(800,y-i5),(r,255,0),1)
我們用opencv中的cv2.rectangle來畫矩形
cv2.rectangle(frame, (x_rand,y_rand), (x_rand+5,y_rand+5), (r,255,0),2)
掃描後,我們必須識別二維碼:
識別二維碼我用的是一個庫
import pyzbar.pyzbar as pyzbar
def decodeDisplay(frame):
# cv2.rectangle(frame, (200, 180), (400, 400), green) #12
barcodes = pyzbar.decode(frame)
for barcode in barcodes:
# 提取條形碼的邊界框的位置
# 畫出圖像中條形碼的邊界框
(x, y, w, h) = barcode.rect
if(x>200 and y>180 and x+w<400 and y+h <400):
cv2.rectangle(frame, (x, y), (x + w, y + h), (205, 127, 50), 3)
# 條形碼數據爲字節對象,所以如果我們想在輸出圖像上
# 畫出來,就需要先將它轉換成字符串
barcodeData = barcode.data.decode("utf-8")
barcodeType = barcode.type
# 繪出圖像上條形碼的數據和條形碼類型
text = "{} ({})".format(barcodeData, barcodeType)
cv2.putText(frame, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
.5, (205, 127, 50), 2)
print(1)
os.system('"C:/Program Files/Internet Explorer/iexplore.exe" {}'.format(barcodeData))
# 向終端打印條形碼數據和條形碼類型
#print("[INFO] Found {} barcode: {}".format(barcodeType, barcodeData))
return 1
else:
print('請規範你的位置!')
return 0
識別出來,在ipython中打印 識別出來的網站,我們利用系統自帶瀏覽器打開它,代替手機識別二維碼後彈出的畫面:
os.system(’“C:/Program Files/Internet Explorer/iexplore.exe” {}’.format(barcodeData))
在此之前還得引入標準庫:
import os
這就是本例主要內容,有別的思路的老鐵就留言,一起研究下,發佈完這篇,我就用Fast-R-CNN來封裝他,進行目標檢測掃描…