Development of the ocr part of AOI
2fec6b84c1ebc8ea0c257185b83266aae9f57639..9c222b2a0b151e7219e30f0145aa92872890d838
2018-11-21 Samo Penic
Fixes in qr code, in sid third algoritm in answer matrix finding locations...
9c222b diff | tree
2018-11-21 Samo Penic
Fixing template matching
d88ce4 diff | tree
3 files modified
1 files added
54 ■■■■■ changed files
aoiOcr.py 9 ●●●●● patch | view | raw | blame | history
aoi_ocr/Ocr.py 33 ●●●●● patch | view | raw | blame | history
aoi_ocr/sid_process.py 12 ●●●● patch | view | raw | blame | history
aoi_ocr/template-sq.png patch | view | raw | blame | history
aoiOcr.py
@@ -6,7 +6,7 @@
from glob import glob
settings = {"sid_mask": "64xx0xxx", "answer_threshold": 0.25}
settings = {"sid_mask": "11x0xxxx", "answer_threshold": 0.25}
classifier = joblib.load(filepath)
#p = Paper(filename="testpage300dpi_scan1.png")
@@ -26,7 +26,8 @@
    "processed_scans/20160408140801098_0004.tif",
    "processed_scans/20160510075445995_0026.tif"
]
p=Paper(filename=pa[9], sid_classifier=classifier, settings=settings)
#p=Paper(filename=pa[9], sid_classifier=classifier, settings=settings)
p=Paper(filename='test3.tif', sid_classifier=classifier, settings=settings)
# print(p.QRData)
# print(p.errors)
@@ -40,7 +41,7 @@
print(p.get_paper_ocr_data())
exit(0)
filelist = glob("processed_scans/*.tif")
wrong_sid=0;
total=0
@@ -55,4 +56,4 @@
    if total%10 == 0:
        print("Total:{}, wrong SID: {}".format(total,wrong_sid))
print("Total:{}, wrong SID: {}".format(total,wrong_sid))
print("Total:{}, wrong SID: {}".format(total,wrong_sid))
aoi_ocr/Ocr.py
@@ -5,7 +5,7 @@
import os
import pkg_resources
markerfile = '/template.png'  # always use slash
markerfile = '/template-sq.png'  # always use slash
markerfilename = pkg_resources.resource_filename(__name__, markerfile)
@@ -41,6 +41,7 @@
            return
        self.decodeQRandRotate()
        self.imgTreshold()
        cv2.imwrite('/tmp/debug_threshold.png', self.bwimg)
        skewAngle = 0
        #         try:
        #             skewAngle=self.getSkewAngle()
@@ -96,10 +97,10 @@
        self.imgHeight, self.imgWidth = self.img.shape[0:2]
        # todo, make better tresholding
    def imgTreshold(self):
        (self.thresh, self.bwimg) = cv2.threshold(
            self.img, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU
            self.img, 128, 255,
            cv2.THRESH_BINARY | cv2.THRESH_OTSU
        )
    def getSkewAngle(self):
@@ -135,7 +136,7 @@
    def locateUpMarkers(self, threshold=0.85, height=200):
        template = cv2.imread(markerfilename, 0)
        w, h = template.shape[::-1]
        crop_img = self.img[0:height, :]
        crop_img = self.bwimg[0:height, :]
        res = cv2.matchTemplate(crop_img, template, cv2.TM_CCOEFF_NORMED)
        loc = np.where(res >= threshold)
        cimg = cv2.cvtColor(crop_img, cv2.COLOR_GRAY2BGR)
@@ -172,7 +173,8 @@
    def locateRightMarkers(self, threshold=0.85, width=200):
        template = cv2.imread(markerfilename, 0)
        w, h = template.shape[::-1]
        crop_img = self.img[:, -width:]
        crop_img = self.bwimg[:, -width:]
        cv2.imwrite('/tmp/debug_right.png', crop_img)
        res = cv2.matchTemplate(crop_img, template, cv2.TM_CCOEFF_NORMED)
        loc = np.where(res >= threshold)
        cimg = cv2.cvtColor(crop_img, cv2.COLOR_GRAY2BGR)
@@ -188,9 +190,13 @@
                    loc_filtered_y.append(pt[1])
                    loc_filtered_x.append(pt[0])
                    # order by y coordinate
            loc_filtered_y, loc_filtered_x = zip(
                *sorted(zip(loc_filtered_y, loc_filtered_x))
            )
            try:
                loc_filtered_y, loc_filtered_x = zip(
                    *sorted(zip(loc_filtered_y, loc_filtered_x))
                )
            except:
                self.yMarkerLocations=[np.array([1,1]),np.array([1,2])]
                return self.yMarkerLocations
            # loc=[loc_filtered_y,loc_filtered_x]
            # remove duplicates
            a = np.diff(loc_filtered_y) > 40
@@ -237,7 +243,7 @@
        es, err, warn = getSID(
            self.img[
                int(0.04 * self.imgHeight) : int(0.095 * self.imgHeight),
                int(0.7 * self.imgWidth) : int(0.99 * self.imgWidth),
                int(0.65 * self.imgWidth) : int(0.95 * self.imgWidth),
            ],
            self.sid_classifier,
            sid_mask,
@@ -270,9 +276,10 @@
            data = qrdata.split(",")
            retval = {
                "exam_id": int(data[1]),
                "page_no": int(data[3])+1,
                "page_no": int(data[3]),
                "paper_id": int(data[2]),
                "faculty_id": int(data[0]),
                "sid": None
            }
            if len(data) > 4:
                retval["sid"] = data[4]
@@ -285,11 +292,11 @@
        data["errors"] = self.errors
        data["warnings"] = self.warnings
        data["up_position"] = (
            list(self.xMarkerLocations[1] / self.imgWidth),
            list(self.yMarkerLocations[1] / self.imgHeight),
            list(self.xMarkerLocations[0] / self.imgWidth),
            list(self.xMarkerLocations[1] / self.imgHeight),
        )
        data["right_position"] = (
            list(self.xMarkerLocations[1] / self.imgWidth),
            list(self.yMarkerLocations[0] / self.imgWidth),
            list(self.yMarkerLocations[1] / self.imgHeight),
        )
        data["ans_matrix"] = (
aoi_ocr/sid_process.py
@@ -17,14 +17,14 @@
def find_biggest_blob(image, original_image,sid_mask):
    if sid_mask[0] == "1":
        move_left = 45
        move_left = 35
    elif sid_mask[0] == "x":
        move_left = 55
        move_left = 40
    else:
        move_left = 0
       # Remove noise
    image2 = cv2.morphologyEx(
        original_image, cv2.MORPH_OPEN, kernel(2, 2), iterations=7
        original_image, cv2.MORPH_OPEN, kernel(2, 2), iterations=3
    )
    # find biggest block of pixels
    image1 = cv2.morphologyEx(image2, cv2.MORPH_DILATE, kernel(5, 25), iterations=4)
@@ -186,14 +186,14 @@
    sid_err = []
    image = 255 - image
    image_original = image.copy()
    image = img_as_ubyte(image > 100)
    image = img_as_ubyte(image > 70)
    cv2.imwrite("/tmp/enSID0.png", image)
    # Remove noise
    image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel(2, 2), iterations=3)
    #image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel(2, 2), iterations=3)
    # Closing. Connect non connected parts
    image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel(5, 3), iterations=4)
    image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel(5, 1), iterations=4)
    # Again noise removal after closing
    # image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel(8, 8), iterations=1)
aoi_ocr/template-sq.png