diff options
Diffstat (limited to 'megapixels/notebooks/utils/imx.py')
| -rw-r--r-- | megapixels/notebooks/utils/imx.py | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/megapixels/notebooks/utils/imx.py b/megapixels/notebooks/utils/imx.py new file mode 100644 index 00000000..0dbdcf7e --- /dev/null +++ b/megapixels/notebooks/utils/imx.py @@ -0,0 +1,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 +
\ No newline at end of file |
