class CameoDepth(Cameo): def __init__(self): self._windowManager = WindowManager('Cameo', self.onKeypress) self._captureManager = CaptureManager( cv2.VideoCapture(0), self._windowManager, True) self._faceTracker = FaceTracker() self._shouldDrawDebugRects = False self._curveFilter = filters.BGRPortraCurveFilter() def run(self): """Run the main loop.""" self._windowManager.createWindow() while self._windowManager.isWindowCreated: self._captureManager.enterFrame() frame = self._captureManager.frame self._faceTracker.update(frame) faces = self._faceTracker.faces rects.swapRects(frame, frame, [face.faceRect for face in faces]) filters.strokeEdges(frame, frame) self._curveFilter.apply(frame, frame) if self._shouldDrawDebugRects: self._faceTracker.drawDebugRects(frame) self._captureManager.exitFrame() self._windowManager.processEvents()
class Cameo(object): def __init__(self): self._windowManager = WindowManager('Cameo', self.onKeypress) self._captureManager = CaptureManager( cv2.VideoCapture(0), self._windowManager, True) self._faceTracker = FaceTracker() self._shouldDrawDebugRects = False self._curveFilter = filters.BGRPortraCurveFilter() def run(self): """Run the main loop.""" self._windowManager.createWindow() while self._windowManager.isWindowCreated: self._captureManager.enterFrame() frame = self._captureManager.frame if frame is not None: self._faceTracker.update(frame) faces = self._faceTracker.faces rects.swapRects(frame, frame, [face.faceRect for face in faces]) #filters.strokeEdges(frame, frame) self._curveFilter.apply(frame, frame) if self._shouldDrawDebugRects: self._faceTracker.drawDebugRects(frame) self._captureManager.exitFrame() self._windowManager.processEvents() def onKeypress(self, keycode): """Handle a keypress. space -> Take a screenshot. tab -> Start/stop recording a screencast. x -> Start/stop drawing debug rectangles around faces. escape -> Quit. """ if keycode == 32: # space self._captureManager.writeImage('screenshot.png') elif keycode == 9: # tab if not self._captureManager.isWritingVideo: self._captureManager.startWritingVideo( 'screencast.avi') else: self._captureManager.stopWritingVideo() elif keycode == 120: # x self._shouldDrawDebugRects = \ not self._shouldDrawDebugRects elif keycode == 27: # escape self._windowManager.destroyWindow()
class Cameo(object): def __init__(self): self._windowManager = WindowManager('Cameo', self.onKeypress) self._captureManager = CaptureManager( cv2.VideoCapture(0), self._windowManager, True) self._faceTracker = FaceTracker() self._shouldDrawDebugRects = False self._curveFilter = filters.BGRPortraCurveFilter() def run(self): """Run the main loop.""" self._windowManager.createWindow() while self._windowManager.isWindowCreated: self._captureManager.enterFrame() frame = self._captureManager.frame if frame is not None: self._faceTracker.update(frame) faces = self._faceTracker.faces rects.swapRects(frame, frame, [face.faceRect for face in faces]) filters.strokeEdges(frame, frame) self._curveFilter.apply(frame, frame) if self._shouldDrawDebugRects: self._faceTracker.drawDebugRects(frame) self._captureManager.exitFrame() self._windowManager.processEvents() def onKeypress(self, keycode): """Handle a keypress. space -> Take a screenshot. tab -> Start/stop recording a screencast. x -> Start/stop drawing debug rectangles around faces. escape -> Quit. """ if keycode == 32: # space self._captureManager.writeImage('screenshot.png') elif keycode == 9: # tab if not self._captureManager.isWritingVideo: self._captureManager.startWritingVideo( 'screencast.avi') else: self._captureManager.stopWritingVideo() elif keycode == 120: # x self._shouldDrawDebugRects = \ not self._shouldDrawDebugRects elif keycode == 27: # escape self._windowManager.destroyWindow()
class CameoDepth(Cameo): def __init__(self): self._windowManager = WindowManager('Cameo', self.onKeypress) #device = cv2.CAP_OPENNI2 # uncomment for Microsoft Kinect via OpenNI2 device = cv2.CAP_OPENNI2_ASUS # uncomment for Asus Xtion via OpenNI2 self._captureManager = CaptureManager(cv2.VideoCapture(device), self._windowManager, True) self._faceTracker = FaceTracker() self._shouldDrawDebugRects = False self._curveFilter = filters.BGRPortraCurveFilter() def run(self): """Run the main loop.""" self._windowManager.createWindow() while self._windowManager.isWindowCreated: self._captureManager.enterFrame() self._captureManager.channel = cv2.CAP_OPENNI_DISPARITY_MAP disparityMap = self._captureManager.frame self._captureManager.channel = cv2.CAP_OPENNI_VALID_DEPTH_MASK validDepthMask = self._captureManager.frame self._captureManager.channel = cv2.CAP_OPENNI_BGR_IMAGE frame = self._captureManager.frame if frame is None: # Failed to capture a BGR frame. # Try to capture an infrared frame instead. self._captureManager.channel = cv2.CAP_OPENNI_IR_IMAGE frame = self._captureManager.frame if frame is not None: self._faceTracker.update(frame) faces = self._faceTracker.faces masks = [ depth.createMedianMask( disparityMap, validDepthMask, face.faceRect) \ for face in faces ] rects.swapRects(frame, frame, [face.faceRect for face in faces], masks) if self._captureManager.channel == cv2.CAP_OPENNI_BGR_IMAGE: # A BGR frame was captured. # Apply filters to it. filters.strokeEdges(frame, frame) self._curveFilter.apply(frame, frame) if self._shouldDrawDebugRects: self._faceTracker.drawDebugRects(frame) self._captureManager.exitFrame() self._windowManager.processEvents()
class CameoDepth(Cameo): def __init__(self): self._windowManager = WindowManager('Cameo', self.onKeypress) device = depth.CV_CAP_OPENNI # uncomment for Microsoft Kinect #device = depth.CV_CAP_OPENNI_ASUS # uncomment for Asus Xtion self._captureManager = CaptureManager( cv2.VideoCapture(device), self._windowManager, True) self._faceTracker = FaceTracker() self._shouldDrawDebugRects = False self._curveFilter = filters.BGRPortraCurveFilter() def run(self): """Run the main loop.""" self._windowManager.createWindow() while self._windowManager.isWindowCreated: self._captureManager.enterFrame() self._captureManager.channel = \ depth.CV_CAP_OPENNI_DISPARITY_MAP disparityMap = self._captureManager.frame self._captureManager.channel = \ depth.CV_CAP_OPENNI_VALID_DEPTH_MASK validDepthMask = self._captureManager.frame self._captureManager.channel = \ depth.CV_CAP_OPENNI_BGR_IMAGE frame = self._captureManager.frame self._faceTracker.update(frame) faces = self._faceTracker.faces masks = [ depth.createMedianMask( disparityMap, validDepthMask, face.faceRect) \ for face in faces ] rects.swapRects(frame, frame, [face.faceRect for face in faces], masks) filters.strokeEdges(frame, frame) self._curveFilter.apply(frame, frame) if self._shouldDrawDebugRects: self._faceTracker.drawDebugRects(frame) self._captureManager.exitFrame() self._windowManager.processEvents()
class Cameo(object): ''' Cameo object for the vision framework''' def __init__(self): self._windowManager = WindowManager('Cameo', self.onKeypress) self._captureManager = CaptureManager(cv2.VideoCapture(0), self._windowManager, True) self._curves = [None, filters.BGRCrossProcessCurveFilter(), filters.BGRPortraCurveFilter(), filters.BGRProviaCurveFilter(), filters.BGRVelviaCurveFilter()] self._curveIndex = 0 self._curveFilter = self._curves[self._curveIndex] self._recolorFilters = [None, filters.recolorCMV, filters.recolorRC, filters.recolorRGV] self._recolorIndex = 0 self._recolor = self._recolorFilters[self._recolorIndex] self._convolutionFilters = [None, filters.findEdgesFilter(), filters.sharpenFilter(), filters.blurFilter(), filters.embossFilter()] self._convolutionIndex = 0 self._convolution = self._convolutionFilters[self._convolutionIndex] self._strokeEdges = False self._faceTracker = FaceTracker() self._shouldDrawDebugRects = True def run(self): ''' Run the main loop''' self._windowManager.createWindow() self._windowManager.setStatus("K={},C={},R={},S={}".format(self._convolutionIndex,self._curveIndex,self._recolorIndex,self._strokeEdges)) print"Cameo Vision Framework\n"\ "Tab to start/stop recording\n"\ "Space to grab a screenshot\n"\ "r to cycle through recolor filters <none>, CMV, RC, RGV\n"\ "c to cycle through tonemapping curves <none>,crossprocess, porta, provia, velvia\n"\ "k to cycle through convolution filters <none>, find edges,sharpen, blur, emboss\n"\ "s to apply stroke edges filter\n" while self._windowManager.isWindowCreated: self._captureManager.enterFrame() frame = self._captureManager.frame self._faceTracker.update(frame) faces = self._faceTracker.faces rects.swapRects(frame,frame,[face.faceRect for face in faces]) if self._convolution is not None: self._convolution.apply(frame, frame) if self._curveFilter is not None: self._curveFilter.apply(frame, frame) if self._recolor is not None: self._recolor(frame, frame) if self._strokeEdges: filters.strokeEdges(frame, frame) filters.deSkew(frame,frame) if self._shouldDrawDebugRects: self._faceTracker.drawDebugRects(frame) self._captureManager.exitFrame() self._windowManager.processEvents() def onKeypress(self, keycode): ''' Handle keypresses Space -> take screenshot tab -> Start stop recording excape -> quit ''' rawkey = keycode # for handling esc etc keycode = chr(keycode & 255) if rawkey == 0x20: # space self._captureManager.writeImage('screenshot.png') elif rawkey == 0x09: #tab if not self._captureManager.isWritingVideo: self._captureManager.startWritingVideo('screencast.avi') else: self._captureManager.stopWritingVideo() elif rawkey == 0x1b: # escape self._windowManager.destroyWindow() elif keycode in ['c','C']: self._curveIndex += 1 if self._curveIndex >= len(self._curves): self._curveIndex = 0 self._curveFilter = self._curves[self._curveIndex] elif keycode in ['r','R']: self._recolorIndex += 1 if self._recolorIndex >= len(self._recolorFilters): self._recolorIndex = 0 self._recolor = self._recolorFilters[self._recolorIndex] elif keycode in ['k','K']: self._convolutionIndex += 1 if self._convolutionIndex >= len(self._convolutionFilters): self._convolutionIndex = 0 self._convolution = self._convolutionFilters[self._convolutionIndex] elif keycode in ['s','S']: if self._strokeEdges: self._strokeEdges = False else: self._strokeEdges = True elif keycode in ['x','X']: if self._shouldDrawDebugRects: self._shouldDrawDebugRects = False else: self._shouldDrawDebugRects = True statusString="K={},C={},R={},f={}".format(self._convolutionIndex,self._curveIndex,self._recolorIndex,self._strokeEdges) self._windowManager.setStatus(statusString)
class Browser(object): def __init__(self, video_source): self._windowManager = WindowManager('Browser', self.onKeypress) self._captureManager = CaptureManager(video_source, self._windowManager, True) self._faceTracker = FaceTracker() self._shouldDrawDebugRects = False self._curveFilter = filters.BGRPortraCurveFilter() def run(self): """Run the main loop.""" self._windowManager.createWindow() while self._windowManager.isWindowCreated: self._captureManager.enterFrame() frame = self._captureManager.frame if frame is not None: print "got frame" self._faceTracker.update(frame) faces = self._faceTracker.faces rects.swapRects(frame, frame, [face.faceRect for face in faces]) #filters.strokeEdges(frame, frame) #self._curveFilter.apply(frame, frame) if self._shouldDrawDebugRects: self._faceTracker.drawDebugRects(frame) else: print "got None frame" print "press any key to exit." cv2.waitKey(0) break self._captureManager.exitFrame() waitkey_time = 1 if self._captureManager._video_source != 0: waitkey_time = 500 self._windowManager.processEvents(waitkey_time) def onKeypress(self, keycode): """Handle a keypress. space -> Take a screenshot. tab -> Start/stop recording a screencast. x -> Start/stop drawing debug rectangles around faces. escape -> Quit. """ if keycode == 32: # space self._captureManager.writeImage('screenshot.png') elif keycode == 9: # tab if not self._captureManager.isWritingVideo: self._captureManager.startWritingVideo( '/Users/xcbfreedom/Documents/screencast.avi') else: self._captureManager.stopWritingVideo() elif keycode == 120: # x self._shouldDrawDebugRects = \ not self._shouldDrawDebugRects elif keycode == 27: # escape self._windowManager.destroyWindow()
class Facedetect(object): def __init__(self): self._windowManager = WindowManager('Facedetect', self.onKeypress) self._captureManager = CaptureManager(cv2.VideoCapture(camera_nr), self._windowManager, True) self._faceTracker = FaceTracker() self._shouldDrawDebugRects = True self._curveFilter = filters.BGRPortraCurveFilter() def run(self): """Run the main loop.""" self._windowManager.createWindow() while self._windowManager.isWindowCreated: self._captureManager.enterFrame() frame = self._captureManager.frame if frame is not None: t = cv2.getTickCount() self._faceTracker.update(frame) faces = self._faceTracker.faces t = cv2.getTickCount() - t print("time taken for detection = %gms" % (t/(cv2.getTickFrequency())*1000.)) # uncomment this line for swapping faces #rects.swapRects(frame, frame, [face.faceRect for face in faces]) #filters.strokeEdges(frame, frame) #self._curveFilter.apply(frame, frame) if self._shouldDrawDebugRects: self._faceTracker.drawDebugRects(frame) self._faceTracker.drawLinesFromCenter(frame) self._captureManager.exitFrame() self._windowManager.processEvents() def onKeypress(self, keycode): """Handle a keypress. space -> Take a screenshot. tab -> Start/stop recording a screencast. x -> Start/stop drawing debug rectangles around faces. escape -> Quit. """ if keycode == 32: # space self._captureManager.writeImage('screenshot.png') elif keycode == 9: # tab if not self._captureManager.isWritingVideo: self._captureManager.startWritingVideo( 'screencast.avi') else: self._captureManager.stopWritingVideo() elif keycode == 120: # x self._shouldDrawDebugRects = \ not self._shouldDrawDebugRects elif keycode == 27: # escape self._windowManager.destroyWindow() # When everything is done, release the capture self._captureManager.release()
cv2.destroyAllWindows() if __name__=="__main__": arr = os.listdir('faces/') path = 'faces/' for img_path in arr: img = cv2.imread(path + img_path) _faceTracker = FaceTracker() _faceTracker.update(img) faces = _faceTracker.faces _faceTracker.drawDebugRects(img) cv2.imshow('Faces', img) cv2.waitKey(0) cv2.destroyAllWindows() #overlays """sunglasses_img = cv2.imread('overlays/blackhat.png') mask_sungla = create_mask(cv2.imread('overlays/blackhat.png', -1)) path = 'faces/' face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_alt.xml') eye_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_eye.xml')
import cv2 from trackers import FaceTracker faceTracker = FaceTracker() vc = cv2.VideoCapture(0) if vc.isOpened(): rval, frame = vc.read() else: rval = False if rval: image = frame cv2.imwrite('prev.png', image) #image = cv2.imread('Pic.jpg') faceTracker.update(image) faceTracker.drawDebugRects(image) cv2.imwrite('Faces.png', image) else: print('Shit...')
class Browser(object): def __init__(self,video_source): self._windowManager = WindowManager('Browser', self.onKeypress) self._captureManager = CaptureManager(video_source, self._windowManager, True) self._faceTracker = FaceTracker() self._shouldDrawDebugRects = False self._curveFilter = filters.BGRPortraCurveFilter() def run(self): """Run the main loop.""" self._windowManager.createWindow() while self._windowManager.isWindowCreated: self._captureManager.enterFrame() frame = self._captureManager.frame if frame is not None: print "got frame" self._faceTracker.update(frame) faces = self._faceTracker.faces rects.swapRects(frame, frame, [face.faceRect for face in faces]) #filters.strokeEdges(frame, frame) #self._curveFilter.apply(frame, frame) if self._shouldDrawDebugRects: self._faceTracker.drawDebugRects(frame) else: print "got None frame" print "press any key to exit." cv2.waitKey(0) break self._captureManager.exitFrame() waitkey_time=1 if self._captureManager._video_source!=0: waitkey_time=500 self._windowManager.processEvents(waitkey_time) def onKeypress(self, keycode): """Handle a keypress. space -> Take a screenshot. tab -> Start/stop recording a screencast. x -> Start/stop drawing debug rectangles around faces. escape -> Quit. """ if keycode == 32: # space self._captureManager.writeImage('screenshot.png') elif keycode == 9: # tab if not self._captureManager.isWritingVideo: self._captureManager.startWritingVideo( '/Users/xcbfreedom/Documents/screencast.avi') else: self._captureManager.stopWritingVideo() elif keycode == 120: # x self._shouldDrawDebugRects = \ not self._shouldDrawDebugRects elif keycode == 27: # escape self._windowManager.destroyWindow()
class Cameo(object): def __init__(self): self._windowManager = WindowManager('Cameo', self.onKeypress) self._captureManager = CaptureManager(cv2.VideoCapture(0), self._windowManager, True) self._curveFilter = filters.BGRProviaCurveFilter() self._faceTracker = FaceTracker() self._shouldDrawDebugRects = False def run(self): """ Run the main loop """ self._windowManager.createWindow() print("Window '{}' Created".format(self._windowManager.windowName)) print("\n{}\n{}\n{}\n{}".format("Controls:", "space --> Take a screenshot", "tab --> Start/stop recording a screencast", "escape --> Quit")) while self._windowManager.isWindowCreated: self._captureManager.enterFrame() frame = self._captureManager.frame self._faceTracker.update(frame) faces = self._faceTracker.faces rects.swapRects(frame, frame, [face.faceRect for face in faces]) # Add filtering to the frame filters.strokeEdges(frame,frame) self._curveFilter.apply(frame,frame) if self._shouldDrawDebugRects: self._faceTracker.drawDebugRects(frame) self._captureManager.exitFrame() self._windowManager.processEvents() def stop(self): print("[CAMEO] closing all processes") self._captureManager._capture.release() self._windowManager.destroyWindow() def onKeypress(self, keycode): """ Handle a keypress space --> Take a screenshot tab --> Start/stop recording a screencast x --> Toggle drawing debug rectangles around faces escape --> Quit """ if keycode == 32: # Space self._captureManager.writeImage('screenshot.png'); print("Writing image to file....") elif keycode == 9: # Tab if not self._captureManager.isWritingVideo: self._captureManager.startWritingVideo('screencast.avi') print("Writing video to file...") else: self._captureManager.stopWritingVideo() print("Stopped writing video") elif keycode == 120: # x self._shouldDrawDebugRects = not self._shouldDrawDebugRects print("Toggled drawing rectangles") elif keycode == 27: # escape print("Closing Window...") self._windowManager.destroyWindow()