Skip to content

Instantly share code, notes, and snippets.

@yiminglin-ai
Last active January 7, 2021 10:46
Show Gist options
  • Save yiminglin-ai/c1d1fb9c07d09b4ccd5a4c3ed0eccfd3 to your computer and use it in GitHub Desktop.
Save yiminglin-ai/c1d1fb9c07d09b4ccd5a4c3ed0eccfd3 to your computer and use it in GitHub Desktop.
[visualization] visualizing images or videos #python #moviepy #matplotlib
from collections import OrderedDict
#For dlib’s 68-point facial landmark detector:
FACIAL_LANDMARKS_68_IDXS = OrderedDict([
("mouth", (48, 68)),
("inner_mouth", (60, 68)),
("right_eyebrow", (17, 22)),
("left_eyebrow", (22, 27)),
("right_eye", (36, 42)),
("left_eye", (42, 48)),
("nose", (27, 36)),
("jaw", (0, 17))
])
#For dlib’s 5-point facial landmark detector:
FACIAL_LANDMARKS_5_IDXS = OrderedDict([
("right_eye", (2, 3)),
("left_eye", (0, 1)),
("nose", (4))
])
def visualize_facial_landmarks(image, shape, colors=None, alpha=0.75):
# create two copies of the input image -- one for the
# overlay and one for the final output image
overlay = image.copy()
output = image.copy()
# if the colors list is None, initialize it with a unique
# color for each facial landmark region
if colors is None:
colors = [(19, 199, 109), (79, 76, 240), (230, 159, 23),
(168, 100, 168), (158, 163, 32),
(163, 38, 32), (180, 42, 220)]
# loop over the facial landmark regions individually
for (i, name) in enumerate(FACIAL_LANDMARKS_IDXS.keys()):
# grab the (x, y)-coordinates associated with the
# face landmark
(j, k) = FACIAL_LANDMARKS_IDXS[name]
pts = shape[j:k]
# check if are supposed to draw the jawline
if name == "jaw":
# since the jawline is a non-enclosed facial region,
# just draw lines between the (x, y)-coordinates
for l in range(1, len(pts)):
ptA = tuple(pts[l - 1])
ptB = tuple(pts[l])
cv2.line(overlay, ptA, ptB, colors[i], 2)
# otherwise, compute the convex hull of the facial
# landmark coordinates points and display it
else:
hull = cv2.convexHull(pts)
cv2.drawContours(overlay, [hull], -1, colors[i], -1)
# apply the transparent overlay
cv2.addWeighted(overlay, alpha, output, 1 - alpha, 0, output)
# return the output image
return output

Adjusting the Order of Legend items¶

ax = subplot(1,1,1)
p1, = ax.plot([1,2,3], label="line 1")
p2, = ax.plot([3,2,1], label="line 2")
p3, = ax.plot([2,3,1], label="line 3")

handles, labels = ax.get_legend_handles_labels()

# reverse the order
ax.legend(handles[::-1], labels[::-1])

# or sort them by labels
import operator
hl = sorted(zip(handles, labels),
            key=operator.itemgetter(1))
handles2, labels2 = zip(*hl)

ax.legend(handles2, labels2)
def show_batch_images(image_batch,box_batch=None):
'''
Show a batch of sequences
input:
image_batch: N * T * C * W * H
box_batch: N * T * 4
'''
columns = image_batch.shape[1]
batch_size = image_batch.shape[0]
rows = batch_size
fig = plt.figure(figsize = (64,(64 // columns) * rows))
gs = gridspec.GridSpec(rows, columns)
data = torch.reshape(image_batch, [-1] + list(image_batch.shape[2:]))
if box_batch is not None:
bboxes = np.reshape(box_batch, [-1] + list(box_batch.shape[2:]))
for j in range(rows*columns):
img = data[j].cpu().numpy().transpose(1,2,0).astype(np.uint8).copy()
if box_batch is not None:
x,y,w,h = [int(n) for n in bboxes[j]]
cv2.rectangle(img, (x,y), (w,h), (255,0,0),3)
plt.subplot(gs[j])
plt.axis("off")
plt.imshow(img)
def make_video(im_folder, save_folder, save_name=None, fps=15):
folder_name = osp.basename(im_folder)
if save_name:
fn_out = osp.join(save_folder, save_name)
else:
fn_out = osp.join(save_folder, folder_name+'.mp4')
print('converting {} to a video {} at fps={}'.format(folder_name, fn_out,
fps))
from moviepy.editor import ImageSequenceClip
im_list = sorted(os.listdir(im_folder))
im_list = [osp.join(im_folder, im) for im in im_list]
clip = ImageSequenceClip(im_list, fps=fps)
clip.write_videofile(fn_out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment