File size: 1,848 Bytes
7670d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
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
43
44
45
46
47
48
49
50
51
import streamlit as st
from PIL import Image
from ultralytics import YOLO
from transformers import pipeline

# Load YOLOv8 model (you can use 'yolov8n.pt' for small, or upload a custom model)
model = YOLO("yolov8n.pt")

# Load language model pipeline (can swap with Groq/LLaMA3 API if needed)
summarizer = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1")

# Function to detect objects
def detect_objects(image_path):
    results = model(image_path)
    names = results[0].names
    boxes = results[0].boxes
    detected = [names[int(cls)] for cls in boxes.cls]
    return results, detected

# Function to generate report from detected objects
def generate_report(detected_items):
    prompt = f"""
    Generate a construction site report based on the following detected items:
    {', '.join(detected_items)}.

    Mention safety compliance issues if helmets, vests, or barriers are missing.
    """
    output = summarizer(prompt, max_length=250, do_sample=True)[0]["generated_text"]
    return output

# Streamlit UI
st.set_page_config(page_title="Photo to Construction Report", layout="centered")
st.title("πŸ“· Photo to Construction Report Generator")

uploaded_image = st.file_uploader("Upload a construction site photo", type=["jpg", "jpeg", "png"])

if uploaded_image is not None:
    with open("uploaded.jpg", "wb") as f:
        f.write(uploaded_image.read())

    st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)

    with st.spinner("Running object detection..."):
        results, detected_items = detect_objects("uploaded.jpg")
        st.image(results[0].plot(), caption="Detected Objects", use_column_width=True)

    with st.spinner("Generating AI report..."):
        report = generate_report(detected_items)
        st.subheader("πŸ“„ AI-Generated Report")
        st.write(report)