예제 #1
0
def detect_and_save(img, cascade, output_name):
    # allocate temporary images
    gray = cv.CreateImage((img.width, img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(
        img.width / image_scale), cv.Round(img.height / 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 (cascade):
        t = cv.GetTickCount()
        faces = cv.HaarDetectObjects(small_img, cascade,
                                     cv.CreateMemStorage(0), haar_scale,
                                     min_neighbors, haar_flags, min_size)
        t = cv.GetTickCount() - t
        print "detection time = %gms" % (t / (cv.GetTickFrequency() * 1000.))
        f = open(output_name, 'w')
        if faces:
            for ((x, y, w, h), n) in faces:
                f.write("%d %d %d %d\n" % (x, y, w, h))
예제 #2
0
def detect_and_draw(img, cascade):
    # allocate temporary images
    gray = cv.CreateImage((img.width, img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(
        img.width / image_scale), cv.Round(img.height / 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 (cascade):
        t = cv.GetTickCount()
        faces = cv.HaarDetectObjects(small_img, cascade,
                                     cv.CreateMemStorage(0), haar_scale,
                                     min_neighbors, haar_flags, min_size)
        t = cv.GetTickCount() - t
        print "detection time = %gms" % (t / (cv.GetTickFrequency() * 1000.))
        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))
                cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)

    cv.ShowImage("result", img)
예제 #3
0
 def __findObjects(self):
     t = cv.GetTickCount()
     self.__objects = cv.HaarDetectObjects(
         self.__capturedImageSmall, self.__detectPattern,
         cv.CreateMemStorage(0), self.__haar_scale, self.__min_neighbors,
         self.__haar_flags, self.__min_size)
     t = cv.GetTickCount() - t
     self.__detectionTime = (t / (cv.GetTickFrequency() * 1000.))
예제 #4
0
class image_converter:

  def __init__(self):
    self.image_pub = rospy.Publisher("/imagecv",Image)

    cv.NamedWindow("Image window", 1)
    self.bridge = CvBridge()
    self.image_sub = rospy.Subscriber("/usb_cam/image_raw",Image,self.callback)
    self.cascade = cv.Load("/home/anton/work_files/ros/my_package/haarcascade_frontalface_alt2.xml")


  def callback(self,data):
    try:
      cv_image = self.bridge.imgmsg_to_cv(data, "bgr8")
    except CvBridgeError, e:
      print e

    gray = cv.CreateImage((cv_image.width,cv_image.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(cv_image.width / image_scale),
  	  	       cv.Round (cv_image.height / image_scale)), 8, 1)

    # convert color input image to grayscale
    cv.CvtColor(cv_image, 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)

    #say = 0
    if(self.cascade):
        t = cv.GetTickCount()
        faces = cv.HaarDetectObjects(small_img, self.cascade, cv.CreateMemStorage(0),
                                     haar_scale, min_neighbors, haar_flags, min_size)
        t = cv.GetTickCount() - t
        print "detection time = %gms" % (t/(cv.GetTickFrequency()*1000.))
        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))
                cv.Rectangle(cv_image, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)
		#say = 1
	   
    cv.ShowImage("Image window", cv_image)
    cv.WaitKey(3)

    try:
      self.image_pub.publish(self.bridge.cv_to_imgmsg(cv_image, "bgr8"))
      #if say == 1:
      #  import festival
      #  tts = pyTTS.Create()
      #  tts.SetVoiceByName('MSSam')
      #  tts.Speak("Hello, fellow Python programmer")
    except CvBridgeError, e:
      print e
예제 #5
0
def detect_and_draw(img, cascade):
    # allocate temporary images
    gray = cv.CreateImage((img.width, img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(
        img.width / image_scale), cv.Round(img.height / 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)

    window = cv.CreateImage((cv.Round(img.width), cv.Round(img.height)), 8, 3)
    if (cascade):
        t = cv.GetTickCount()
        faces = local_haar_detect(small_img, cascade, cv.CreateMemStorage(0),
                                  haar_scale, min_neighbors, haar_flags,
                                  min_size)
        t = cv.GetTickCount() - t
        print "detection time = %gms" % (t / (cv.GetTickFrequency() * 1000.))
        channels = None
        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 = (cv.Round(
                    (x + w * .2) * image_scale), cv.Round(y * image_scale))
                pt2 = (cv.Round(
                    (x + w * .8) * image_scale), cv.Round(
                        (y + h) * image_scale))

                window = cv.CreateImage((cv.Round(w * .6) * image_scale,
                                         cv.Round(h) * image_scale), 8, 3)
                cv.Smooth(window, window, cv.CV_GAUSSIAN)
                channels = [
                    cv.CreateImage((cv.Round(w * .6) * image_scale,
                                    cv.Round(h) * image_scale), 8, 1),
                    cv.CreateImage((cv.Round(w * .6) * image_scale,
                                    cv.Round(h) * image_scale), 8, 1),
                    cv.CreateImage((cv.Round(w * .6) * image_scale,
                                    cv.Round(h) * image_scale), 8, 1)
                ]
                cv.GetRectSubPix(img, window, (cv.Round(
                    (pt1[0] + pt2[0]) / 2.0), cv.Round(
                        (pt1[1] + pt2[1]) / 2.0)))
                cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)
                cv.Split(window, channels[0], channels[1], channels[2], None)
                result.append([
                    cv.Avg(channels[0])[0],
                    cv.Avg(channels[1])[0],
                    cv.Avg(channels[2])[0]
                ])

    cv.ShowImage("result", img)
예제 #6
0
	def classifyKNN(self, onImg, theSign, neighbors):
		#0) get training data data and the labels
		indexs,labels,train = self.getDataLabels(onImg, theSign, True)

		#1) initialize the svm and compute the model
		problem = mlpy.Knn(neighbors, dist='se')

		#2) shuffle input data to do the 10-fold split 
		shuffle(indexs)
		labels = labels[indexs]
		train  = train[indexs,:] 

		#3) define the folds, train and test
		pred_err     = 0.0
		err_rock     = 0.0
		err_paper    = 0.0
		err_scissors = 0.0
		fold_ind = 0
		folds    = self.myFolds(labels, [1,2,3], 50)
		for (trainI,testI) in folds:
			fold_ind += 1
			trainSet, testSet = train[trainI], train[testI]
			trainLab, testLab = labels[trainI], labels[testI]			
			learned           = problem.compute(trainSet, trainLab)
			print "it learned ["+str(fold_ind)+"] >>> "+str(learned)

	 		zaTime     = cv.GetTickCount() 
			totalTime  = 0
			prediction = problem.predict(testSet)
			wrong      = numpy.where(numpy.array(prediction) != numpy.array(testLab))[0]
			if(theSign == "rock"):
				labWrong       = testLab[map(None,wrong)]
				wrong_rock     = numpy.where(labWrong == 1)[0]
				wrong_paper    = numpy.where(labWrong == 2)[0]
				wrong_scissors = numpy.where(labWrong == 3)[0]
				rocks          = numpy.where(testLab == 1)[0]
				papers         = numpy.where(testLab == 2)[0]
				scissors       = numpy.where(testLab == 3)[0]
				err_rock      += float(float(wrong_rock.shape[0])/float(len(rocks)))
				err_paper     += float(float(wrong_paper.shape[0])/float(len(papers)))
				err_scissors  += float(float(wrong_scissors.shape[0])/float(len(scissors)))
			pred_err  += float(float(wrong.shape[0])/float(len(testLab)))
			print prediction
			print testLab
			zaTime     = cv.GetTickCount() - zaTime
	    		totalTime += zaTime/(cv.GetTickFrequency()*1000.0*float(len(testLab)))
			print "cumulative error %f >>> prediction time/image %gms" % (pred_err, totalTime)                	
		avg_err = float(pred_err)/float(len(folds))
		if(theSign == "rock"):		
			avg_rock     = float(err_rock)/float(len(folds))
			avg_paper    = float(err_paper)/float(len(folds))
			avg_scissors = float(err_scissors)/float(len(folds))

		print "\nAvg Error:"+str(avg_err)+" >>> Avg Rock Error:"+str(avg_rock)+" >>> Avg Paper Error:"+str(avg_paper)+" >>> Avg Scissors Error:"+str(avg_scissors)
		return problem
예제 #7
0
def track(img, threshold=100):
    '''Accepts BGR image and optional object threshold between 0 and 255 (default = 100).
       Returns: (x,y) coordinates of centroid if found
                (-1,-1) if no centroid was found
                None if user hit ESC
    '''
    cascade = cv.Load("haarcascade_frontalface_alt_tree.xml")
    #cascade = cv.Load("haarcascade_frontalface_default.xml")
    gray = cv.CreateImage((img.width, img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(
        img.width / image_scale), cv.Round(img.height / 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)

    center = (-1, -1, -1)
    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:
            faces.sort()
            ((x, y, w, h), n) = faces[-1]
            # 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(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)
            #cv.Rectangle(img, (x,y), (x+w,y+h), 255)
            # get the xy corner co-ords, calc the center location
            x1 = pt1[0]
            x2 = pt2[0]
            y1 = pt1[1]
            y2 = pt2[1]
            centerx = x1 + ((x2 - x1) / 2)
            centery = y1 + ((y2 - y1) / 2)
            center = (centerx, centery, h * 3)

    if cmp(center, (-1, -1, -1)) == 0:
        center = None

    cv.NamedWindow(WINDOW_NAME, 1)
    cv.ShowImage(WINDOW_NAME, img)

    if cv.WaitKey(5) == 27:
        center = None
    return center
예제 #8
0
def detect_and_draw(img, cascade):
    gray = cv.CreateImage((img.width,img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(img.width / image_scale),
			       cv.Round (img.height / 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(cascade):
        t = cv.GetTickCount()
        faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0),
                                     haar_scale, min_neighbors, haar_flags, min_size)
        index = 0 
        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
                x1, y1 = (int(x * image_scale), int(y * image_scale))
                x2, y2 = (int((x + w) * image_scale), int((y + h) * image_scale))

                pi = Image.fromstring("L", cv.GetSize(gray), gray.tostring())
                pi = pi.crop((x1, y1, x2, y2))
                pi = pi.resize((64, 64), Image.ANTIALIAS)
                path = os.path.join(sys.argv[1])
                pi.save(path)
                index += 1 
        else:
            os.remove(sys.argv[1])
예제 #9
0
    def findFrame(img, haarcascade, scaleFactor=1.2, minNeighbors=2):
        global loaded, lastcascadefile, cascade

        if (haarcascade <> lastcascadefile):
            loaded = False

        if (loaded == False):
            loaded = True
            print haarcascade
            cascade = cv.Load(haarcascade)
            lastcascadefile = haarcascade

        # allocate temporary images
        gray = cv.CreateImage((img.width, img.height), 8, 1)
        small_img = cv.CreateImage((cv.Round(
            img.width / image_scale), cv.Round(img.height / 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)
        faces = None
        if (cascade):
            t = cv.GetTickCount()
            faces = cv.HaarDetectObjects(small_img, cascade,
                                         cv.CreateMemStorage(0), haar_scale,
                                         min_neighbors, haar_flags, min_size)
            t = cv.GetTickCount() - t
            print "detection time = %gms" % (t /
                                             (cv.GetTickFrequency() * 1000.))
            if faces:
                print "found 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))
                    cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)
                    print "x= " + str(x) + " y= " + str(y) + " w= " + str(
                        w) + " h= " + str(h)
        cv.ShowImage("Face detection", img)
        return faces
예제 #10
0
def detect_and_draw(img, cascade):

    rect = (1, 1, 1, 1)

    # allocate temporary images
    gray = cv.CreateImage((img.width, img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(
        img.width / image_scale), cv.Round(img.height / 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 (cascade):
        t = cv.GetTickCount()
        faces = cv.HaarDetectObjects(small_img, cascade,
                                     cv.CreateMemStorage(0), haar_scale,
                                     min_neighbors, haar_flags, min_size)
        t = cv.GetTickCount() - t
        print "detection time = %gms" % (t / (cv.GetTickFrequency() * 1000.))
        if faces:
            for ((x, y, w, h), n) in faces:
                faceX = int(x * image_scale)
                faceY = int(y * image_scale)
                faceW = int((x + w))
                faceH = int((y + h))
                rect = (faceX, faceY, faceW, faceH)
                # 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(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)

    cv.ShowImage("result", img)

    #sets the Region of Interest
    cv.SetImageROI(img, rect)
    face = cv.CreateImage(cv.GetSize(img), img.depth, img.nChannels)
    #copy subimage */
    cv.Copy(img, face)
    #always reset the Region of Interest */
    cv.ResetImageROI(img)
    cv.ShowImage("resultFace", face)
예제 #11
0
 def doPrediction(self, zaType, model, problem, what, image):
     totalTime = 0
     if (model == "svm"):
         classifyWhat = {
             "hands": {
                 "1": "hands",
                 "-1": "garb"
             },
             "rock": {
                 "1": "rock",
                 "-1": "paper or scissors"
             },
             "paper": {
                 "1": "paper",
                 "-1": "scissors"
             }
         }
     else:
         classifyWhat = {
             "hands": {
                 "1": "hands",
                 "-1": "garb",
                 "0": "none"
             },
             "rock": {
                 "1": "rock",
                 "2": "paper",
                 "3": "scissors",
                 "0": "none"
             }
         }
     zaTime = cv.GetTickCount()
     testImg = self.preprocessImg(image, zaType)
     prediction = problem.predict(testImg)
     zaTime = cv.GetTickCount() - zaTime
     totalTime += zaTime / (cv.GetTickFrequency() * 1000.0)
     if ((what != "hands") or (prediction[0] == 0 and what == "hands")):
         print "it is a..." + str(
             classifyWhat[str(what)][str(prediction[0])]
         ) + " >>> prediction time/image %gms" % totalTime
     return str(classifyWhat[str(what)][str(prediction[0])])
def detect_and_draw(img, cascade):
    gray = cv.CreateImage((img.width, img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(
        img.width / image_scale), cv.Round(img.height / 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)

    centre = 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:
            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(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)
                # get the xy corner co-ords, calc the centre location
                x1 = pt1[0]
                x2 = pt2[0]
                y1 = pt1[1]
                y2 = pt2[1]
                centrex = x1 + ((x2 - x1) / 2)
                centrey = y1 + ((y2 - y1) / 2)
                centre = (centrex, centrey)

    cv.ShowImage("result", img)
    return centre
예제 #13
0
def find_objects(img, cascade, shiftX = 0, shiftY = 0):
    min_size = (20, 20)
    image_scale = 2
    haar_scale = 1.2
    min_neighbors = 2
    haar_flags = cv.CV_HAAR_DO_CANNY_PRUNING

    # allocate temporary images
    gray = cv.CreateImage((img.width,img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(img.width / image_scale),
			       cv.Round (img.height / 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)
    width = 640
    height = 480

    if(cascade):
        t = cv.GetTickCount()
        faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0),
                                     haar_scale, min_neighbors, haar_flags, min_size)
        t = cv.GetTickCount() - t
        print "detection time = %gms" % (t/(cv.GetTickFrequency()*1000.))
        locations = []
        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 = (width-(shiftX+int(x * image_scale)), height-(shiftY+int(y * image_scale)))
                pt2 = (width-(shiftX+int((x + w) * image_scale)), height-(shiftY+int((y + h) * image_scale)))
                locations.append((pt2, pt1))
                #cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)

    #cv.ShowImage("result", img)
    return locations
def detect_and_draw(img, cascade):
    gray = cv.CreateImage((img.width, img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(
        img.width / image_scale), cv.Round(img.height / 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 (cascade):
        t = cv.GetTickCount()
        faces = cv.HaarDetectObjects(small_img, cascade,
                                     cv.CreateMemStorage(0), haar_scale,
                                     min_neighbors, haar_flags, min_size)
        index = 0
        if faces:
            for ((x, y, w, h), n) in faces:
                x1, y1 = (int(x * image_scale), int(y * image_scale))
                x2, y2 = (int((x + w) * image_scale), int(
                    (y + h) * image_scale))
                print index, x1, y1, x2, y2
                index += 1
예제 #15
0
    hasHist = False

    bestFaceX = 0
    bestFaceY = 0
    bestFaceW = 0
    bestFaceH = 0

    if capture:
        inputFrame = None
        while True:
            frameCount += 1

            if frameCount % 50 == 0:
                hasHist = False

            t = cv.GetTickCount()  #start timer
            frame = cv.QueryFrame(capture)
            if not frame:
                cv.WaitKey(0)
                break
            if not inputFrame:
                inputFrame = cv.CreateImage((frame.width, frame.height),
                                            cv.IPL_DEPTH_8U, frame.nChannels)
            if frame.origin == cv.IPL_ORIGIN_TL:
                cv.Copy(frame, inputFrame)
            else:
                cv.Flip(frame, inputFrame, 0)

            #### resize and convert images ####
            # resized color input image
            frameSmall = cv.CreateImage(
예제 #16
0
def detect_and_draw(img, cascade):
    t = cv.GetTickCount()  ## start counter
    cv.CvtColor(img, gray, cv.CV_BGR2GRAY)
    cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)

    #Ages all trackedFaces
    for f in trackedFaces:
        f.updateLife()
    #Remove expired faces
    for f in trackedFaces:
        if (f.isTooOld()):
            trackedFaces.remove(f)

    faces = cv.HaarDetectObjects(small_img, cascade, storage, haar_scale,
                                 min_neighbors, haar_flags, min_size)
    drawline = 0
    if faces:
        #found a face
        for ((x, y, w, h), n) in faces:
            matchedFace = False
            pt1 = (int(x * image_scale), int(y * image_scale))
            pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
            pt3 = (int(x * image_scale) + int(
                ((x + w) * image_scale - x * image_scale) / 3),
                   int(y * image_scale))
            pt4 = (int((x + w) * image_scale) - int(
                ((x + w) * image_scale - x * image_scale) / 3),
                   int((y * image_scale) + int((
                       (y + h) * image_scale) - int(y * image_scale)) / 3))

            #check if there are trackedFaces
            if (len(trackedFaces) > 0):
                #each face being tracked
                for f in trackedFaces:
                    #the face is found (small movement)
                    if ((abs(f.xpt - pt1[0]) < FACE_MAX_MOVEMENT)
                            and (abs(f.ypt - pt1[1]) < FACE_MAX_MOVEMENT)):
                        matchedFace = True
                        f.updateFace(int(w * image_scale),
                                     int(h * image_scale), pt1[0], pt1[1])
                        mf = f
                        break

                #if face not found, add a new face
                if (matchedFace == False):
                    f = Face(0, int(w * image_scale), int(h * image_scale),
                             pt1[0], pt1[1], 0)
                    trackedFaces.append(f)
                    mf = f
            #No tracked faces: adding one
            else:
                f = Face(0, int(w * image_scale), int(h * image_scale), pt1[0],
                         pt1[1], 0)
                trackedFaces.append(f)
                mf = f
            #where to draw face and properties
            if (mf.age > 5):

                #draw attention line
                lnpt1 = (int(mf.xpt * scale), int(mf.ypt * scale - 5) - 5)
                if (mf.age > mf.width):
                    lnpt2 = (int(mf.xpt * scale + mf.width),
                             int(mf.ypt * scale - 5))
                else:
                    lnpt2 = (int(mf.xpt * scale + mf.age),
                             int(mf.ypt * scale - 5))

                cv.Rectangle(img, lnpt1, lnpt2, RED, 4, 8,
                             0)  ## drawing bolded attention line

                ### draw eyes
                cv.Rectangle(img, mf.eyeLeft1, mf.eyeLeft2, MAGENTA, 3, 8, 0)
                cv.Rectangle(img, mf.eyeRight1, mf.eyeRight2, MAGENTA, 3, 8, 0)
                #
                ### draw mouth
                cv.Rectangle(img, mf.mouthTopLeft, mf.mouthBotRight, ORANGE, 3,
                             8, 0)
                #
                ### draw face
                cv.Rectangle(img, pt1, pt2, getColor(mf), 3, 8, 0)
                #cv.Rectangle( img, pt3, pt4, MAGENTA, 1, 8, 0 ) #forehead
                drawline = mf.age

    if (CAPTURING): saveAsJPG(img)
    if (osName == "nt"): cv.Flip(img, img, 0)
    cv.ShowImage('Camera', img)
    t = cv.GetTickCount() - t  ## counter for FPS
    print "%i fps." % (cv.GetTickFrequency() * 1000000. / t)  ## print FPS
예제 #17
0
def detect_and_draw(img, cascade):

    global time_point
    global frame_no
    global input_data
    global fs
    global max_bps

    global last_f

    # allocate temporary images
    gray = cv.CreateImage((img.width, img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(
        img.width / image_scale), cv.Round(img.height / 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)

    window = cv.CreateImage((cv.Round(img.width), cv.Round(img.height)), 8, 3)
    if (cascade):
        faces = local_haar_detect(small_img, cascade, cv.CreateMemStorage(0),
                                  haar_scale, min_neighbors, haar_flags,
                                  min_size)

        channels = None
        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 = (cv.Round(
                    (x + w * .2) * image_scale), cv.Round(y * image_scale))
                pt2 = (cv.Round(
                    (x + w * .8) * image_scale), cv.Round(
                        (y + h) * image_scale))

                window = cv.CreateImage((cv.Round(w * .6) * image_scale,
                                         cv.Round(h) * image_scale), 8, 3)
                #cv.Smooth(window, window, cv.CV_GAUSSIAN, 3, 3)
                channels = [
                    cv.CreateImage((cv.Round(w * .6) * image_scale,
                                    cv.Round(h) * image_scale), 8, 1),
                    cv.CreateImage((cv.Round(w * .6) * image_scale,
                                    cv.Round(h) * image_scale), 8, 1),
                    cv.CreateImage((cv.Round(w * .6) * image_scale,
                                    cv.Round(h) * image_scale), 8, 1)
                ]

                cv.GetRectSubPix(img, window, (cv.Round(
                    (pt1[0] + pt2[0]) / 2.0), cv.Round(
                        (pt1[1] + pt2[1]) / 2.0)))

                cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)
                cv.Split(window, channels[0], channels[1], channels[2], None)
                input_data.append([
                    cv.Avg(channels[0])[0],
                    cv.Avg(channels[1])[0],
                    cv.Avg(channels[2])[0]
                ])

                #measure the sampling frequency
                now_point = cv.GetTickCount()

                if float(fs) / 2 < max_bps and fs != 0:
                    max_bps = float(fs) / 2

                if len(input_data) > frame_no:
                    fs = cv.GetTickFrequency() * 1000000. / (now_point -
                                                             time_point)
                    input_data.pop(0)

                    #print my_functions.calc_heart_rate(input_data)
                    final_data = my_functions.calc_heart_rate(input_data)
                    tmp_last_f = my_functions.plot_diagrams(
                        final_data, fs, last_f)
                    last_f = tmp_last_f
                    print last_f

                time_point = now_point
        else:
            print "Can not detect face"

    cv.ShowImage("result", img)
예제 #18
0
def detect_and_draw(img, cascade):
    # allocate temporary images
    global var1
    global pos
    gray = cv.CreateImage((img.width, img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(
        img.width / image_scale), cv.Round(img.height / 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 (cascade):
        t = cv.GetTickCount()
        faces = cv.HaarDetectObjects(small_img, cascade,
                                     cv.CreateMemStorage(0), haar_scale,
                                     min_neighbors, haar_flags, min_size)
        t = cv.GetTickCount() - t
        #print "detection time = %gms" % (t/(cv.GetTickFrequency()*1000.))
        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))
                cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)
                var1 = var1 + 1
                if var1 == 5:
                    center = ((pt1[0] + pt2[0]) / 2, (pt1[1] + pt2[1]) / 2)
                    print center[0], center[1]
                    #LEFT
                    #if center[0]>220 and center[0]<320:
                    #	client_socket.send("6")

                    #elif center[0]>120 and center[0]<220:
                    #	client_socket.send("5")

                    if center[0] > 420:
                        if pos < 9:
                            pos = pos + 1
                        client_socket.send(str(pos))

                    #RIGHT

                    #elif center[0]>320 and center[0]<420:
                    #	client_socket.send("7")

                    #elif center[0]>420 and center[0]<520:
                    #	client_socket.send("8")

                    elif center[0] < 200:
                        if pos > 3:
                            pos = pos - 1
                        client_socket.send(str(pos))
                    print pos
                    var1 = 0

    cv.ShowImage("result", img)
예제 #19
0
def detect_and_draw(img, cascade):
    global previousPos
    # allocate temporary images
    gray = cv.CreateImage((img.width,img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(img.width / image_scale),
			       cv.Round (img.height / 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(cascade):
        t = cv.GetTickCount()
        xcenter = 640/2
        ycenter = 480/2
	centersize = 150
        innercentersize = 100
	cv.Rectangle(img, (xcenter-centersize,ycenter-centersize), (xcenter+centersize,ycenter+centersize), cv.RGB(0, 255, 0), 3, 8, 0)
        cv.Rectangle(img, (xcenter-innercentersize,ycenter-innercentersize), (xcenter+innercentersize,ycenter+innercentersize), cv.RGB(0, 0, 255), 3, 8, 0)
        faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0),
                                     haar_scale, min_neighbors, 1, min_size)
        t = cv.GetTickCount() - t
        print "detection time = %gms" % (t/(cv.GetTickFrequency()*1000.))
        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))
                cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)
                
		xval = x+w/2
		yval = y+h/2
                print "(%sx%s,%sx%s)" % (x,y,w,h)
		print "(%s,%s)=%s" % (xcenter,xval,xcenter-xval)
                msgs = False
                if 1:
                #if previousPos:
                    if (xval>170):
                       controlMessage("left")
                       msgs = True
                    elif (xval<130):
                       controlMessage("right")
                       msgs = True
                    #else:
                    #	   controlMessage("stayx")
                    if (w>centersize):
                       msgs = True
                       controlMessage("up")
                    elif (w<innercentersize):
                       msgs = True
                       controlMessage("down")
                    
                    if not msgs:
                       controlMessage("stay")
                #previousPos = (xval,yval)
	#	if (xval
		

    cv.ShowImage("result", img)
예제 #20
0
def detect_and_send(img, cascade):
    global screenW, screenH, threshold, Debug, showCamera
    global oldTimeDebug
    global somme, cpt

    # allocate temporary images
    gray = cv.CreateImage((img.width, img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(
        img.width / image_scale), cv.Round(img.height / image_scale)), 8, 1)

    # l'algo de reconnaissance de forme est basé sur des images en niveaux de gris
    cv.CvtColor(img, gray, cv.CV_BGR2GRAY)

    # Réduit la taille de l'image pour un traitement plus rapide
    cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)

    #Augmente le contraste (égalise la luminosité)
    cv.EqualizeHist(small_img, small_img)
    centreCercle = (0, 0)
    if (cascade):

        t = cv.GetTickCount()
        faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0), \
                                    haar_scale, min_neighbors, haar_flags, min_size)
        t = cv.GetTickCount() - t
        cpt += 1
        somme += t / (cv.GetTickFrequency() * 1000.)
        if Debug == 1:
            print "detection time = %gms" % (t /
                                             (cv.GetTickFrequency() * 1000.))

        if faces:
            oldx = centreCercle[0]
            oldy = centreCercle[1]

            for ((x, y, w, h), n) in faces:
                #la bounding box encadrant le visage, sachant que l'image a été remdimensionnée pour le traitement (+ rapide)
                pt1 = (int(x * image_scale), int(y * image_scale))
                pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))

                cv.Rectangle(img, pt1, pt2, (255, 0, 0))
                #On doit calculer l'opposé de la valeur qu'on trouverait car la caméra n'agit pas comme un miroir
            #en effet si je bouge vers la droite l'image fait bouger la tête vers la gauche (donc vers la droite selon mon
            #point de vue)
            ex = -(float(centreCercle[0]) / float(img.width) * screenW -
                   screenW / 2.0)
            ey = -(
                (float(centreCercle[1]) / float(img.height) * screenH -
                 screenH / 2.0)
            )  #+ screenH / 2.0 #compense le fait que la caméra est au dessus de l'écran
            oldx = centreCercle[0]
            oldy = centreCercle[1]
            #Détermination de ez en fonction de la taille de la tête à l'écran
            ez = -1.50567 * (h * image_scale) + 893.688

            #assure la stabilité de l'image, si on enlève ce test ça tremblote à mort car la détection n'étant pas ultra précise
            #la position de la tête est considérée comme changeante en permanence
            #la constante a été déterminée empiriquement sur une webcam intégrée de mcbook 13,3"(voir fichier conf.cfg)
            #Gestion du temps pour permettre de bouger lentement (fonctionne mal pour l'instant)
            newTime = time.time()
            if abs(oldx - centreCercle[0]) > threshold or abs(
                    oldy - centreCercle[1]
            ) > threshold:  #or (newTime - oldTimeThr) > 1
                liblo.send(target, "/coordonnees/", ex, ey, ez)
                oldTimeThr = newTime

            #DEBUG, affiche la position des yeux toutes les 1/2 secondes
            if Debug == 1:
                if (newTime - oldTimeDebug) > 0.5:
                    print "\n-----------DEBUG----------"
                    print "Coordonnée centre sur image : " + str(
                        centreCercle[0]) + " " + str(centreCercle[1])
                    print "Coordonnées yeux dans référentiel 3D : "
                    print "ex = " + str(ex) + " ey = " + str(
                        ey) + " ez = " + str(ez)
                    oldTimeDebug = newTime

    #debug tracking visage
    if showCamera == 1:
        cv.ShowImage("result", img)
예제 #21
0
    if capture:
        frame_copy = None
        while True:
            frame = cv.QueryFrame(capture)
            if not frame:
                cv.WaitKey(0)
                break
            if not frame_copy:
                frame_copy = cv.CreateImage((frame.width, frame.height),
                                            cv.IPL_DEPTH_8U, frame.nChannels)
            if frame.origin == cv.IPL_ORIGIN_TL:
                cv.Copy(frame, frame_copy)
            else:
                cv.Flip(frame, frame_copy, 0)

            time_point = cv.GetTickCount()
            detect_and_draw(frame_copy, cascade)

            if cv.WaitKey(10) >= 0:
                break
    else:
        image = cv.LoadImage(input_name, 1)
        detect_and_draw(image, cascade)
        cv.WaitKey(0)

    #for i in input_data:
    #print ",".join(["%s" % k for k in i])

    cv.DestroyWindow("result")