Enhancing Workplace Safety with AI: A Worker Safety Monitoring Application

harshitlyzr

harshit-lyzr

Posted on June 24, 2024

Enhancing Workplace Safety with AI: A Worker Safety Monitoring Application

In today’s fast-paced industrial landscape, ensuring the safety of workers remains a top priority for businesses. With advancements in technology, artificial intelligence (AI) is increasingly playing a crucial role in improving workplace safety. One such innovative application is the Worker Safety Monitoring system, designed to analyze factory site images and generate customized safety measures using AI-powered image analysis technology.

How It Works
Image Upload: Users upload an image of their factory site through the application interface.
AI Analysis: The uploaded image is processed using AI-powered image analysis technology. The system identifies potential safety hazards and generates a detailed report outlining specific safety measures to address each identified risk.
Customized Recommendations: The generated report provides tailored recommendations that comply with current industrial safety regulations. It prioritizes actions based on the severity of the hazards, categorizing them into immediate, short-term, and long-term measures.
User Interface: The application offers a user-friendly interface where users can view the uploaded image, along with the AI-generated safety report. Additionally, users can input their OpenAI API key to access the advanced image analysis capabilities.
Key Features
Comprehensive Hazard Identification: The AI algorithm scans the uploaded image to detect various safety hazards commonly found in factory environments, ensuring a thorough assessment.
Detailed Safety Measures: The generated report includes detailed safety measures tailored to address each identified risk. This ensures that users receive actionable recommendations to improve workplace safety effectively.
Prioritization of Actions: Safety measures are prioritized based on the urgency and severity of the identified hazards, enabling businesses to focus their resources on addressing the most critical issues first.
Integration with OpenAI: The application leverages the OpenAI API for advanced image analysis, enhancing its capabilities and accuracy in hazard detection and safety recommendation generation.
Benefits
Enhanced Workplace Safety: By proactively identifying and addressing safety hazards, the application helps create a safer working environment for employees, reducing the risk of workplace accidents and injuries.
Regulatory Compliance: The generated safety measures align with established industrial safety regulations, ensuring that businesses remain compliant with OSHA standards and other regulatory requirements.
Cost Savings: By preventing workplace accidents and injuries, businesses can save on potential costs associated with worker compensation claims, medical expenses, and productivity losses.
Efficiency and Accuracy: The use of AI technology streamlines the hazard identification process, providing accurate and timely safety recommendations to businesses.

Step-by-Step Guide
Setting Up the App:

pip install lyzr-automata streamlit pillow
Enter fullscreen mode Exit fullscreen mode

gptvision.py

from typing import Any, Dict, List
from lyzr_automata.ai_models.model_base import AIModel
from openai import OpenAI
from lyzr_automata.data_models import FileResponse
from lyzr_automata.utils.resource_handler import ResourceBox


class GPTVISION(AIModel):
    def __init__(self, api_key, parameters: Dict[str, Any]):
        self.parameters = parameters
        self.client = OpenAI(api_key=api_key)
        self.api_key = api_key

    def generate_text(
        self,
        task_id: str=None,
        system_persona: str=None,
        prompt: str=None,
        image_url: str=None,
        messages: List[dict] = None,
    ):
        if messages is None:
            messages = [
                {"role": "user", "content": [
                    {"type": "text",
                     "text": prompt
                     },
                    {
                      "type": "image_url",
                      "image_url": {
                        "url": f"data:image/jpeg;base64,{image_url}",
                      },
                    },
                    ]
                 },
            ]

        response = self.client.chat.completions.create(
                **self.parameters,
                model="gpt-4o",
                messages=messages,
                )

        return response.choices[0].message.content

    def generate_image(
        self, task_id: str, prompt: str, resource_box: ResourceBox
    ) -> FileResponse:
        pass
Enter fullscreen mode Exit fullscreen mode

