Update src/utils/text_ocr.py

#3
by juliajo - opened
Files changed (1) hide show
  1. src/utils/text_ocr.py +20 -0
src/utils/text_ocr.py CHANGED
@@ -5,6 +5,7 @@ import cv2
5
  import io
6
  import re
7
  import pandas as pd
 
8
 
9
  from azure.ai.vision.imageanalysis import ImageAnalysisClient
10
  from azure.ai.vision.imageanalysis.models import VisualFeatures
@@ -100,6 +101,25 @@ def easy_ocr_detection(image_path):
100
 
101
  return detected_text
102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  def plot_text_bboxes(image_path,detected_text):
104
  img = cv2.imread(image_path)
105
 
 
5
  import io
6
  import re
7
  import pandas as pd
8
+ import pytesseract
9
 
10
  from azure.ai.vision.imageanalysis import ImageAnalysisClient
11
  from azure.ai.vision.imageanalysis.models import VisualFeatures
 
101
 
102
  return detected_text
103
 
104
+ def pytesseract_ocr_detection(image_path):
105
+
106
+ image = cv2.imread(image_path)
107
+
108
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
109
+ _, thresh = cv2.threshold(gray, 180, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
110
+ data = pytesseract.image_to_data(thresh, lang='nor', output_type=pytesseract.Output.DICT)
111
+
112
+ detected_text = []
113
+ n_boxes = len(data['text'])
114
+ for i in range(n_boxes):
115
+ text = data['text'][i].strip()
116
+ if text != "":
117
+ (x, y, w, h) = (data['left'][i], data['top'][i], data['width'][i], data['height'][i])
118
+ rect_bbox = (x, y, x + w, y + h)
119
+ detected_text.append((text, rect_bbox))
120
+
121
+ return detected_text
122
+
123
  def plot_text_bboxes(image_path,detected_text):
124
  img = cv2.imread(image_path)
125