Python – How to detect a single QR code using OpenCV, Python and zbar

opencvpythonzbar

I'm using the bottom camera of an AR Drone to detect a QR code in order for me to know the whereabouts of the drone on a chessboard. The drone hovers over the chessboard, where each single square is a QR code holding the position(e.g. A1, C5, E7, etc.). When I press a certain key, it scans the QR code and returns to me the position.

Right now, I would like to be able to detect a single QR code out of many. Since it is possible that the drone has multiple QR codes in sight. Because I need to know which exact square the drone is on, or at least the closest one(for example: 2/3 above A1 and 1/3 above A2 should result in A1). Here's the code I'm currently using:

#!/usr/bin/python
from sys import argv
import zbar
import Image
import cv2

class DetectQRCode(object):

    @classmethod
    def detect_qr(self, image):
        # create a reader
        scanner = zbar.ImageScanner()

        # configure the reader
        scanner.parse_config('enable')

        # obtain image data
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY,dstCn=0)
        pil = Image.fromarray(gray)
        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
            if symbol.data == "None":
                return "Drone bevindt zich buiten het raster"
            else:
                return symbol.data

Can this be done using OpenCV, Python? Does zbar have something I could use?

Best Solution

I would suggest the best way to do this would be to detect all the boundaries around the QR codes (the largest squares) first, this will give you a list of QR codes in an image.

You can then sort through this list for the one that is closest the drone position & then run your QR code reader on that specific QR code.

Here is a tutorial on how to detect squares using opencv.

& here is a stack overflow question showing how to detect the bounding box that has additional squares inside of it.