title: How to do face recognition in background in python?
body: <h4> Problem I am Facing </h4>
  I am new to python and I want to do face recognition on the footage of webcam. So, I am using below code to achieve the task I want. I got it from youtube. My laptop has intel i7 7th gen, 4 core processor and 8 GB RAM, 4 GB graphics card (AMD R7 M445). When I start streaming I only get  20 fps even if my camera is capable of transmitting 30fps. On top of that when there is a person in frame, stream gets extremely unusable and it gets lower fps like 0.5 and even 0.2 fps. 
  <h4> My theory </h4>
  So, I am guessing that this is a linear and pretty basic program. So, all the tasks including streaming and face recognition is being done on the same thread. That is why fps is getting delayed.
  <h4>Solution I want </h4>
  <p>So, I want to create a structure where the face recognition will be done on a background thread and streaming will be displayed by on going foreground thread.</p>
  <p>I still don't understand why my pc is displaying streaming at 20fps and not in 30fps.</p>
  <p><strong>Updated Code (Added perf_counter and added user defined functions)</strong></p>
  <p>Please refer to below link for old code.</p>
  <p><a href="https://drive.google.com/file/d/1qkpIwkVNddMLx_tB-hJDMi8IKKd2QNeU/view?usp=sharing" rel="nofollow noreferrer">Old py code</a></p>
  <pre><code>import cv2
  from simple_facerec import SimpleFacerec
  from datetime import datetime
  
  from time import perf_counter
  
  
  prev = 0
  count = 0
  vid_cod = &quot;&quot;
  cap = &quot;&quot;
  path =&quot;&quot;
  vid_name = &quot;&quot;
  output = &quot;&quot;
  sfr = &quot;&quot;
  
  
  def vidCapture():
      global cap,prev,count,sfr
  
      while (cap.isOpened()):
          ret, frame = cap.read()
          
          # Detect Faces
          startDetect = perf_counter()
          face_locations, face_names = sfr.detect_known_faces(frame)
          for face_loc, name in zip(face_locations, face_names):
              y1, x2, y2, x1 = face_loc[0], face_loc[1], face_loc[2], face_loc[3]
  
              #if the face is recognised then 
              if name != &quot;Unknown&quot; :
                  print(&quot;known Person detected :) =&gt; &quot; + name)
                  stopDetect = perf_counter()
                  print(f&quot;Time taken to identify face : {stopDetect-startDetect}&quot;)
  
  
              #else if the face is unknown then 
              else :
                  stopDetect = perf_counter()
                  print(f&quot;Time taken to identify face : {stopDetect-startDetect}&quot;)
                  print(&quot;Unknown Person detected :( =&gt; Frame captured...&quot;)
  
          #show the frames on the display
          cv2.imshow(&quot;Frame&quot;, frame)
  
          #write frames to output object so it can save the video
          output.write(frame)
  
  
          #to terminate the process press &quot;x&quot;
          if cv2.waitKey(1) &amp; 0XFF == ord('x'):
              break
  
      cap.release()
      output.release()
      cv2.destroyAllWindows()
  
  
  def __init__():
  
      global prev,count,vid_cod,vid_name,cap,path,output,sfr
      startSfr = perf_counter()
      # Encode faces from a folder
      sfr = SimpleFacerec()
      sfr.load_encoding_images(&quot;images/&quot;)
      stopSfr = perf_counter()
  
      loadSfr = stopSfr-startSfr
      print(f&quot;Time taken to load sfr : {loadSfr}&quot;)
  
      startCam = perf_counter()
      # Load Camera
      cap = cv2.VideoCapture('https://192.xxx.xxx.xxx:8080/videofeed')
      #cap = cv2.VideoCapture(0)
      stopCam = perf_counter()
  
      loadCam = stopCam-startCam
  
      print(f&quot;Time taken to load cam : {loadCam}&quot;)
  
      startCodec = perf_counter()
      #Use MPEG video codec
      vid_cod = cv2.VideoWriter_fourcc(*'MPEG')
      stopCodec = perf_counter()
  
      loadCodec = stopCodec-startCodec
      print(f&quot;Time taken to load codec : {loadCodec}&quot;)
  
  
      startInit = perf_counter()
      prev = 0
      count = 0
      #defining the path where video will be saved
      path = 'C:/Users/Username/Documents/Recorded/'
  
      #Encode video name with date and time
      vid_name = str(&quot;recording_&quot;+datetime.now().strftime(&quot;%b-%d-%Y_%H:%M&quot;).replace(&quot;:&quot;,&quot;_&quot;)+&quot;.avi&quot;)
  
      #initialize video saving process as a &quot;output&quot; object
      output = cv2.VideoWriter( str(path+vid_name) , vid_cod, 20.0 ,(640,480))
      stopInit = perf_counter()
  
      loadInit = stopInit - startInit
      print(f&quot;Time taken to load rest : {loadInit}&quot;)
      vidCapture()
  
  __init__()
  
  </code></pre>
  <p>This below file is simplefacerec which is imported in the above code.</p>
  <p><a href="https://drive.google.com/file/d/104CiwSee2zQfkXYkffkkk3c_kSDyfgAt/view?usp=sharing" rel="nofollow noreferrer">simple_facerec.py</a></p>
  <p><strong>Note:</strong></p>
  <p>When I change fps in <code>output</code> from 20 to 25, saved video length gets decreased and when I change fps from 20 to 15 then video length gets increased. This is how I got the streaming fps of a video.</p>
  <h4> My Logs </h4>
  <p>After putting <code>perf_counter()</code> I found that face recognition alone is taking about 0.5 seconds. This whole code ran for about 7 to 8 seconds in which only 5 frames were displayed.</p>
  <pre><code>Time taken to load sfr : 2.3252885000001697
  Time taken to load cam : 0.3193597999998019
  Time taken to load codec : 1.3199998647905886e-05
  Time taken to load rest : 0.0018557999974291306
  known Person detected :) =&gt; Anirudhdhsinh Jadeja
  Time taken to identify face : 0.4715902999996615
  known Person detected :) =&gt; Anirudhdhsinh Jadeja
  Time taken to identify face : 0.5326913999997487
  known Person detected :) =&gt; Anirudhdhsinh Jadeja
  Time taken to identify face : 0.4969602000019222
  known Person detected :) =&gt; Anirudhdhsinh Jadeja
  Time taken to identify face : 0.4868558000016492
  known Person detected :) =&gt; Anirudhdhsinh Jadeja
  Time taken to identify face : 0.4679383000002417
  </code></pre>