From 8dad6520729d26d90ebee48939d7f2a1fd73dd38 Mon Sep 17 00:00:00 2001
From: Samo Penic <samo.penic@gmail.com>
Date: Thu, 24 Jan 2019 15:24:06 +0000
Subject: [PATCH] Fix in pages that don't have qr code

---
 aoi_ocr/sid_process.py |   46 ++++++++++++++++++++++++++--------------------
 1 files changed, 26 insertions(+), 20 deletions(-)

diff --git a/aoi_ocr/sid_process.py b/aoi_ocr/sid_process.py
index 04dbc8d..5d965ec 100644
--- a/aoi_ocr/sid_process.py
+++ b/aoi_ocr/sid_process.py
@@ -4,7 +4,7 @@
 
 import pkg_resources
 
-templatefile = '/template-8.png'  # always use slash
+templatefile = "/template-8.png"  # always use slash
 template8 = pkg_resources.resource_filename(__name__, templatefile)
 
 
@@ -15,19 +15,19 @@
     return np.ones((x, y), np.uint8)
 
 
-def find_biggest_blob(image, original_image,sid_mask):
+def find_biggest_blob(image, original_image, sid_mask):
     if sid_mask[0] == "1":
         move_left = 45
     elif sid_mask[0] == "x":
-        move_left = 55
+        move_left = 50
     else:
         move_left = 0
-       # Remove noise
+    # 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)
+    image1 = cv2.morphologyEx(image, cv2.MORPH_DILATE, kernel(5, 25), iterations=3)
     image1 = img_as_ubyte(image1 > 50)
     cv2.imwrite("/tmp/sidblock1.png", image1)
     im2, ctrs, hier = cv2.findContours(
@@ -37,8 +37,9 @@
         ctrs, key=lambda ctr: cv2.contourArea(ctr)
     )  # get bigges contour
     x, y, w, h = cv2.boundingRect(sorted_ctrs[-1])
-    image = image[y : y + h, x + 25 - move_left : x + w - 40]  # +25,-25
+    image = image[y : y + h, x + 25 - move_left : x + w - 30]  # +25,-25
     return image
+
 
 def sid_compare(sid_no, sid_mask):
     """
@@ -53,7 +54,7 @@
     return True
 
 
-def segment_by_contours(image, original_image, classifier,sid_mask):
+def segment_by_contours(image, original_image, classifier, sid_mask):
     """
     First algorithm. it segments numerals with contours. It works with numbers where individual numerals does not touch.
     :param image:
@@ -63,8 +64,8 @@
     """
 
     sid_no = ""
-    image=find_biggest_blob(image,original_image,sid_mask)
-    cv2.imwrite("/tmp/sid_contour1.png",image)
+    image = find_biggest_blob(image, original_image, sid_mask)
+    cv2.imwrite("/tmp/sid_contour1.png", image)
     im2, ctrs, hier = cv2.findContours(
         image.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
     )
@@ -74,7 +75,7 @@
         # Get bounding box
         x, y, w, h = cv2.boundingRect(ctr)
         # Getting ROI
-        if w < h / 2:
+        if w < h / 3:
             sid_no = sid_no + "1"
             continue
         roi = image[y : y + h, x : x + w]
@@ -83,7 +84,11 @@
 
         # cv2.rectangle(image,(x,y),( x + w, y + h ),(0,255,0),2)
         cv2.imwrite("/tmp/sid_no_{}.png".format(i), roi)
-        sid_no = sid_no + str(classifier.predict(roi.reshape(1, -1) / 255.0)[0])
+        sid_currno= str(classifier.predict(roi.reshape(1, -1) / 255.0)[0])
+        sid_no = sid_no + sid_currno
+        fname="/tmp/SID/"+str(sid_currno)+"/sid"+str(np.random.randint(0,1000000))+".png"
+        print("Writing all the sid images ", fname)
+        cv2.imwrite(fname, roi)
     return sid_no
 
 
@@ -100,7 +105,7 @@
     """
     sid_no = ""
     sid_len = len(sid_mask)
-    image=find_biggest_blob(image,original_image,sid_mask)
+    image = find_biggest_blob(image, original_image, sid_mask)
     cv2.imwrite("/tmp/sidblock2.png", image)
     imgHeight, imgWidth = image.shape[0:2]
     numWidth = int(imgWidth / (sid_len))
@@ -186,27 +191,28 @@
     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(3, 3), iterations=2)
 
     # 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)
+    #image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel(8, 8), iterations=1)
     # don't do too much noise removal.
-    image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel(3, 3), iterations=1)
+    #image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel(3, 3), iterations=2)
 
     # Skeletonization
+    #image = img_as_ubyte(morphology.skeletonize(image > 128))
     image = img_as_ubyte(morphology.thin(image > 128))
     cv2.imwrite("/tmp/enSID1.png", image)
 
     # Stub removal (might not be necessary if thinning instead of skeletonize is used above
     # Making lines stronger
-    image = cv2.morphologyEx(image, cv2.MORPH_DILATE, kernel(5, 5), iterations=1)
+    image = cv2.morphologyEx(image, cv2.MORPH_DILATE, kernel(5, 2), iterations=1)
     image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel(10, 10))
 
     # Thining again
@@ -226,5 +232,5 @@
 
     if not sid_compare(sid_no, sid_mask):
         sid_err = ["Wrong SID!"]
-
+    cv2.imwrite("/tmp/SID_"+sid_no+".png", image)
     return sid_no, sid_err, sid_warn

--
Gitblit v1.9.3