File size: 1,466 Bytes
92d6d14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import pipeline
from PIL import Image

# 1. Setup the Page
st.title("Concrete Crack Detection")
st.write("Upload an image of a concrete surface, and the AI will predict if there is a crack.")

# 2. Load the Model 
# Use a public model for now since you don't have a trained one yet
# This model is a common one for crack detection
MODEL_ID = "Guna-M/concrete-crack-ai" 

try:
    classifier = pipeline("image-classification", model=MODEL_ID)
except Exception as e:
    st.error(f"Error loading model: {e}")
    st.stop()

# 3. Image Upload
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])

if uploaded_file is not None:
    image = Image.open(uploaded_file)
    st.image(image, caption='Uploaded Image', use_container_width=True)
    st.write("Analyzing...")
    
    # 4. Make Prediction
    results = classifier(image)
    
    st.subheader("Prediction Results:")
    
    # Show the top result
    top_result = results[0]
    label = top_result['label']
    score = top_result['score'] * 100
    
    if "crack" in label.lower() and "no" not in label.lower():
        st.error(f"Prediction: CRACK DETECTED (Confidence: {score:.2f}%)")
    else:
        st.success(f"Prediction: NO CRACK (Confidence: {score:.2f}%)")

    # Show all probabilities
    with st.expander("See all probabilities"):
        for res in results:
            st.write(f"- {res['label']}: {res['score']*100:.2f}%")