Beginners: please read this post and this post before posting to the forum.
0 Members and 1 Guest are viewing this topic.
I'm mainly concerned about the slow framerate not because I want to see fluid video, but because my robot's responses to what it see's have to be fluid as well. For instance, if my robot is tracking a human face, I'm hoping to have the robot keep up with any of the face's positions, so it doesn't lose the face too easily (or make the servo movements too jittery if the frame rate is too slow).
I'm using python to program in OpenCV, and I'm leaning toward sticking with OpenCV right now. If I do so, however, I'm wondering how I can speed up the processing by optimizing my scripts...
How would I do some profiling on my application? CPU meter or something else maybe? Just to be sure it's not CPU or memory that's bottle necking the rate.
import sysimport cvdef detect(image): image_size = cv.GetSize(image) # create grayscale version global grayscale grayscale = cv.CreateImage(image_size, 8, 1) cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY) # create storage global storage storage = cv.CreateMemStorage(0) # equalize histogram cv.EqualizeHist(grayscale, grayscale) #detect objects global cascade cascade = cv.Load("C:\OpenCV2.1\data\haarcascades\haarcascade_frontalface_default.xml") global faces faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2, 0, (50, 50)) if faces: for (x,y,w,h),n in faces: pt1 = (x,y) pt2 = (x+w,y+h) cv.Rectangle(image, pt1, pt2, 255)if __name__ == "__main__": print "Press ESC to exit ..." # create windows cv.NamedWindow('Camera') # create capture device device = 1 # assume we want first device capture = cv.CreateCameraCapture(1) cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, 320) cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, 240) # 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: print "breaking" 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
import sysimport cvrunning = 1capture_l = 0capture_r = 1left = cv.CreateCameraCapture(1)right = cv.CreateCameraCapture(2)cv.SetCaptureProperty(left, cv.CV_CAP_PROP_FRAME_WIDTH, 320)cv.SetCaptureProperty(left, cv.CV_CAP_PROP_FRAME_HEIGHT, 240)cv.SetCaptureProperty(right, cv.CV_CAP_PROP_FRAME_WIDTH, 320)cv.SetCaptureProperty(right, cv.CV_CAP_PROP_FRAME_HEIGHT, 240)if not left: print "Error opening left capture device" sys.exit(1)if not right: print "Error opening right capture device" sys.exit(1)while running == 1: frameL = cv.QueryFrame(left) frameR = cv.QueryFrame(right) if frameL is None: print "breaking: Failed to capture from Left Camera" break if frameR is None: print "breaking: Failed to capture from Right Camera" break cv.ShowImage('Left Camera', frameL) cv.ShowImage('Right Camera', frameR) k = cv.WaitKey(10) if k == 0x1b: # ESC print 'ESC pressed. Exiting ...' break
Thanks again!
Here is the source code I'm using for face detection:Code: [Select]import sysimport cv# # for the variables later suggested to be put in a more global location,# # create them here and define them in your main program (not the loop portion).def detect(image): # # maybe hardcode this - no sense in requiring a function to be called # # every single face detection go around i saw 320x240 hardcoded # # somewhere else in here image_size = cv.GetSize(image) # # Maybe brake out this image creation and variable from inside this fuction to # # something truly global, then simply copy that instance of the image to a local # # variable. this would again prevent additional un-necessary external API calls # create grayscale version global grayscale grayscale = cv.CreateImage(image_size, 8, 1) cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY) # create storage global storage storage = cv.CreateMemStorage(0) # # This appears that it may be broken out with the image creation of the grayscale object. # # In general, when going to speed/performance you want to prevent repetative work. # equalize histogram cv.EqualizeHist(grayscale, grayscale) # # This 'cascade' file is loading every single time you run this function. Move it out of the function to # # something global and use it where necessary. While Windows will LIKELY cache this file in memory so # # you aren't actually grabbing this from disk every time, it still takes time to open a file and load something. # # Note: This loading process probably has some sort of back end routine that it does to actually process # # the contents of that file into a memory structure representation of the data - also taking up CPU time. #detect objects global cascade cascade = cv.Load("C:\OpenCV2.1\data\haarcascades\haarcascade_frontalface_default.xml") global faces faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2, 0, (50, 50)) if faces: for (x,y,w,h),n in faces: pt1 = (x,y) pt2 = (x+w,y+h) cv.Rectangle(image, pt1, pt2, 255)if __name__ == "__main__": print "Press ESC to exit ..." # create windows cv.NamedWindow('Camera') # create capture device device = 1 # assume we want first device capture = cv.CreateCameraCapture(1) cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, 320) cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, 240) # 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: print "breaking" 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 ...' breakThis one just uses one camera. Here's the source code I'm trying to implement for displaying the images of both cameras (no stereoscopic vision yet). I'm working on the stereo vision script and making progress but still needs a little debugging.Code: [Select]import sysimport cvrunning = 1capture_l = 0capture_r = 1left = cv.CreateCameraCapture(1)right = cv.CreateCameraCapture(2)cv.SetCaptureProperty(left, cv.CV_CAP_PROP_FRAME_WIDTH, 320)cv.SetCaptureProperty(left, cv.CV_CAP_PROP_FRAME_HEIGHT, 240)cv.SetCaptureProperty(right, cv.CV_CAP_PROP_FRAME_WIDTH, 320)cv.SetCaptureProperty(right, cv.CV_CAP_PROP_FRAME_HEIGHT, 240)if not left: print "Error opening left capture device" sys.exit(1)if not right: print "Error opening right capture device" sys.exit(1)while running == 1: frameL = cv.QueryFrame(left) frameR = cv.QueryFrame(right) if frameL is None: print "breaking: Failed to capture from Left Camera" break if frameR is None: print "breaking: Failed to capture from Right Camera" break cv.ShowImage('Left Camera', frameL) cv.ShowImage('Right Camera', frameR) k = cv.WaitKey(10) if k == 0x1b: # ESC print 'ESC pressed. Exiting ...' breakAs I believe you supposed, they are running on the same thread...I'll definitely take a look at the threading information you posted and see if I can implement that into the script. Then I'll see if its appears to run more smoothly.On a side note, I haven't actually tested my robot's movement for choppiness. I'm in the middle of reconstructing the frame. I'll work with what I have in the meantime, however.
import sysimport cv# # for the variables later suggested to be put in a more global location,# # create them here and define them in your main program (not the loop portion).def detect(image): # # maybe hardcode this - no sense in requiring a function to be called # # every single face detection go around i saw 320x240 hardcoded # # somewhere else in here image_size = cv.GetSize(image) # # Maybe brake out this image creation and variable from inside this fuction to # # something truly global, then simply copy that instance of the image to a local # # variable. this would again prevent additional un-necessary external API calls # create grayscale version global grayscale grayscale = cv.CreateImage(image_size, 8, 1) cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY) # create storage global storage storage = cv.CreateMemStorage(0) # # This appears that it may be broken out with the image creation of the grayscale object. # # In general, when going to speed/performance you want to prevent repetative work. # equalize histogram cv.EqualizeHist(grayscale, grayscale) # # This 'cascade' file is loading every single time you run this function. Move it out of the function to # # something global and use it where necessary. While Windows will LIKELY cache this file in memory so # # you aren't actually grabbing this from disk every time, it still takes time to open a file and load something. # # Note: This loading process probably has some sort of back end routine that it does to actually process # # the contents of that file into a memory structure representation of the data - also taking up CPU time. #detect objects global cascade cascade = cv.Load("C:\OpenCV2.1\data\haarcascades\haarcascade_frontalface_default.xml") global faces faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2, 0, (50, 50)) if faces: for (x,y,w,h),n in faces: pt1 = (x,y) pt2 = (x+w,y+h) cv.Rectangle(image, pt1, pt2, 255)if __name__ == "__main__": print "Press ESC to exit ..." # create windows cv.NamedWindow('Camera') # create capture device device = 1 # assume we want first device capture = cv.CreateCameraCapture(1) cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, 320) cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, 240) # 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: print "breaking" 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
import sysimport cv#Parameters for haar detectionmin_size = (20, 20)image_scale = 2haar_scale = 1.2min_neighbors = 2haar_flags = cv.CV_HAAR_DO_CANNY_PRUNING# create capture devicedevice = 1capture = cv.CreateCameraCapture(1)cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, 320)cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, 240)storage = cv.CreateMemStorage(0)def face_detect(image, cascade): MakeImage = cv.CreateImage((320, 240), 8, 1) Scale_Image = cv.CreateImage((cv.Round(320 / image_scale), cv.Round (240 / image_scale)), 8, 1) #convert to grayscale cv.CvtColor(image, MakeImage, cv.CV_BGR2GRAY) #scale image to increase processing cv.Resize(MakeImage, Scale_Image, cv.CV_INTER_LINEAR) cv.EqualizeHist(Scale_Image, Scale_Image) if(cascade): t = cv.GetTickCount() faces = cv.HaarDetectObjects(MakeImage, cascade, storage, 1.2, 2, 0, (50, 50)) t = cv.GetTickCount() - t print "Detection Time =", t if faces: for (x,y,w,h),n in faces: pt1 = (x,y) pt2 = (x+w,y+h) cv.Rectangle(image, pt1, pt2, 255)if __name__ == "__main__": print "Press ESC to exit ..." # create windows cv.NamedWindow('Camera') cascade = cv.Load("C:\OpenCV2.1\data\haarcascades\haarcascade_frontalface_default.xml") # 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: print "breaking" break # mirror cv.Flip(frame, None, 1) # face detection face_detect(frame, cascade) # display webcam image cv.ShowImage('Camera', frame) # handle events k = cv.WaitKey(10) if k == 0x1b: # ESC print 'ESC pressed. Exiting ...' break
import sysimport cv#Parameters for haar detectionmin_size = (20, 20)image_scale = 2haar_scale = 1.2min_neighbors = 2haar_flags = cv.CV_HAAR_DO_CANNY_PRUNING# create capture devicedevice = 1capture = cv.CreateCameraCapture(1)cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, 320)cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, 240)storage = cv.CreateMemStorage(0)# # Last chunk to move out of the loopMakeImage = cv.CreateImage((320, 240), 8, 1)Scale_Image = cv.CreateImage((cv.Round(320 / image_scale), cv.Round (240 / image_scale)), 8, 1)def face_detect(image, cascade): # # Copy global variables to something local for work... # # Memory copying is relatively less expensive than the # # calculations and API calls above in the global tmp_MakeImage = MakeImage tmp_Scale_Image = Scale_Image #convert to grayscale cv.CvtColor(image, tmp_MakeImage, cv.CV_BGR2GRAY) #scale image to increase processing cv.Resize(tmp_MakeImage, tmp_Scale_Image, cv.CV_INTER_LINEAR) cv.EqualizeHist(tmp_Scale_Image, tmp_Scale_Image) if(cascade): t = cv.GetTickCount() faces = cv.HaarDetectObjects(tmp_MakeImage, cascade, storage, 1.2, 2, 0, (50, 50)) t = cv.GetTickCount() - t print "Detection Time =", t if faces: for (x,y,w,h),n in faces: pt1 = (x,y) pt2 = (x+w,y+h) cv.Rectangle(image, pt1, pt2, 255)if __name__ == "__main__": print "Press ESC to exit ..." # create windows cv.NamedWindow('Camera') cascade = cv.Load("C:\OpenCV2.1\data\haarcascades\haarcascade_frontalface_default.xml") # 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: print "breaking" break # mirror cv.Flip(frame, None, 1) # face detection face_detect(frame, cascade) # display webcam image cv.ShowImage('Camera', frame) # handle events k = cv.WaitKey(10) if k == 0x1b: # ESC print 'ESC pressed. Exiting ...' break
import sysimport cvclass CaptureDeviceCreationError(Exception): ''' Raised when a capture device can not be created. '''# Unused variables. I wasn't sure if these could be removed. One reason you# can not haphazardly remove variables, is that other modules may import this# module, and look for these variables.min_size = (20, 20)haar_scale = 1.2min_neighbors = 2haar_flags = cv.CV_HAAR_DO_CANNY_PRUNING# Parameters for haar detection# Upcase constants -- it's programming a convention.IMAGE_SCALE = 2WIDTH, HEIGHT = 320, 240DEVICE = 1 # Keycode for escapeKEY_ESC = 0x1bdef create_capture_device(index): ''' Create the capture device. If the device can't be captured, an CaptureDeviceCreationError is raised. index: Index of the camera to be used. If there is only one camera or it does not matter what camera to use -1 may be passed. ''' capture = cv.CreateCameraCapture(index) # Did you mean to use DEVICE here instead of 1? # Making the assumption that None is returned if the camera cant be created. if not capture: raise CaptureDeviceCreationError() cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, WIDTH) cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, HEIGHT) return capturedef face_detect(image, cascade, capture): # I think make_image and scale_image can be reused, if so make this global # by moving the below statements just above this function definition. make_image = cv.CreateImage((WIDTH, HEIGHT), 8, 1) scale_image = cv.CreateImage((cv.Round(WIDTH / IMAGE_SCALE), cv.Round (HEIGHT / IMAGE_SCALE)), 8, 1) # convert to grayscale cv.CvtColor(image, make_image, cv.CV_BGR2GRAY) # scale image to increase processing cv.Resize(make_image, scale_image, cv.CV_INTER_LINEAR) cv.EqualizeHist(scale_image, scale_image) if cascade: t = cv.GetTickCount() faces = cv.HaarDetectObjects(make_image, cascade, storage, 1.2, 2, 0, (50, 50)) t = cv.GetTickCount() - t print "Detection Time =", t for (x,y,w,h),n in faces: pt1 = (x,y) pt2 = (x+w,y+h) cv.Rectangle(image, pt1, pt2, 255)def main(): print "Press ESC to exit ..." # create windows cv.NamedWindow('Camera') cascade = cv.Load("C:\OpenCV2.1\data\haarcascades\haarcascade_frontalface_default.xml") capture = create_capture_device(DEVICE) storage = cv.CreateMemStorage(0) while 1: # do forever # capture the current frame frame = cv.QueryFrame(capture) return 'Could not QueryFrame' # mirror cv.Flip(frame, None, 1) # face detection face_detect(frame, cascade, capture) # display webcam image cv.ShowImage('Camera', frame) # handle events if cv.WaitKey(10) == KEY_ESC: print 'ESC pressed. Exiting ...' return 0if __name__ == '__main__': sys.exit(main())
import sysimport cvclass CaptureDeviceCreationError(Exception): ''' Raised when a capture device can not be created. '''# Unused variables. I wasn't sure if these could be removed. One reason you# can not haphazardly remove variables, is that other modules may import this# module, and look for these variables.min_size = (20, 20)haar_scale = 1.2min_neighbors = 2haar_flags = cv.CV_HAAR_DO_CANNY_PRUNING# Parameters for haar detection# Upcase constants -- it's programming a convention.IMAGE_SCALE = 2WIDTH, HEIGHT = 320, 240DEVICE = 1 # Keycode for escapeKEY_ESC = 0x1bdef create_capture_device(index): ''' Create the capture device. If the device can't be captured, an CaptureDeviceCreationError is raised. index: Index of the camera to be used. If there is only one camera or it does not matter what camera to use -1 may be passed. ''' capture = cv.CreateCameraCapture(index) # Did you mean to use DEVICE here instead of 1? # Making the assumption that None is returned if the camera cant be created. if not capture: raise CaptureDeviceCreationError() cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, WIDTH) cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, HEIGHT) return capturemake_image = cv.CreateImage((WIDTH, HEIGHT), 8, 1)scale_image = cv.CreateImage((cv.Round(WIDTH / IMAGE_SCALE), cv.Round (HEIGHT / IMAGE_SCALE)), 8, 1)def face_detect(image, cascade, capture): # convert to grayscale cv.CvtColor(image, make_image, cv.CV_BGR2GRAY) # scale image to increase processing cv.Resize(make_image, scale_image, cv.CV_INTER_LINEAR) cv.EqualizeHist(scale_image, scale_image) storage = cv.CreateMemStorage(0) if cascade: t = cv.GetTickCount() faces = cv.HaarDetectObjects(make_image, cascade, storage, 1.2, 2, 0, (50, 50)) t = cv.GetTickCount() - t print "Detection Time =", t for (x,y,w,h),n in faces: pt1 = (x,y) pt2 = (x+w,y+h) cv.Rectangle(image, pt1, pt2, 255)def main(): print "Press ESC to exit ..." # create windows cv.NamedWindow('Camera') cascade = cv.Load("C:\OpenCV2.1\data\haarcascades\haarcascade_frontalface_default.xml") capture = create_capture_device(DEVICE) while 1: # do forever # capture the current frame frame = cv.QueryFrame(capture) # mirror cv.Flip(frame, None, 1) # face detection face_detect(frame, cascade, capture) # display webcam image cv.ShowImage('Camera', frame) # handle events if cv.WaitKey(10) == KEY_ESC: print 'ESC pressed. Exiting ...' return 0if __name__ == '__main__': sys.exit(main())
import sysimport cvclass CaptureDeviceCreationError(Exception): ''' Raised when a capture device can not be created. '''min_size = (20, 20)haar_scale = 1.2min_neighbors = 2haar_flags = cv.CV_HAAR_DO_CANNY_PRUNING# Parameters for haar detectionIMAGE_SCALE = 2WIDTH, HEIGHT = 320, 240DEVICE = 1 # Keycode for escapeKEY_ESC = 0x1bdef Left_Optic(index): ''' Create the capture device. If the device can't be captured, an CaptureDeviceCreationError is raised. index: Index of the camera to be used. If there is only one camera or it does not matter what camera to use -1 may be passed. ''' Left = cv.CreateCameraCapture(1) if not capture: raise CaptureDeviceCreationError() cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, WIDTH) cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, HEIGHT) return Leftdef Right_Optic(index): Right = cv.CreateCameraCapture(2) if not capture: raise CaptureDeviceCreationError() cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, WIDTH) cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, HEIGHT) return Rightmake_image = cv.CreateImage((WIDTH, HEIGHT), 8, 1)scale_image = cv.CreateImage((cv.Round(WIDTH / IMAGE_SCALE), cv.Round (HEIGHT / IMAGE_SCALE)), 8, 1)def face_detect(image, cascade, Left, Right): # convert to grayscale cv.CvtColor(image, make_image, cv.CV_BGR2GRAY) # scale image to increase processing cv.Resize(make_image, scale_image, cv.CV_INTER_LINEAR) cv.EqualizeHist(scale_image, scale_image) storage = cv.CreateMemStorage(0) if cascade: t = cv.GetTickCount() faces = cv.HaarDetectObjects(make_image, cascade, storage, 1.2, 2, 0, (50, 50)) t = cv.GetTickCount() - t print "Detection Time =", t for (x,y,w,h),n in faces: pt1 = (x,y) pt2 = (x+w,y+h) cv.Rectangle(image, pt1, pt2, 255)def main(): print "Press ESC to exit ..." # create windows cv.NamedWindow('Left Optic') cv.NamedWindow('Right Optic') cascade = cv.Load("C:\OpenCV2.1\data\haarcascades\haarcascade_frontalface_default.xml") captureL = Left_Optic(DEVICE) captureR = Right_Optic(DEVICE) while 1: # do forever # capture the current frame frameL = cv.QueryFrame(Left) frameR = cv.QueryFrame(Right) # mirror cv.Flip(frame, None, 1) # face detection face_detect(frame, cascade, Left, Right) # display webcam image cv.ShowImage('Left Optic', frameL) cv.ShowImage('Right Optic', frameR) # handle events if cv.WaitKey(10) == KEY_ESC: print 'ESC pressed. Exiting ...' return 0if __name__ == '__main__': sys.exit(main())
Again, be warned that the below code freezes up my python window, sometimes I need to log off and log on again to get it to go away...Code: [Select]import sysimport cvclass CaptureDeviceCreationError(Exception): ''' Raised when a capture device can not be created. '''min_size = (20, 20)haar_scale = 1.2min_neighbors = 2haar_flags = cv.CV_HAAR_DO_CANNY_PRUNING# Parameters for haar detectionIMAGE_SCALE = 2WIDTH, HEIGHT = 320, 240DEVICE = 1 # Keycode for escapeKEY_ESC = 0x1b# # one function will suffice for bothdef capture_Optic(deviceIndex): ''' Create the capture device. If the device can't be captured, an CaptureDeviceCreationError is raised. index: Index of the camera to be used. If there is only one camera or it does not matter what camera to use -1 may be passed. ''' capDevice = cv.CreateCameraCapture(deviceIndex) if not capture: raise CaptureDeviceCreationError() cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, WIDTH) cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, HEIGHT) return capDevicemake_image = cv.CreateImage((WIDTH, HEIGHT), 8, 1)scale_image = cv.CreateImage((cv.Round(WIDTH / IMAGE_SCALE), cv.Round (HEIGHT / IMAGE_SCALE)), 8, 1)def face_detect(image, cascade, Left, Right): # convert to grayscale cv.CvtColor(image, make_image, cv.CV_BGR2GRAY) # scale image to increase processing cv.Resize(make_image, scale_image, cv.CV_INTER_LINEAR) cv.EqualizeHist(scale_image, scale_image) storage = cv.CreateMemStorage(0) if cascade: t = cv.GetTickCount() faces = cv.HaarDetectObjects(make_image, cascade, storage, 1.2, 2, 0, (50, 50)) t = cv.GetTickCount() - t print "Detection Time =", t for (x,y,w,h),n in faces: pt1 = (x,y) pt2 = (x+w,y+h) cv.Rectangle(image, pt1, pt2, 255)def main(): print "Press ESC to exit ..." # create windows cv.NamedWindow('Left Optic') cv.NamedWindow('Right Optic') cascade = cv.Load("C:\OpenCV2.1\data\haarcascades\haarcascade_frontalface_default.xml") # # Made these reflect the dvice to capture captureL = capture_Optic(1) captureR = capture_Optic(2) while 1: # do forever # capture the current frame frameL = cv.QueryFrame(Left) frameR = cv.QueryFrame(Right) # # Which frame, why do we mirror here? # mirror cv.Flip(frame, None, 1) # face detection face_detect(frameL, cascade, Left, Right) face_detect(frameR, cascade, Left, Right) # display webcam image cv.ShowImage('Left Optic', frameL) cv.ShowImage('Right Optic', frameR) # handle events if cv.WaitKey(10) == KEY_ESC: print 'ESC pressed. Exiting ...' return 0if __name__ == '__main__': sys.exit(main())
import sysimport cvclass CaptureDeviceCreationError(Exception): ''' Raised when a capture device can not be created. '''min_size = (20, 20)haar_scale = 1.2min_neighbors = 2haar_flags = cv.CV_HAAR_DO_CANNY_PRUNING# Parameters for haar detectionIMAGE_SCALE = 2WIDTH, HEIGHT = 320, 240DEVICE = 1 # Keycode for escapeKEY_ESC = 0x1b# # one function will suffice for bothdef capture_Optic(deviceIndex): ''' Create the capture device. If the device can't be captured, an CaptureDeviceCreationError is raised. index: Index of the camera to be used. If there is only one camera or it does not matter what camera to use -1 may be passed. ''' capDevice = cv.CreateCameraCapture(deviceIndex) if not capture: raise CaptureDeviceCreationError() cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, WIDTH) cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, HEIGHT) return capDevicemake_image = cv.CreateImage((WIDTH, HEIGHT), 8, 1)scale_image = cv.CreateImage((cv.Round(WIDTH / IMAGE_SCALE), cv.Round (HEIGHT / IMAGE_SCALE)), 8, 1)def face_detect(image, cascade, Left, Right): # convert to grayscale cv.CvtColor(image, make_image, cv.CV_BGR2GRAY) # scale image to increase processing cv.Resize(make_image, scale_image, cv.CV_INTER_LINEAR) cv.EqualizeHist(scale_image, scale_image) storage = cv.CreateMemStorage(0) if cascade: t = cv.GetTickCount() faces = cv.HaarDetectObjects(make_image, cascade, storage, 1.2, 2, 0, (50, 50)) t = cv.GetTickCount() - t print "Detection Time =", t for (x,y,w,h),n in faces: pt1 = (x,y) pt2 = (x+w,y+h) cv.Rectangle(image, pt1, pt2, 255)def main(): print "Press ESC to exit ..." # create windows cv.NamedWindow('Left Optic') cv.NamedWindow('Right Optic') cascade = cv.Load("C:\OpenCV2.1\data\haarcascades\haarcascade_frontalface_default.xml") # # Made these reflect the dvice to capture captureL = capture_Optic(1) captureR = capture_Optic(2) while 1: # do forever # capture the current frame frameL = cv.QueryFrame(Left) frameR = cv.QueryFrame(Right) # # Which frame, why do we mirror here? # mirror cv.Flip(frame, None, 1) # face detection face_detect(frameL, cascade, Left, Right) face_detect(frameR, cascade, Left, Right) # display webcam image cv.ShowImage('Left Optic', frameL) cv.ShowImage('Right Optic', frameR) # handle events if cv.WaitKey(10) == KEY_ESC: print 'ESC pressed. Exiting ...' return 0if __name__ == '__main__': sys.exit(main())
def create_capture_device(index): ''' Create the capture device. If the device can't be captured, an CaptureDeviceCreationError is raised. index: Index of the camera to be used. If there is only one camera or it does not matter what camera to use -1 may be passed. ''' capture = cv.CreateCameraCapture(index) # Making the assumption that None is returned if the camera cant be created. if not capture: raise CaptureDeviceCreationError() cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, WIDTH) cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, HEIGHT) return capture
captureL = create_capture_device(1) captureR = create_capture_device(2)
frameL = cv.QueryFrame(Left)
import sysimport cvclass CaptureDeviceCreationError(Exception): ''' Raised when a capture device can not be created. '''min_size = (20, 20)haar_scale = 1.2min_neighbors = 2haar_flags = cv.CV_HAAR_DO_CANNY_PRUNING# Parameters for haar detectionIMAGE_SCALE = 2WIDTH, HEIGHT = 320, 240DEVICE = 1 # Keycode for escapeKEY_ESC = 0x1bdef create_capture_device(index): ''' Create the capture device. If the device can't be captured, an CaptureDeviceCreationError is raised. index: Index of the camera to be used. If there is only one camera or it does not matter what camera to use -1 may be passed. ''' capture = cv.CreateCameraCapture(index) # Making the assumption that None is returned if the camera cant be created. if not capture: raise CaptureDeviceCreationError() cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, WIDTH) cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, HEIGHT) return capturemake_image = cv.CreateImage((WIDTH, HEIGHT), 8, 1)scale_image = cv.CreateImage((cv.Round(WIDTH / IMAGE_SCALE), cv.Round (HEIGHT / IMAGE_SCALE)), 8, 1)def face_detect(image, cascade, Left, Right): # convert to grayscale cv.CvtColor(image, make_image, cv.CV_BGR2GRAY) # scale image to increase processing cv.Resize(make_image, scale_image, cv.CV_INTER_LINEAR) cv.EqualizeHist(scale_image, scale_image) storage = cv.CreateMemStorage(0) if cascade: t = cv.GetTickCount() faces = cv.HaarDetectObjects(make_image, cascade, storage, 1.2, 2, 0, (50, 50)) t = cv.GetTickCount() - t print "Detection Time =", t for (x,y,w,h),n in faces: pt1 = (x,y) pt2 = (x+w,y+h) cv.Rectangle(image, pt1, pt2, 255)def main(): print "Press ESC to exit ..." # create windows cv.NamedWindow('Left Optic') cv.NamedWindow('Right Optic') cascade = cv.Load("C:\OpenCV2.1\data\haarcascades\haarcascade_frontalface_default.xml") captureL = create_capture_device(1) captureR = create_capture_device(2) while 1: # do forever # capture the current frame frameL = cv.QueryFrame(captureL) frameR = cv.QueryFrame(captureR) # mirror cv.Flip(frame, None, 1) # face detection face_detect(frame, cascade, captureL, captureR) # display webcam image cv.ShowImage('Left Optic', frameL) cv.ShowImage('Right Optic', frameR) # handle events if cv.WaitKey(10) == KEY_ESC: print 'ESC pressed. Exiting ...' return 0if __name__ == '__main__': sys.exit(main())
#Detects faces using OpenCV on two cameras running at the same time#Not stereoscopic vision, just two cameras running face detectionimport sysimport cvclass CaptureDeviceCreationError(Exception): ''' Raised when a capture device can not be created. '''#These are the parameters used to detect faces.#They are not used in this software, but good to know for reference.#It's possible to tweak them in code, but these will suffice for now.min_size = (20, 20)haar_scale = 1.2min_neighbors = 2haar_flags = cv.CV_HAAR_DO_CANNY_PRUNING# Parameters for haar detectionIMAGE_SCALE = 2WIDTH, HEIGHT = 320, 240DEVICE_L = 0DEVICE_R = 1# Keycode for escapeKEY_ESC = 0x1b#The main function for setting up a camera.def capture_Optic(deviceIndex): ''' Create the capture device. If the device can't be captured, an CaptureDeviceCreationError is raised. index: Index of the camera to be used. If there is only one camera or it does not matter what camera to use -1 may be passed. ''' capDevice = cv.CreateCameraCapture(deviceIndex) if not capDevice: raise CaptureDeviceCreationError() cv.SetCaptureProperty(capDevice, cv.CV_CAP_PROP_FRAME_WIDTH, WIDTH) cv.SetCaptureProperty(capDevice, cv.CV_CAP_PROP_FRAME_HEIGHT, HEIGHT) return capDevicemake_image = cv.CreateImage((WIDTH, HEIGHT), 8, 1)scale_image = cv.CreateImage((cv.Round(WIDTH / IMAGE_SCALE), cv.Round (HEIGHT / IMAGE_SCALE)), 8, 1)def face_detect(image, cascade, captureL, captureR): # convert to grayscale cv.CvtColor(image, make_image, cv.CV_BGR2GRAY) # scale image to increase processing cv.Resize(make_image, scale_image, cv.CV_INTER_LINEAR) cv.EqualizeHist(scale_image, scale_image) storage = cv.CreateMemStorage(0) if cascade: # find out how fast the detection time is taking. t = cv.GetTickCount() faces = cv.HaarDetectObjects(make_image, cascade, storage, 1.2, 2, 0, (50, 50)) t = cv.GetTickCount() - t print "Detection Time =", t # Draw a rectangle around any detected faces. for (x,y,w,h),n in faces: pt1 = (x,y) pt2 = (x+w,y+h) cv.Rectangle(image, pt1, pt2, 255)def main(): print "Press ESC to exit ..." # create windows cv.NamedWindow('Left Optic') cv.NamedWindow('Right Optic') #load the information for detecting faces. cascade = cv.Load("C:\OpenCV2.1\data\haarcascades\haarcascade_frontalface_default.xml") #Capture two Images from Left and Right Cameras captureL = capture_Optic(DEVICE_L) captureR = capture_Optic(DEVICE_R) while 1: # do forever # capture the current frame from the two cameras. frameL = cv.QueryFrame(captureL) frameR = cv.QueryFrame(captureR) # face detection face_detect(frameL, cascade, captureL, captureR) face_detect(frameR, cascade, captureL, captureR) # display webcam image cv.ShowImage('Left Optic', frameL) cv.ShowImage('Right Optic', frameR) # handle events if cv.WaitKey(10) == KEY_ESC: print 'ESC pressed. Exiting ...' return 0if __name__ == '__main__': sys.exit(main())