O'Reilly logo

OpenCV with Python By Example by Prateek Joshi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 -= ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required