Skip to content

Instantly share code, notes, and snippets.

@kenoir
Created October 16, 2019 09:45
Show Gist options
  • Save kenoir/dc41957536592d1440300dc958a5d127 to your computer and use it in GitHub Desktop.
Save kenoir/dc41957536592d1440300dc958a5d127 to your computer and use it in GitHub Desktop.
import cv2
import signal
import matplotlib.pyplot as plt
from bokeh.plotting import figure
from bokeh.io import output_notebook, show, push_notebook
import cv2
import time
# https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml
face_cascade = cv2.CascadeClassifier('/Users/k/Desktop/haarcascade_frontalface_default.xml')
output_notebook()
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
frameColor=cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
frameColor=cv2.flip(frameColor, -1)
frameGrey=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frameGrey=cv2.flip(frameGrey, -1)
decolored = cv2.cvtColor(frameGrey, cv2.COLOR_GRAY2RGBA)
width=decolored.shape[1]
height=decolored.shape[0]
p = figure(x_range=(0,width), y_range=(0,height), output_backend="webgl", width=width, height=height)
myImage = p.image_rgba(image=[decolored], x=0, y=0, dw=width, dh=height)
show(p, notebook_handle=True)
while True:
ret, frame = cap.read()
frameColor=cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
frameColor=cv2.flip(frameColor, -1)
frameGrey=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frameGrey=cv2.flip(frameGrey, -1)
decolored = cv2.cvtColor(frameGrey, cv2.COLOR_GRAY2RGBA)
faces = face_cascade.detectMultiScale(frameGrey, 1.1, 4)
for (x, y, w, h) in faces:
colorFace = frameColor[y:y+h,x:x+w]
decolored[y:y+h,x:x+w] = colorFace
myImage.data_source.data['image']=[decolored]
push_notebook()
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment