Files
sd-webui-old-photo-restoration/Global/data/Load_Bigfile.py
Haoming 89a8626838 Squashed commit of the following:
commit cd7a9c103d1ea981ecd236d4e9111fd3c1cd6c2b
Author: Haoming <hmstudy02@gmail.com>
Date:   Tue Dec 19 11:33:44 2023 +0800

    add README

commit 30127cbb2a8e5f461c540729dc7ad457f66eb94c
Author: Haoming <hmstudy02@gmail.com>
Date:   Tue Dec 19 11:12:16 2023 +0800

    fix Face Enhancement distortion

commit 6d52de5368c6cfbd9342465b5238725c186e00b9
Author: Haoming <hmstudy02@gmail.com>
Date:   Mon Dec 18 18:27:25 2023 +0800

    better? args handling

commit 0d1938b59eb77a038ee0a91a66b07fb9d7b3d6d4
Author: Haoming <hmstudy02@gmail.com>
Date:   Mon Dec 18 17:40:19 2023 +0800

    bug fix related to Scratch

commit 8315cd05ffeb2d651b4c57d70bf04b413ca8901d
Author: Haoming <hmstudy02@gmail.com>
Date:   Mon Dec 18 17:24:52 2023 +0800

    implement step 2 ~ 4

commit a5feb04b3980bdd80c6b012a94c743ba48cdfe39
Author: Haoming <hmstudy02@gmail.com>
Date:   Mon Dec 18 11:55:20 2023 +0800

    process scratch

commit 3b18f7b042
Author: Haoming <hmstudy02@gmail.com>
Date:   Wed Dec 13 11:57:20 2023 +0800

    "init"

commit d0148e0e82
Author: Haoming <hmstudy02@gmail.com>
Date:   Wed Dec 13 10:34:39 2023 +0800

    clone repo
2023-12-19 11:35:38 +08:00

43 lines
1.6 KiB
Python

# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import io
import os
import struct
from PIL import Image
class BigFileMemoryLoader(object):
def __load_bigfile(self):
print('start load bigfile (%0.02f GB) into memory' % (os.path.getsize(self.file_path)/1024/1024/1024))
with open(self.file_path, 'rb') as fid:
self.img_num = struct.unpack('i', fid.read(4))[0]
self.img_names = []
self.img_bytes = []
print('find total %d images' % self.img_num)
for i in range(self.img_num):
img_name_len = struct.unpack('i', fid.read(4))[0]
img_name = fid.read(img_name_len).decode('utf-8')
self.img_names.append(img_name)
img_bytes_len = struct.unpack('i', fid.read(4))[0]
self.img_bytes.append(fid.read(img_bytes_len))
if i % 5000 == 0:
print('load %d images done' % i)
print('load all %d images done' % self.img_num)
def __init__(self, file_path):
super(BigFileMemoryLoader, self).__init__()
self.file_path = file_path
self.__load_bigfile()
def __getitem__(self, index):
try:
img = Image.open(io.BytesIO(self.img_bytes[index])).convert('RGB')
return self.img_names[index], img
except Exception:
print('Image read error for index %d: %s' % (index, self.img_names[index]))
return self.__getitem__((index+1)%self.img_num)
def __len__(self):
return self.img_num