AWS rekog PPE detection

Viewed 21

please help me, I am trying to implement AWS rekog PPE detection in a video file. I was able to implement it in an image but I can't implement it in a video for detection, what I can't do is I can't apply the bounding box and detect the PPE. I don't know if it will be the same method of creating a bounding box in an image. this is the link for the PPE detection using the image https://docs.aws.amazon.com/rekognition/latest/dg/ppe-example-image-bounding-box.html What I am trying to figure out is if it is possible to draw a bounding box using the draw function in a video frame? according to aws rekog they compute the bounding box base on the width and height of the image, frame and image are the same right? but I cant somehow implement it.

See the code below, this is the same code on the website this is the code for the video.

import base64
import csv
import boto3
import streamlit as st
import io
from PIL import Image, ImageDraw, ExifTags, ImageColor
import os
import cv2
import math
import json


def rescale_frame(frame,percent=30):
    scale_percent = 30
    width = int(frame.shape[1] * scale_percent / 100)
    height = int(frame.shape[0] * scale_percent / 100)
    dim = (width, height)
    return cv2.resize(frame, dim, interpolation = cv2.INTER_AREA)

fill_green = '#00d400'
fill_red = '#ff0000'
fill_yellow = '#ffff00'
line_width = 3

with open("new_user_credentials.csv", 'r') as input:
    next(input)
    reader = csv.reader(input)
    for line in reader:
        Access_key_id = line[2]
        Secret_access_key = line[3]

videoFile = "ppevideotest.mp4"

rekognition = boto3.client('rekognition', aws_access_key_id=Access_key_id, aws_secret_access_key=Secret_access_key,
                           region_name='ap-southeast-1')

ppeLabels = []
cap = cv2.VideoCapture(videoFile)
frameRate = cap.get(5)  # frame rate
while (cap.isOpened()):
    frameId = cap.get(1)  # current frame number
    print("Processing frame id: {}".format(frameId))
    ret, frame = cap.read()
    frame = rescale_frame(frame, percent=30)

    imgWidth = int(cap.get(3))
    imgHeight = int(cap.get(4))

    img = cv2.line(frame)

    cv2.imshow("Frame", img)

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

    if (ret != True):
        break
    if (frameId % math.floor(frameRate) == 0):
        hasFrame, imageBytes = cv2.imencode(".jpg", frame)

        if (hasFrame):
            response = rekognition.detect_protective_equipment(
                Image={
                    'Bytes': imageBytes.tobytes(),
                }
            )

        for person in response['Persons']:

            found_mask = False
            found_hat = False
            found_hand = False

            for body_part in person['BodyParts']:
                ppe_items = body_part['EquipmentDetections']

                for ppe_item in ppe_items:

                    if ppe_item['Type'] == 'HEAD_COVER':
                        fill_color = fill_green
                        found_hat = True
                        # check if mask covers face
                        if not ppe_item['CoversBodyPart']['Value']:
                            fill_color = fill = '#ff0000'
                        # draw bounding box around mask
                        box = ppe_item['BoundingBox']
                        left = imgWidth * box['Left']
                        top = imgHeight * box['Top']
                        width = imgWidth * box['Width']
                        height = imgHeight * box['Height']
                        points = (
                            (left, top),
                            (left + width, top),
                            (left + width, top + height),
                            (left, top + height),
                            (left, top)
                        )
                        img.line(points, fill=fill_yellow, width=line_width)
0 Answers
Related