示例#1
0
def track(i=0):
   cap = cv2.VideoCapture(i)
   cap.set(cv2.cv.CV_CAP_PROP_FPS, 10)
   kernel = np.ones((5,5),np.uint8)
   out = False
   while not out:
       a, img = cap.read()

       # ======================================================
       #        Detecta las caras de la imagen y devuelve
       #    las esquinas  de los rectangulos q las contienen
       # ======================================================
       puntos = dtct.face(img)

       try:
          # ================================================================
          #    Set the coor properly so the faces are correctly extracted
          # ================================================================
          faces = []
          for pto in puntos:
             P1 = pto[0:2]
             P2 = pto[2:]
             f = img[P1[1]:P2[1], P1[0]:P2[0]]
             faces.append(f)

          # ==========================================================
          #    Rescale the faces and show them in upper left corner
          # ==========================================================
          cont = 0
          for face in faces:
             n = 70
             face = cv2.resize(face,(n, n), interpolation = cv2.INTER_AREA)
             img[0:n ,n*cont:n*(cont+1)] = face
             cont += 1

       except IndexError:
          pass # if there are no faces

       # ==================================
       #    Draws a box around each face
       # ==================================
       dtct.box(puntos, img)


       # ===============================
       #    Shows the processed image
       # ===============================
       cv2.imshow('face',img)
       k = cv2.waitKey(5) & 0xFF  #   Waits for  <Esc> key
       if k == 27:                # to exit the infinite loop
         out = True               #
#         break                   #
   cv2.destroyAllWindows()
def track(i=0):
   cap = cv2.VideoCapture(i)
   cap.set(cv2.cv.CV_CAP_PROP_FPS, 10)
   kernel = np.ones((5,5),np.uint8)
   
   plt.ion()
   plt.show()
   fig = plt.figure()
   ax = fig.add_subplot(111)

   out = False
   Npersonas=0
   while not out:
       a, img = cap.read()

       # ======================================================
       #        Detecta las caras de la imagen y devuelve
       #    las esquinas  de los rectangulos q las contienen
       # ======================================================
       puntos = dtct.face(img)

       try:
          # ==================================
          #    get the centers of the faces
          # ==================================
          centros = dtct.center(puntos,img)
       except IndexError:
          pass # if there are no faces

       # ==================================
       #    Draws a box around each face
       # ==================================
       dtct.box(puntos, img)

       # ==================================
       #    Draws a box around each face
       # ==================================
       dtct.box(puntos, img)


       # ===============================
       #    Shows the processed image
       # ===============================
       cv2.imshow('face',img)
       k = cv2.waitKey(5) & 0xFF  #   Waits for  <Esc> key
       if k == 27:                # to exit the infinite loop
         out = True               #
#         break                   #

       # =====================================================
       #    Prepares the data to be plotted with matplotlib
       # =====================================================
       X, Y = [], []
       for centro in centros:
          X.append(centro[0])
          Y.append(centro[1])
       ax.plot(X,Y,'ro',markersize=10)
       ax.axis([0, img.shape[0], 0, img.shape[1]])
       fig.canvas.draw()
       ax.cla()
       if len(centros) > Npersonas:
         Npersonas = len(centros)

   cv2.destroyAllWindows()
示例#3
0
arduino.write(pos_x)
sleep(1)
print('done')

flip = False
out = False
pos_x = '90'
pos_y = '90'
prev_angle = 90
while not out:
    a, img = cap.read()
    if flip: img = cv2.flip(img, 0)
    vid_y, vid_x, _ = img.shape

    # Look for faces
    centers, corners = dtct.face(img)

    # Draw a box around each face
    dtct.box(corners, img)

    if len(centers) > 0:
        # Find centroid of all the faces (weighted by the size of the face)
        M = np.array(centers)
        X = M[:, 0]
        Y = M[:, 1]
        A = M[:, 2]
        x0 = np.average(X, weights=A)  # position in pixels
        y0 = np.average(Y, weights=A)  #
        x0_deg = 180 - (170 * (x0 / vid_x) + 10)

        x0_deg = (0.5 - (x0 / vid_x)) * 2  # \in [-1,1]