This function will be executed by each worker process.
(args)
| 25 | |
| 26 | |
| 27 | def process_item(args): |
| 28 | """This function will be executed by each worker process.""" |
| 29 | uid, metadata, source_folder, lmdb_path, lock = args |
| 30 | |
| 31 | img_folder = os.path.join(source_folder, uid) |
| 32 | if not os.path.exists(img_folder): |
| 33 | return # No images, skip |
| 34 | |
| 35 | images = sorted(glob.glob(f"{img_folder}/*.png")) |
| 36 | if len(images) != 40: |
| 37 | return # Not enough images, skip |
| 38 | |
| 39 | img_list = [] |
| 40 | for img_path in images: |
| 41 | try: |
| 42 | img = Image.open(img_path).convert("RGB") |
| 43 | except UnidentifiedImageError: |
| 44 | print(f"Error opening image") |
| 45 | return |
| 46 | except Exception as e: |
| 47 | print(f"Error") |
| 48 | return |
| 49 | |
| 50 | img_byte_arr = io.BytesIO() |
| 51 | img.save(img_byte_arr, format="PNG") |
| 52 | img_list.append(img_byte_arr.getvalue()) |
| 53 | |
| 54 | metadata_values = [int(m) for m in metadata] |
| 55 | metadata_tensor = torch.tensor(metadata_values) |
| 56 | |
| 57 | key = uid.encode("ascii") |
| 58 | value = pickle.dumps((uid, img_list, metadata_tensor)) |
| 59 | |
| 60 | # Locking LMDB access to avoid race conditions |
| 61 | with lock: |
| 62 | map_size = int(1e12) # Size of the database |
| 63 | db = lmdb.open(lmdb_path, map_size=map_size) |
| 64 | with db.begin(write=True) as txn: |
| 65 | txn.put(key, value) |
| 66 | db.close() |
| 67 | |
| 68 | |
| 69 | def create_lmdb_dataset(source_folder, lmdb_path, all_metadata): |
nothing calls this directly
no outgoing calls
no test coverage detected