proj 2...

This commit is contained in:
2024-04-04 23:02:55 -07:00
parent 582233502d
commit 6223ff41d3
122 changed files with 709 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
#__copyright__ = "Copyright 2024, VISA Lab"
#__license__ = "MIT"
# Define global args
ARG FUNCTION_DIR="/home/app/"
ARG RUNTIME_VERSION="3.8"
ARG DISTRO_VERSION="3.12"
FROM alpine:latest
FROM python:${RUNTIME_VERSION} AS python-alpine
#RUN apt-get update \
# && apt-get install -y cmake ca-certificates libgl1-mesa-glx
RUN python${RUNTIME_VERSION} -m pip install --upgrade pip
FROM python-alpine AS build-image
# Include global args in this stage of the build
ARG FUNCTION_DIR
ARG RUNTIME_VERSION
# Create function directory
RUN mkdir -p ${FUNCTION_DIR}
# Install Lambda Runtime Interface Client for Python
RUN python${RUNTIME_VERSION} -m pip install awslambdaric --target ${FUNCTION_DIR}
# Stage 3 - final runtime image
# Grab a fresh copy of the Python image
FROM python-alpine
# Include global arg in this stage of the build
ARG FUNCTION_DIR
# Set working directory to function root directory
WORKDIR ${FUNCTION_DIR}
# Copy in the built dependencies
COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}
# (Optional) Add Lambda Runtime Interface Emulator and use a script in the ENTRYPOINT for simpler local runs
ADD https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie /usr/bin/aws-lambda-rie
RUN chmod 755 /usr/bin/aws-lambda-rie
# Install ffmpeg
RUN apt-get update
RUN apt-get install -y ffmpeg
#RUN apk --no-cache add ffmpeg
# Copy handler function
COPY requirements.txt ${FUNCTION_DIR}
#COPY ffmpeg ${FUNCTION_DIR}
#COPY ffmpeg /usr/bin
RUN python${RUNTIME_VERSION} -m pip install -r requirements.txt --target ${FUNCTION_DIR}
COPY entry.sh /
# Copy function code
COPY handler.py ${FUNCTION_DIR}
RUN chmod 777 /entry.sh
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
ENTRYPOINT [ "/entry.sh" ]
CMD [ "handler.handler" ]

View File

@@ -0,0 +1,6 @@
#!/bin/sh
if [ -z "${AWS_LAMBDA_RUNTIME_API}" ]; then
exec /usr/bin/aws-lambda-rie /usr/local/bin/python -m awslambdaric $1
else
exec /usr/local/bin/python -m awslambdaric $1
fi

View File

@@ -0,0 +1,37 @@
__copyright__ = "Copyright 2024, VISA Lab"
__license__ = "MIT"
import os
import imutils
import cv2
import json
from PIL import Image
from facenet_pytorch import MTCNN
from shutil import rmtree
mtcnn = MTCNN(keep_all=True, device='cpu')
def face_extraction_function(folder_name):
if not os.path.exists(folder_name):
print(f"Folder {folder_name} does not exist!")
return None
pics = sorted(os.listdir(folder_name))
for pic in pics:
path = os.path.join(folder_name, pic)
frame = cv2.imread(path, cv2.IMREAD_COLOR)
boxes, _ = mtcnn.detect(frame)
if boxes is None:
rmtree(folder_name)
return
for box in boxes:
cv2.rectangle(frame,
(int(box[0]), int(box[1])),
(int(box[2]), int(box[3])),
(0, 255, 0),
2)
cv2.imwrite(path,frame)
return folder_name

View File

