title: Real-time face recognition: PermissionError: [Errno 13] Permission denied:
body: <p>I am trying to work out on real-time face recognition using OpenCV and Python, but in training the dataset/training images, I got a permission error. I don't know what is the problem in the code. I had finished detecting the face and gathering data but when running the below code, I received this error.</p>
  <p>Here is my code:</p>
  <pre><code>path = 'C:\MyComputer\Data Science\Works\FaceRecognition\Dataset'
  
  recognizer = cv2.face.LBPHFaceRecognizer_create()
  detector = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml');
  
  def getImagesAndLabels(path):
  
      imagePaths = [os.path.join(path,f) for f in os.listdir(path)]  
      faceSamples=[]
      ids = []
  
      for imagePath in imagePaths:
  
          PIL_img = Image.open(imagePath).convert('L')
          
          img_numpy = np.array(PIL_img,'uint8')
  
          id = int(os.path.split(imagePath)[-1].split(&quot;.&quot;)[1])
          faces = detector.detectMultiScale(img_numpy)
  
          for (x,y,w,h) in faces:
              faceSamples.append(img_numpy[y:y+h,x:x+w])
              ids.append(id)
  
      return faceSamples,ids
  
  print (&quot;\n [INFO] Training faces. It will take a few seconds. Wait ...&quot;)
  faces,ids = getImagesAndLabels(path)
  recognizer.train(faces, np.array(ids))
  
  recognizer.write('trainer/trainer.yml') 
  
  print(&quot;\n [INFO] {0} faces trained. Exiting Program&quot;.format(len(np.unique(ids))))
  </code></pre>
  <p>I wrote this code by referring to <a href="https://www.instructables.com/id/Real-time-Face-Recognition-an-End-to-end-Project/#:%7E:text=OpenCV%20was%20designed%20for%20computational,Face%20Detection%20and%20Data%20Gathering" rel="nofollow noreferrer">this tutorial</a></p>
  <p>The error is:</p>
  <pre><code>PIL_img = Image.open(imagePath).convert('L')
  
    File &quot;C:\Users\nadha\anaconda3\lib\site-packages\PIL\Image.py&quot;, line 2809, in open
    fp = builtins.open(filename, &quot;rb&quot;)
  
    PermissionError: [Errno 13] Permission denied: 'C:\\MyComputer\\DataScience\\Works\\FaceRecognition\\Dataset\\TrainedData'
  </code></pre>