LuqmanAC commited on
Commit
c7b5498
·
verified ·
1 Parent(s): ea9b29f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ import cv2
4
+ import numpy as np
5
+ from PIL import Image
6
+ from groq import Groq
7
+ import os
8
+
9
+ # Load Pretrained Model (YOLOv8 for Structural Damage Detection)
10
+ model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
11
+
12
+ def process_image(image):
13
+ # Convert PIL image to OpenCV format
14
+ img = np.array(image)
15
+ img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
16
+
17
+ # Perform inference
18
+ results = model(img)
19
+
20
+ # Draw bounding boxes on detected damages
21
+ for *box, conf, cls in results.xyxy[0].tolist():
22
+ x1, y1, x2, y2 = map(int, box)
23
+ cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
24
+ label = f"{model.names[int(cls)]}: {conf:.2f}"
25
+ cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
26
+
27
+ return img
28
+
29
+ # Initialize Groq API Client
30
+ groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
31
+
32
+ def get_ai_analysis():
33
+ chat_completion = groq_client.chat.completions.create(
34
+ messages=[
35
+ {"role": "user", "content": "Explain the impact of structural damage in buildings."}
36
+ ],
37
+ model="llama-3.3-70b-versatile",
38
+ stream=False,
39
+ )
40
+ return chat_completion.choices[0].message.content
41
+
42
+ # Streamlit UI
43
+ st.title("AI-Based Structural Damage Detection")
44
+ st.write("Upload an image of a structure, and the AI will detect any visible structural damage.")
45
+
46
+ uploaded_file = st.file_uploader("Choose an image", type=["jpg", "png", "jpeg"])
47
+
48
+ if uploaded_file is not None:
49
+ image = Image.open(uploaded_file)
50
+ st.image(image, caption="Uploaded Image", use_column_width=True)
51
+
52
+ if st.button("Analyze Image"):
53
+ processed_img = process_image(image)
54
+ st.image(processed_img, caption="Detected Structural Damage", use_column_width=True)
55
+
56
+ st.subheader("AI Analysis")
57
+ st.write(get_ai_analysis())