File size: 1,687 Bytes
81c782d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e76f9f5
81c782d
 
 
 
 
 
 
 
 
 
 
a3d2d6f
abffb61
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import base64
import cv2
from typing import List
from io import BytesIO
import numpy as np
from PIL import Image

def encode_frame_to_base64(frame: cv2.Mat) -> str:
    """
    Convert a cv2.Mat frame to a base64-encoded string.
    """
    _, buffer = cv2.imencode('.jpg', frame)
    encoded_string = base64.b64encode(buffer).decode('utf-8')
    return encoded_string

def decode_base64_to_frame(encoded_string: str) -> cv2.Mat:
    """
    Convert a base64-encoded string back to a cv2.Mat frame.
    """
    decoded_data = base64.b64decode(encoded_string)
    np_array = np.frombuffer(decoded_data, dtype=np.uint8)
    return cv2.imdecode(np_array, cv2.IMREAD_COLOR)

def get_bytes(image_path: str) -> bytes:
    with open(image_path, "rb") as f:
        return f.read()

def get_text_file_contents(file_path):
    """
    Reads a text file from the specified path and returns its content as a string.

    Args:
        file_path (str): The path to the text file.

    Returns:
        str: The content of the file, or None if an error occurred.
    """
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            content = file.read()
            print(content)
        return content
    except FileNotFoundError:
        print(f"Error: File not found at path: {file_path}")
        return None
    except Exception as e:
        print(f"An error occurred while reading the file: {e}")
        return None
    
def clean_text(text: str) -> str:
    return text.encode("ascii", errors="ignore").decode()

def get_base64(file_path: str) -> str:
    with open(file_path, "r", encoding="utf-8") as f:
        base64_data = f.read().strip()
    return base64_data