R.Aiesha Siddiqua
Published © LGPL

Intelligent Security Guard For Historical Places

Smart security guard for protection.

IntermediateShowcase (no instructions)846
Intelligent Security Guard For Historical Places

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
logitech usb camera
×1

Software apps and online services

Python IDLE
Html

Story

Read more

Code

Face detection , Capturing and Uploading to AWS Cloud

Python
import cv2
import sys
import logging as log
import datetime as dt
from time import sleep
import os
import boto3

global a



s3_bucket = 'dayaalibucket1'
s3_bucket1 = 'historical123'
s3_bucket_region = 'us-east-1'

s3 = boto3.resource('s3')
my_bucket = s3.Bucket(s3_bucket)


entry_folder_name = "entry/"
exit_folder_name = "exit/"

q = []
############################# function for listing faces in folders of a bucket
entry_folder = "entry/"
exit_folder = "exit/"
a = []
b = []
images = []
c=0
###############################################################

faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
log.basicConfig(filename='webcam.log',level=log.INFO)

video_capture = cv2.VideoCapture(0)
anterior = 0
i=0
offset=10

name=dt.datetime.now()
name = str(name.strftime('%Y:%m:%d'))


folder = 'entry'
s3 = boto3.client('s3')
key_name = folder + '/' 
s3_connect = boto3.client('s3', s3_bucket_region)

try:
    bucket = s3_connect.put_object(Bucket=s3_bucket, Key=key_name)
    #print ("Bucket:", bucket)
except Exception as e:
    print ("Bucket Error " , e)

 # i eddited not to get ret=false  ................................................final haarcascade_frontalface_default.xml'



while(not video_capture.isOpened()):    # i eddited not to get ret=false
    video_capture = cv2.VideoCapture(0)

ret = video_capture.isOpened()
print (ret)

def list_faces():
    c=0
   
    del images[:]
    
    for file in my_bucket.objects.all():
        g = file.key
        print (".......after:" + str(g))
        images.append(g)


    
    for x in images:
        if( x == entry_folder ):
            continue

        elif(x != exit_folder):
            a.append(x)
           
        if(x == exit_folder):
            break

    for y in  images:
        if(y != exit_folder):
            if(c==1):
                b.append(y)
                
            else:
                continue

        elif(y == exit_folder):
            c=1

        else:
            break

################################delete object

def delete_faces(bucket, key, region="us-east-1"):
                        rekognition = boto3.client("rekognition", region)
                        objs = list(my_bucket.objects.filter(Prefix=key))
                        response = my_bucket.delete_objects(
                            Delete={
                                'Objects': [
                                    {
                                        'Key': key,
                                     
                                        },
                                    ],
                                'Quiet': True|False
                                },
                            )
                        return response


############################## 


##############################
    
while True:

    name1 = dt.datetime.now()
    name = str(name1.strftime('%d:%m:%Y:%H:%M:%S'))
  
    
    if not video_capture.isOpened():
        print('Unable to load camera.')
        sleep(2)
        pass

    # Capture frame-by-frame
    ret, frame = video_capture.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.2,
        minNeighbors=5,
        minSize=(30, 30)
    )

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
        if i%25 == 0:
             cv2.imwrite("entry/"+ name +'.'+ str(i) + ".jpg", frame)
             filename= name +'.'+ str(i) + ".jpg"
             # upload File to S3
             file_key_name = folder + '/' + filename
             local_path = os.getcwd()
             local_name = local_path + '/' + key_name + filename
             upload = s3_connect.upload_file(local_name, s3_bucket, file_key_name)
             #upload = s3_connect.upload_file(local_name, s3_bucket1, "visitors" + '/' + filename)
             upload = s3_connect.upload_file(local_name, s3_bucket1, "visitors" + '/' + str(name1.strftime('%d:%m:%Y')) + '/' + filename)
             
           
        i=i+1
        

    if anterior != len(faces):
        anterior = len(faces)
        log.info("faces: "+str(len(faces))+" at "+str(dt.datetime.now()))
        


    # Display the resulting frame
    cv2.imshow('Video', frame)


    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    # Display the resulting frame
    cv2.imshow('Video', frame)

# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()

Comparison and deleting the same faces in entry from AWS Cloud

Python
import cv2
import sys
import logging as log
import datetime as dt
from time import sleep
import os
import boto3
global a

import requests
import json

s3_bucket = 'dayaalibucket1'
s3_bucket_region = 'us-east-1'

s3 = boto3.resource('s3')
my_bucket = s3.Bucket(s3_bucket)


entry_folder_name = "entry/"
exit_folder_name = "exit/"

