libfidl.py
def facial_recogntion(props, smartLock):
    """
        Processing camera pictures with multiple AI's and executing background jobs.
        This is the main loop for the Door Lock.
        AI stages: Movement detection -> Face detection -> Face classification

        :param props: Necessery predefined properties 
        :type props: Dictonary
        :param smartLock: object used to access unlocking functions and door status
        :type smartLock: SmartLock object
    """
    cap = cv2.VideoCapture(0)

    #get labels
    with open(props['classification']['labels']) as f:
        labels_map = json.load(f)

    #loading detection model
    detection = DetectionEngine(props['detection'])
    classification = ClassificationEngine(props['classification']['path'])
    while cap.isOpened():
        ret, cv2_im = cap.read()
        if not ret:
            break
        # flip picture
        cv2_im = cv2.flip(cv2_im,-1)
        # Skip classification if door is open
        if not smartLock.is_door_closed():
            #shuffle all the pixels and make the picture unrecoginzable
            cv2_im = cv2.randShuffle(cv2_im)
            #TODO Indicate that door is open
            cv2.imshow('FIDL', cv2_im)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
            continue

        pil_im = Image.fromarray(cv2_im)
        # TODO check first if the there is movement in the picture before utilizing edge tpu (power saving)
        #searching for faces in the picture 
        faces = detection.detect_with_image(pil_im,threshold = 0.5, top_k =3,keep_aspect_ratio=True,relative_coord=True)
        #check each face and append results to frame
        height, width, _= cv2_im.shape
        for face in faces:
            x0, y0, x1, y1 = face.bounding_box.flatten().tolist()
            x0, y0, x1, y1 = int(x0*width), int(y0*height), int(x1*width), int(y1*height)
            #crop out face from camera picture
            face_im = Image.fromarray(cv2_im[y0:y1,x0:x1])
            #classify face
            results = classification.classify_with_image(face_im,threshold=0.1,top_k=3)   
            #annotate frame
            best_result = (0,0) # index, score
            text_lines = []
            for index, score in results:
                if score > best_result[1]:
                    best_result = (index, score)
                text_lines.append('score=%.2f: %s' % (score, labels_map[str(index)])) #TODO decide to show all scores or only best score
            
            access_granted = props['user'][labels_map[str(best_result[0])]]['access'] and score >= 0.9 
            #open the door
            if access_granted and smartLock.unlocking == False:
                threading.Thread(target = smartLock.unlock, daemon = True).start()
            # TODO indicate unlocking
            #Coloring green if access was granted 
            cv2_im = cv2.rectangle(cv2_im, (x0, y0), (x1, y1), (0, 255, 0) if access_granted else (0,0,255), 2)
            for y, line in enumerate(text_lines):
                cv2.putText(cv2_im,line,(x0 + 10,y0 + y*20+20),fontFace=cv2.FONT_HERSHEY_SIMPLEX,fontScale =0.5, color=(255, 255, 255)) 
        cv2.imshow('FIDL', cv2_im)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

        Processing camera pictures with multiple AI's and executing background jobs.
        This is the main loop for the Door Lock.
        AI stages: Movement detection -> Face detection -> Face classification

        :param props: Necessery predefined properties 
        :type props: Dictonary
        :param smartLock: object used to access unlocking functions and door status
        :type smartLock: SmartLock object
    
Processing camera pictures with multiple AI's and executing background jobs.
This is the main loop for the Door Lock.
AI stages: Movement detection -> Face detection -> Face classification
Processing camera pictures with multiple AI ' s and executing background jobs . This is the main loop for the Door Lock . AI stages : Movement detection - > Face detection - > Face classification
def facial_recogntion(props, smartLock):
    cap = cv2.VideoCapture(0)
    with open(props['classification']['labels']) as f:
        labels_map = json.load(f)
    detection = DetectionEngine(props['detection'])
    classification = ClassificationEngine(props['classification']['path'])
    while cap.isOpened():
        ret, cv2_im = cap.read()
        if not ret:
            break
        cv2_im = cv2.flip(cv2_im,-1)
        if not smartLock.is_door_closed():
            cv2_im = cv2.randShuffle(cv2_im)
            cv2.imshow('FIDL', cv2_im)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
            continue
        pil_im = Image.fromarray(cv2_im)
        faces = detection.detect_with_image(pil_im,threshold = 0.5, top_k =3,keep_aspect_ratio=True,relative_coord=True)
        height, width, _= cv2_im.shape
        for face in faces:
            x0, y0, x1, y1 = face.bounding_box.flatten().tolist()
            x0, y0, x1, y1 = int(x0*width), int(y0*height), int(x1*width), int(y1*height)
            face_im = Image.fromarray(cv2_im[y0:y1,x0:x1])
            results = classification.classify_with_image(face_im,threshold=0.1,top_k=3)   
            best_result = (0,0) 
            text_lines = []
            for index, score in results:
                if score > best_result[1]:
                    best_result = (index, score)
                text_lines.append('score=%.2f: %s' % (score, labels_map[str(index)])) 
            access_granted = props['user'][labels_map[str(best_result[0])]]['access'] and score >= 0.9 
            if access_granted and smartLock.unlocking == False:
                threading.Thread(target = smartLock.unlock, daemon = True).start()
            cv2_im = cv2.rectangle(cv2_im, (x0, y0), (x1, y1), (0, 255, 0) if access_granted else (0,0,255), 2)
            for y, line in enumerate(text_lines):
                cv2.putText(cv2_im,line,(x0 + 10,y0 + y*20+20),fontFace=cv2.FONT_HERSHEY_SIMPLEX,fontScale =0.5, color=(255, 255, 255)) 
        cv2.imshow('FIDL', cv2_im)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()
