text: # -*- coding: utf-8 -*-
  """Running the Smile CNN in Real-time.
  
  Access our webcam/video file and apply smile detection to each frame.
  
  Example:
      $ python detect_smile.py --cascade haarcascade_frontalface_default.xml --model output/lenet.hdf5
  
  Attributes:
      cascade (str):
          Path to a Haar cascade used to detect faces in images
      model (str):
          Path to our serialized LeNet weights on disk
      video (str, optional):
          Path to the (optional) video file. {Default is to read frames from a built-in/USB webcam}
  """
  import argparse
  import cv2
  import numpy as np
  import imutils
  from keras.preprocessing.image import img_to_array
  from keras.models import load_model
  
  
  def main():
      """Run the real-time face detection.
      """
      # construct the argument parse and parse the arguments
      args = argparse.ArgumentParser()
      args.add_argument("-c", "--cascade", required=True, help="path to where the face cascade resides")
      args.add_argument("-m", "--model", required=True, help="path to pre-trained smile detector CNN")
      args.add_argument("-v", "--video", help="path to the (optional) video file")
      args = vars(args.parse_args())
  
      # load the face detector cascade and smile detector CNN
      detector = cv2.CascadeClassifier(args["cascade"])
      model = load_model(args["model"])
      # if a video path was not supplied, grab the reference to the webcam
      if not args.get("video", False):
          camera = cv2.VideoCapture(0)
      # otherwise, load the video file from the disk
      else:
          camera = cv2.VideoCapture(args["video"])
  
      # keep looping
      while True:
          # grab the current frame
          (grabbed, frame) = camera.read()
          # if we are viewing a video and we did not grab a frame, then we
          # have reached the end of the video
          if args.get("video") and not grabbed:
              break
          # resize the frame, convert it to grayscale, and then clone the
          # original frame so we can draw on it later in the program
          frame = imutils.resize(frame, width=300)
          gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
          frame_clone = frame.copy()
          # detect faces in the input frame, then clone the frame so that we can draw on it
          rects = detector.detectMultiScale(
              gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE
          )
          # loop over the face bounding boxes
          for (x, y, width, height) in rects:
              # extract the ROI of the face from the grayscale image, resize it to a fixed 28x28
              # pixels, and then prepare the ROI for classification via the CNN
              roi = gray[y : y + height, x : x + width]
              roi = cv2.resize(roi, (28, 28))
              roi = roi.astype("float") / 255.0
              roi = img_to_array(roi)
              roi = np.expand_dims(roi, axis=0)
              # determine the probabilities of both "smiling" and "not
              # smiling", then set the label accordingly
              (not_smiling, smiling) = model.predict(roi)[0]
              label = "Smiling" if smiling > not_smiling else "Not Smiling"
              # display the label and bounding box rectangle on the output
              # frame
              cv2.putText(frame_clone, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
              cv2.rectangle(frame_clone, (x, y), (x + width, y + height), (0, 0, 255), 2)
          # show our detected faces along with smiling/not smiling labels
          cv2.imshow("Face", frame_clone)
          # if the 'q' key is pressed, stop the loop
          if cv2.waitKey(1) & 0xFF == ord("q"):
              break
  
      # cleanup the camera and close any open windows
      camera.release()
      cv2.destroyAllWindows()
  
  
  if __name__ == "__main__":
      main()
  