Video feed with face detection example.

This commit is contained in:
Edgar O Silva 2014-01-09 19:12:03 -06:00
parent 9e0367e9f9
commit a6d6be2d7d
3 changed files with 26257 additions and 0 deletions

View File

@ -0,0 +1,67 @@
cv = require 'opencv'
# First we create a new VideoCapture object to get
# video from the camera (0 for default camera)
camera = new cv.VideoCapture(1)
# we create a window to display the Video frames
namedWindow = new cv.NamedWindow('Video',1)
color = [255,0,0]
# We set an interval to retrieve frames from the
# video source and we get the intervalId so we can
# stop the program from the video feed window.
intervalId = setInterval(()->
# camera.read allows us to retrieve the current
# frame to be displayed in the video window.
camera.read((err, im) ->
# We can check for errors and even break the
# program execution if an error is detected here.
console.log "The err ==>#{ err }" if err
# There is no need to display the image width or Height
# but I leave this here in case anyone needs to check them.
#console.log("Width: #{im.width()}")
#console.log("height: #{im.height()}")
# Before working with the frame we need to check the image
# is already available and has a width and height greater than 0,
# otherwise it will fail when trying to do namedWindow.show()
# and the image has width or height equal or less than 0.
if im.width() > 0 and im.height() > 0
# We apply filters and effects to the frame we got from the
# camera to manipulate the video that we'll display.
# First we convert to grayscale.
#im.convertGrayscale()
# We then apply GaussianBlur.
# It takes an array of type double and size 2
# that indicates how strong the gaussian blur should be.
im.gaussianBlur([7,7])
# We then apply canny edge filtering.
# Params are lower and higher threshold and aperture size,
# although nodejs-opencv overwrite the aperture size param
# with 0. An update to consider it is needed.
#im.canny(0, 30, 3)
#gray = new cv.Matrix(src1.width(), src2.height())
#gray = im.copy()
#gray.convertGrayscale()
im.detectObject('./haarcascades/haarcascade_frontalface_alt.xml', {}, (err, faces) ->
console.log("Faces length => #{ faces }")
for face in faces
im.rectangle([face.x, face.y], [face.x + face.width, face.y + face.height], [0,255,0], 2)
namedWindow.show(im)
)
# We use the previously created namedWindow to display the
# video frame to wich we applied the blur and filter.
# Finally we get the key pressed on the window to terminate
# execution of the program.
res = namedWindow.blockingWaitKey(0, 20)
# In this case I terminate the program if any key is pressed.
if res >= 0 then clearInterval(intervalId)
)
, 50)

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,29 @@
cv = require('opencv')
cv.readImage("./images/mona.png", (err, im) ->
# Create new NamedWindow object to hold the image
# NamedWindow takes two arguments String WindowName and String windowSize
namedWindow = new cv.NamedWindow('Display Window', '400x400')
# We add a couple of basic shapes to the image, just to show how
# to create basic shapes.
# im.rectangle([startX, StartY], [width, height], color(array of RGB)
im.rectangle([50, 50], [200, 200], [0, 255, 0], 2)
# im.ellipse(centerX, centerY, width, height, color(array of RGB)
im.ellipse(150, 150, 50, 50, [255, 255, 0], 2)
# We then tell the image to show the image we loaded.
namedWindow.show(im)
console.log("Image should be displayed inside a window.")
# Finally we tell the NamedWindow to wait for any key being pressed to close
# itself (by passing a 0 as the first param, or wait a defined amount of time
# by passing the time as a second argument (in milliseconds)
#
# If we do not tell the window to wait it will just load and show the image
# and close so fast that it will appear nothing happened.
namedWindow.blockingWaitKey(0, 5000)
console.log("And the window should close automatically or by pressing any key on it.")
)