Development of the ocr part of AOI
Samo Penic
2018-11-18 2fec6b84c1ebc8ea0c257185b83266aae9f57639
aoi_ocr/Ocr.py
@@ -2,7 +2,7 @@
from .sid_process import getSID
import cv2
import numpy as np
import os
import pkg_resources
markerfile = '/template.png'  # always use slash
@@ -11,8 +11,9 @@
class Paper:
    def __init__(self, filename=None, sid_classifier=None, settings=None):
    def __init__(self, filename=None, sid_classifier=None, settings=None, output_path="/tmp"):
        self.filename = filename
        self.output_path=output_path
        self.invalid = None
        self.QRData = None
        self.settings = {"answer_threshold": 0.25} if settings is None else settings
@@ -260,7 +261,7 @@
        if self.QRDecode[0].type == "EAN13":
            return {
                "exam_id": int(qrdata[0:7]),
                "page_no": int(qrdata[7]),
                "page_no": int(qrdata[7])+1,
                "paper_id": int(qrdata[-5:-1]),
                "faculty_id": None,
                "sid": None,
@@ -269,7 +270,7 @@
            data = qrdata.split(",")
            retval = {
                "exam_id": int(data[1]),
                "page_no": int(data[3]),
                "page_no": int(data[3])+1,
                "paper_id": int(data[2]),
                "faculty_id": int(data[0]),
            }
@@ -280,7 +281,7 @@
    def get_paper_ocr_data(self):
        data = self.get_code_data()
        data["qr"] = self.QRData
        data["qr"] = bytes.decode(self.QRData, 'utf8')
        data["errors"] = self.errors
        data["warnings"] = self.warnings
        data["up_position"] = (
@@ -294,6 +295,9 @@
        data["ans_matrix"] = (
            (np.array(self.answerMatrix) > self.settings["answer_threshold"]) * 1
        ).tolist()
        if data["sid"] is None and data["page_no"] == 0:
        if data["sid"] is None and data["page_no"] == 1:
            data["sid"] = self.get_enhanced_sid()
        output_filename=os.path.join(self.output_path, '.'.join(self.filename.split('/')[-1].split('.')[:-1])+".png")
        cv2.imwrite(output_filename, self.img)
        data['output_filename']=output_filename
        return data