title: NameError: name 'capture' is not defined
body: <p>I am working on some code for Face-Recognition. I am trying to combine the csv files that I have into one file that the program will read and once it has an accuracy level of at least 0.8 or higher it will scan my face and determine my expression. When running the program it keeps complaining about the variable &quot;capture&quot;. I have tried to change the name but to no avail. I'm not sure what the exact problem is can someone help me in the right direction?</p>
  <pre><code>import os
  import pandas as pd
  from os import path
  from sklearn.model_selection import train_test_split
  from sklearn.preprocessing import StandardScaler
  from sklearn.ensemble import RandomForestClassifier
  from sklearn.metrics import accuracy_score
  import variable_record
  import cv2
  import face_recognition
  import numpy
  
  os.chdir(&quot;D:\Ezequiel Soler\csv_files/&quot;)
  
  
  def file_combination():
      extension = 'csv'
      all_filenames = [i for i in glob.glob('*.{}'.format(extension))]
      combined_csv = pd.concat([pd.read_csv(f) for f in all_filenames])
      combined_csv.to_csv(&quot;combined_csv.csv&quot;, index=False, encoding='utf-8')
      print(&quot;Combination is finished&quot;)
  
  
  def create_file():
      print(&quot;Prepare for csv files combination...&quot;)
  
      # check whether the combined file is existed
      is_existed = path.exists(&quot;combined_csv.csv&quot;)
  
      # ask user whether he/she want to renew the file if the file is already existed
      if is_existed:
          renew = input(&quot;The file is already existed, do you renew the file? (Y/N) &quot;)
          if renew.upper() == 'Y':
              os.remove(&quot;combined_csv.csv&quot;)
              file_combination()
          else:
              print(&quot;Program is ended.&quot;)
              exit(0)
      else:
          file_combination()
      print(&quot;--------------------------------------------&quot;)
  
  
  def main():
      create_file()
  
      # import dataset
      dataSet = pd.read_csv(&quot;combined_csv.csv&quot;)
  
      # preparing data for training
      X = dataSet.iloc[:, 0:7].values
      y = dataSet.iloc[:, 7].values
  
      # divide data into training data and test data
      X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
  
      # scaling features
      sc = StandardScaler()
      X_train = sc.fit_transform(X_train)
      X_test = sc.transform(X_test)
  
      # train the data
      classifier = RandomForestClassifier(n_estimators=50)
      classifier.fit(X_train, y_train)
      y_pred = classifier.predict(X_test)
      print(&quot;Accuracy: &quot;, accuracy_score(y_test, y_pred))
  
      # turn on the webcam and check the status
      capture = cv2.VideoCapture(0)
      if capture.isOpened() is False:
          print(&quot;Camera Error, please check your camera @_@&quot;)
          exit()
  
      # initial values
  if __name__ == &quot;__main__&quot;:
      main()
  
      while True:
          # change the BGR frame to gray frame
          ret, frame = capture.read()
          gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  
          # use face_recognition library to locate the landmarks
          face_marks = face_recognition.face_landmarks(gray, None, &quot;large&quot;)
  
          if face_marks.__len__() != 0:
              # calculate EAR, MAR, PUC, and MOE
              L_EAR = variable_record.cal_EAR(face_marks[0][&quot;left_eye&quot;])
              R_EAR = variable_record.cal_EAR(face_marks[0][&quot;right_eye&quot;])
              MAR = variable_record.cal_MAR(face_marks[0][&quot;top_lip&quot;], face_marks[0][&quot;bottom_lip&quot;])
              PUC = variable_record.cal_PUC(face_marks[0][&quot;left_eye&quot;])
              EBA = variable_record.cal_EBA(face_marks[0][&quot;right_eyebrow&quot;])
              CAR = variable_record.cal_CAR(face_marks[0][&quot;chin&quot;])
              MOE = MAR / L_EAR
  
              predict_data = numpy.array([[L_EAR, R_EAR, MAR, PUC, MOE, EBA, CAR]])
              predict_data = sc.transform(predict_data)
              expression = classifier.predict(predict_data)
  
              # transfer expression value to english
              if expression == 1:
                  expression = &quot;Neutral&quot;
              elif expression == 2:
                  expression = &quot;Happiness&quot;
              elif expression == 3:
                  expression = &quot;Sadness&quot;
              elif expression == 4:
                  expression = &quot;Fear&quot;
              elif expression == 5:
                  expression = &quot;Angry&quot;
              elif expression == 6:
                  expression = &quot;Surprise&quot;
              else:
                  expression = &quot;Other&quot;
              print(expression)
  
              # rectangle the face
              face_point = face_recognition.face_locations(gray)
              for pts in face_point:
                  cv2.rectangle(frame, (pts[3], pts[0]), (pts[1], pts[2]), (0, 255, 0), 2)
  
                  # show the result on the frame
                  cv2.putText(frame, expression, (pts[3], pts[0]), cv2.FONT_HERSHEY_DUPLEX, 1.0, (0, 255, 0), 1)
  
          # press q to exit the loop
          if cv2.waitKey(1) &amp; 0xff == ord('q'):
              capture.release()
              cv2.destroyAllWindows()
              break
  
          # display the frame
          cv2.imshow(&quot;Expression Prediction&quot;, frame)
  
      # release the memory
      capture.release()
      cv2.destroyAllWindows()
      exit(0)
  </code></pre>