-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractText.py
55 lines (38 loc) · 1.34 KB
/
extractText.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
import pytesseract
from PIL import Image, ImageEnhance, ImageFilter
import json
import os
from tqdm import tqdm
import cv2
import numpy as np
# get grayscale image
def get_grayscale(image):
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#thresholding
def thresholding(image):
return cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
my_dict = {}
# Loading frames from directory
frames = os.listdir('./image_frames')
# Sorting frames according to their numbers
frames.sort(key=lambda x: int(x[5:-4]))
# printing total number of frames loaded
print("Number of frames :- " + str(len(frames)))
for frame in tqdm(frames):
image=cv2.imread('./image_frames/'+frame)
gray = get_grayscale(image)
thresh = thresholding(gray)
# Perform text extraction
data = pytesseract.image_to_string(thresh, lang='eng')
# replacing \n
text_in_image = data.replace('\n', '')
# Removing unwanted spaces
text_in_image = text_in_image.strip()
# removing unicode characters
text_in_image = text_in_image.encode('ascii', 'ignore').decode()
# appending output to dictionary (only those frames which contain text)
if(len(text_in_image) != 0):
my_dict[str(frame)] = text_in_image
# Saving output of dictionary to json file
with open('text.json', 'w') as outputFile:
json.dump(my_dict, outputFile)