summaryrefslogtreecommitdiff
path: root/megapixels/app/utils
diff options
context:
space:
mode:
authoradamhrv <adam@ahprojects.com>2019-10-08 16:02:47 +0200
committeradamhrv <adam@ahprojects.com>2019-10-08 16:02:47 +0200
commit27340ac4cd43f8eec7414495b541a65566ae2656 (patch)
treecd43fcf1af026c75e6045d71d7d783ec460ba3ee /megapixels/app/utils
parenta4ea2852f4b46566a61f988342aa04e4059ccef9 (diff)
update site, white
Diffstat (limited to 'megapixels/app/utils')
-rw-r--r--megapixels/app/utils/draw_utils.py42
-rw-r--r--megapixels/app/utils/im_utils.py14
2 files changed, 45 insertions, 11 deletions
diff --git a/megapixels/app/utils/draw_utils.py b/megapixels/app/utils/draw_utils.py
index 7044a62f..1836768b 100644
--- a/megapixels/app/utils/draw_utils.py
+++ b/megapixels/app/utils/draw_utils.py
@@ -3,8 +3,10 @@ from math import sqrt
import numpy as np
import cv2 as cv
+import PIL
+from PIL import ImageDraw
-from app.utils import logger_utils
+from app.utils import logger_utils, im_utils
log = logger_utils.Logger.getLogger()
@@ -118,6 +120,22 @@ def draw_landmarks2D(im, points_norm, radius=3, color=(0,255,0)):
cv.circle(im_dst, pt, radius, color, -1, cv.LINE_AA)
return im_dst
+def draw_landmarks2D_pil(im, points_norm, radius=3, color=(0,255,0)):
+ '''Draws facial landmarks, either 5pt or 68pt
+ '''
+ im_pil = im_utils.ensure_pil(im_utils.bgr2rgb(im))
+ draw = ImageDraw.Draw(im_pil)
+ dim = im.shape[:2][::-1]
+ for x,y in points_norm:
+ x1, y1 = (int(x*dim[0]), int(y*dim[1]))
+ xyxy = (x1, y1, x1+radius, y1+radius)
+ draw.ellipse(xyxy, fill='white')
+ del draw
+ im_dst = im_utils.ensure_np(im_pil)
+ im_dst = im_utils.rgb2bgr(im_dst)
+ return im_dst
+
+
def draw_landmarks3D(im, points, radius=3, color=(0,255,0)):
'''Draws 3D facial landmarks
'''
@@ -126,12 +144,26 @@ def draw_landmarks3D(im, points, radius=3, color=(0,255,0)):
cv.circle(im_dst, (x,y), radius, color, -1, cv.LINE_AA)
return im_dst
-def draw_bbox(im, bbox_norm, color=(0,255,0), stroke_weight=2):
+def draw_bbox(im, bboxes_norm, color=(0,255,0), stroke_weight=2):
'''Draws BBox onto cv image
+ :param color: RGB value
'''
- im_dst = im.copy()
- bbox_dim = bbox_norm.to_dim(im.shape[:2][::-1])
- cv.rectangle(im_dst, bbox_dim.pt_tl, bbox_dim.pt_br, color, stroke_weight, cv.LINE_AA)
+ #im_dst = im.copy()
+ if not type(bboxes_norm) == list:
+ bboxes_norm = [bboxes_norm]
+
+ im_pil = im_utils.ensure_pil(im_utils.bgr2rgb(im))
+ im_pil_draw = ImageDraw.ImageDraw(im_pil)
+
+ for bbox_norm in bboxes_norm:
+ bbox_dim = bbox_norm.to_dim(im.shape[:2][::-1])
+ #cv.rectangle(im_dst, bbox_dim.pt_tl, bbox_dim.pt_br, color, stroke_weight, cv.LINE_AA)
+ xyxy = (bbox_dim.pt_tl, bbox_dim.pt_br)
+ im_pil_draw.rectangle(xyxy, outline=color, width=stroke_weight)
+ # draw.rectangle([x1, y1, x2, y2], outline=, width=3)
+ im_dst = im_utils.ensure_np(im_pil)
+ im_dst = im_utils.rgb2bgr(im_dst)
+ del im_pil_draw
return im_dst
def draw_pose(im, pt_nose, image_pts):
diff --git a/megapixels/app/utils/im_utils.py b/megapixels/app/utils/im_utils.py
index d36c1c32..670d5168 100644
--- a/megapixels/app/utils/im_utils.py
+++ b/megapixels/app/utils/im_utils.py
@@ -11,11 +11,6 @@ from skimage import feature
import imutils
import time
import numpy as np
-import torch
-import torch.nn as nn
-import torchvision.models as models
-import torchvision.transforms as transforms
-from torch.autograd import Variable
from sklearn.metrics.pairwise import cosine_similarity
import datetime
@@ -293,6 +288,13 @@ def bgr2rgb(im):
"""
return cv.cvtColor(im,cv.COLOR_BGR2RGB)
+def rgb2bgr(im):
+ """Wrapper for cv2.cvtColor transform
+ :param im: Numpy.ndarray (BGR)
+ :returns: Numpy.ndarray (RGB)
+ """
+ return cv.cvtColor(im,cv.COLOR_RGB2BGR)
+
def compute_laplacian(im):
# below 100 is usually blurry
return cv.Laplacian(im, cv.CV_64F).var()
@@ -329,7 +331,7 @@ def normalizedGraylevelVariance(img):
s = stdev[0]**2 / mean[0]
return s[0]
-def compute_if_blank(im,width=100,sigma=0,thresh_canny=.1,thresh_mean=4,mask=None):
+def is_blank(im,width=100,sigma=0,thresh_canny=.1,thresh_mean=4,mask=None):
# im is graysacale np
#im = imutils.resize(im,width=width)
#mask = imutils.resize(mask,width=width)