From ac766ed5ec375a384da5c454103aef055aa9344a Mon Sep 17 00:00:00 2001 From: Samo Penic <samo.penic@gmail.com> Date: Fri, 16 Nov 2018 20:41:33 +0000 Subject: [PATCH] recognition is a bit more robust.... --- aoiOcr.py | 4 ++-- sid_process.py | 47 +++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/aoiOcr.py b/aoiOcr.py index 74e4fdb..02eef68 100644 --- a/aoiOcr.py +++ b/aoiOcr.py @@ -6,8 +6,8 @@ classifier = joblib.load("filename.joblib") #p = Paper(filename="testpage300dpi_scan1.png") -p=Paper(filename='sizif111.tif', sid_classifier=classifier, settings=settings) -# p=Paper(filename='processed_scans/20141016095134535_0028.tif') +#p=Paper(filename='sizif111.tif', sid_classifier=classifier, settings=settings) +p=Paper(filename='processed_scans/20141016095134535_0006.tif', sid_classifier=classifier, settings=settings) # print(p.QRData) # print(p.errors) diff --git a/sid_process.py b/sid_process.py index 67c689a..48326c0 100644 --- a/sid_process.py +++ b/sid_process.py @@ -79,6 +79,32 @@ return sid_no +def segment_by_sid_len(image,sid_len, classifier): + sid_no="" + #find biggest block of pixels + + image1=cv2.morphologyEx(image,cv2.MORPH_DILATE, kernel(5,25), iterations=3) + cv2.imwrite("sidblock1.png",image1) + im2, ctrs, hier = cv2.findContours( + image1.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE + ) + sorted_ctrs = sorted(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:x+w-25] + cv2.imwrite("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.rectangle(image,(x,y),( x + w, y + h ),(0,255,0),2) + cv2.imwrite("sid_no_{}.png".format(i), num) + sid_no = sid_no + str(classifier.predict(num.reshape(1, -1) / 255.0)[0]) + return sid_no + + def getSID(image, classifier, sid_mask): image = 255 - image image = img_as_ubyte(image > 100) @@ -89,7 +115,9 @@ image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel(5, 3), 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) + image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel(3, 3), iterations=1) + # Skeletonization image = img_as_ubyte(morphology.thin(image > 128)) cv2.imwrite("enSID1.png", image) @@ -101,18 +129,21 @@ # 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) im2, ctrs, hier = cv2.findContours( image.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE ) sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0]) sid_no = "" - sid_len = 0 - if sid_mask is not None: - if len(sid_mask)==len(sorted_ctrs): - sid_no=segment_by_contours(image,sorted_ctrs,classifier) - else: - print("Ooops have to find another way") + #sid_len = len(sid_mask) + #sid_no = segment_by_sid_len(image, sid_len, classifier) + #if sid_mask is not None: + print(len(sid_mask),len(sorted_ctrs)) + #if len(sid_mask)==len(sorted_ctrs): + sid_no=segment_by_contours(image,sorted_ctrs[1:],classifier) print(sid_no) + if(len(sid_no)!=len(sid_mask)): + print("Ooops have to find another way") + sid_no=segment_by_sid_len(image,len(sid_mask),classifier) return sid_no -- Gitblit v1.9.3