File size: 2,482 Bytes
9aa6aea
 
 
 
 
 
 
 
ce50c4e
9aa6aea
 
 
 
 
 
 
 
 
ce50c4e
9aa6aea
 
 
 
 
 
ce50c4e
9aa6aea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
# -*- coding: utf-8 -*-

# =================================================== 
#
#    Author        : Fan Zhang
#    Email         : [email protected]
#    Institute     : Beijing Academy of Artificial Intelligence (BAAI)
#    Create On     : 2023-12-13 09:48
#    Last Modified : 2023-12-20 04:10
#    File Name     : utils.py
#    Description   : 
#
# =================================================== 

from datetime import datetime
import logging
import logging.config
import hashlib
import os
import os.path as osp
import uuid
from PIL import Image

from decord import VideoReader

os.makedirs(osp.join(osp.dirname(__file__), "..", "log"), exist_ok=True)

def config_logger(logger_name):
    logger_config = {
        "version": 1,
        "formatters": {
            "standard": {
                "format": "%(asctime)s - %(filename)s: %(lineno)d - [%(levelname)s] - %(message)s",
                "datefmt": "%Y-%m-%d %H:%M:%S",
            },
        },
        "handlers": {
            "console": {
                "class": "logging.StreamHandler",
                "formatter": "standard",
                "level": "INFO",
            },
            "file": {
                "class": "logging.handlers.TimedRotatingFileHandler",
                "filename": osp.join(osp.dirname(__file__), "..", "log", f"{logger_name}.log"),
                "formatter": "standard",
                "level": "INFO",
                "when": "D",
                "interval": 7,
                "backupCount": 90,
            },
        },
        "loggers": {
            logger_name: {
                "handlers": ["file", "console"],
                "level": "INFO",
                "propagate": True,
            },
        },
    }

    logging.config.dictConfig(logger_config)
    logger = logging.getLogger(logger_name)
    return logger

frontend_logger = config_logger("Emu-v2_frontend")
beckend_logger = config_logger("Emu-v2_backend")


def extract_frames(video, num_frames):
    video = VideoReader(video)
    total_frames = len(video)
    segment = int(total_frames // num_frames)

    frames = video.get_batch(list(range(int(segment//2), total_frames, segment))).asnumpy()
    frames = [Image.fromarray(f) for f in frames]
    return frames


def image2md5(image: Image.Image):
    md5hash = hashlib.md5(image.tobytes())
    return md5hash.hexdigest()


def gen_id():
    logid = datetime.now().strftime("%Y%m%d%H%M%d")
    logid += f"{uuid.uuid4().hex}"
    return logid