File size: 2,739 Bytes
e368cec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from datetime import datetime
import pytz
import PIL
import os 

def detect_language(text: str) -> str:
    """Detect the langauge of a string."""
    import polyglot  # pip3 install polyglot pyicu pycld2
    from polyglot.detect import Detector
    from polyglot.detect.base import logger as polyglot_logger
    import pycld2

    polyglot_logger.setLevel("ERROR")

    try:
        lang_code = Detector(text).language.name
    except (pycld2.error, polyglot.detect.base.UnknownLanguage):
        lang_code = "unknown"
    return lang_code


def get_time_stamp_from_date(date_str:str):
    """
    Convert a date string to a Unix timestamp
    Args:
        date_str (str): The input date string in the format 'YYYY-MM-DD-HH:MM-TZ', e.g. '2024-02-10-14:00-PT'
    """
    
    # Convert the date string into a format that Python's datetime can understand
    # and specify the correct timezone for PT, which is 'US/Pacific'
    date_format = "%Y-%m-%d-%H:%M-%Z"

    # Parse the date string into a datetime object
    # Note: PT is not directly recognized by pytz, so we manually map it to 'US/Pacific'
    timezone_map = {
        "PT": "US/Pacific",
    }

    # Extract the timezone abbreviation
    tz_abbr = date_str.split("-")[-1]
    # Map the abbreviation to a pytz timezone
    tz_info = pytz.timezone(timezone_map[tz_abbr])

    # Remove the timezone abbreviation for parsing
    date_str_parsed = date_str.rsplit("-", 1)[0]

    # Create a datetime object with the corresponding timezone
    dt = datetime.strptime(date_str_parsed, "%Y-%m-%d-%H:%M").replace(tzinfo=tz_info)

    # Convert the datetime object to a Unix timestamp
    unix_timestamp = dt.timestamp()
    return unix_timestamp

def get_date_from_time_stamp(unix_timestamp: int):
    # Create a datetime object from the Unix timestamp
    dt = datetime.fromtimestamp(unix_timestamp)

    # Convert the datetime object to a string with the desired format
    date_str = dt.strftime("%Y-%m-%d %H:%M:%S %Z")
    return date_str


def get_input_image_path(tstamp, conv_id):
    # from tstamp to date e.g. 2024-02-10
    date_str = datetime.fromtimestamp(tstamp, tz=pytz.timezone("US/Pacific")).strftime("%Y-%m-%d")
    LOGDIR = os.getenv("LOGDIR")
    return f"{LOGDIR}/{date_str}-convinput_images/input_image_{conv_id}.png"

def load_image_from_path(image_path):
    # Load the image from the specified
    # path using the Python Imaging Library (PIL)
    try:
        image = PIL.Image.open(image_path)
        return image
    except FileNotFoundError:
        print(f"Image not found at path: {image_path}")
        return None
    except PIL.UnidentifiedImageError:
        print(f"Unidentified image format at path: {image_path}")
        return None