def DetectEyes(imageCV, faceCascade, eyeCascade): minSize = (20, 20) imageScale = 2 haarScale = 1.2 minNeighbors = 2 haarFlags = 0 # Allocate the temporary images #gray = cv2.CreateImage((imageCV.width, image.height), 8, 1) #smallImage = cv.CreateImage((cv.Round(image.width / image_scale), cv2.Round (image.height / image_scale)), 8 ,1) # Convert color input image to grayscale cv2.cvtColor(image, gray, cv.CV_BGR2GRAY) # Scale input image for faster processing cv2.Resize(gray, smallImage, cv.CV_INTER_LINEAR) # Equalize the histogram cv2.EqualizeHist(smallImage, smallImage) # Detect the faces faces = cv2.HaarDetectObjects(smallImage, faceCascade, cv2.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) # If faces are found if faces: for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) cv2.Rectangle(image, pt1, pt2, cv2.RGB(255, 0, 0), 3, 8, 0)
def DetectFace(image, faceCascade, returnImage=False): min_size = (20, 20) haar_scale = 1.1 min_neighbors = 3 haar_flags = 0 # Equalize the histogram cv.EqualizeHist(image, image) # Detect the faces faces = cv.HaarDetectObjects(image, faceCascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) # If faces are found if faces and returnImage: for ((x, y, w, h), n) in faces: # Convert bounding box to two CvPoints pt1 = (int(x), int(y)) pt2 = (int(x + w), int(y + h)) cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 5, 8, 0) if returnImage: return image else: return faces
def DetectFace(image, faceCascade, returnImage=False): # This function takes a grey scale cv image and finds # the patterns defined in the haarcascade function # modified from: http://www.lucaamore.com/?p=638 #variables min_size = (20, 20) haar_scale = 1.1 min_neighbors = 3 haar_flags = 0 # Equalize the histogram cv2.EqualizeHist(image, image) # Detect the faces faces = cv2.HaarDetectObjects(image, faceCascade, cv2.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) # If faces are found if faces and returnImage: for ((x, y, w, h), n) in faces: # Convert bounding box to two CvPoints pt1 = (int(x), int(y)) pt2 = (int(x + w), int(y + h)) cv2.Rectangle(image, pt1, pt2, cv2.RGB(255, 0, 0), 5, 8, 0) if returnImage: return image else: return faces
def detect(self, obj, event): # First, reset image, in case of previous detections: active_handle = self.get_active('Media') media = self.dbstate.db.get_media_from_handle(active_handle) self.load_image(media) min_face_size = (50, 50) # FIXME: get from setting self.cv_image = cv2.LoadImage(self.full_path, cv2.CV_LOAD_IMAGE_GRAYSCALE) o_width, o_height = self.cv_image.width, self.cv_image.height cv2.EqualizeHist(self.cv_image, self.cv_image) cascade = cv2.Load(HAARCASCADE_PATH) faces = cv2.HaarDetectObjects(self.cv_image, cascade, cv2.CreateMemStorage(0), 1.2, 2, cv2.CV_HAAR_DO_CANNY_PRUNING, min_face_size) references = self.find_references() rects = [] o_width, o_height = [ float(t) for t in (self.cv_image.width, self.cv_image.height) ] for ((x, y, width, height), neighbors) in faces: # percentages: rects.append((x / o_width, y / o_height, width / o_width, height / o_height)) self.draw_rectangles(rects, references)
def detect_no_draw(self, img): # allocate temporary images gray = cv.CreateImage((img.width, img.height), 8, 1) small_img = cv.CreateImage((cv.Round(img.width / self.image_scale), cv.Round(img.height / self.image_scale)), 8, 1) # convert color input image to grayscale cv.CvtColor(img, gray, cv.CV_BGR2GRAY) # scale input image for faster processing cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) cv.EqualizeHist(small_img, small_img) if self.cascade: t = cv.GetTickCount() faces = cv.HaarDetectObjects(small_img, self.cascade, cv.CreateMemStorage(0), self.haar_scale, self.min_neighbors, self.haar_flags, self.min_size) t = cv.GetTickCount() - t if faces: return True else: return False
def DetectFace(image, faceCascade): #modified from: http://www.lucaamore.com/?p=638 min_size = (20, 20) image_scale = 1 haar_scale = 1.1 min_neighbors = 3 haar_flags = 0 # Allocate the temporary images smallImage = cv2.CreateImage((cv2.Round( image.width / image_scale), cv2.Round(image.height / image_scale)), 8, 1) # Scale input image for faster processing cv2.Resize(image, smallImage, cv2.CV_INTER_LINEAR) # Equalize the histogram cv2.EqualizeHist(smallImage, smallImage) # Detect the faces faces = cv2.HaarDetectObjects(smallImage, faceCascade, cv2.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) # If faces are found if faces: for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) cv2.Rectangle(image, pt1, pt2, cv2.RGB(255, 0, 0), 5, 8, 0) return image
def get_faces(self, image): """ Given an opencv image, return a ((x,y,w,h), certainty) tuple for each face detected. """ # Convert the image to grayscale and normalise cv.CvtColor(image, self.gray, cv.CV_BGR2GRAY) cv.EqualizeHist(self.gray, self.gray) # Detect faces return cv.HaarDetectObjects(self.gray, self.cascade, self.storage, scale_factor=1.3, min_neighbors=2, flags=cv.CV_HAAR_DO_CANNY_PRUNING, min_size=(40, 40))
def get_hands(image): """ Returns the hand as white on black. Uses value in HSV to determine hands.""" size = cv2.GetSize(image) hsv = cv2.CreateImage(size, 8, 3) hue = cv2.CreateImage(size, 8, 1) sat = cv2.CreateImage(size, 8, 1) val = cv2.CreateImage(size, 8, 1) hands = cv2.CreateImage(size, 8, 1) cv2.Cv2tColor(image, hsv, cv2.CV2_BGR2HSV) cv2.Split(hsv, hue, sat, val, None) cv2.ShowImage('Live', image) cv2.ShowImage('Hue', hue) cv2.ShowImage('Saturation', sat) cv2.Threshold( hue, hue, 10, 255, cv2.CV2_THRESH_TOZERO) #set to 0 if <= 10, otherwise leave as is cv2.Threshold( hue, hue, 244, 255, cv2.CV2_THRESH_TOZERO_INV) #set to 0 if > 244, otherwise leave as is cv2.Threshold(hue, hue, 0, 255, cv2.CV2_THRESH_BINARY_INV) #set to 255 if = 0, otherwise 0 cv2.Threshold( sat, sat, 64, 255, cv2.CV2_THRESH_TOZERO) #set to 0 if <= 64, otherwise leave as is cv2.EqualizeHist(sat, sat) cv2.Threshold(sat, sat, 64, 255, cv2.CV2_THRESH_BINARY) #set to 0 if <= 64, otherwise 255 cv2.ShowImage('Saturation threshold', sat) cv2.ShowImage('Hue threshold', hue) cv2.Mul(hue, sat, hands) #smooth + threshold to filter noise # cv2.Smooth(hands, hands, smoothtype=cv2.CV2_GAUSSIAN, param1=13, param2=13) # cv2.Threshold(hands, hands, 200, 255, cv2.CV2_THRESH_BINARY) cv2.ShowImage('Hands', hands) return hands
def detect_and_draw(self, img): # allocate temporary images gray = cv.CreateImage((img.width, img.height), 8, 1) small_img = cv.CreateImage((cv.Round(img.width / self.image_scale), cv.Round(img.height / self.image_scale)), 8, 1) # convert color input image to grayscale cv.CvtColor(img, gray, cv.CV_BGR2GRAY) # scale input image for faster processing cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) cv.EqualizeHist(small_img, small_img) if self.cascade: t = cv.GetTickCount() faces = cv.HaarDetectObjects(small_img, self.cascade, cv.CreateMemStorage(0), self.haar_scale, self.min_neighbors, self.haar_flags, self.min_size) t = cv.GetTickCount() - t # print "time taken for detection = %gms" % (t/(cv.GetTickFrequency()*1000.)) if faces: face_found = True for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints pt1 = (int(x * self.image_scale), int(y * self.image_scale)) pt2 = (int((x + w) * self.image_scale), int((y + h) * self.image_scale)) cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) else: face_found = False cv.ShowImage("video", img) return face_found
def detectFace(self, cam_img, faceCascade, eyeCascade, mouthCascade): # cam_img should be cv2.cv.iplcam_img min_size = (20, 20) image_scale = 2 haar_scale = 1.2 min_neighbors = 2 haar_flags = 0 image_width = int(cam_img.get(cv.CV_CAP_PROP_FRAME_WIDTH)) image_height = int(cam_img.get(cv.CV_CAP_PROP_FRAME_HEIGHT)) # Allocate the temporary images gray = cv.CreateImage((image_width, image_height), 8, 1) # tuple as the first arg smallImage = cv.CreateImage((cv.Round(image_width / image_scale), cv.Round(image_height / image_scale)), 8, 1) (ok, img) = cam_img.read() # print 'gray is of ',type(gray) >>> gray is of <type 'cv2.cv.iplimage'> # print type(smallImage) >>> <type 'cv2.cv.iplimage'> # print type(image) >>> <type 'cv2.VideoCapture'> # print type(img) >>> <type 'numpy.ndarray'> # convert numpy.ndarray to iplimage ipl_img = cv2.cv.CreateImageHeader((img.shape[1], img.shape[0]), cv.IPL_DEPTH_8U, 3) cv2.cv.SetData(ipl_img, img.tostring(), img.dtype.itemsize * 3 * img.shape[1]) # Convert color input image to grayscale cv.CvtColor(ipl_img, gray, cv.CV_BGR2GRAY) # Scale input image for faster processing cv.Resize(gray, smallImage, cv.CV_INTER_LINEAR) # Equalize the histogram cv.EqualizeHist(smallImage, smallImage) # Detect the faces faces = cv.HaarDetectObjects(smallImage, faceCascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) # => The function returns a list of tuples, (rect, neighbors) , where rect is a CvRect specifying the object’s extents and neighbors is a number of neighbors. # => CvRect cvRect(int x, int y, int width, int height) # If faces are found if faces: face = faces[0] self.faceX = face[0][0] self.faceY = face[0][1] for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) cv.Rectangle(ipl_img, pt1, pt2, cv.RGB(0, 0, 255), 3, 8, 0) # face_region = cv.GetSubRect(ipl_img,(x,int(y + (h/4)),w,int(h/2))) cv.SetImageROI(ipl_img, (pt1[0], pt1[1], pt2[0] - pt1[0], int((pt2[1] - pt1[1]) * 0.7))) eyes = cv.HaarDetectObjects(ipl_img, eyeCascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, (15, 15)) if eyes: # For each eye found for eye in eyes: # Draw a rectangle around the eye cv.Rectangle(ipl_img, # image (eye[0][0], # vertex pt1 eye[0][1]), (eye[0][0] + eye[0][2], # vertex pt2 opposite to pt1 eye[0][1] + eye[0][3]), cv.RGB(255, 0, 0), 1, 4, 0) # color,thickness,lineType(8,4,cv.CV_AA),shift cv.ResetImageROI(ipl_img) return ipl_img
import cv2 as cv img = cv.LoadImage("friend1.jpg") image_size = cv.GetSize(img) #获取图片的大小 greyscale = cv.CreateImage(image_size, 8, 1) #建立一个相同大小的灰度图像 cv.CvtColor(img, greyscale, cv.CV_BGR2GRAY) #将获取的彩色图像,转换成灰度图像 storage = cv.CreateMemStorage(0) #创建一个内存空间,人脸检测是要利用,具体作用不清楚 cv.EqualizeHist(greyscale, greyscale) #将灰度图像直方图均衡化,貌似可以使灰度图像信息量减少,加快检测速度 # detect objects cascade = cv.Load('haarcascade_frontalface_alt2.xml') #加载Intel公司的训练库 #检测图片中的人脸,并返回一个包含了人脸信息的对象faces faces = cv.HaarDetectObjects(greyscale, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, (50, 50)) #获得人脸所在位置的数据 j = 0 #记录个数 for (x, y, w, h), n in faces: j += 1 cv.SetImageROI(img, (x, y, w, h)) #获取头像的区域 cv.SaveImage("face" + str(j) + ".jpg", img) #保存下来
if frame.origin == cv.IPL_ORIGIN_TL: cv.Flip(frame, frame, -1) # Our operations on the frame come here gray = cv.CreateImage((frame.width, frame.height), 8, 1) small_img = cv.CreateImage((cv.Round( frame.width / image_scale), cv.Round(frame.height / image_scale)), 8, 1) # convert color input image to grayscale cv.CvtColor(frame, gray, cv.CV_BGR2GRAY) # scale input image for faster processing cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) cv.EqualizeHist(small_img, small_img) midFace = None if (cascade): t = cv.GetTickCount() # HaarDetectObjects takes 0.02s faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) t = cv.GetTickCount() - t if faces: lights(50 if len(faces) == 0 else 0, 50 if len(faces) > 0 else 0, 0, 50) for ((x, y, w, h), n) in faces: