MCPcopy Create free account
hub / github.com/Meshcapade/difflocks / __getitem__

Method __getitem__

data_loader/dataloader.py:397–643  ·  view source on GitHub ↗
(self, idx)

Source from the content-addressed store, hash-verified

395 return len(self.hairstyles_path_list)
396
397 def __getitem__(self, idx):
398 out_dict = {}
399
400 if self.overfit==True:
401 # idx=4
402 idx=5
403 np.random.seed(0)
404
405 hairstyle_data= self.hairstyles_path_data_list[idx]
406 hairstyle_path = hairstyle_data.hairstyle_path
407
408 #if we need any of the processed data, we load also the path for it
409 # if self.load_scalp_texture:
410 if self.processed_difflocks_path is not None:
411 hairstyle_path_processed = os.path.join(self.processed_difflocks_path, "processed_hairstyles", os.path.basename(hairstyle_path))
412
413 # print("hairstyle_path",hairstyle_path)
414
415
416 flip = False
417 if self.randomly_flip:
418 if random.random()<0.5:
419 flip = True
420
421
422 #read metadata
423 with open(os.path.join(hairstyle_path,"metadata.json"),'r') as f:
424 try:
425 meta_data = f.read()
426 meta_data = json.loads(meta_data)
427 if isinstance(meta_data, str):
428 print("why is this a str", hairstyle_path)
429 exit(1)
430 except:
431 print("couldn't load metadata for", hairstyle_path)
432 exit(1)
433
434
435 if self.load_rgb_imgs:
436 img_path = os.path.join(hairstyle_path,"rgb.png")
437 img = read_image(img_path).float()/255.0
438 if flip:
439 img=torchvision.transforms.functional.hflip(img)
440 if self.subsample_factor is not None:
441 scale_factor=[1.0/self.subsample_factor, 1.0/self.subsample_factor]
442 img=torch.nn.functional.interpolate(img.unsqueeze(0), scale_factor=scale_factor, mode="area").squeeze(0)
443 out_dict["rgb_img"] = img
444
445 if self.load_hair_mask:
446 img_path = os.path.join(hairstyle_path_processed,"hair_masks","hair_mask.png")
447 if flip:
448 img_path = os.path.join(hairstyle_path_processed,"hair_masks_flip","hair_mask.png")
449 img = read_image(img_path).float()/255.0
450 img = img[0:1,:,:]
451 if self.subsample_factor is not None:
452 scale_factor=[1.0/self.subsample_factor, 1.0/self.subsample_factor]
453 img=torch.nn.functional.interpolate(img.unsqueeze(0), scale_factor=scale_factor, mode="nearest").squeeze(0)
454 out_dict["hair_mask"] = img

Callers

nothing calls this directly

Calls 3

to_dictMethod · 0.95
StrandsDataClass · 0.85
loadMethod · 0.45

Tested by

no test coverage detected