DEV Community

EvolveDev
EvolveDev

Posted on

Building a Deep Face Detection Model with Python and TensorFlow (Part 2)

Part 1

4. Apply Image Augmentation on Images and Labels using Albumentations

4.1 Setup Albumentations Transform Pipeline

import albumentations as alb

augmentor = alb.Compose([
    alb.RandomCrop(width=450, height=450), 
    alb.HorizontalFlip(p=0.5), 
    alb.RandomBrightnessContrast(p=0.2),
    alb.RandomGamma(p=0.2), 
    alb.RGBShift(p=0.2), 
    alb.VerticalFlip(p=0.5)], 
    bbox_params=alb.BboxParams(format='albumentations', label_fields=['class_labels']))
Enter fullscreen mode Exit fullscreen mode

4.2 Load a Test Image and Annotation with OpenCV and JSON

img = cv2.imread(os.path.join('data', 'train', 'images', 'ffd85fc5-cc1a-11ec-bfb8-a0cec8d2d278.jpg'))

with open(os.path.join('data', 'train', 'labels', 'ffd85fc5-cc1a-11ec-bfb8-a0cec8d2d278.json'), 'r') as f:
    label = json.load(f)

label['shapes'][0]['points']
Enter fullscreen mode Exit fullscreen mode

4.3 Extract Coordinates and Rescale to Match Image Resolution

coords = [0, 0, 0, 0]
coords[0] = label['shapes'][0]['points'][0][0]
coords[1] = label['shapes'][0]['points'][0][1]
coords[2] = label['shapes'][0]['points'][1][0]
coords[3] = label['shapes'][0]['points'][1][1]

coords = list(np.divide(coords, [640, 480, 640, 480]))
coords
Enter fullscreen mode Exit fullscreen mode

4.4 Apply Augmentations and View Results

augmented = augmentor(image=img, bboxes=[coords], class_labels=['face'])
augmented['bboxes'][0][2:]

augmented['bboxes']

cv2.rectangle(augmented['image'], 
              tuple(np.multiply(augmented['bboxes'][0][:2], [450, 450]).astype(int)),
              tuple(np.multiply(augmented['bboxes'][0][2:], [450, 450]).astype(int)), 
              (255, 0, 0), 2)

plt.imshow(augmented['image'])
Enter fullscreen mode Exit fullscreen mode

Stay tuned for the next sections where we'll continue with the augmentation pipeline and training the deep learning model!

Top comments (0)