Spaces:
Runtime error
Runtime error
# -*- 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 | |