zmbfeng commited on
Commit
c6269e3
1 Parent(s): 8dfad76

segments divided by line now with green outlines

Browse files
Files changed (1) hide show
  1. utils.py +78 -1
utils.py CHANGED
@@ -208,6 +208,55 @@ def extract_bounding_boxes_from_image_np(image_np, bounding_boxes_list, above_ch
208
  # else:
209
  # print("box="+str(box)+"filled")
210
  return rect_content_list,above_rect_content_list, figures_image_list, tables_image_list, image_np_copy
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
  def gray_pdf_image_np_to_text(image_index,gray_pdf_image_np, debug=False):
212
  bounding_boxes_list = extract_rectangle_from_image(gray_pdf_image_np, 500, 20)
213
  bounding_boxes_list = remove_close_boxes (bounding_boxes_list, 10)
@@ -240,4 +289,32 @@ def gray_pdf_image_np_to_text(image_index,gray_pdf_image_np, debug=False):
240
  print(table[0])
241
  # st.write(table[0])#to_be_displayed
242
  # st.image(Image.fromarray(table[1]))#to_be_displayed
243
- st.image(Image.fromarray(cropped_image))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
  # else:
209
  # print("box="+str(box)+"filled")
210
  return rect_content_list,above_rect_content_list, figures_image_list, tables_image_list, image_np_copy
211
+ def find_hor_lines_in_image_np(min_width, min_height,image_np):
212
+ # Apply a horizontal kernel to emphasize horizontal lines
213
+ kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1050, 5)) # Adjust size according to your document
214
+ morphed = cv2.morphologyEx(image_np, cv2.MORPH_CLOSE, kernel)
215
+
216
+ # Detect edges
217
+ edges = cv2.Canny(morphed, 50, 150, apertureSize=3)
218
+
219
+ # Detect lines using HoughLinesP
220
+ lines = cv2.HoughLinesP(edges, 1, np.pi / 180, threshold=100, minLineLength=100, maxLineGap=10) # Adjust parameters as needed
221
+ return lines
222
+ def draw_colored_lines_on_image_np(image, lines,color_tuple):
223
+ for line in lines:
224
+ x1, y1, x2, y2 = line[0]
225
+ cv2.line(image, (x1, y1), (x2, y2), color_tuple, 3)
226
+ def segment_image_np(image_np,hor_lines_list):
227
+ # print("in segment_image_np image_np start")
228
+ # display_image_np(image_np)
229
+ # print("in segment_image_np image_np end")
230
+ segments = []
231
+ previous_y = 0
232
+ for line in sorted(hor_lines_list, key=lambda x: x[0][1]): # Sort lines by their y-coordinate
233
+ x1, y1, x2, y2 = line[0]
234
+ segment = image_np[previous_y:y1, :]
235
+ segments.append(segment)
236
+ previous_y = y2 # Update to start the next segment from the end of the current line
237
+
238
+ # Don't forget the last segment
239
+ last_segment =image_np[previous_y:, :]
240
+ segments.append(last_segment)
241
+ return segments
242
+ def filter_segments_by_min_height(segments, min_height):
243
+ return [segment for segment in segments if segment.shape[0] > min_height]
244
+
245
+ def draw_edges(np_image):
246
+ color = (0, 255, 0) # Green
247
+
248
+ # Define the thickness of the rectangle lines
249
+ thickness = 5
250
+
251
+ # Get the dimensions of the image
252
+ height, width = np_image.shape[:2]
253
+
254
+ # Coordinates for the rectangle: start from (0,0) to (width, height)
255
+ # We draw from 0+thickness//2 and width-thickness//2 to respect the thickness and not go out of bounds
256
+ cv2.rectangle(np_image, (thickness // 2, thickness // 2), (width - thickness // 2, height - thickness // 2), color,
257
+ thickness)
258
+
259
+
260
  def gray_pdf_image_np_to_text(image_index,gray_pdf_image_np, debug=False):
261
  bounding_boxes_list = extract_rectangle_from_image(gray_pdf_image_np, 500, 20)
262
  bounding_boxes_list = remove_close_boxes (bounding_boxes_list, 10)
 
289
  print(table[0])
290
  # st.write(table[0])#to_be_displayed
291
  # st.image(Image.fromarray(table[1]))#to_be_displayed
292
+ st.image(Image.fromarray(cropped_image))#to_be_displayed
293
+ found_hor_lines_list = find_hor_lines_in_image_np(1050, 5, cropped_image)
294
+ if found_hor_lines_list is not None:
295
+ bgr_image = cv2.cvtColor(gray_pdf_image_np, cv2.COLOR_GRAY2BGR)
296
+ draw_colored_lines_on_image_np(bgr_image, found_hor_lines_list, (0, 255, 0))
297
+ print("detected Lines start")
298
+ st.image(Image.fromarray(bgr_image)) #to_be_displayed
299
+
300
+ print("detected lines end")
301
+ page_segment_np_list = segment_image_np(cropped_image, found_hor_lines_list)
302
+ if debug:
303
+ debug_page_segment_index = 0
304
+ for element in page_segment_np_list:
305
+ print("element start")
306
+ bgr_image = cv2.cvtColor(element, cv2.COLOR_GRAY2BGR)
307
+ draw_edges(bgr_image)
308
+ st.image(Image.fromarray(bgr_image))#to_be_displayed
309
+
310
+ debug_page_segment_index = debug_page_segment_index + 1
311
+ print("element end")
312
+ min_height_filtered_page_segment_np_list = filter_segments_by_min_height(page_segment_np_list, 50)
313
+ max_height_image = max(min_height_filtered_page_segment_np_list, key=lambda image: image.shape[0])
314
+ if debug:
315
+ print("max height image start")
316
+ st.image(Image.fromarray(max_height_image))#to_be_displayed
317
+ print("max height image end")
318
+ else:
319
+ max_height_image = cropped_image.copy()
320
+ st.write("selected segment")