summaryrefslogtreecommitdiff
path: root/notebooks/utils/imx.py
blob: 0dbdcf7ecc0210fc63e361cd3faeeb5d8bc3096c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os
from os.path import join
import cv2

from PIL import Image, ImageDraw
import matplotlib.pyplot as plt


def ensure_pil(im):
    """Ensure image is Pillow format"""
    try:
        im.verify()
        return im
    except:
        return Image.fromarray(im.astype('uint8'), 'RGB')

def ensure_np(im):
    """Ensure image is numpy array"""
    if type(im) == np.ndarray:
        return im
    return np.asarray(im, np.uint8)

def ensure_dir(d):
    """Create directories"""
    if not os.path.exists(d):
        os.makedirs(d)

def filter_pixellate(src,num_cells):
    """Downsample, then upsample image for pixellation"""
    w,h = src.size
    dst = src.resize((num_cells,num_cells), Image.NEAREST)
    dst = dst.resize((w,h), Image.NEAREST)
    return dst

# Plot images inline using Matplotlib
def pltimg(im,title=None,mode='rgb',figsize=(8,12),dpi=160,output=None):
    plt.figure(figsize=figsize)
    plt.xticks([]),plt.yticks([])
    if title is not None:
        plt.title(title)
    if mode.lower() == 'bgr':
        im = cv2.cvtColor(im,cv2.COLOR_BGR2RGB)
    f = plt.gcf()
    plt.imshow(im)
    plt.show()
    plt.draw()
    if output is not None:
        bbox_inches='tight'
        ext=osp.splitext(output)[1].replace('.','')
        f.savefig(output,dpi=dpi,format=ext)
        print('Image saved to: {}'.format(output))


# Define a function to detect faces using OpenCV's haarcascades
def detect_faces(classifier,src,scale_factor=1.1,overlaps=3,
                min_size=70, max_size=700,
                flags=0):
    
    min_size = (min_size, min_size) # minimum face size
    max_size = (max_size, max_size) # maximum face size
    
    # Convert to grayscale
    src_gray = cv2.cvtColor(src,cv2.COLOR_BGR2GRAY)
    
    # Run detector
    matches = classifier.detectMultiScale(src_gray, 
                                          scale_factor, 
                                          overlaps, 
                                          flags, 
                                          min_size, 
                                          max_size)
    # By default, this returns x,y,w,w
    # Modify to return x1,y1,x2,y2
    matches = [ (r[0],r[1],r[0]+r[2],r[1]+r[3]) for r in matches]
    
    return matches

def detect_faces_dlib(im,pyramids=0):
    rects = detector(im, pyramids)
    faces = [ (r.left(),r.top(),r.right(),r.bottom()) for r in rects] #x1,y1,x2,y2
    return faces