Guest User

Untitled

a guest
Jan 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. import sys
  2. sys.path.append("/home/pc128/opencv/lib/site-packages")
  3.  
  4. from opencv.cv import *
  5. from opencv.highgui import *
  6.  
  7. # Global Variables
  8. cascade = None
  9. storage = cvCreateMemStorage(0)
  10. cascade_name = "/home/pc128/opencv/opencv/OpenCV-2.0.0/data/haarcascades/haarcascade_frontalface_alt.xml"
  11. input_name = 0
  12.  
  13. # Parameters for haar detection
  14. # From the API:
  15. # The default parameters (scale_factor=1.1, min_neighbors=3, flags=0) are tuned
  16. # for accurate yet slow object detection. For a faster operation on real video
  17. # images the settings are:
  18. # scale_factor=1.2, min_neighbors=2,
  19. flags=CV_HAAR_DO_CANNY_PRUNING,
  20. # min_size=<minimum possible face size
  21. min_size = cvSize(20,20)
  22. image_scale = 1.3
  23. haar_scale = 1.2
  24. min_neighbors = 2
  25. haar_flags = 0
  26.  
  27.  
  28. def detect_and_draw( img ):
  29. # allocate temporary images
  30. gray = cvCreateImage( cvSize(img.width,img.height), 8, 1 )
  31. small_img = cvCreateImage((cvRound(img.width/image_scale),
  32. cvRound (img.height/image_scale)), 8, 1 )
  33.  
  34. # convert color input image to grayscale
  35. cvCvtColor( img, gray, CV_BGR2GRAY )
  36.  
  37. # scale input image for faster processing
  38. cvResize( gray, small_img, CV_INTER_LINEAR )
  39.  
  40. cvEqualizeHist( small_img, small_img )
  41.  
  42. cvClearMemStorage( storage )
  43.  
  44. if( cascade ):
  45. t = cvGetTickCount()
  46. faces = cvHaarDetectObjects( small_img, cascade,storage,haar_scale, min_neighbors, haar_flags, min_size)
  47. t = int(cvGetTickCount())-int(t)
  48. print "detection time = %gms" % (float(t)/(cvGetTickFrequency()*1000.))
  49. if faces:
  50. for face_rect in faces:
  51. # the input to cvHaarDetectObjects was resized, so scale the
  52. # bounding box of each face and convert it to two CvPoints
  53. pt1 = cvPoint( int(face_rect.x*image_scale), int(face_rect.y*image_scale))
  54. pt2 = cvPoint( int((face_rect.x+face_rect.width)*image_scale),
  55. int((face_rect.y+face_rect.height)*image_scale) )
  56. cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 )
  57.  
  58. cvShowImage( "result", img )
  59.  
  60.  
  61. if __name__ == '__main__':
  62.  
  63. if len(sys.argv) > 1:
  64.  
  65. if sys.argv[1].startswith("--cascade="):
  66. cascade_name = sys.argv[1][ len("--cascade="): ]
  67. if len(sys.argv) > 2:
  68. input_name = sys.argv[2]
  69.  
  70. elif sys.argv[1] == "--help" or sys.argv[1] == "-h":
  71. print "Usage: facedetect --cascade=\"<cascade_path>\"filename|camera_index]\n"
  72. sys.exit(-1)
  73.  
  74. else:
  75. input_name = sys.argv[1]
  76.  
  77. # the OpenCV API says this function is obsolete, but we can't
  78. # cast the output of cvLoad to a HaarClassifierCascade, so use this anyways
  79. # the size parameter is ignored
  80. cascade = cvLoadHaarClassifierCascade( cascade_name, cvSize(1,1) )
  81.  
  82. if not cascade:
  83. print "ERROR: Could not load classifier cascade"
  84. sys.exit(-1)
  85.  
  86.  
  87. if input_name.isdigit():
  88. capture = cvCreateCameraCapture( int(input_name) )
  89. else:
  90. capture = cvCreateFileCapture( input_name )
  91.  
  92. cvNamedWindow( "result", 1 )
  93.  
  94. if capture:
  95. frame_copy = None
  96. while True:
  97. frame = cvQueryFrame( capture )
  98. if not frame:
  99. cvWaitKey(0)
  100. break
  101. if not frame_copy:
  102. frame_copy = cvCreateImage( cvSize(frame.width,frame.height),
  103. IPL_DEPTH_8U, frame.nChannels )
  104. if frame.origin == IPL_ORIGIN_TL:
  105. cvCopy( frame, frame_copy )
  106. else:
  107. cvFlip( frame, frame_copy, 0 )
  108.  
  109. detect_and_draw( frame_copy )
  110.  
  111. if( cvWaitKey( 10 ) >= 0 ):
  112. break
  113.  
  114. else:
  115. image = cvLoadImage( input_name, 1 )
  116.  
  117. if image:
  118. detect_and_draw( image )
  119. cvWaitKey(0)
  120.  
  121. cvDestroyWindow("result")
Add Comment
Please, Sign In to add comment