在 windows 8.1 上的 python 中使用带有 zbar 的 opencv 来检测二维码
using opencv with zbar in python on windows 8.1 to detect qr codes
我将 opencv 3.1.0 版与 zbar(截至 post 的最新版本)和 PIL(截至 post 的最新版本)
一起使用
import zbar
import Image
import cv2
# create a reader
scanner = zbar.ImageScanner()
# configure the reader
scanner.parse_config('enable')
#create video capture feed
cap = cv2.VideoCapture(0)
while(True):
ret, cv = cap.read()
cv = cv2.cvtColor(cv, cv2.COLOR_BGR2RGB)
pil = Image.fromarray(cv)
width, height = pil.size
raw = pil.tostring()
# wrap image data
image = zbar.Image(width, height, 'Y800', raw)
# scan the image for barcodes
scanner.scan(image)
# extract results
for symbol in image:
# do something useful with results
print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data
# clean up
print "/n ...Done"
我不明白为什么这不起作用,它应该不断检查视频流当前帧中的二维码,如果它看到一个二维码,它就会解码并打印里面的内容我坚持打印出来我的网络摄像头前的二维码不起作用它表明我的摄像头已打开并且有视频流出现所以 while 循环中的某个地方出了问题
我之前试过,电脑上的二维码没有打印出来,效果很好
我也试过用 cv2.imshow("out",cv)
让它显示当前帧,但是当我这样做时,程序只显示一个大的灰色方块,它应该显示视频流,然后它冻结了,所以我不得不杀死 Netbeans .
zbar 适用于灰度图像。将 cv = cv2.cvtColor(cv, cv2.COLOR_BGR2RGB)
更改为 cv = cv2.cvtColor(cv, cv2.COLOR_BGR2GRAY)
。
我猜您正在使用 this example code 作为您的程序的基础。他们在第 15 行使用 convert('L')
将颜色转换为灰度。
我将 opencv 3.1.0 版与 zbar(截至 post 的最新版本)和 PIL(截至 post 的最新版本)
一起使用import zbar
import Image
import cv2
# create a reader
scanner = zbar.ImageScanner()
# configure the reader
scanner.parse_config('enable')
#create video capture feed
cap = cv2.VideoCapture(0)
while(True):
ret, cv = cap.read()
cv = cv2.cvtColor(cv, cv2.COLOR_BGR2RGB)
pil = Image.fromarray(cv)
width, height = pil.size
raw = pil.tostring()
# wrap image data
image = zbar.Image(width, height, 'Y800', raw)
# scan the image for barcodes
scanner.scan(image)
# extract results
for symbol in image:
# do something useful with results
print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data
# clean up
print "/n ...Done"
我不明白为什么这不起作用,它应该不断检查视频流当前帧中的二维码,如果它看到一个二维码,它就会解码并打印里面的内容我坚持打印出来我的网络摄像头前的二维码不起作用它表明我的摄像头已打开并且有视频流出现所以 while 循环中的某个地方出了问题
我之前试过,电脑上的二维码没有打印出来,效果很好
我也试过用 cv2.imshow("out",cv)
让它显示当前帧,但是当我这样做时,程序只显示一个大的灰色方块,它应该显示视频流,然后它冻结了,所以我不得不杀死 Netbeans .
zbar 适用于灰度图像。将 cv = cv2.cvtColor(cv, cv2.COLOR_BGR2RGB)
更改为 cv = cv2.cvtColor(cv, cv2.COLOR_BGR2GRAY)
。
我猜您正在使用 this example code 作为您的程序的基础。他们在第 15 行使用 convert('L')
将颜色转换为灰度。