capradeepgujaran
commited on
Commit
•
b70b1c9
1
Parent(s):
e37b756
Upload README.md
Browse files
README.md
CHANGED
@@ -1,166 +1,90 @@
|
|
1 |
-
|
2 |
-
license: mit
|
3 |
-
title: Construction Site Safety Analyzer
|
4 |
-
sdk: gradio
|
5 |
-
emoji: 🏢
|
6 |
-
colorFrom: blue
|
7 |
-
colorTo: green
|
8 |
-
short_description: Enhance workplace safety and compliance with AI
|
9 |
-
sdk_version: 5.1.0
|
10 |
-
---
|
11 |
-
## 🏗️ Construction Site Safety Analyzer
|
12 |
|
13 |
-
|
14 |
|
15 |
-
|
16 |
|
17 |
-
|
18 |
|
19 |
-
###
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
- **
|
23 |
-
- **
|
24 |
-
- **
|
25 |
|
26 |
-
###
|
|
|
|
|
|
|
27 |
|
28 |
-
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
|
34 |
-
###
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
### Running the Application
|
43 |
-
|
44 |
-
To launch the application, simply execute the main script:
|
45 |
-
|
46 |
```bash
|
47 |
-
python
|
48 |
```
|
49 |
|
50 |
-
|
51 |
|
52 |
-
|
53 |
|
54 |
-
|
|
|
|
|
|
|
55 |
|
56 |
-
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
67 |
|
68 |
-
|
69 |
-
logging.basicConfig(level=logging.DEBUG)
|
70 |
-
logger = logging.getLogger(__name__)
|
71 |
-
```
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
The `encode_image` function encodes an image into a base64 string, which is useful for sending images over APIs. The `analyze_construction_image` function handles the main logic of image analysis, including sending requests to the Groq API and processing the results.
|
76 |
-
|
77 |
-
```python
|
78 |
-
def encode_image(image):
|
79 |
-
try:
|
80 |
-
if isinstance(image, str):
|
81 |
-
with open(image, "rb") as image_file:
|
82 |
-
return base64.b64encode(image_file.read()).decode('utf-8')
|
83 |
-
elif isinstance(image, Image.Image):
|
84 |
-
buffered = io.BytesIO()
|
85 |
-
image.save(buffered, format="PNG")
|
86 |
-
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
87 |
-
else:
|
88 |
-
raise ValueError(f"Unsupported image type: {type(image)}")
|
89 |
-
except Exception as e:
|
90 |
-
logger.error(f"Error encoding image: {str(e)}")
|
91 |
-
raise
|
92 |
-
|
93 |
-
def analyze_construction_image(image):
|
94 |
-
if image is None:
|
95 |
-
logger.warning("No image provided")
|
96 |
-
return [("No image uploaded", "Error: Please upload an image for analysis.")]
|
97 |
-
...
|
98 |
-
```
|
99 |
|
100 |
-
|
101 |
-
|
102 |
-
The Gradio interface is created using the `gr.Blocks` context manager. It includes a form for uploading images, a button to trigger analysis, and a chatbot for displaying results and handling user queries.
|
103 |
-
|
104 |
-
```python
|
105 |
-
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
|
106 |
-
gr.HTML(
|
107 |
-
"""
|
108 |
-
<div class="container">
|
109 |
-
<div class="header">
|
110 |
-
<h1>🏗️ Construction Site Safety Analyzer</h1>
|
111 |
-
</div>
|
112 |
-
<p class="subheader">Enhance workplace safety and compliance with AI-powered image analysis using Llama 3.2 90B Vision and expert chat assistance.</p>
|
113 |
-
</div>
|
114 |
-
"""
|
115 |
-
)
|
116 |
-
|
117 |
-
with gr.Row():
|
118 |
-
with gr.Column(scale=1):
|
119 |
-
image_input = gr.Image(type="pil", label="Upload Construction Site Image", elem_classes="image-container")
|
120 |
-
analyze_button = gr.Button("🔍 Analyze Safety Hazards", elem_classes="analyze-button")
|
121 |
-
with gr.Column(scale=2):
|
122 |
-
with gr.Group(elem_classes="chat-container"):
|
123 |
-
chatbot = gr.Chatbot(label="Safety Analysis Results and Expert Chat", elem_classes="chatbot")
|
124 |
-
with gr.Row(elem_classes="input-row"):
|
125 |
-
msg = gr.Textbox(
|
126 |
-
label="Ask about safety measures or regulations",
|
127 |
-
placeholder="E.g., 'What OSHA guidelines apply to this hazard?'",
|
128 |
-
show_label=False,
|
129 |
-
elem_classes="chat-input"
|
130 |
-
)
|
131 |
-
clear = gr.Button("🗑️ Clear", elem_classes="clear-button")
|
132 |
-
|
133 |
-
def update_chat(history, new_message):
|
134 |
-
history = history or []
|
135 |
-
history.append(new_message)
|
136 |
-
return history
|
137 |
-
|
138 |
-
analyze_button.click(
|
139 |
-
analyze_construction_image,
|
140 |
-
inputs=[image_input],
|
141 |
-
outputs=[chatbot],
|
142 |
-
postprocess=lambda x: update_chat(chatbot.value, x[0])
|
143 |
-
)
|
144 |
-
|
145 |
-
msg.submit(chat_about_image, [msg, chatbot], [msg, chatbot])
|
146 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
147 |
-
|
148 |
-
gr.HTML(
|
149 |
-
"""
|
150 |
-
<div class="groq-badge">Powered by Groq</div>
|
151 |
-
"""
|
152 |
-
)
|
153 |
-
```
|
154 |
-
|
155 |
-
#### Launching the App
|
156 |
|
157 |
-
|
158 |
|
159 |
-
|
160 |
-
if __name__ == "__main__":
|
161 |
-
iface.launch(debug=True)
|
162 |
-
```
|
163 |
|
164 |
-
|
165 |
|
166 |
-
|
|
|
1 |
+
# 🏗️ Construction Site Safety Analyzer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
## Overview
|
4 |
|
5 |
+
The **Construction Site Safety Analyzer** is a powerful tool designed to enhance workplace safety and compliance through AI-powered image and video analysis. Leveraging the capabilities of Llama 3.2 90B Vision and expert chat assistance, this application helps identify safety issues, categorize them, provide detailed descriptions, and suggest steps to resolve them.
|
6 |
|
7 |
+
## Features
|
8 |
|
9 |
+
### 1. Image Analysis
|
10 |
+
- **Upload Multiple Images**: Easily upload multiple construction site images for comprehensive analysis.
|
11 |
+
- **Automatic Image Resizing**: Ensures that images do not exceed API size limits while maintaining quality.
|
12 |
+
- **Safety Hazard Identification**: Identifies potential safety hazards, categorizes them, and provides detailed descriptions.
|
13 |
+
- **Resolution Suggestions**: Offers actionable steps to mitigate identified risks.
|
14 |
|
15 |
+
### 2. Video Analysis
|
16 |
+
- **Upload Videos**: Upload videos to analyze specific frames for safety hazard identification.
|
17 |
+
- **Key Frame Extraction**: Extracts key frames from videos at specified time points (default: start, middle, end).
|
18 |
+
- **Detailed Analysis**: Provides detailed analysis for each extracted frame, similar to image analysis.
|
19 |
|
20 |
+
### 3. Expert Chat Assistance
|
21 |
+
- **Interactive Chat**: Engage in real-time conversations with an AI assistant specializing in construction site safety.
|
22 |
+
- **Question-Based Analysis**: Ask questions about specific safety measures or regulations related to the analyzed hazards.
|
23 |
+
- **Comprehensive Insights**: Receive detailed answers based on the initial analysis, enhancing understanding and decision-making.
|
24 |
|
25 |
+
## Getting Started
|
26 |
|
27 |
+
### Prerequisites
|
28 |
+
- Python 3.8+
|
29 |
+
- `pip` (Python package installer)
|
30 |
|
31 |
+
### Installation
|
32 |
+
1. Clone the repository:
|
33 |
+
```bash
|
34 |
+
git clone https://github.com/your-repo/construction-site-safety-analyzer.git
|
35 |
+
cd construction-site-safety-analyzer
|
36 |
+
```
|
37 |
+
|
38 |
+
2. Install dependencies:
|
39 |
+
```bash
|
40 |
+
pip install -r requirements.txt
|
41 |
+
```
|
42 |
+
|
43 |
+
3. Set up environment variables:
|
44 |
+
```bash
|
45 |
+
export GROQ_API_KEY="your-groq-api-key"
|
46 |
+
```
|
47 |
|
48 |
### Running the Application
|
49 |
+
To launch the application, simply run the main script:
|
|
|
|
|
50 |
```bash
|
51 |
+
python app.py
|
52 |
```
|
53 |
|
54 |
+
This will start a local server, and you can access the application via your web browser at `http://localhost:7860`.
|
55 |
|
56 |
+
## Usage
|
57 |
|
58 |
+
1. **Upload Images/Videos**: Navigate to the "Upload Construction Site Images" or "Upload Construction Site Video" sections and select the appropriate files.
|
59 |
+
2. **Analyze Safety Hazards**: Click the "🔍 Analyze Safety Hazards" button to initiate the analysis process.
|
60 |
+
3. **View Analysis Results**: The safety analysis results will be displayed in the "Safety Analysis Results and Expert Chat" section.
|
61 |
+
4. **Chat with the Assistant**: Engage in interactive conversations with the AI assistant to get more insights or ask questions about specific safety measures.
|
62 |
|
63 |
+
## Requirements
|
64 |
|
65 |
+
The application requires the following Python packages:
|
66 |
+
- `os`
|
67 |
+
- `base64`
|
68 |
+
- `gradio`
|
69 |
+
- `PIL`
|
70 |
+
- `io`
|
71 |
+
- `json`
|
72 |
+
- `groq`
|
73 |
+
- `logging`
|
74 |
+
- `cv2`
|
75 |
+
- `numpy`
|
76 |
+
- `traceback`
|
77 |
|
78 |
+
These dependencies can be installed using `pip` as shown in the installation instructions.
|
|
|
|
|
|
|
79 |
|
80 |
+
## Contributing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
+
Contributions are welcome! Feel free to submit pull requests, report bugs, or suggest improvements.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
+
## License
|
85 |
|
86 |
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
|
|
|
|
|
87 |
|
88 |
+
---
|
89 |
|
90 |
+
Built with ❤️ by [Your Name]
|