File size: 1,877 Bytes
c7b5498 c8283c2 c7b5498 c8283c2 c7b5498 | 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 52 53 54 55 56 57 58 | import streamlit as st
import torch
import cv2
import numpy as np
from PIL import Image
from groq import Groq
import os
# Load Pretrained Model (YOLOv8 for Structural Damage Detection)
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
def process_image(image):
# Convert PIL image to OpenCV format
img = np.array(image)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
# Perform inference
results = model(img)
# Draw bounding boxes on detected damages
for *box, conf, cls in results.xyxy[0].tolist():
x1, y1, x2, y2 = map(int, box)
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
label = f"{model.names[int(cls)]}: {conf:.2f}"
cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
return img
# Initialize Groq API Client
groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
def get_ai_analysis():
chat_completion = groq_client.chat.completions.create(
messages=[
{"role": "user", "content": "Explain the impact of structural damage in buildings."}
],
model="llama-3.3-70b-versatile",
stream=False,
)
return chat_completion.choices[0].message.content
# Streamlit UI
st.title("AI-Based Structural Damage Detection")
st.write("Upload an image of a structure, and the AI will detect any visible structural damage.")
uploaded_file = st.file_uploader("Choose an image", type=["jpg", "png", "jpeg"])
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image", use_container_width=True)
if st.button("Analyze Image"):
processed_img = process_image(image)
st.image(processed_img, caption="Detected Structural Damage", use_container_width=True)
st.subheader("AI Analysis")
st.write(get_ai_analysis())
|