@@ -0,0 +1,44 @@
__copyright__ = "Copyright 2024, VISA Lab"
__license__ = "MIT"
import os
import imutils
import cv2
import json
from PIL import Image, ImageDraw, ImageFont
from facenet_pytorch import MTCNN, InceptionResnetV1
from shutil import rmtree
import numpy as np
import torch
mtcnn = MTCNN(image_size=240, margin=0, min_face_size=20) # initializing mtcnn for face detection
resnet = InceptionResnetV1(pretrained='vggface2').eval() # initializing resnet for face img to embeding conversion
def face_recognition_function(folder_name):
if not os.path.exists(folder_name):
print(f"Folder {folder_name} does not exist!")
return None
name = ""
pics = sorted(os.listdir(folder_name))
return_names = dict()
for pic in pics:
path = os.path.join(folder_name, pic)
img = cv2.imread(path, cv2.IMREAD_COLOR)
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
face,prob = mtcnn(img, return_prob=True, save_path=None)
saved_data = torch.load('/tmp/data.pt') # loading data.pt file
if face != None:
emb = resnet(face.unsqueeze(0)).detach() # detech is to make required gradient false
embedding_list = saved_data[0] # getting embedding data
name_list = saved_data[1] # getting list of names
dist_list = [] # list of matched distances, minimum distance is used to identify the person
for idx, emb_db in enumerate(embedding_list):
dist = torch.dist(emb, emb_db).item()
dist_list.append(dist)
idx_min = dist_list.index(min(dist_list))
pic_extension = pic.split(".")[0]
return_names[pic_extension] = name_list[idx_min]
return return_names

View File

@@ -0,0 +1,8 @@
#__copyright__ = "Copyright 2024, VISA Lab"
#__license__ = "MIT"
from boto3 import client as boto3_client
def handler(event, context):
print("Hello")

View File

@@ -0,0 +1,53 @@
__copyright__ = "Copyright 2024, VISA Lab"
__license__ = "MIT"
import os
import imutils
import cv2
def detect(lgray, frame, min_area):
frame = imutils.resize(frame, width=320)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
frameDelta = cv2.absdiff(lgray, gray)
thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(contours)
for c in contours:
if cv2.contourArea(c) > min_area:
return True, gray
return False, gray
def motion_detection_function(folder_name):
if not os.path.exists(folder_name):
print(f"Folder {folder_name} does not exist!")
return None
# Motion detection specific parameters
min_area = 10 # if 10% of the frame was changed, motion happens
last_gray = None
pics = sorted(os.listdir(folder_name))
for pic in pics:
path = os.path.join(folder_name, pic)
frame = cv2.imread(path, cv2.IMREAD_COLOR)
if frame is None:
print("failed to open picture %s" % path)
return None
if last_gray is None:
frame = imutils.resize(frame, width=320)
last_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
last_gray = cv2.GaussianBlur(last_gray, (21, 21), 0)
continue
detected, gray = detect(last_gray, frame, min_area)
if detected:
break
else:
last_gray = gray
os.remove(path)
return folder_name

View File

@@ -0,0 +1,4 @@
boto3
ffmpeg
ffmpeg-python

View File

@@ -0,0 +1,30 @@
#__copyright__ = "Copyright 2024, VISA Lab"
#__license__ = "MIT"
import os
import ffmpeg
import subprocess
import math
def video_splitting_cmdline(video_filename):
filename = os.path.basename(video_filename)
outdir = os.path.splitext(filename)[0]
outdir = os.path.join("/tmp",outdir)
output_dir = outdir
if not os.path.exists(outdir):
os.makedirs(outdir)
split_cmd = '/usr/bin/ffmpeg -ss 0 -r 1 -i ' +video_filename+ ' -vf fps=1/10 -start_number 0 -vframes 10 ' + outdir + "/" + 'output-%02d.jpg -y'
try:
subprocess.check_call(split_cmd, shell=True)
except subprocess.CalledProcessError as e:
print(e.returncode)
print(e.output)
fps_cmd = 'ffmpeg -i ' + video_filename + ' 2>&1 | sed -n "s/.*, \\(.*\\) fp.*/\\1/p"'
fps = subprocess.check_output(fps_cmd, shell=True).decode("utf-8").rstrip("\n")
fps = math.ceil(float(fps))
return outdir