def facial_recogntion ( props , smartLock ) : cap = cv2 . VideoCapture ( 0 ) with open ( props [ 'classification' ] [ 'labels' ] ) as f : labels_map = json . load ( f ) detection = DetectionEngine ( props [ 'detection' ] ) classification = ClassificationEngine ( props [ 'classification' ] [ 'path' ] ) while cap . isOpened ( ) : ret , cv2_im = cap . read ( ) if not ret : break cv2_im = cv2 . flip ( cv2_im , - 1 ) if not smartLock . is_door_closed ( ) : cv2_im = cv2 . randShuffle ( cv2_im ) cv2 . imshow ( 'FIDL' , cv2_im ) if cv2 . waitKey ( 1 ) & 0xFF == ord ( 'q' ) : break continue pil_im = Image . fromarray ( cv2_im ) faces = detection . detect_with_image ( pil_im , threshold = 0.5 , top_k = 3 , keep_aspect_ratio = True , relative_coord = True ) height , width , _ = cv2_im . shape for face in faces : x0 , y0 , x1 , y1 = face . bounding_box . flatten ( ) . tolist ( ) x0 , y0 , x1 , y1 = int ( x0 * width ) , int ( y0 * height ) , int ( x1 * width ) , int ( y1 * height ) face_im = Image . fromarray ( cv2_im [ y0 : y1 , x0 : x1 ] ) results = classification . classify_with_image ( face_im , threshold = 0.1 , top_k = 3 ) best_result = ( 0 , 0 ) text_lines = [ ] for index , score in results : if score > best_result [ 1 ] : best_result = ( index , score ) text_lines . append ( 'score=%.2f: %s' % ( score , labels_map [ str ( index ) ] ) ) access_granted = props [ 'user' ] [ labels_map [ str ( best_result [ 0 ] ) ] ] [ 'access' ] and score >= 0.9 if access_granted and smartLock . unlocking == False : threading . Thread ( target = smartLock . unlock , daemon = True ) . start ( ) cv2_im = cv2 . rectangle ( cv2_im , ( x0 , y0 ) , ( x1 , y1 ) , ( 0 , 255 , 0 ) if access_granted else ( 0 , 0 , 255 ) , 2 ) for y , line in enumerate ( text_lines ) : cv2 . putText ( cv2_im , line , ( x0 + 10 , y0 + y * 20 + 20 ) , fontFace = cv2 . FONT_HERSHEY_SIMPLEX , fontScale = 0.5 , color = ( 255 , 255 , 255 ) ) cv2 . imshow ( 'FIDL' , cv2_im ) if cv2 . waitKey ( 1 ) & 0xFF == ord ( 'q' ) : break cap . release ( ) cv2 . destroyAllWindows ( )
Processing camera pictures with multiple AI's and executing background jobs.
Processing camera pictures with multiple AI ' s and executing background jobs .
"""
        Processing camera pictures with multiple AI's and executing background jobs.
        This is the main loop for the Door Lock.
        AI stages: Movement detection -> Face detection -> Face classification

        :param props: Necessery predefined properties 
        :type props: Dictonary
        :param smartLock: object used to access unlocking functions and door status
        :type smartLock: SmartLock object
    """ #get labels #loading detection model # flip picture # Skip classification if door is open #shuffle all the pixels and make the picture unrecoginzable #TODO Indicate that door is open # TODO check first if the there is movement in the picture before utilizing edge tpu (power saving) #searching for faces in the picture  #check each face and append results to frame #crop out face from camera picture #classify face #annotate frame # index, score #TODO decide to show all scores or only best score #open the door # TODO indicate unlocking #Coloring green if access was granted 
props
Necessery predefined properties
Necessery predefined properties
smartLock
object used to access unlocking functions and door status
object used to access unlocking functions and door status