Get the location of the QR code on a multi-choice answer form

I am not really computer-fit, but I made my own multi-choice marking programme with Python and cv2.

It works OK, does what I want. I use it regularly. I am always trying to make small improvements

In the top right corner of each multi-choice Answer Form I have a QR code containing the name and student number of each student. That’s how the programme knows where to allocate the score.

Recently, I scanned some forms in, one form was upside down. I need all Answer Forms the right way up, or my marking programme will not function properly.

The first thing my programme does is split the given PDF to jpgs, read the QR code and save the jpg as whatever-student-number.jpg, like 1234567890.jpg.

I’m wondering if there is a way to find the location of the QR code within the jpg, and if it is bottom left, not top right, I can rotate the image 180 degrees.

Can I get that info from pyzbar??

Any tips or pointers please?

I read the QR code with pyzbar

These are the modules I import:

import glob
import os
import pdf2image
import csv
import pyzbar.pyzbar as pyzbar
import numpy as np
import cv2
from PIL import Image, ImageOps
import img2pdf

not sure about pyzbar (probably). OpenCV’s QR code detector/decoder returns the corner points in order, i.e. the first point is always the top left point.

1 Like

Thanks for your reply!

It was easier than I thought!

def checkImageOrentation(path):
    files = glob.glob(path + '*.jpg')
    for f in files:
        print('file is', f)        
        original = cv2.imread(f)
        decodedObjects = pyzbar.decode(original)
        # decodedObjects[0][2][0] is the top left x value, should be about 667
        # if it is less than 500, the page must be upside down, rotate the image
        if decodedObjects[0][2][0] < 500:
            rotateImage(original, f)

decodedObjects[0][2][0] is the top left x value of the QR code.
If the page is the right way up, decodedObjects[0][2][0] should be about 667, depending on the printer.

If a page is upside down, decodedObjects[0][2][0] is only about 47