q = []
############################# function for listing faces in folders of a bucket
entry_folder = "entry/"
exit_folder = "exit/"
visitor_folder = "visitors/"
a = []
b = []
images = []
c=0
###############################################################

faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
log.basicConfig(filename='webcam.log',level=log.INFO)

video_capture = cv2.VideoCapture(0)
anterior = 0
i=0
offset=10

name = dt.datetime.now()
name = name.strftime('%H:%M:%S')


folder = 'exit'
s3 = boto3.client('s3')
key_name = folder + '/' 
s3_connect = boto3.client('s3', s3_bucket_region)

try:
    bucket = s3_connect.put_object(Bucket=s3_bucket, Key=key_name)
    #print ("Bucket:", bucket)
except Exception as e:
    print ("Bucket Error " , e)

 # i eddited not to get ret=false  ................................................final haarcascade_frontalface_default.xml'



while(not video_capture.isOpened()):    # i eddited not to get ret=false
    video_capture = cv2.VideoCapture(0)

ret = video_capture.isOpened()
print (ret)

################################


def list_faces():
    c=0
   
    del images[:]
    
    for file in my_bucket.objects.all():
        g = file.key
        print (".......after:" + str(g))
        images.append(g)


    
    for x in images:
        if( x == entry_folder ):
            continue

        elif(x != exit_folder):
            a.append(x)
           
        if(x == exit_folder):
            break

    for y in  images:
        if(y != exit_folder):
            if(c==1):
                b.append(y)
                
            else:
                continue

        elif(y == exit_folder):
            c=1

        else:
            break


################################


def list_faces1():
    c=0
   
    del images[:]
    
    for file in my_bucket.objects.all():
        g = file.key
        print (".......after:" + str(g))
        images.append(g)

    print("images:",images)


    
    for x in images:
        if( x == entry_folder ):
            continue

        elif(x != exit_folder):
            a.append(x)
           
        if(x == exit_folder):
            break

    for y in  images:
        if(y != exit_folder):
            if(c==1 and y != visitor_folder):
                b.append(y)
                
            elif(c ==1 and y == visitor_folder):
                break
            
            else:
                continue

        elif(y == exit_folder):
            c=1

        else:
            break

        

################################delete object

def delete_faces(bucket, key, region="us-east-1"):
                        rekognition = boto3.client("rekognition", region)
                        objs = list(my_bucket.objects.filter(Prefix=key))
                        response = my_bucket.delete_objects(
                            Delete={
                                'Objects': [
                                    {
                                        'Key': key,
                                     
                                        },
                                    ],
                                'Quiet': True|False
                                },
                            )
                        return response

############################## face comparision
def compare_faces(bucket, key, bucket_target, key_target, threshold=0, region="us-east-1"):
    rekognition = boto3.client("rekognition", region)
    response = rekognition.compare_faces(
        SourceImage={
            "S3Object": {
                "Bucket": bucket,
                "Name": key,
                }
            },
        TargetImage={
            "S3Object": {
                "Bucket": bucket_target,
                "Name": key_target,
                }
            },
        SimilarityThreshold=threshold,
        )
    return response['SourceImageFace'], response['FaceMatches']

############################## face comparision

def compare():
 
    list_faces()
    print(a,b,q)
    for i in a:
        for j in b:
            KEY_SOURCE = i
            KEY_TARGET = j

            print ("............face comparision started.....................")
            print (i,j)

            source_face, matches = compare_faces(s3_bucket, KEY_SOURCE, s3_bucket, KEY_TARGET)


            print ("Source Face ({Confidence}%)".format(**source_face))


            for match in matches:
                    print ("Target Face ({Confidence}%)".format(**match['Face']))
                    percent = match['Similarity']
                            
                    print ("  Similarity : {}%".format(match['Similarity']))
                    print("......................face comparision ended ............................................")

                    print(percent)
                    if(percent>80):
                        if i not in q:
                            q.append(i)
                       
    print (q)

    for key in q:
        delete_faces(s3_bucket, key, region="us-east-1")

    print("...................wait until comparision ended.................................................")

    #q.clear()
  

##############################
        
def verification():
    print('Authorized')
    ##espeak.synth(str (i))


##############################

