Search code examples
pythonopencvpython-imaging-libraryqr-codezbar

using opencv with zbar in python on windows 8.1 to detect qr codes


I am using opencv version 3.1.0 with zbar (latest version as of this post) and PIL (latest version as of this 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"

I don't understand why this is not working it is supposed to constantly check for qrcodes in the current frame of the Video Stream and if it sees one it decodes it and prints what it says inside I hold up printed out qrcodes in front of my webcam and it does not work it shows that my camera is on and that there is a video stream occurring so somewhere in the while loop something is going wrong

I tried it before with qr codes on my computer not printed out and it worked fine

I also tried having it show me the current frame with cv2.imshow("out",cv) but when I did the program showed just a big grey square where it should show the video stream and then it froze so I had to kill Netbeans.


Solution

  • zbar works with grayscale images. Change cv = cv2.cvtColor(cv, cv2.COLOR_BGR2RGB) to cv = cv2.cvtColor(cv, cv2.COLOR_BGR2GRAY).

    I'm guessing you're using this example code to base your program off of. They do the color to grayscale conversion with convert('L') on line 15.