Spaces:
Runtime error
Runtime error
Update utility/utils.py
Browse files95 unmodified lines969798991001011021031041052 unmodified lines1081091101111121131141151161171181191201211221231241251261271285 unmodified lines13413513613713813914014114214314495 unmodified linesreturn final_image# Function for OCR with PaddleOCR, returning both text and bounding boxesdef ocr_with_paddle(img):final_text = ''logging.info(f"PADDLEOCR_HOME: {os.environ['PADDLEOCR_HOME']}")ocr = PaddleOCR(lang='en',use_angle_cls=True,2 unmodified linescls_model_dir=os.path.join(os.environ['PADDLEOCR_HOME'], 'whl/cls/ch_ppocr_mobile_v2.0_cls_infer'))result = ocr.ocr(img)boxes = []for line in result[0]:box, text, _ = lineboxes.append(box) # Append the bounding boxfinal_text += ' ' + textreturn final_text, boxes# Function to draw bounding boxes around textdef draw_boxes(image, boxes):draw = ImageDraw.Draw(image)for box in boxes:draw.polygon(box, outline="red", width=3)return image# Extract text and create a result image with bounding boxesdef extract_text_from_images(image_paths):5 unmodified linesenhanced_image = process_image(image_path, scale=2)# Perform OCR on the enhanced image and get boxesresult, boxes = ocr_with_paddle(enhanced_image)# Draw bounding boxes on the processed imageimg_result = Image.fromarray(enhanced_image)img_with_boxes = draw_boxes(img_result, boxes)# Save the image with boxesresult_image_path = os.path.join(RESULT_FOLDER, f'result_{os.path.basename(image_path)}')95 unmodified lines969798991001011021031041051062 unmodified lines1091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401415 unmodified lines14714814915015115215315415515615795 unmodified linesreturn final_image# Function for OCR with PaddleOCR, returning both text and bounding boxesdef ocr_with_paddle(img_path):final_text = ''boxes = []# Initialize PaddleOCRocr = PaddleOCR(lang='en',use_angle_cls=True,2 unmodified linescls_model_dir=os.path.join(os.environ['PADDLEOCR_HOME'], 'whl/cls/ch_ppocr_mobile_v2.0_cls_infer'))# Perform OCR on the imageresult = ocr.ocr(img_path)# Load image with OpenCVimg = cv2.imread(img_path)# Iterate through OCR resultsfor line in result[0]:box, text, _ = linefinal_text += ' ' + textboxes.append(box) # Save the bounding box coordinates# Convert points to integer and draw the bounding boxpoints = [(int(point[0]), int(point[1])) for point in box]cv2.polylines(img, [np.array(points)], isClosed=True, color=(0, 255, 0), thickness=2)# Save the image with drawn boxes in memory (as a variable)img_with_boxes = img # This image can be used later or saved to disk if neededreturn final_text, img_with_boxes# Function to draw bounding boxes around text#def draw_boxes(image, boxes):# draw = ImageDraw.Draw(image)# for box in boxes:# draw.polygon(box, outline="red", width=3)# return image# Extract text and create a result image with bounding boxesdef extract_text_from_images(image_paths):5 unmodified linesenhanced_image = process_image(image_path, scale=2)# Perform OCR on the enhanced image and get boxesresult, img_with_boxes = ocr_with_paddle(enhanced_image)# Draw bounding boxes on the processed imageimg_result = Image.fromarray(enhanced_image)#img_with_boxes = draw_boxes(img_result, boxes)# Save the image with boxesresult_image_path = os.path.join(RESULT_FOLDER, f'result_{os.path.basename(image_path)}')
utility/utils.py+27-14