Development of the ocr part of AOI
Samo Penic
2018-11-21 d88ce4da04499fd9f48d7a21a7ecded8535e9ab2
Fixing template matching
2 files modified
1 files added
29 ■■■■■ changed files
aoiOcr.py 7 ●●●●● patch | view | raw | blame | history
aoi_ocr/Ocr.py 22 ●●●●● patch | view | raw | blame | history
aoi_ocr/template-sq.png patch | view | raw | blame | history
aoiOcr.py
@@ -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,7 +97,6 @@
        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
@@ -135,7 +135,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 +172,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 +189,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
@@ -273,6 +278,7 @@
                "page_no": int(data[3])+1,
                "paper_id": int(data[2]),
                "faculty_id": int(data[0]),
                "sid": None
            }
            if len(data) > 4:
                retval["sid"] = data[4]
@@ -295,7 +301,7 @@
        data["ans_matrix"] = (
            (np.array(self.answerMatrix) > self.settings["answer_threshold"]) * 1
        ).tolist()
        if data["sid"] is None and data["page_no"] == 1:
        if data["sid"] is None and data["page_no"] == 2:
            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)
aoi_ocr/template-sq.png