| import streamlit as st |
|
|
| import io |
| import pandas as pd |
| import numpy as np |
| import cv2 |
| import pytesseract |
| from PIL import Image |
|
|
| from ultralytics import YOLO |
| from sahi import AutoDetectionModel |
| from sahi.predict import get_prediction, get_sliced_prediction |
| from sahi.utils.cv import visualize_object_predictions, read_image |
|
|
|
|
| def split_pdf_to_images(uploaded_file, read_status=None): |
| """ |
| Takes a PDF file, converts each page to a PNG image, |
| and returns a list of byte objects. |
| """ |
| try: |
| |
| images = convert_from_bytes(uploaded_file.read()) |
| |
| png_files = [] |
| for i, image in enumerate(images): |
| |
| if read_status is not None: |
| try: |
| read_status.update(label=f"Downloading image {i+1}/{len(images)}") |
| except Exception: |
| |
| try: |
| read_status.text(f"Downloading image {i+1}/{len(images)}") |
| except Exception: |
| st.write(f"Downloading Image: {i+1}/{len(images)}") |
| else: |
| st.write(f"Downloading Image: {i+1}/{len(images)}") |
| |
| buf = io.BytesIO() |
| image.save(buf, format="PNG") |
| byte_im = buf.getvalue() |
| png_files.append({"name": f"page_{i+1}.png", "content": byte_im}) |
| |
| |
| uploaded_file.seek(0) |
| return png_files |
| except Exception as e: |
| return f"Error processing PDF: {e}" |
|
|
|
|
| def analyze_data_types(uploaded_file): |
| |
|
|
| try: |
| df = pd.read_csv(uploaded_file) |
| uploaded_file.seek(0) |
| return df.dtypes |
| except Exception as e: |
| return f"Error: {e}" |
|
|
|
|
| def run_computer_vision(image_list, analysis_status=None): |
| """ |
| Runs CV model on images, draws bounding boxes, |
| and returns analysis with processed images. |
| """ |
| results = [] |
| |
| |
| |
| detection_model = AutoDetectionModel.from_pretrained( |
| model_type="ultralytics", |
| model_path="Model/Prod/10_epoch_model_fixed_text_size.pt", |
| confidence_threshold=0.3, |
| device="cpu", |
| ) |
| |
| results = [] |
| for i in range(len(image_list)): |
| |
| |
| if analysis_status is not None: |
| try: |
| analysis_status.update(label=f"Processing image {i+1}/{len(image_list)}") |
| except Exception: |
| |
| try: |
| analysis_status.text(f"Processing image {i+1}/{len(image_list)}") |
| except Exception: |
| st.write(f"Processing Image: {i+1}/{len(image_list)}") |
| else: |
| st.write(f"Processing Image: {i+1}/{len(image_list)}") |
| |
| image_mem = Image.open(io.BytesIO(image_list[i]["content"])).convert("RGB") |
| |
| sahi_result = get_sliced_prediction( |
| image_mem, |
| detection_model, |
| slice_height=256, |
| slice_width=256, |
| overlap_height_ratio=0.2, |
| overlap_width_ratio=0.2, |
| ) |
| |
| object_prediction_list = sahi_result.object_prediction_list |
|
|
| visualization_result = visualize_object_predictions( |
| image=np.array(image_mem), |
| object_prediction_list=object_prediction_list, |
| hide_labels=False, |
| hide_conf=False, |
| rect_th=2 |
| ) |
|
|
| |
| annotated_canvas = visualization_result["image"] |
| final_image = Image.fromarray(annotated_canvas) |
| results.append(final_image.copy()) |
| |
| return memory_images_to_pdf(results) |
|
|
|
|
| def memory_images_to_pdf(pil_image_list): |
| if not pil_image_list: |
| return None |
|
|
| |
| pdf_buffer = io.BytesIO() |
| |
| |
| rgb_images = [img.convert("RGB") for img in pil_image_list] |
| |
| |
| rgb_images[0].save( |
| pdf_buffer, |
| format="PDF", |
| save_all=True, |
| append_images=rgb_images[1:] |
| ) |
| |
| |
| pdf_buffer.seek(0) |
| return pdf_buffer |
|
|
| def get_codes_from_image(): |
|
|
| |
| |
|
|
| |
| image = cv2.imread('your_image.png') |
|
|
| |
| gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) |
|
|
| |
| text = pytesseract.image_to_string(gray_image) |
|
|
| |
| return text |
|
|