<jupyter_start><jupyter_text>VGG2 face bboxes<jupyter_code>bb_path = Path('/data/faces/VGG2/bb_landmark/loose_bb_train.csv')
vgg_im2bbox = {} # NOTE: key is w/out stem (.jpg) -> t,l,b,r
for i,line in enumerate(progress_bar(open(bb_path).readlines()[1:])):
    im_name, x,y,w,h = line[:-1].split(',')
    x,y,w,h = map(int, [x,y,w,h])
    vgg_im2bbox[im_name[1:-1]] = (max(y,0),max(x,0),y+h,x+w)<jupyter_output><empty_output><jupyter_text>MouthOpen<jupyter_code>out_path = Path('../data/icao/mouthopen_frontal_vgg/')
MOUTH_OPEN, MOUTH_CLOSED = 'MouthOpen', 'MouthClosed'
try: (out_path/MOUTH_CLOSED).mkdir(); (out_path/MOUTH_OPEN).mkdir()
except: print('dirs already exist')
def prepare_dataset():
    out_path = Path('../data/icao/mouthopen/')
    vgg_train_path = Path('/data/faces/VGG2/train/')
    vgg_attr_path = Path('/data/faces/VGG2/attributes/11-Mouth_Open.txt')
    doesnt_exist = 0
    ims = []
    for line in progress_bar(open(vgg_attr_path).readlines()):
        img_str, lbl_str = line[:-1].split('\t')
        ims.append(img_str)
        label = MOUTH_OPEN if lbl_str == '1' else MOUTH_CLOSED
        img_path = vgg_train_path/img_str
        if not img_path.exists(): doesnt_exist += 1
        else:
            t,l,b,r = vgg_im2bbox[ img_str[:-4] ]
            image = Image(open_image(img_path).data[:,t:b,l:r])
            image.save(out_path/label/'_'.join(img_str.split('/')))
    print('doesnt exist:', doesnt_exist)
# prepare_dataset()
images_dir = Path('../data/vgg_frontal_faces_hd/')
frontal_faces = [str(x).split('/')[-1] for x in images_dir.iterdir()] # personid_imgid.jpg
def prepare_frontal_dataset():
    out_path = Path('../data/icao/mouthopen_frontal_vgg/')
    vgg_train_path = Path('/data/faces/VGG2/train/')
    vgg_attr_path = Path('/data/faces/VGG2/attributes/11-Mouth_Open.txt')
    not_frontal = 0
    ims = []
    for line in progress_bar(open(vgg_attr_path).readlines()):
        img_str, lbl_str = line[:-1].split('\t')
        ims.append(img_str)
        label = MOUTH_OPEN if lbl_str == '1' else MOUTH_CLOSED
        img_path = vgg_train_path/img_str
        if img_str.replace('/','_') not in frontal_faces: not_frontal += 1
        else:
            t,l,b,r = vgg_im2bbox[ img_str[:-4] ]
            image = Image(open_image(img_path).data[:,t:b,l:r])
            image.save(out_path/label/'_'.join(img_str.split('/')))
    print('not_frontal:', not_frontal)
# prepare_frontal_dataset()<jupyter_output><empty_output><jupyter_text>MouthOpen TestData<jupyter_code>testdata_dir = Path('../data/icao/mouthopen_testdata/')
from facelib.labeling_icao import Labeler
openmouth_imagedir = Path('../data/icao/mouthopen_celeba_black/MouthOpen/')
images = [str(path) for path in openmouth_imagedir.iterdir()]
json_outputdir = Path('../data/icao/json_labels_mouthslightlyopen/')
labeler = Labeler(['MouthSlightlyOpen'], images, output_dir=json_outputdir, label_labeled=False)
labeler.show_next_batch()
openmouth_imagedir = Path('../data/face/cropped_celeba/')
good_im_names = [path.name[:-5] for path in json_outputdir.iterdir() if json.load(open(path))['labels']['MouthSlightlyOpen'] == 'green']
len(good_im_names)
for name in good_im_names: open_image(openmouth_imagedir/name).save(testdata_dir/MOUTH_OPEN/name)<jupyter_output><empty_output><jupyter_text>MouthClosed TestData<jupyter_code>from facenet_pytorch import MTCNN
mtcnn = MTCNN(
    image_size=224, margin=0, min_face_size=20,
    thresholds=[0.6, 0.7, 0.7], factor=0.709, post_process=False,
    device='cpu'
)

