LAXMAYDAY commited on
Commit
b6ac2b8
1 Parent(s): 390707b

Upload 2 files

Browse files
freeway_demo_ema_model/mp_rank_00_model_states.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:489eed99d7bf7f8ecfdd933c7f0017efab6678942b7f8187091fc379a32018bc
3
+ size 6066952715
script/process_images.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+
3
+ import os
4
+ from PIL import Image
5
+ import numpy as np
6
+ import json
7
+
8
+ Image.MAX_IMAGE_PIXELS = None
9
+
10
+ from concurrent.futures import ThreadPoolExecutor
11
+ from tqdm import tqdm
12
+
13
+ max_pixels=2048*2048
14
+
15
+ max_long_size=4096
16
+ def has_alpha(img:Image.Image):
17
+ for band in img.getbands():
18
+ if band in {'A','a','P'}:
19
+ return True
20
+ return False
21
+
22
+ def add_white_background(img:Image.Image)->Image.Image:
23
+ img=img.convert('RGBA') #转换为RGBA
24
+ background = Image.new('RGBA', img.size, (255, 255, 255))
25
+ img = Image.alpha_composite(background, img)
26
+ return img
27
+
28
+ def resize_image(image:Image.Image)->Image.Image:
29
+
30
+ width, height = image.size
31
+ max_side = max(width, height)
32
+ current_pixels=width*height
33
+
34
+ # 检查是否需要调整大小
35
+ if max_side > max_long_size or current_pixels>max_pixels:
36
+
37
+ # 计算缩放比例
38
+ scale = min((max_long_size / max_side),
39
+ ((max_pixels / current_pixels) ** 0.5))
40
+ # 计算新的尺寸
41
+ new_width = int(width * scale)
42
+ new_height = int(height * scale)
43
+ # 调整图片大小
44
+ resized_image = image.resize((new_width, new_height),
45
+ Image.BICUBIC
46
+ )
47
+ return resized_image
48
+ # 如果不需要调整大小,返回原始图片
49
+ return image
50
+
51
+ def load_image(image_path:str)->Image.Image:
52
+ try:
53
+ with Image.open(image_path) as img:
54
+ img.load()#读取图片加载到内存
55
+ np.array(img) #尝试用numpy加载图片
56
+ img=resize_image(img) #resize图片
57
+ if has_alpha(img): #读取并移除透明图层
58
+ img=add_white_background(img) #添加白色背景
59
+ if not img.mode == "RGB":
60
+ img = img.convert("RGB")
61
+ return img
62
+ except:
63
+ return None
64
+
65
+ def get_image_metainfo(img):
66
+ if img is None:
67
+ return None
68
+ else:
69
+ width, height = img.size
70
+ return {'width':width,
71
+ 'height':height,
72
+ 'pixel_num':width*height,
73
+
74
+ }
75
+
76
+
77
+ def process_image(input_image_path:str,output_image_path:str):
78
+
79
+ img=load_image(input_image_path)
80
+
81
+ image_metainfo=get_image_metainfo(img)
82
+
83
+ output_image_json_path=output_image_path.replace(".webp",".json")
84
+
85
+
86
+ if img is not None and image_metainfo is not None:
87
+ img.save(output_image_path,"WEBP",quality=90) #保存图像
88
+ with open(output_image_json_path,'w') as f: #保存metainfo
89
+ json.dump(image_metainfo,f,indent=4)
90
+
91
+ def get_image_paths(input_dir, output_dir):
92
+ for root, _, files in os.walk(input_dir):
93
+ for file in files:
94
+ if file.lower().endswith(('.png', '.jpg', '.jpeg', '.webp', '.bmp')):
95
+ input_path = os.path.join(root, file)
96
+ rel_path = os.path.relpath(input_path,
97
+ input_dir)
98
+ output_path = os.path.join(output_dir,
99
+ os.path.splitext(rel_path)[0] + '.webp')
100
+ os.makedirs(os.path.dirname(output_path), exist_ok=True)
101
+ yield input_path, output_path
102
+
103
+ def process_images_with_thread_pool(input_image_dir:str,
104
+ output_image_dir:str,
105
+ num_threads=16):
106
+ os.makedirs(output_image_dir, exist_ok=True)
107
+ image_paths = get_image_paths(input_image_dir, output_image_dir)
108
+ with ThreadPoolExecutor(max_workers=num_threads) as executor:
109
+ # 创建任务列表
110
+ futures = []
111
+ for input_path, output_path in image_paths:
112
+ futures.append(executor.submit(process_image,
113
+ input_path,
114
+ output_path))
115
+ for _ in tqdm(
116
+ executor.map(lambda f: f.result(), futures),
117
+ total=len(futures),
118
+ desc="Processing images"):
119
+ pass
120
+
121
+ if __name__ == "__main__":
122
+ # process_image(
123
+ # input_image_path="test.png",
124
+ # output_image_path='test.webp')
125
+ process_images_with_thread_pool(input_image_dir=r"20240808\unsplash-research-dataset-lite-latest\test",
126
+ output_image_dir=r"20240808\unsplash-research-dataset-lite-latest\output",
127
+ num_threads=16)