-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
57 lines (41 loc) · 1.67 KB
/
example.py
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
from photo_ocr import load_image, ocr, detection, recognition, draw_ocr, draw_detections
# you can also use PIL Image.open, but load_image automatically rotates the image according to the EXIF metadata!
# (download example.jpg here: https://github.com/krasch/photo_ocr/blob/master/example.jpg)
image = load_image("example.jpg")
# run the ocr
results = ocr(image)
# loop over the results
for result in results:
# polygon around the text
# (list of xy coordinates: [(x0, y0), (x1, y1), ....])
print(result.polygon)
# the actual text (a string)
print(result.text)
# the recognition confidence (a number in [0.0, 1.0])
print(result.confidence)
# an alternative way to loop over the results
for polygon, text, confidence in results:
print(polygon)
print(text)
print(confidence)
# draw bounding polygons and text on the image
image = draw_ocr(image, results)
# done!
image.save("example_ocr.jpg")
# use the `detection` function to only run the text detection step
image = load_image("example.jpg")
polygons = detection(image)
# list of polygons where text was found
for polygon in polygons:
# polygon around the text
# (list of xy coordinates: [(x0, y0), (x1, y1), ....])
print(polygon)
# draw_detections draws detection results
image = draw_detections(image, polygons)
image.save("example_detections.jpg")
# use the `recognition` function to only run the text recognition step
# the input image must have been cropped to text polygon, text should be aligned horizontally
# (download crop.jpg here: https://github.com/krasch/photo_ocr/blob/master/crop.jpg)
image = load_image("crop.jpg")
text, confidence = recognition(image)
print(text, confidence)