from torchvision import datasets
class ImagesWithPath(datasets.ImageFolder):
    def __getitem__(self, index):
        original_tuple = super().__getitem__(index)
        path = self.imgs[index][0]
        return (original_tuple + (path,))
    
def create_cropped_images(loader, output_dir):
    not_found_names = []
    for x, y, path in progress_bar(loader):
        name = Path(path).name
        try:Image(mtcnn(x)/255.).save(str(output_dir/name))
        except: not_found_names.append(Path(path).name)
    return not_found_names
mouthclosed_dirpath = Path('/data/faces/IC_6K_MATCHES/HITS/')
dataset = ImagesWithPath(str(mouthclosed_dirpath.parent))
dataset.idx_to_class = {i:c for c, i in dataset.class_to_idx.items()}
loader = DataLoader(dataset, collate_fn=lambda x: x[0], num_workers=1)
create_cropped_images(loader, testdata_dir/MOUTH_CLOSED)
from facelib.core import read_dict, save_dict
save_dict(list(map(lambda p:p.name, get_image_files(testdata_dir, recurse=True))), '../data/icao/mouthopen_testdata.pkl')<jupyter_output><empty_output><jupyter_text>Glasses<jupyter_code>SUNGLASSES, EYEGLASSES, NOGLASSES = 'SunGlasses', 'EyeGlasses', 'NoGlasses'
def prepare_glasses_dataset():
    out_path = Path('../data/icao/glasses_vgg/')
    vgg_train_path = Path('/data/faces/VGG2/train/')
    vgg_attr_eyeglasses_path = Path('/data/faces/VGG2/attributes/09-Eyeglasses.txt')
    vgg_attr_sunglasses_path = Path('/data/faces/VGG2/attributes/10-Sunglasses.txt')
    doesnt_exist = 0
    ims = []
    sun_lines, eye_lines = map(lambda path: open(path).readlines(), [vgg_attr_sunglasses_path, vgg_attr_eyeglasses_path])
    for sun_line, eye_line in progress_bar(list(zip(sun_lines, eye_lines))):
        img_str_eye, lbl_str_eye = eye_line[:-1].split('\t')
        img_str_sun, lbl_str_sun = sun_line[:-1].split('\t')
        assert img_str_eye == img_str_sun
        img_str = img_str_eye
        ims.append(img_str)
        label = EYEGLASSES if lbl_str_eye == '1' else NOGLASSES
        if lbl_str_sun == '1': label = SUNGLASSES
        img_path = vgg_train_path/img_str
        if not img_path.exists(): doesnt_exist += 1
        else:
            t,l,b,r = vgg_im2bbox[ img_str[:-4] ]
            image = Image(open_image(img_path).data[:,t:b,l:r])
            image.save(out_path/label/'_'.join(img_str.split('/')))
    print('doesnt exist:', doesnt_exist)
prepare_glasses_dataset()<jupyter_output><empty_output><jupyter_text>LookingAway<jupyter_code>def read_json(data_name, first=True):
    out = {}
    FACE_PATH = Path("/data/faces/")
    PATH = FACE_PATH/"unlabeled"
    for file_path in PATH.iterdir():
        fn = str(file_path).split('/')[-1]
        if fn.startswith('labels_'+data_name):
            if first:
                out.update( json.load(open(file_path)) )
                first = False
            else:
                out['images'].extend( json.load(open(file_path))['images'] )
    return out
data_name = 'fc'
j = read_json(data_name)
# images_dirpath = Path("/data/faces/unlabeled/images_"+data_name)
images_dirpath = Path("/data/faces/unlabeled/images")
def parse_points(pts_string):
    ''' 'x,y,w,h' or 'x,y' '''
    return list(map(int, pts_string.split(',')))
