Spaces:
Sleeping
Sleeping
| 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 | |