This code defines a class GPTVISION that inherits from Lyzr Automata'sAIModel. It interacts with the OpenAI API to generate text based on user prompts and potentially images.

It takes an API key and parameters during initialization.
The generate_text function builds a message containing the prompt and optionally an image URL.
It sends this message to the gpt-4o model for text completion and returns the generated response.
The generate_image function is not implemented here.
App.py

from gptvision import GPTVISION
import streamlit as st
from PIL import Image
import utils
import base64
import os
Enter fullscreen mode Exit fullscreen mode

Imports necessary libraries like GPTVISION from gptvision, streamlit for the web app, PIL for image processing, and utility functions from utils.Imports necessary libraries like GPTVISION from gptvision, streamlit for the web app, PIL for image processing, and utility functions from utils.

OpenAI API Key Validation:

api = st.sidebar.text_input("Enter Your OPENAI API KEY HERE",type="password")

if api:
        openai_4o_model = GPTVISION(api_key=api,parameters={})
else:
        st.sidebar.error("Please Enter Your OPENAI API KEY")
Enter fullscreen mode Exit fullscreen mode

Takes user input for their OpenAI API key through a password field in the sidebar.
Checks if the API key is entered.
If not, displays an error message in the sidebar asking the user to enter the key.

Uploading Construction Map:

data = "data"
os.makedirs(data, exist_ok=True)

def encode_image(image_path):
        with open(image_path, "rb") as image_file:
                return base64.b64encode(image_file.read()).decode('utf-8')
Enter fullscreen mode Exit fullscreen mode

Creates a folder named data to store uploaded files.
Defines a function encode_image that takes an image path, opens the image in binary mode, and encodes it to base64 format for sending to the OpenAI API.

GPT Model and Prompt:

prompt = f"""
Please analyze the uploaded image of our factory site. Identify and highlight potential safety hazards based on Industrial Safety Regulations(Occupational Safety and Health Administration (OSHA) standards) such as unguarded machinery, tripping hazards, missing safety signs, and workers not using PPE. 
Provide a detailed report suggesting specific safety measures to mitigate each identified risk. 
Ensure the suggestions comply with current industrial safety regulations and prioritize actions based on the severity of the hazards.

Output Requirements:
1/ Potential Safety Hazards
2/ Detailed Safety Measures
3/ Prioritization of Actions
       1/ Immediate
       2/ short-term
       3/ long-term

IF IMAGE IS NOT RELATED TO WORKERS SAFETY OR FACTORY SITE THEN REPLY "Please upload Image related to factory site.This image does not belong to Factory site."""
Enter fullscreen mode Exit fullscreen mode

Defines a prompt for the GPTVISION model:

Analyzes the image for hazards based on OSHA standards.
Identifies specific risks and suggests safety measures.
Prioritizes actions based on severity (immediate, short-term, long-term).
Handles cases where the image isn’t relevant to a factory site.

Uploading and Processing:

if uploaded_files is not None:
        st.success(f"File uploaded: {uploaded_files.name}")
        file_path = utils.save_uploaded_file(uploaded_files)
        if file_path is not None:
            st.sidebar.image(file_path)
            if st.button("Generate"):
                encoded_image = encode_image(file_path)
                planning = openai_4o_model.generate_text(prompt=prompt, image_url=encoded_image)
                st.markdown(planning)
Enter fullscreen mode Exit fullscreen mode

If “Generate” is clicked, encodes the image using the encode_image function.
Calls the openai_4o_model.generate_text function (likely from GPTVISION) with the prompt and encoded image.
This function presumably uses the OpenAI API to analyze the image and generate text based on the prompt.
The generated text containing safety analysis is displayed in the app using st.markdown.
try it now: https://lyzr-worker-safety-monitor.streamlit.app/

For more information explore the website: Lyzr

Github: https://github.com/harshit-lyzr/worker_safety_monitering

💖 💪 🙅 🚩
harshitlyzr
harshit-lyzr

Posted on June 24, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related