WebashalarForML commited on
Commit
ecfb74f
1 Parent(s): f1e58f7

Update utility/utils.py

Browse files
Files changed (1) hide show
  1. utility/utils.py +15 -10
utility/utils.py CHANGED
@@ -111,26 +111,31 @@ def ocr_with_paddle(img):
111
 
112
  # Check if img is a file path or an image array
113
  if isinstance(img, str):
114
- # If img is a file path, load the image
115
  img = cv2.imread(img)
116
-
117
- # Perform OCR (Note: You might need to adapt this based on how PaddleOCR handles input images)
118
  result = ocr.ocr(img)
119
 
120
- # Iterate through OCR results
121
  for line in result[0]:
122
- box, text, _ = line
 
 
 
 
 
 
123
  final_text += ' ' + text
124
- boxes.append(box) # Save the bounding box coordinates
125
 
126
- # Convert points to integer and draw the bounding box
127
  points = [(int(point[0]), int(point[1])) for point in box]
128
  cv2.polylines(img, [np.array(points)], isClosed=True, color=(0, 255, 0), thickness=2)
129
 
130
- # Save the image with drawn boxes in memory (as a variable)
131
- img_with_boxes = img # This image can be used later or saved to disk if needed
132
 
133
- return final_text, boxes, img_with_boxes
134
 
135
  # Function to draw bounding boxes around text
136
  #def draw_boxes(image, boxes):
 
111
 
112
  # Check if img is a file path or an image array
113
  if isinstance(img, str):
 
114
  img = cv2.imread(img)
115
+
116
+ # Perform OCR
117
  result = ocr.ocr(img)
118
 
119
+ # Iterate through the OCR result
120
  for line in result[0]:
121
+ # Check how many values are returned (2 or 3) and unpack accordingly
122
+ if len(line) == 3:
123
+ box, text, _ = line # When 3 values are returned
124
+ elif len(line) == 2:
125
+ box, text = line # When only 2 values are returned
126
+
127
+ # Store the recognized text and bounding boxes
128
  final_text += ' ' + text
129
+ boxes.append(box)
130
 
131
+ # Draw the bounding box
132
  points = [(int(point[0]), int(point[1])) for point in box]
133
  cv2.polylines(img, [np.array(points)], isClosed=True, color=(0, 255, 0), thickness=2)
134
 
135
+ # Store the image with bounding boxes in a variable
136
+ img_with_boxes = img
137
 
138
+ return final_text, img_with_boxes
139
 
140
  # Function to draw bounding boxes around text
141
  #def draw_boxes(image, boxes):