From 0d97e9b2d738682ed0aa6349b43a9719e0ca0aa9 Mon Sep 17 00:00:00 2001
From: Samo Penic <samo.penic@gmail.com>
Date: Sat, 17 Nov 2018 18:55:57 +0000
Subject: [PATCH] Created package and setup.py.
---
aoi_ocr/sid_process.py | 27 ++++++++++++++++-----------
1 files changed, 16 insertions(+), 11 deletions(-)
diff --git a/sid_process.py b/aoi_ocr/sid_process.py
similarity index 91%
rename from sid_process.py
rename to aoi_ocr/sid_process.py
index f21fafb..b14d1d3 100644
--- a/sid_process.py
+++ b/aoi_ocr/sid_process.py
@@ -2,6 +2,11 @@
import numpy as np
from skimage import morphology, img_as_ubyte
+import pkg_resources
+
+templatefile = '/template-8.png' # always use slash
+template8 = pkg_resources.resource_filename(__name__, templatefile)
+
def kernel(x, y):
"""
@@ -24,7 +29,7 @@
# find biggest block of pixels
image1 = cv2.morphologyEx(image2, cv2.MORPH_DILATE, kernel(5, 25), iterations=4)
image1 = img_as_ubyte(image1 > 50)
- cv2.imwrite("sidblock1.png", image1)
+ cv2.imwrite("/tmp/sidblock1.png", image1)
im2, ctrs, hier = cv2.findContours(
image1.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
)
@@ -77,7 +82,7 @@
roi = cv2.resize(roi, (32, 32))
# cv2.rectangle(image,(x,y),( x + w, y + h ),(0,255,0),2)
- cv2.imwrite("sid_no_{}.png".format(i), roi)
+ cv2.imwrite("/tmp/sid_no_{}.png".format(i), roi)
sid_no = sid_no + str(classifier.predict(roi.reshape(1, -1) / 255.0)[0])
return sid_no
@@ -96,14 +101,14 @@
sid_no = ""
sid_len = len(sid_mask)
image=find_biggest_blob(image,original_image,sid_mask)
- cv2.imwrite("sidblock2.png", image)
+ cv2.imwrite("/tmp/sidblock2.png", image)
imgHeight, imgWidth = image.shape[0:2]
numWidth = int(imgWidth / (sid_len))
for i in range(0, sid_len):
num = image[:, i * numWidth : (i + 1) * numWidth]
num = img_as_ubyte(num < 128)
num = cv2.resize(num, (32, 32))
- cv2.imwrite("sid_no_{}.png".format(i), num)
+ cv2.imwrite("/tmp/sid_no_{}.png".format(i), num)
sid_no = sid_no + str(classifier.predict(num.reshape(1, -1) / 255.0)[0])
return sid_no
@@ -123,8 +128,8 @@
original_image, cv2.MORPH_CLOSE, kernel(2, 2), iterations=10
)
block_image = img_as_ubyte(block_image < 50)
- cv2.imwrite("sid_3rd1.png", block_image)
- template = cv2.imread("template-8.png", 0)
+ cv2.imwrite("/tmp/sid_3rd1.png", block_image)
+ template = cv2.imread(template8, 0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(block_image, template, cv2.TM_CCOEFF_NORMED)
loc = np.where(res >= 0.75)
@@ -147,7 +152,7 @@
points = [loc_filtered_y[a], loc_filtered_x[a]]
for pt in zip(*points[::-1]):
cv2.rectangle(cimg, pt, (pt[0] + w, pt[1] + h), (0, 255, 255), 2)
- cv2.imwrite("sid_3rd2.png", cimg)
+ cv2.imwrite("/tmp/sid_3rd2.png", cimg)
sid_no = ""
for i, pt in enumerate(zip(*points[::-1])):
@@ -158,7 +163,7 @@
num = cv2.resize(num, (32, 32))
except:
return ""
- cv2.imwrite("sid_3no_{}.png".format(i), num)
+ cv2.imwrite("/tmp/sid_3no_{}.png".format(i), num)
sid_no = sid_no + str(classifier.predict(num.reshape(1, -1) / 255.0)[0])
return sid_no
@@ -182,7 +187,7 @@
image = 255 - image
image_original = image.copy()
image = img_as_ubyte(image > 100)
- cv2.imwrite("enSID0.png", image)
+ cv2.imwrite("/tmp/enSID0.png", image)
# Remove noise
image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel(2, 2), iterations=3)
@@ -197,7 +202,7 @@
# Skeletonization
image = img_as_ubyte(morphology.thin(image > 128))
- cv2.imwrite("enSID1.png", image)
+ cv2.imwrite("/tmp/enSID1.png", image)
# Stub removal (might not be necessary if thinning instead of skeletonize is used above
# Making lines stronger
@@ -207,7 +212,7 @@
# Thining again
image = img_as_ubyte(morphology.skeletonize(image > 0.5))
image = cv2.morphologyEx(image, cv2.MORPH_DILATE, kernel(10, 10))
- cv2.imwrite("enhancedSID.png", image)
+ cv2.imwrite("/tmp/enhancedSID.png", image)
sid_no = segment_by_contours(image, image_original, classifier, sid_mask)
--
Gitblit v1.8.0