MOHAN KRISHNA

0 %
Mohan Krishna
Multimedia Professional
Ai & ML Researcher & Enthusiast
  • Residence:
    India
  • City:
    Vijayawada
  • Age:
    46
AI/ML Enthusiast. New Media Trainer, VFX Artist, Non Linear Video Editor, Graphic Designer, Sound Editor and iOS App Designer.
Telugu
English
Hindi
Tamil
Proficiency:
Graphic Design
Web Design
Video & VFX
Machine Learning
Artificial Intelligence
Digital Marketing
Areas of Interest:
Take a look at some of the things I love working on.
  • Non Linear Video Editing
  • Graphic Design
  • Web Design
  • Audio Editing
  • Content Management Systems
  • Python
  • Deep Learning
  • OpenCV
  • Image Classification

Face Recognition Door Lock

September 26, 2022

Recognition:

from picamera import PiCamera
import time
import boto3
# http://www.pillalamarri.in/python/face-recognition-door-lock/
directory = '' #folder name on your raspberry pi

P=PiCamera()
P.resolution= (800,600)
P.start_preview()
collectionId='' #collection name

rek_client=boto3.client('rekognition',
                        aws_access_key_id='',# add the aws access key
                        aws_secret_access_key='',# add the aws secret access key
                        region_name='ap-south-1',)# add the region here

if __name__ == "__main__":

        #camera warm-up time
        time.sleep(2)
        
        milli = int(round(time.time() * 1000))
        image = '{}/image_{}.jpg'.format(directory,milli)
        P.capture(image) #capture an image
        print('captured '+image)
        with open(image, 'rb') as image:
            try: #match the captured imges against the indexed faces
                match_response = rek_client.search_faces_by_image(CollectionId=collectionId, Image={'Bytes': image.read()}, MaxFaces=1, FaceMatchThreshold=85)
                if match_response['FaceMatches']:
                    print('Hello, ',match_response['FaceMatches'][0]['Face']['ExternalImageId'])
                    print('Similarity: ',match_response['FaceMatches'][0]['Similarity'])
                    print('Confidence: ',match_response['FaceMatches'][0]['Face']['Confidence'])
    
                else:
                    print('No faces matched')
            except:
                print('No face detected')
            

        time.sleep(1)       
# http://www.pillalamarri.in/python/face-recognition-door-lock/

Training:

import boto3
# http://www.pillalamarri.in/python/face-recognition-door-lock/
s3_client = boto3.client(
    's3',
    aws_access_key_id='',# add the aws access key
    aws_secret_access_key=''# add the aws secret access key
    
)

collectionId='' #collection name

rek_client=boto3.client('rekognition',
                            aws_access_key_id='',# add the aws access key
                            aws_secret_access_key='',# add the aws secret access key
                            region_name='',)# add the region here


bucket = '' #S3 bucket name
all_objects = s3_client.list_objects(Bucket =bucket )


'''
delete existing collection if it exists
'''
list_response=rek_client.list_collections(MaxResults=2)

if collectionId in list_response['CollectionIds']:


    rek_client.delete_collection(CollectionId=collectionId)

'''
create a new collection 
'''
rek_client.create_collection(CollectionId=collectionId)

'''
add all images in current bucket to the collections
use folder names as the labels
'''

for content in all_objects['Contents']:
    collection_name,collection_image =content['Key'].split('/')
    if collection_image:
        label = collection_name
        print('indexing: ',label)
        image = content['Key']    
        index_response=rek_client.index_faces(CollectionId=collectionId,
                                Image={'S3Object':{'Bucket':bucket,'Name':image}},
                                ExternalImageId=label,
                                MaxFaces=1,
                                QualityFilter="AUTO",
                                DetectionAttributes=['ALL'])
        print('FaceId: ',index_response['FaceRecords'][0]['Face']['FaceId'])

Main Script:

# http://www.pillalamarri.in/python/face-recognition-door-lock/
import subprocess
import sys
import os
from gpiozero import Button
from gpiozero import LED
from time import sleep
LOCK = LED(17)
def face():
	returned_text = subprocess.check_output("python3 match.py", shell=True, universal_newlines=True)
	a = returned_text
	print("Face Recognized")
	if "---" in a:# Add the name of the person to be recognized from the S3 bucket folder name
		LOCK.on()# Connect Door Lock Digital pin to GPIO 17
		sleep(10)
		LOCK.off()
		print("Door is Open")
	else:
		print("Unknown Person")
	print(returned_text)
	remember = open('text.txt','w')
	remember.write(returned_text)
	remember.close()

button = Button("GPIO26")# Connect the Botton with GPIO 26
while True:
    if button.is_pressed:
        print("Button is pressed")
	face()
        exit()
# http://www.pillalamarri.in/python/face-recognition-door-lock/
Posted in PythonTags: