WebashalarForML commited on
Commit
84d7e55
1 Parent(s): 8bd631d

Update utility/utils.py

Browse files
Files changed (1) hide show
  1. utility/utils.py +27 -14
utility/utils.py CHANGED
@@ -96,10 +96,11 @@ def process_image(image_path, scale=2):
96
  return final_image
97
 
98
  # Function for OCR with PaddleOCR, returning both text and bounding boxes
99
- def ocr_with_paddle(img):
100
  final_text = ''
101
-
102
- logging.info(f"PADDLEOCR_HOME: {os.environ['PADDLEOCR_HOME']}")
 
103
  ocr = PaddleOCR(
104
  lang='en',
105
  use_angle_cls=True,
@@ -108,21 +109,33 @@ def ocr_with_paddle(img):
108
  cls_model_dir=os.path.join(os.environ['PADDLEOCR_HOME'], 'whl/cls/ch_ppocr_mobile_v2.0_cls_infer')
109
  )
110
 
111
- result = ocr.ocr(img)
112
- boxes = []
 
 
 
 
 
113
  for line in result[0]:
114
  box, text, _ = line
115
- boxes.append(box) # Append the bounding box
116
  final_text += ' ' + text
 
 
 
 
 
117
 
118
- return final_text, boxes
 
 
 
119
 
120
  # Function to draw bounding boxes around text
121
- def draw_boxes(image, boxes):
122
- draw = ImageDraw.Draw(image)
123
- for box in boxes:
124
- draw.polygon(box, outline="red", width=3)
125
- return image
126
 
127
  # Extract text and create a result image with bounding boxes
128
  def extract_text_from_images(image_paths):
@@ -134,11 +147,11 @@ def extract_text_from_images(image_paths):
134
  enhanced_image = process_image(image_path, scale=2)
135
 
136
  # Perform OCR on the enhanced image and get boxes
137
- result, boxes = ocr_with_paddle(enhanced_image)
138
 
139
  # Draw bounding boxes on the processed image
140
  img_result = Image.fromarray(enhanced_image)
141
- img_with_boxes = draw_boxes(img_result, boxes)
142
 
143
  # Save the image with boxes
144
  result_image_path = os.path.join(RESULT_FOLDER, f'result_{os.path.basename(image_path)}')
 
96
  return final_image
97
 
98
  # Function for OCR with PaddleOCR, returning both text and bounding boxes
99
+ def ocr_with_paddle(img_path):
100
  final_text = ''
101
+ boxes = []
102
+
103
+ # Initialize PaddleOCR
104
  ocr = PaddleOCR(
105
  lang='en',
106
  use_angle_cls=True,
 
109
  cls_model_dir=os.path.join(os.environ['PADDLEOCR_HOME'], 'whl/cls/ch_ppocr_mobile_v2.0_cls_infer')
110
  )
111
 
112
+ # Perform OCR on the image
113
+ result = ocr.ocr(img_path)
114
+
115
+ # Load image with OpenCV
116
+ img = cv2.imread(img_path)
117
+
118
+ # Iterate through OCR results
119
  for line in result[0]:
120
  box, text, _ = line
 
121
  final_text += ' ' + text
122
+ boxes.append(box) # Save the bounding box coordinates
123
+
124
+ # Convert points to integer and draw the bounding box
125
+ points = [(int(point[0]), int(point[1])) for point in box]
126
+ cv2.polylines(img, [np.array(points)], isClosed=True, color=(0, 255, 0), thickness=2)
127
 
128
+ # Save the image with drawn boxes in memory (as a variable)
129
+ img_with_boxes = img # This image can be used later or saved to disk if needed
130
+
131
+ return final_text, img_with_boxes
132
 
133
  # Function to draw bounding boxes around text
134
+ #def draw_boxes(image, boxes):
135
+ # draw = ImageDraw.Draw(image)
136
+ # for box in boxes:
137
+ # draw.polygon(box, outline="red", width=3)
138
+ # return image
139
 
140
  # Extract text and create a result image with bounding boxes
141
  def extract_text_from_images(image_paths):
 
147
  enhanced_image = process_image(image_path, scale=2)
148
 
149
  # Perform OCR on the enhanced image and get boxes
150
+ result, img_with_boxes = ocr_with_paddle(enhanced_image)
151
 
152
  # Draw bounding boxes on the processed image
153
  img_result = Image.fromarray(enhanced_image)
154
+ #img_with_boxes = draw_boxes(img_result, boxes)
155
 
156
  # Save the image with boxes
157
  result_image_path = os.path.join(RESULT_FOLDER, f'result_{os.path.basename(image_path)}')