-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
73 lines (64 loc) · 2.24 KB
/
utils.py
File metadata and controls
73 lines (64 loc) · 2.24 KB
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
import os
import base64
from glob import glob
from io import BytesIO
from typing import List, Tuple, Dict, Any
from pydantic import create_model, BaseModel, Field
#
import yaml
from PIL import Image
def read_config(filepath: str) -> Dict[str, Any]:
"""
Reads a configuration file given by `filepath`.
:param filepath: Path to the yaml config file.
:returns: A dictionary representing the content of the config file.
"""
with open(filepath, "r") as file:
config = yaml.safe_load(file)
return config
def gather_samples(data_dir: str, extensions: List[str]) -> List[str]:
"""
Recursively find all samples within `data_dir` with all the extensions within `extensions`.
:param data_dir: Base path
:param extensions: List of extensions to gather.
:returns: A list of file paths of all the match samples.
"""
#
all_samples = []
for extension in extensions:
all_samples += list(glob(os.path.join(data_dir, "**", f"*.{extension}"), recursive=True))
#
return all_samples
def image_to_base64(image: Image.Image) -> str:
"""
Converts PIL image to base64 string.
:param image: The source image.
:returns: The base64 representation of `image`
"""
buffered = BytesIO()
image.save(buffered, format="JPEG")
return base64\
.b64encode(buffered.getvalue())\
.decode("utf-8")
def create_output_model(accessories: Dict[str, str]) -> BaseModel:
"""
Creates a dynamic Pydantic BaseModel given the list of `accessories`.
:param accessories: The accessories dictionary whose key is the accessory name and value is the description of the accessory.
:returns: A BaseModel
"""
fields = {
name: (int, Field(0, description=desc))
for name, desc in accessories.items()
}
return create_model("OutputModel", **fields)
def construct_system_message(accessories: Dict[str, str]) -> str:
message = """
You are FABLE, a vision-language model for face accessory labeling.
Given an image of a person face, determine if the following accessories are present:
"""
#
for i, (name, description) in enumerate(accessories.items()):
message += "\n"
message += f"{i+1}. {name}: {description}."
#
return message