-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOCR.py
91 lines (65 loc) · 2 KB
/
OCR.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# -*- coding: utf-8 -*-
"""ocr2.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1w3zJN1M2L8L9ai2Z0-9M2RJfaLcqWRd4
"""
!sudo apt install tesseract-ocr
!pip install pytesseract
!pip install easyocr
!pip install gTTS
reader=easyocr.Reader(['en'])
import pytesseract
import cv2
import matplotlib.pyplot as plt
import easyocr
from gtts import gTTS
from IPython.display import Audio
img=cv2.imread("/content/ocr3.PNG")
import PIL
from PIL import ImageDraw
im=PIL.Image.open("/content/ocr3.PNG")
im
img2char=pytesseract.image_to_string(img)
print(img2char)
bounds=reader.readtext("/content/ocr3.PNG",add_margin=0.55, width_ths=0.7, link_threshold=0.8, decoder='beamsearch', blocklist='=-')
bounds
def draw_boxes(image,bounds, color='yellow',width=2):
draw=ImageDraw.Draw(image)
for bound in bounds:
p0,p1,p2,p3=bound[0]
draw.line([*p0, *p1, *p2, *p3], fill=color, width=width)
return image
draw_boxes(im,bounds)
tt=gTTS(img2char)
tt.save("audio.mp3")
Audio("audio.mp3", autoplay=True)
"""FOR VIDEO"""
from google.colab.patches import cv2_imshow
font_scale=1.5
font=cv2.FONT_HERSHEY_PLAIN
cap=cv2.VideoCapture("/content/sample1.mp4")
if not cap.isOpened():
cap=cv2.VideoCapture(0)
if not cap.isOpened():
raise IOError("Can't open video")
cntr=0
while True:
ret,frame=cap.read()
cntr=cntr+1
if( (cntr%20)==0):
imgH,imgW,_= frame.shape
x1,y1,w1,h1=0,0,imgH,imgW
imgchar=pytesseract.image_to_string(frame)
imgboxes=pytesseract.image_to_boxes(frame)
for boxes in imgboxes.splitlines():
boxes=boxes.split(' ')
x,y,w,h=int(boxes[1]),int(boxes[2]),int(boxes[3]),int(boxes[4])
cv2.rectangle(frame,(x,imgH-y),(w,imgH-h),(0,0,255),3)
cv2.putText(frame,imgchar,(x1 + int(w1/50),y1+ int(h1/50)), cv2.FONT_HERSHEY_SIMPLEX,0.7, (0,0,255),2)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2_imshow(frame)
if cv2.waitKey(2) & 0xFF==ord('q'):
break
cap.release()
cv2.destroyAllWindows