Guest User

Untitled

a guest
Jul 16th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. import sys
  2. import cv
  3. from opencv import highgui
  4.  
  5. def detect(image):
  6. image_size = cv.GetSize(image)
  7.  
  8. # create grayscale version
  9. grayscale = cv.CreateImage(image_size, 8, 1)
  10. cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY)
  11.  
  12. # create storage
  13. storage = cv.CreateMemStorage(0)
  14. #cv.ClearMemStorage(storage)
  15.  
  16. # equalize histogram
  17. cv.EqualizeHist(grayscale, grayscale)
  18.  
  19. # detect objects
  20. cascade = cv.Load('/usr/share/doc/opencv-doc/examples/haarcascades/haarcascades/haarcascade_frontalface_alt.xml')
  21. faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, (50, 50))
  22.  
  23. if faces:
  24. print 'face detected!'
  25. for (x,y,w,h),n in faces:
  26. cv.Rectangle(image, ( x, y),
  27. (x + w, y + h),
  28. cv.RGB(0, 255, 0), 3, 8, 0)
  29.  
  30. if __name__ == "__main__":
  31.  
  32.  
  33. print "Press ESC to exit ..."
  34.  
  35. # create windows
  36. highgui.cvNamedWindow('Camera', highgui.CV_WINDOW_AUTOSIZE)
  37.  
  38. # create capture device
  39. device = 0 # assume we want first device
  40. capture = cv.CreateCameraCapture(0)
  41. #highgui.cvSetCaptureProperty(capture, highgui.CV_CAP_PROP_FRAME_WIDTH, 640)
  42. #highgui.cvSetCaptureProperty(capture, highgui.CV_CAP_PROP_FRAME_HEIGHT, 480)
  43.  
  44. # check if capture device is OK
  45. if not capture:
  46. print "Error opening capture device"
  47. sys.exit(1)
  48.  
  49. while 1:
  50. # do forever
  51.  
  52. # capture the current frame
  53. frame = cv.QueryFrame(capture)
  54. if frame is None:
  55. break
  56.  
  57. # mirror
  58. cv.Flip(frame, None, 1)
  59.  
  60. # face detection
  61. detect(frame)
  62.  
  63. # display webcam image
  64. cv.ShowImage('Camera', frame)
  65.  
  66. # handle events
  67. k = cv.WaitKey(10)
  68.  
  69. if k == 0x1b: # ESC
  70. print 'ESC pressed. Exiting ...'
  71. break
Add Comment
Please, Sign In to add comment