-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathocr.py
More file actions
58 lines (46 loc) · 1.86 KB
/
ocr.py
File metadata and controls
58 lines (46 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/python
#
# ./ocr.py : Perform optical character recognition, usage:
# ./ocr.py train-image-file.png train-text.txt test-image-file.png
#
# Authors: (insert names here)
# (based on skeleton code by D. Crandall, Oct 2017)
#
from PIL import Image, ImageDraw, ImageFont
import sys
from ocr_solver import OCRSolver
CHARACTER_WIDTH = 14
CHARACTER_HEIGHT = 25
def load_letters(fname):
im = Image.open(fname)
px = im.load()
(x_size, y_size) = im.size
print im.size
print int(x_size / CHARACTER_WIDTH) * CHARACTER_WIDTH
result = []
for x_beg in range(0, int(x_size / CHARACTER_WIDTH) * CHARACTER_WIDTH, CHARACTER_WIDTH):
result += [["".join(['*' if px[x, y] < 1 else ' ' for x in range(x_beg, x_beg + CHARACTER_WIDTH)]) for y in
range(0, CHARACTER_HEIGHT)], ]
return result
def load_training_letters(fname):
TRAIN_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789(),.-!?\"' "
letter_images = load_letters(fname)
return {TRAIN_LETTERS[i]: letter_images[i] for i in range(0, len(TRAIN_LETTERS))}
#####
# main program
(train_img_fname, train_txt_fname, test_img_fname) = sys.argv[1:]
train_letters = load_training_letters(train_img_fname)
test_letters = load_letters(test_img_fname)
# Below is just some sample code to show you how the functions above work.
# You can delete them and put your own code here!
solver = OCRSolver(train_letters, test_letters, train_txt_fname)
solver.simplified()
solver.hmm_ve()
solver.hmm_viterbi()
# Each training letter is now stored as a list of characters, where black
# dots are represented by *'s and white dots are spaces. For example,
# here's what "a" looks like:
# print "\n".join([r for r in train_letters['a']])
# Same with test letters. Here's what the third letter of the test data
# looks like:
# print "\n".join([r for r in test_letters[2]])