def get_lookingaway_images(data_name):
    j = read_json(data_name)
    # images_dirpath = Path("/data/faces/unlabeled/images_"+data_name)
    images_dirpath = Path("/data/faces/unlabeled/images")
    lbl2idx = {v:k for k,v in j['face_args'].items()}
    icaos = [(x['path'], x['faces'][0][lbl2idx['IcaoWarnings']], x['faces'][0][lbl2idx['LookingAwayConfidence']], 
              parse_points(x['faces'][0][lbl2idx['BoundingRect']])) for x in j['images']]
    lookingaway_images = [(images_dirpath/name,bb) for name,x,conf,bb in icaos if ('LookingAway' in x and -1 < conf and conf < 10)]
    lookingtocamera_images = [(images_dirpath/name,bb) for name,x,conf,bb in icaos if ('LookingAway' not in x and 85 < conf and conf < 101)]
    return lookingtocamera_images, lookingaway_images
lookingtocamera_images_all, lookingaway_images_all = [], []
lookingtocamera_images, lookingaway_images = get_lookingaway_images('fc')
lookingtocamera_images_all += lookingtocamera_images
lookingaway_images_all += lookingaway_images
len(lookingtocamera_images), len(lookingaway_images)
lookingtocamera_images, lookingaway_images = get_lookingaway_images('imdb')
lookingtocamera_images_all += lookingtocamera_images
lookingaway_images_all += lookingaway_images
len(lookingtocamera_images), len(lookingaway_images)
lookingtocamera_images, lookingaway_images = get_lookingaway_images('wiki')
lookingtocamera_images_all += lookingtocamera_images
lookingaway_images_all += lookingaway_images
len(lookingtocamera_images), len(lookingaway_images)
lookingtocamera_images, lookingaway_images = get_lookingaway_images('lfw')
lookingtocamera_images_all += lookingtocamera_images
lookingaway_images_all += lookingaway_images
len(lookingtocamera_images), len(lookingaway_images)
len(lookingtocamera_images_all), len(lookingaway_images_all)
im_path, bb = lookingaway_images[15]
image = open_image(im_path)
image.show(y=ImageBBox.create(*image.size, tensor(bb)[None], scale=True))
def pad_points(bbox, pad): # pad - tensor([4])
    t,l,b,r = bbox
    h_face, w_face = map(int, [b-t, r-l])
    cc = tensor([(t+b)//2, (l+r)//2])
    offset = (tensor(bbox) - torch.cat([cc,cc])).abs()
    offset = (offset * PAD_PCT).long() # scale
    cc = torch.cat([cc,cc])
    t,l = cc[:2] - offset[:2]
    b,r = cc[2:] + offset[2:]
    return list(map(int, [t,l,b,r]))
PAD_PCT = tensor([1.4,1.1,1.1,1.1]) # t,l,b,r
out_path = Path('../data/icao/lookingaway_unlabeled/')
for images_and_bboxes, data_name in [(lookingaway_images_all,'LookingAway'), (lookingtocamera_images_all, 'LookingAhead')]:
    for im_path, (t,l,b,r) in progress_bar(images_and_bboxes):
        image = open_image(im_path)
        t,l,b,r = pad_points((t,l,b,r), PAD_PCT)
        t,l,b,r = max(t,0), max(l,0), min(b,image.data.shape[1]), min(r,image.data.shape[2])
        Image(image.data[:,t:b,l:r]).save(out_path/data_name/im_path.name)<jupyter_output><empty_output><jupyter_text>DarkFace<jupyter_code>images_dir = Path('/data/faces/CELEBA/images/img_align_celeba/')
for im_path in get_image_files(images_dir):
    image = open_image(im_path)
#     image.apply_tfms([crop_pad()], size=224, resize_method=ResizeMethod.CROP, padding_mode='zeros')
    if random.random() > 0.5: data_name = 'BrightFace'
    else: image, data_name = brightness(image, random.random()*0.2+0.05), 'DarkFace'
    image.show()
    break<jupyter_output><empty_output><jupyter_text>LFW<jupyter_code>from facenet_pytorch import MTCNN
mtcnn = MTCNN(
    image_size=224, margin=0, min_face_size=20,
    thresholds=[0.6, 0.7, 0.7], factor=0.709, post_process=False,
    device='cuda'
)

from torchvision import datasets
class ImagesWithPath(datasets.ImageFolder):
    def __getitem__(self, index):
        original_tuple = super().__getitem__(index)
        path = self.imgs[index][0]
        return (original_tuple + (path,))
    
def create_cropped_images(loader, output_dir):
    not_found_names = []
    for x, y, path in progress_bar(loader):
        name = Path(path).name
        try:Image(mtcnn(x)/255.).save(str(output_dir/name))
        except: not_found_names.append(Path(path).name)
    return not_found_names
lfw_images_dir = Path('/data/faces/LFW/lfw/')
lfw_cropped_dir = Path('../data/face/lfw_cropped_images/')
dataset = ImagesWithPath(str(lfw_images_dir))
dataset.idx_to_class = {i:c for c, i in dataset.class_to_idx.items()}
loader = DataLoader(dataset, collate_fn=lambda x: x[0], num_workers=1)
create_cropped_images(loader, lfw_cropped_dir)<jupyter_output><empty_output><jupyter_text>OpenEyes<jupyter_code>lfw_cropped_images_dir = Path('../data/face/lfw_cropped_images/')
attr_path = Path('/data/faces/LFW/lfw_attributes.txt')
attr_names = open(attr_path).readlines()[1][2:-1].split('\t')
print(attr_names)
idx = lambda name: attr_names.index(name)
idx('Eyes Open')
def append_zeros(s, num_chars=4):
    while True:
        if len(s) == num_chars: return s
        s = '0' + s
def print_stats(number_list):
    x = np.array(number_list)
    print(f'num elems: {len(x)}')
    print(f'max: {x.max()}, min: {x.min()}')
    print(f'>0: {(x>0).sum()}, <0: {(x<0).sum()}')
def normalize_vals(number_list):
    x = np.array(number_list)
    x[x>0] =  x[x>0]/x.max()
    x[x<0] = -x[x<0]/x.min()
    return x
def get_lfw_attributes(attribute='Eyes Open'):
    attr_images = []
    for line in open(attr_path).readlines()[2:]:
        attrs = line[:-1].split('\t')
        person, _id, eyes_open = map(lambda name: attrs[idx(name)], ('person','imagenum',attribute))
        person, _id = person.replace(' ', '_'), append_zeros(_id)
        im_path = lfw_cropped_images_dir/(person+'_'+_id+'.jpg')
        assert im_path.exists()
        attr_images.append((im_path, float(eyes_open)))
    return attr_images
eye_open_images = get_lfw_attributes('Eyes Open')
print_stats([val for p,val in eye_open_images])
eye_open_values = normalize_vals([val for p,val in eye_open_images])
plot_histogram(eye_open_values[eye_open_values>0])
# print_stats(eye_open_values)
eye_open_images = [(p,val) for val, (p,_) in zip(eye_open_values, eye_open_images)]
output_dir = Path('../data/icao/eyesopen_lfw/')
for im_path,val in progress_bar(eye_open_images):
    data_name = 'EyesOpen' if val > 0 else 'EyesClosed'
    if data_name == 'EyesOpen' and val < 0.3: continue
    open_image(im_path).save(output_dir/data_name/im_path.name)<jupyter_output><empty_output><jupyter_text>Flash<jupyter_code>flash_images = get_lfw_attributes('Flash')
print_stats([val for p,val in flash_images])
flash_values = normalize_vals([val for p,val in flash_images])
plot_histogram(flash_values[flash_values>0])
output_dir = Path('../data/icao/flash_lfw/')
for im_path,val in progress_bar(flash_images):
    data_name = 'FlashOn' if val > 0 else 'FlashOff'
    open_image(im_path).save(output_dir/data_name/im_path.name)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15116107084750777, 'fraction_numerical': 0.01183256914657595, 'mean_word_length': 4.877009995654063, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '301987', 'license': 'Apache-2.0', 'n_tokens_mistral': 5250, 'n_tokens_neox': 5102, 'n_words': 776, 'path': 'nbs/63_icao_dataset.ipynb', 'repo_name': 'doas3140/face-pipeline', 'size': '13522'}