from CVtypes import cv win = 'Show Cam' cv.NamedWindow(win) cap = cv.CreateCameraCapture(0) while cv.WaitKey(1) != 27: img = cv.QueryFrame(cap) cv.ShowImage(win, img)
frame = cv.QueryFrame (capture) # ### get size of the frame frame_size = cv.GetSize (frame) gray = cv.CreateImage( frame_size, 8, 1 ) small_img = cv.CreateImage( cv.Size( int(frame_size.width/image_scale),int(frame_size.height/image_scale)), 8, 1 ) cascade = cv.LoadHaarClassifierCascade( cascade_name, cv.Size(1,1) ) # while 1: # do forever # capture the current image frame = cv.QueryFrame (capture) if frame is None: # no image captured... end the processing break # ### check OS if (osName == "nt"): cv.Flip(frame, frame, 0) else: cv.Flip(frame, None, 1) # ### detecting faces here detect_and_draw(frame, cascade) # ### handle key events k = cv.WaitKey (5) if k % 0x100 == 27: # user has press the ESC key, so exit cv.DestroyWindow('Camera'); break
cv.SetZero(histimg) # compute the width for each bin do display bin_w = histimg[0].width / hdims for i in range(hdims): # for all the bins # get the value, and scale to the size of the hist image val = int( round(cv.GetReal1D(hist[0].bins, i) * histimg[0].height / 255)) # compute the color color = hsv2rgb(i * 180. / hdims) # draw the rectangle in the wanted color cv.Rectangle(histimg, cv.Point(i * bin_w, histimg[0].height), cv.Point((i + 1) * bin_w, histimg[0].height - val), color, -1, 8, 0) # we can now display the images cv.ShowImage('Camera', frame) cv.ShowImage('Histogram', histimg) # handle events k = cv.WaitKey(10) if k == 0x1b: # user has press the ESC key, so exit break
def detect(image): image_size = cv.GetSize(image) # create grayscale version grayscale = cv.CreateImage(image_size, 8, 1) cv.CvtColor(image, grayscale, cv.BGR2GRAY) # create storage storage = cv.CreateMemStorage(0) cv.ClearMemStorage(storage) # equalize histogram cv.EqualizeHist(grayscale, grayscale) # detect objects cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', cv.Size(1, 1)) faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.HAAR_DO_CANNY_PRUNING, cv.Size(50, 50)) if faces: print 'face detected!' for i in faces: cv.Rectangle(image, cv.Point(int(i.x), int(i.y)), cv.Point(int(i.x + i.width), int(i.y + i.height)), cv.RGB(0, 255, 0), 3, 8, 0) # create windows cv.NamedWindow('Camera', cv.WINDOW_AUTOSIZE) # create capture device device = 0 # assume we want first device capture = cv.CreateCameraCapture(0) cv.SetCaptureProperty(capture, cv.CAP_PROP_FRAME_WIDTH, 640) cv.SetCaptureProperty(capture, cv.CAP_PROP_FRAME_HEIGHT, 480) # check if capture device is OK if not capture: print "Error opening capture device" sys.exit(1) while 1: # do forever # capture the current frame frame = cv.QueryFrame(capture) if frame is None: break # mirror cv.Flip(frame, None, 1) # face detection detect(frame) # display webcam image cv.ShowImage('Camera', frame) # handle events k = cv.WaitKey(10) if k == 0x1b: # ESC print 'ESC pressed. Exiting ...' break
# WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # 02110-1301, USA. import sys from CVtypes import cv from facedetection import FaceDetector d = FaceDetector(show_cam=True) key = 0 escape = 27 while key != escape: (present, is_face) = d.fetch_and_detect() if present: print "Available" else: print "Away" key = cv.WaitKey(1) # needed for event loop print 'Cleaning up resources ...' del d print 'Done, exiting now ...' sys.exit(0)