r = requests.get(url="https://api.thingspeak.com/channels/445400/feeds/last.json?api_key=KY768RJVKAME0SBA")
x1 = r.json()
dh = (x1["field1"])
dm1 = (x1["field2"])
dm2 = (x1["field3"])
print ("webpage: hour: start min :stop min", dh,dm1,dm2)


    
while True:
    
    name = dt.datetime.now()
    name = name.strftime('%H:%M:%S')

    if not video_capture.isOpened():
        print('Unable to load camera.')
        sleep(2)
        pass

    # Capture frame-by-frame
    ret, frame = video_capture.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.2,
        minNeighbors=5,
        minSize=(30, 30)
    )

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
        if i%25 == 0:
             cv2.imwrite("exit/"+ str(name) +'.'+ str(i) + ".jpg", frame)
             filename= name +'.'+ str(i) + ".jpg"
             # upload File to S3
             file_key_name = folder + '/' + filename
             local_path = os.getcwd()
             local_name = local_path + '/' + key_name + filename
             upload = s3_connect.upload_file(local_name, s3_bucket, file_key_name)

             name = dt.datetime.now()
             print("present time:",name)
             
             h = name.strftime('%H')
             s = name.minute;
             print("h,s",h,s)

             if((str(h) =='16') and ( s>3 and s<58)):
                 print('static Time Match')
                 compare()
                         

             elif ((str(h) == str(dh)) and (s > int(dm1)) and (s < int(dm2))):
                 print('dynamic Time Match')                 
                 compare()
                 
             else:
                 print('Time Not Match')             
           
        i=i+1
        

    if anterior != len(faces):
        anterior = len(faces)
        log.info("faces: "+str(len(faces))+" at "+str(dt.datetime.now()))
        


    # Display the resulting frame
    cv2.imshow('Video', frame)


    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    # Display the resulting frame
    cv2.imshow('Video', frame)

# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()

Retrieving Data Using Web page

HTML
<html lang="en">
	<head>
		<title>Intelligent Security Guard for Historical places</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
		<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
		<script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>
			<style>
			.navbar-custom 
				{		
					background-color:#0b5d75ed;
				}
				.header1 
				{
					text-align : center;
					vertical-align:middle;
					float: none;
					 font-size:170%; 
				}
				h2
				{
					color : #f0ad4e;
				}
				.cl
				{
					 margin-top: 90px;
				}
				h3 {
					text-align : center;
					color: #0b5d75ed;
				}
				#s3_images{
					border: 1px solid #f0ad4e;
					border-radius: 5px;
					padding: 10px;
				}
				.cs
				{
					margin: 6px;
				}
					
			</style>
	</head>
	<body>
		<nav class="navbar navbar-default navbar-fixed-top navbar-custom">
			<div class="container-fluid">
				<div class="header1">
					<h2>Intelligent Security Guard for Historical Places</h2>
				</div>			
			</div>
		</nav>
		<div class="container cl" >	
			<div class="row new_search" >
				<div class="col-sm-2" >
				</div>
				<div class="col-sm-10" >
					<div class="row" id="app_serch">
						<div class="col-sm-3">	
							<input type="number" class="form-control" placeholder="Set Hours " id="hour">
						</div>						
						<div class="col-sm-3">	
							<input type="number" class="form-control" placeholder="Set Start Minuts " id="sminut">
						</div>					
						<div class="col-sm-3">	
							<input type="number" class="form-control" placeholder="Set End Minutes" id="eminut">
						</div>
						<div class="col-sm-3">	
							<button class="btn btn-info" id="search_btn"><i class="fa fa-search"></i> Set Time</button>
						</div>
					</div>
				</div>
			</div>
			<div class="row">
				<div id="list_folder">
				</div>
			</div>
			<div class="row">
				<h3>People who are Present in Historical Place after Closing Hours </h3><hr>
				<div class="col-sm-1">
				</div>
				<div id="s3_images" class="col-sm-10">
					<h4>Waiting for Images</h4>
				</div>
				<div class="col-sm-1">
				</div>
			</div>
			<div class="row">
				<h3>People who visited and left the Musium </h3><hr>
				<div class="col-sm-1">
				</div>
				<div id="exit_images" class="col-sm-10">
					<h4>Waiting for Images</h4>
				</div>
				<div class="col-sm-1">
				</div>
			</div>
			<div class="row">
				<h3>Visitors Images</h3><hr>
				<div class="col-sm-1">
				</div>
				<div id="visit_images" class="col-sm-10">
					<h4>Waiting for Images</h4>
				</div>
				<div class="col-sm-1">
				</div>
			</div>
			<div class="row">
			<br><br>
			</div>
		</div>
	</body>
<html>
<script type="text/javascript">
 var html = '';
 var html1 = '';
 var html2 = '';
var albumBucketName = 'dayaalibucket1';
var BucketName = 'historical123';
var bucketRegion = 'us-east-1';
var IdentityPoolId = 'us-east-1:09badf75-df0f-4567-b3c7-f5459a3869a0';
var folder = 'entry';
var folder1 = 'visitors';
var folder2 = 'exit';

AWS.config.update({
  region: bucketRegion,
  credentials: new AWS.CognitoIdentityCredentials({
    IdentityPoolId: IdentityPoolId
  })
});

