Skip to content

Instantly share code, notes, and snippets.

@hackolite
Created May 10, 2017 19:04
Show Gist options
  • Save hackolite/31921ed4df8c22c6a1c0f555f40b8752 to your computer and use it in GitHub Desktop.
Save hackolite/31921ed4df8c22c6a1c0f555f40b8752 to your computer and use it in GitHub Desktop.
import cv2
from pyimagesearch import imutils
import numpy as np
import argparse
import cv2
class VideoCamera(object):
def __init__(self):
fps = 10
# Using OpenCV to capture from device 0. If you have trouble capturing
# from a webcam, comment the line below out and use a video file
# instead.
self.video = cv2.VideoCapture(0)
self.faceCascade = cv2.CascadeClassifier('face.xml')
self.clockCascade = cv2.CascadeClassifier('clock.xml')
import os
def __del__(self):
self.video.release()
self.video.out.release()
self.video.out.close()
def get_face_frame(self):
success, image = self.video.read()
# We are using Motion JPEG, but OpenCV defaults to capture raw images,
# so we must encode it into JPEG in order to correctly display the
# video stream.
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = self.faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=10,
minSize=(30, 30),
flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
ret, jpeg = cv2.imencode('.jpg', image)
return jpeg.tobytes()
def get_clock_frame(self):
success, image = self.video.read()
# We are using Motion JPEG, but OpenCV defaults to capture raw images,
# so we must encode it into JPEG in order to correctly display the
# video stream.
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
clock = self.clockCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=10,
minSize=(30, 30),
flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)
# Draw a rectangle around the faces
for (x, y, w, h) in clock:
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
ret, jpeg = cv2.imencode('.jpg', image)
return jpeg.tobytes()
def get_skin_frame(self):
# grab the current frame
lower = np.array([0, 48, 80], dtype="uint8")
upper = np.array([20, 255, 255], dtype="uint8")
(grabbed, frame) = self.video.read()
# resize the frame, convert it to the HSV color space,
# and determine the HSV pixel intensities that fall into
# the speicifed upper and lower boundaries
frame = imutils.resize(frame, width=400)
converted = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
skinMask = cv2.inRange(converted, lower, upper)
# apply a series of erosions and dilations to the mask
# using an elliptical kernel
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11))
skinMask = cv2.erode(skinMask, kernel, iterations=2)
skinMask = cv2.dilate(skinMask, kernel, iterations=2)
# blur the mask to help remove noise, then apply the
# mask to the frame
skinMask = cv2.GaussianBlur(skinMask, (3, 3), 0)
skin = cv2.bitwise_and(frame, frame, mask=skinMask)
# show the skin in the image along with the mask
frame = np.hstack([frame, skin])
ret, jpeg = cv2.imencode('.jpg',frame)
return jpeg.tobytes()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment