title: Real Time Face Recognition with LBP, Deep Learning and OpenCV
body: <p>I am a novice in Computer Vision. I am trying to implement Real Time Face Recognition with Local Binary Patterns with its Face Detection part based on Deep Learning dnn module. I am using the caltech_faces dataset and have added a folder with my 20 photos to it.</p>
  <p>So, this is my code. I basically transformed the code of the Face Recognition of sample images to a Real Time Face Recognition by making some changes and additions.</p>
  <p>I get the following error when executing the below code:</p>
  <pre><code>predName = le.inverse_transform([predictions[i]])[0]
                                                         ^
  TabError: inconsistent use of tabs and spaces in indentation
  </code></pre>
  <p>I checked all the tabs and indentations, and cant find what and where to fix. I kindly ask you to give me a hint on what to do. Thank you very much!</p>
  <pre><code># import the necessary packages
  
  from sklearn.preprocessing import LabelEncoder
  from sklearn.model_selection import train_test_split
  from sklearn.metrics import classification_report
  from imutils.video import VideoStream
  from imutils import paths
  import matplotlib.pyplot as plt
  import numpy as np
  import argparse
  import imutils
  import time
  import cv2
  import os
  
  
  #Creating our face detector
  
  def detect_faces(net, frame, minConfidence=0.5):
      # grab the dimensions of the image and then construct a blob
      # from it
      (h, w) = frame.shape[:2]
      blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300),
          (104.0, 177.0, 123.0))
  
      # pass the blob through the network to obtain the face detections,
      # then initialize a list to store the predicted bounding boxes
      net.setInput(blob)
      detections = net.forward()
      boxes = []
  
      # loop over the detections
      for i in range(0, detections.shape[2]):
          # extract the confidence (i.e., probability) associated with
          # the detection
          confidence = detections[0, 0, i, 2]
  
          # filter out weak detections by ensuring the confidence is
          # greater than the minimum confidence
          if confidence &gt; minConfidence:
              # compute the (x, y)-coordinates of the bounding box for
              # the object
              box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
              (startX, startY, endX, endY) = box.astype(&quot;int&quot;)
  
              # update our bounding box results list
              boxes.append((startX, startY, endX, endY))
  
      # return the face detection bounding boxes
      return boxes
  
  
  #Loading the CALTECH Faces dataset
  
  def load_face_dataset(inputPath, net, minConfidence=0.5,
      minSamples=15):
      # grab the paths to all images in our input directory, extract
      # the name of the person (i.e., class label) from the directory
      # structure, and count the number of example images we have per
      # face
      imagePaths = list(paths.list_images(inputPath))
      names = [p.split(os.path.sep)[-2] for p in imagePaths]
      (names, counts) = np.unique(names, return_counts=True)
      names = names.tolist()
  
      # initialize lists to store our extracted faces and associated
      # labels
      faces = []
      labels = []
  
      # loop over the image paths
      for imagePath in imagePaths:
          # load the image from disk and extract the name of the person
          # from the subdirectory structure
          frame = cv2.imread(imagePath)
          name = imagePath.split(os.path.sep)[-2]
  
          # only process images that have a sufficient number of
          # examples belonging to the class
          if counts[names.index(name)] &lt; minSamples:
              continue
  
          # perform face detection
          boxes = detect_faces(net, frame, minConfidence)
  
          # loop over the bounding boxes
          for (startX, startY, endX, endY) in boxes:
              # extract the face ROI, resize it, and convert it to
              # grayscale
              faceROI = frame[startY:endY, startX:endX]
              faceROI = cv2.resize(faceROI, (47, 62))
              faceROI = cv2.cvtColor(faceROI, cv2.COLOR_BGR2GRAY)
  
              # update our faces and labels lists
              faces.append(faceROI)
              labels.append(name)
  
      # convert our faces and labels lists to NumPy arrays
      faces = np.array(faces)
      labels = np.array(labels)
  
      # return a 2-tuple of the faces and labels
      return (faces, labels)
  
  #Implementing Local Binary Patterns for face recognition    
  
  # # construct the argument parser and parse the arguments
  # ap = argparse.ArgumentParser()
  # ap.add_argument(&quot;-i&quot;, &quot;--input&quot;, type=str, required=True,
  #   help=&quot;path to input directory of images&quot;)
  # ap.add_argument(&quot;-f&quot;, &quot;--face&quot;, type=str,
  #   default=&quot;face_detector&quot;,
  #   help=&quot;path to face detector model directory&quot;)
  # ap.add_argument(&quot;-c&quot;, &quot;--confidence&quot;, type=float, default=0.5,
  #   help=&quot;minimum probability to filter weak detections&quot;)
  # args = vars(ap.parse_args())
  
  # since we are using Jupyter Notebooks we can replace our argument
  # parsing code with *hard coded* arguments and values
  args = {
      &quot;input&quot;: &quot;caltech_faces&quot;,
      &quot;face&quot;: &quot;face_detector&quot;,
      &quot;confidence&quot;: 0.5,
  }
  
  # load our serialized face detector model from disk
  print(&quot;[INFO] loading face detector model...&quot;)
  prototxtPath = os.path.sep.join([args[&quot;face&quot;], &quot;deploy.prototxt&quot;])
  weightsPath = os.path.sep.join([args[&quot;face&quot;],
      &quot;res10_300x300_ssd_iter_140000.caffemodel&quot;])
  net = cv2.dnn.readNet(prototxtPath, weightsPath)
  
  # load the CALTECH faces dataset
  print(&quot;[INFO] loading dataset...&quot;)
  (faces, labels) = load_face_dataset(args[&quot;input&quot;], net,
      minConfidence=0.5, minSamples=20)
  print(&quot;[INFO] {} images in dataset&quot;.format(len(faces)))
  
  # encode the string labels as integers
  le = LabelEncoder()
  labels = le.fit_transform(labels)
  
  # construct our training and testing split
  (trainX, testX, trainY, testY) = train_test_split(faces,
      labels, test_size=0.25, stratify=labels, random_state=42)
  
  # train our LBP face recognizer
  print(&quot;[INFO] training face recognizer...&quot;)
  recognizer = cv2.face.LBPHFaceRecognizer_create(
      radius=2, neighbors=16, grid_x=8, grid_y=8)
  start = time.time()
  recognizer.train(trainX, trainY)
  end = time.time()
  print(&quot;[INFO] training took {:.4f} seconds&quot;.format(end - start))
  
  
  # initialize the list of predictions and confidence scores
  print(&quot;[INFO] gathering predictions...&quot;)
  predictions = []
  confidence = []
  start = time.time()
  
  # loop over the test data
  for i in range(0, len(testX)):
      # classify the face and update the list of predictions and
      # confidence scores
      (prediction, conf) = recognizer.predict(testX[i])
      predictions.append(prediction)
      confidence.append(conf)
  
  # measure how long making predictions took
  end = time.time()
  print(&quot;[INFO] inference took {:.4f} seconds&quot;.format(end - start))
  
  # show the classification report
  print(classification_report(testY, predictions,
      target_names=le.classes_))
  
  
  # initialize the video stream and allow the cammera sensor to warmup
  print(&quot;[INFO] starting video stream...&quot;)
  vs = VideoStream(src=0).start()
  time.sleep(2.0)
  
  # loop over the frames from the video stream
  while True:
  
      # grab the frame from the threaded video stream and resize it
      # to have a maximum width of 400 pixels
      face = vs.read()
      face = imutils.resize(face, width=400)
  
      # loop over the detections
      for i in range(0, detections.shape[2]):
  
          # grab the predicted name and actual name
      predName = le.inverse_transform([predictions[i]])[0]
      actualName = le.classes_[testY[i]]
  
  
      # draw the predicted name and actual name on the image
      cv2.putText(face, &quot;pred: {}&quot;.format(predName), (5, 25),
      cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
      cv2.putText(face, &quot;actual: {}&quot;.format(actualName), (5, 60),
      cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
  
      # display the predicted name, actual name, and confidence of the
      # prediction (i.e., chi-squared distance; the *lower* the distance
      # is the *more confident* the prediction is)
      print(&quot;[INFO] prediction: {}, actual: {}, confidence: {:.2f}&quot;.format(predName, actualName, confidence[i]))
  
  # show the output frame
  cv2.imshow(&quot;Face&quot;, face)
  key = cv2.waitKey(1) &amp; 0xFF
   
  # if the `q` key was pressed, break from the loop
  if key == ord(&quot;q&quot;):
      break
  </code></pre>