var s3 = new AWS.S3({
  apiVersion: '2006-03-01',
  params: {Bucket: albumBucketName}
});
var s3_new = new AWS.S3({
  apiVersion: '2006-03-01',
  params: {Bucket: BucketName}
});
  viewAlbum(folder);
  viewAlbum1(folder1);
  viewAlbum2(folder2);
function viewAlbum(albumName) {
  var albumPhotosKey = encodeURIComponent(albumName) + '/';
  s3.listObjects({Prefix: albumPhotosKey}, function(err, data) {
    if (err) {
      return alert('There was an error viewing your album: ' + err.message);
    }
    // `this` references the AWS.Response instance that represents the response
    var href = this.request.httpRequest.endpoint.href;
    var bucketUrl = href + albumBucketName + '/';

    var photos = data.Contents.map(function(photo) {
      var photoKey = photo.Key;
	  console.log(photoKey);
      var photoUrl = bucketUrl + encodeURIComponent(photoKey);
	  console.log(photoUrl);
	  if(photoUrl.indexOf('.jpg') > 0 || photoUrl.indexOf('.png') > 0 || photoUrl.indexOf('.jpeg') > 0)
		{
			// Found world
			html += '<div class="col-sm-2">';
			html += '<img src="'+photoUrl+'" class="img-thumbnail cs" alt="Image">';
			html += '<span>';
            html +=   photoKey.replace(albumPhotosKey, '');
            html +=  '</span>';
			html += '</div>';
		}
    });
	$('#s3_images').html(html);
  });
}

function viewAlbum2(albumName) {
console.log(albumName);
  var albumPhotosKey = encodeURIComponent(albumName) + '/';
  s3.listObjects({Prefix: albumPhotosKey}, function(err, data) {
    if (err) {
      return alert('There was an error viewing your album: ' + err.message);
    }
    // `this` references the AWS.Response instance that represents the response
    var href = this.request.httpRequest.endpoint.href;
    var bucketUrl = href + albumBucketName + '/';

    var photos = data.Contents.map(function(photo) {
      var photoKey = photo.Key;
      var photoUrl = bucketUrl + encodeURIComponent(photoKey);
	  console.log(photoUrl);
	  if(photoUrl.indexOf('.jpg') > 0 || photoUrl.indexOf('.png') > 0 || photoUrl.indexOf('.jpeg') > 0)
		{
			// Found world
			html2 += '<div class="col-sm-2">';
			html2 += '<img src="'+photoUrl+'" class="img-thumbnail cs" alt="Image">';
			html2 += '<span>';
            html2 +=   photoKey.replace(albumPhotosKey, '');
            html2 +=  '</span>';
			html2 += '</div>';
		}
    });
	$('#exit_images').html(html2);
  });
}
function viewAlbum1(albumName) {
  var albumPhotosKey = encodeURIComponent(albumName) + '/';
  s3_new.listObjects({Prefix: albumPhotosKey}, function(err, data) {
    if (err) {
      return alert('There was an error viewing your album: ' + err.message);
    }
    // `this` references the AWS.Response instance that represents the response
    var href = this.request.httpRequest.endpoint.href;
    var bucketUrl = href + BucketName + '/';
    var photos = data.Contents.map(function(photo) {
      var photoKey = photo.Key;
      var photoUrl = bucketUrl + encodeURIComponent(photoKey);
	  console.log(photoUrl);
	  if(photoUrl.indexOf('.jpg') > 0 || photoUrl.indexOf('.png') > 0 || photoUrl.indexOf('.jpeg') > 0)
		{
			// Found world
			html1 += '<div class="col-sm-2">';
			html1 += '<img src="'+photoUrl+'" class="img-thumbnail cs" alt="Image">';
			html1 += '<div style="word-wrap: break-word;">';
            html1 +=   photoKey.replace(albumPhotosKey, '');
            html1 +=  '</div>';
			html1 += '</div>';
		}
    });
	$('#visit_images').html(html1);
  });
}
$('#search_btn').click(function(){
var hr1 = $('#hour').val();
var smt1 = $('#sminut').val();
var smt2 = $('#eminut').val();
	if((hr1!= "")&&(smt1 != "")&&(smt2 != ""))
	{
		$.post("https://api.thingspeak.com/update?api_key=71L2TOEK1LBXUBVA&field1="+hr1+"&field2="+smt1+"&field3="+smt2,
		{
		},
		function(data,status){
		alert("Time Updated Successfully ");
		
		});
	}
	else
	{
		alert("Please Enter Hours ,Starting Minuts , Ending Minuts Properly");
	}
	$('#hour').val('');
	$('#sminut').val('');
	$('#eminut').val('');
});
</script>

Credits

R.Aiesha Siddiqua
1 project • 3 followers

Comments