From 77a6d19c3b9e3c0899296a6af9a6248200db0087 Mon Sep 17 00:00:00 2001 From: Edgar O Silva Date: Fri, 10 Jan 2014 15:59:29 -0600 Subject: [PATCH 1/2] Reading less frames per second avoids the issue of multiple faces detected when only one exists due to the same frame being updated while image detection processing is executed. --- examples/coffeescript/camera-face-detection.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/coffeescript/camera-face-detection.coffee b/examples/coffeescript/camera-face-detection.coffee index fb41f90..07da68d 100644 --- a/examples/coffeescript/camera-face-detection.coffee +++ b/examples/coffeescript/camera-face-detection.coffee @@ -41,9 +41,9 @@ intervalId = setInterval(()-> # Finally we get the key pressed on the window to terminate # execution of the program. - res = namedWindow.blockingWaitKey(0, 20) + res = namedWindow.blockingWaitKey(0, 50) # In this case I terminate the program if any key is pressed. if res >= 0 then clearInterval(intervalId) ) -, 50) +, 150) From 1c96622c6728fb373d0a7b272ea4318f666f4fa5 Mon Sep 17 00:00:00 2001 From: Edgar O Silva Date: Fri, 10 Jan 2014 16:01:01 -0600 Subject: [PATCH 2/2] Use recursinve frame reading (instead of time interval) from video feed to try and hit max frames per second. --- .../camera-face-detect-max-frames.coffee | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 examples/coffeescript/camera-face-detect-max-frames.coffee diff --git a/examples/coffeescript/camera-face-detect-max-frames.coffee b/examples/coffeescript/camera-face-detect-max-frames.coffee new file mode 100644 index 0000000..5e6eee7 --- /dev/null +++ b/examples/coffeescript/camera-face-detect-max-frames.coffee @@ -0,0 +1,56 @@ +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', 0) +color = [255,0,0] + +frameRead = () -> + # 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 + #console.log("Interval ID => #{ intervalId }") + #console.log(intervalId) + clearInterval(intervalId) if intervalId? + im.detectObject('./haarcascades/haarcascade_frontalface_alt.xml', {}, (err, faces) -> + for face in faces + im.rectangle([face.x, face.y], [face.x + face.width, face.y + face.height], [0,255,0], 2) + + # We use the previously created namedWindow to display the + # video frame to wich we applied the blur and filter. + namedWindow.show(im) + + # Finally we get the key pressed on the window to terminate + # execution of the program. + res = namedWindow.blockingWaitKey(0, 20) + + # In this case we terminate the program if any key is pressed. + #if res >= 0 then do not set a new timeout to get a new frame. + setTimeout(frameRead, 5) if res < 0 + ) + + ) + +# We set an interval to retrieve frames from the +# video source and we get the intervalId so we can +# stop the program by pressing any key on the video feed window. +intervalId = setInterval(() -> + frameRead(intervalId) +, 100)