It's time for a moustache

Let's overlay a moustache on top:

import cv2 import numpy as np mouth_cascade = cv2.CascadeClassifier('./cascade_files/haarcascade_mcs_mouth.xml') moustache_mask = cv2.imread('../images/moustache.png') h_mask, w_mask = moustache_mask.shape[:2] if mouth_cascade.empty(): raise IOError('Unable to load the mouth cascade classifier xml file') cap = cv2.VideoCapture(0) scaling_factor = 0.5 while True: ret, frame = cap.read() frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) mouth_rects = mouth_cascade.detectMultiScale(gray, 1.3, 5) if len(mouth_rects) > 0: (x,y,w,h) = mouth_rects[0] h, w = int(0.6*h), int(1.2*w) x -= 0.05*w y -= ...

Get OpenCV with Python By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.