Menu

Facial Recognition using OpenCV and DLib-Improving the frame rate

Help
Nikhil B
2023-11-16
2023-11-19
  • Nikhil B

    Nikhil B - 2023-11-16

    The code below only runs at 1-2FPS. Need help making the necessary changes to improve the frame rate to about 10fps. The code is implemented on the jetson nano.

    import face_recognition
    import cv2
    import os
    import dlib
    import pickle
    import time
    print(cv2.__version__)
    
    fpsReport=0
    scaleFactor=.25
    
    Encodings=[]
    Names=[]
    
    with open('train.pkl','rb') as f:
        Names=pickle.load(f)
        Encodings=pickle.load(f)
    
    font=cv2.FONT_HERSHEY_SIMPLEX
    cam=cv2.VideoCapture(0)   
    
    timeStamp=time.time()
    while True:
        _,frame=cam.read()
        frameSmall=cv2.resize(frame,(0,0),fx=scaleFactor,fy=scaleFactor)
        frameRGB=cv2.cvtColor(frameSmall,cv2.COLOR_BGR2RGB)
        facePositions=face_recognition.face_locations(frameRGB,model='CNN')
        allEncodings=face_recognition.face_encodings(frameRGB,facePositions)
        for (top,right,bottom,left),face_encoding in zip(facePositions,allEncodings):
            name='Unknown Person'
            matches=face_recognition.compare_faces(Encodings,face_encoding)
            if True in matches:
                first_match_index=matches.index(True)
                name=Names[first_match_index]
            top=int(top/scaleFactor)
            right=int(right/scaleFactor)
            bottom=int(bottom/scaleFactor)
            left=int(left/scaleFactor)
            cv2.rectangle(frame,(left,top),(right,bottom),(0,0,255),2)
            cv2.putText(frame,name,(left,top-6),font, .75, (0,0,255),2) 
        dt=time.time()-timeStamp
        fps=1/dt
        fpsReport=0.90*fps + 0.1*fps
        #print('fps is: ',round(fpsReport,2))
        timeStamp=time.time()
        cv2.rectangle(frame,(0,0),(100,40),(0,0,255),-1)
        cv2.putText(frame,str(round(fpsReport,1)) + 'fps',(0,25), font, .75, (0,255,255,2))
        cv2.imshow('Picture',frame)
        cv2.moveWindow('Picture',0,0)
        if cv2.waitKey(1)==ord('q'):
            break
    
    cam.release()        
    cv2.destroyAllWindows()
    

    Kindly please help.

     
    • Davis

      Davis - 2023-11-19

      Aside from making sure compiler optimizations are on, use smaller images.
      See also http://dlib.net/faq.html#Whyisdlibslow

      On Thu, Nov 16, 2023 at 6:33 AM Nikhil B bwajster@users.sourceforge.net
      wrote:

      The code below only runs at 1-2FPS. Need help making the necessary changes
      to improve the frame rate to about 10fps. The code is implemented on the
      jetson nano.

      ~~~
      import face_recognition
      import cv2
      import os
      import dlib
      import pickle
      import time
      print(cv2.version)

      fpsReport=0
      scaleFactor=.25

      Encodings=[]
      Names=[]

      with open('train.pkl','rb') as f:
      Names=pickle.load(f)
      Encodings=pickle.load(f)

      font=cv2.FONT_HERSHEY_SIMPLEX
      cam=cv2.VideoCapture(0)

      timeStamp=time.time()
      while True:
      _,frame=cam.read()
      frameSmall=cv2.resize(frame,(0,0),fx=scaleFactor,fy=scaleFactor)
      frameRGB=cv2.cvtColor(frameSmall,cv2.COLOR_BGR2RGB)
      facePositions=face_recognition.face_locations(frameRGB,model='CNN')
      allEncodings=face_recognition.face_encodings(frameRGB,facePositions)
      for (top,right,bottom,left),face_encoding in
      zip(facePositions,allEncodings):
      name='Unknown Person'
      matches=face_recognition.compare_faces(Encodings,face_encoding)
      if True in matches:
      first_match_index=matches.index(True)
      name=Names[first_match_index]
      top=int(top/scaleFactor)
      right=int(right/scaleFactor)
      bottom=int(bottom/scaleFactor)
      left=int(left/scaleFactor)
      cv2.rectangle(frame,(left,top),(right,bottom),(0,0,255),2)
      cv2.putText(frame,name,(left,top-6),font, .75, (0,0,255),2)
      dt=time.time()-timeStamp
      fps=1/dt
      fpsReport=0.90fps + 0.1fps
      #print('fps is: ',round(fpsReport,2))
      timeStamp=time.time()
      cv2.rectangle(frame,(0,0),(100,40),(0,0,255),-1)
      cv2.putText(frame,str(round(fpsReport,1)) + 'fps',(0,25), font, .75,
      (0,255,255,2))
      cv2.imshow('Picture',frame)
      cv2.moveWindow('Picture',0,0)
      if cv2.waitKey(1)==ord('q'):
      break

      cam.release()
      cv2.destroyAllWindows()

      ~~~
      Kindly please help.


      Facial Recognition using OpenCV and DLib-Improving the frame rate


      Sent from sourceforge.net because you indicated interest in <
      https://sourceforge.net/p/dclib/discussion/442518/>

      To unsubscribe from further messages, please visit <
      https://sourceforge.net/auth/subscriptions/>

       

Log in to post a comment.