-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_utils.py
65 lines (51 loc) · 2.35 KB
/
data_utils.py
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
import os
import logging
from pathlib import Path
from typing import Set, List, Optional
from PIL import Image
def list_images(image_dir: str, extensions: Set[str] = None) -> List[str]:
"""List all image files with supported extensions in directory."""
extensions = extensions or {".jpg", ".jpeg", ".png"}
image_dir = Path(image_dir)
if not image_dir.exists():
raise FileNotFoundError(f"Image directory {image_dir} does not exist")
images = []
for ext in extensions:
images.extend(image_dir.glob(f"*{ext}"))
images.extend(image_dir.glob(f"*{ext.upper()}"))
if not images:
raise ValueError(f"No images found in {image_dir} with extensions: {extensions}")
return sorted([str(img) for img in images])
def create_annotations(image_dir: str, prompt: str) -> str:
"""Create simple annotations file with fixed prompt for all images."""
images = list_images(image_dir)
annotations_path = Path(image_dir) / "annotations.txt"
logging.info(f"Creating annotations for {len(images)} images")
with open(annotations_path, "w") as f:
for img_path in images:
f.write(f"{img_path}\t{prompt}\n")
return str(annotations_path)
def create_annotations(image_dir: str, instance_prompt: str, class_prompt: Optional[str] = None) -> str:
"""Create annotations file mapping images to prompts.
Args:
image_dir: Directory containing training images
instance_prompt: Prompt template for instance images (e.g., "a photo of <myspecialstyle>")
class_prompt: Optional prompt template for class images
Returns:
Path to created annotations file
"""
images = list_images(image_dir)
if not images:
raise ValueError(f"No images found in {image_dir} with supported extensions")
# Create instance annotations
annotations_path = Path(image_dir) / "annotations.txt"
with open(annotations_path, 'w') as f:
for img_path in images:
f.write(f"{img_path}\t{instance_prompt}\n")
# Create class annotations if provided
if class_prompt:
class_annotations_path = Path(image_dir) / "class_annotations.txt"
with open(class_annotations_path, 'w') as f:
for img_path in images:
f.write(f"{img_path}\t{class_prompt}\n")
return str(annotations_path)