-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d04b2e7
Showing
2 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import cv2 | ||
import numpy as np | ||
import pytesseract | ||
from autocorrect import spell | ||
from string import printable | ||
|
||
def getRoi(image, x1, x2, y1, y2): | ||
ht, wd, ch = image.shape | ||
print('height {} width {} channels {}'.format(ht, wd, ch)) | ||
xmin = x1 - 12 | ||
xmax = x2 + 12 | ||
ymin = y1 - 12 | ||
ymax = y2 + 12 | ||
|
||
if xmin < 0: | ||
xmin = 0 | ||
if ymin < 0: | ||
ymin = 0 | ||
if xmax > wd: | ||
xmax = wd | ||
if ymax > ht: | ||
ymax = ht | ||
print('xmin {} xmax {} ymin {} ymax {}'.format(xmin, xmax, ymin, ymax)) | ||
tb = image[ymin:ymax, xmin:xmax] | ||
return tb | ||
|
||
def cleanUpTextArea(image): | ||
try: | ||
height, width = image.shape[:2] | ||
res = cv2.resize(image, (3 * width, 3 * height), interpolation=cv2.INTER_CUBIC) | ||
gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY) | ||
blur = cv2.GaussianBlur(gray, (5, 5), 0) | ||
denoise = cv2.fastNlMeansDenoising(blur) | ||
thresh = cv2.threshold(denoise, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] | ||
final = cv2.GaussianBlur(thresh, (5, 5), 0) | ||
return final | ||
except Exception as e: | ||
print("unable to display text block") | ||
return e | ||
|
||
def correctSkew(image): | ||
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | ||
gray = cv2.bitwise_not(gray) | ||
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] | ||
coords = np.column_stack(np.where(thresh > 0)) | ||
angle = cv2.minAreaRect(coords)[-1] | ||
if angle < -45: | ||
angle = -(90 + angle) | ||
else: | ||
angle = -angle | ||
|
||
(h, w) = image.shape[:2] | ||
center = (w // 2, h // 2) | ||
M = cv2.getRotationMatrix2D(center, angle, 1.0) | ||
rotated = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE) | ||
print("[INFO] angle: {:.3f}".format(angle)) | ||
return rotated | ||
|
||
def RunSpellCheck(InputString): | ||
""" | ||
Takes a string and attempts to correct spelling on each word | ||
:param InputString: | ||
:return: suggested auto corrected spelling | ||
""" | ||
words = InputString.split(' ') | ||
OutPutString = '' | ||
for word in words: | ||
OutPutString += spell(word) + ' ' | ||
return OutPutString | ||
|
||
def RemoveNonUtf8BadChars(line): | ||
"""Remove junk characters from OCR text output. | ||
Tesseract is pretty good, but sometimes it spits out a bunch of garbage characters | ||
""" | ||
return "".join([ch for ch in line if ch in printable]) | ||
|
||
def ocrImage(image, extractBadChars=False, spellCheck=False): | ||
text = pytesseract.image_to_string(image) | ||
if extractBadChars: | ||
text = RemoveNonUtf8BadChars(text) | ||
if spellCheck: | ||
text = RunSpellCheck(text) | ||
return text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import picamera | ||
from time import sleep | ||
import re | ||
from PIL import Image | ||
import pytesseract | ||
import cv2 | ||
import os | ||
import tess_lib as ip | ||
def processing(im): | ||
image = cv2.imread(im) | ||
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | ||
cv2.imshow("Image", gray) | ||
filename = "{}.png".format(os.getpid()) | ||
cv2.imwrite(filename, gray) | ||
tb = cv2.imread(filename) | ||
clean = ip.cleanUpTextArea(tb) | ||
txt = ip.ocrImage(clean, extractBadChars=True, spellCheck=False) | ||
if txt == '': | ||
print('Sorry, I am unable to read the page.Please try again.') | ||
else: | ||
print(txt) | ||
print("Your wordlist") | ||
wordList = re.sub("[^\w]", " ", txt).split() | ||
for x in wordList: | ||
print(x) | ||
print("Next Word") | ||
return wordList | ||
def main(): | ||
finalmark=0 | ||
print("Enter the mark for this question") | ||
mark=int(input()) | ||
print("Place the vision kit before the answer key") | ||
camera = picamera.PiCamera() | ||
camera.start_preview() | ||
sleep(5) | ||
camera.capture('image1.jpeg') | ||
print("Image captured") | ||
camera.stop_preview() | ||
key=processing("image1.jpeg") | ||
divide=len(key) | ||
each_mark=mark/divide | ||
print("Place the vision kit before the answer key") | ||
camera.start_preview() | ||
sleep(5) | ||
camera.capture('image2.jpeg') | ||
camera.stop_preview() | ||
print("Image Captured") | ||
answer=processing("image2.jpeg") | ||
for y in key: | ||
for z in answer: | ||
if y==z: | ||
finalmark+=each_mark | ||
print("Your mark is") | ||
print(finalmark) | ||
main() |