File size: 1,990 Bytes
1f9a45b
c6a1409
1f9a45b
 
 
 
 
c6a1409
1f9a45b
199e804
1f9a45b
 
c6a1409
 
 
 
 
9aaac5d
c6a1409
9aaac5d
c6a1409
dacd259
 
 
 
9aaac5d
dacd259
 
9aaac5d
1f9a45b
 
c6a1409
1f9a45b
1408ebf
 
2c867d4
 
 
c6a1409
2c867d4
 
 
3500d9f
c6a1409
3500d9f
c6a1409
 
 
dacd259
c6a1409
 
 
1408ebf
2c867d4
 
 
 
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
from ultralytics import YOLO
from PIL import Image
import requests
from io import BytesIO
import time

# Load the YOLO model only once using Streamlit session state
if 'models_loaded' not in st.session_state:
    st.session_state.yolo_model = YOLO('best.pt')  # Update with your model path
    st.session_state.models_loaded = True

# Define function for inference using YOLO
def predict_with_yolo(image):
    # Run inference on the image using the YOLO model
    results = st.session_state.yolo_model(image)
    
    # Print the classification results
    if results:
        # YOLOv8 classification results contain label and confidence for the whole image
        for result in results:
            return {
                "Class": result.names[result.probs.top1],
                "Confidence": float(result.probs.top1conf)  # Convert confidence to float
            }
    else:
        st.warning("No classification results found.")
        return None
    

# Streamlit app UI
st.title("Clothing Detection with YOLO")

url = st.text_input("Paste image URL here...")
if url:
    try:
        response = requests.get(url)
        if response.status_code == 200:
            image = Image.open(BytesIO(response.content)).convert('RGB')
            st.image(image.resize((200, 200)), caption="Uploaded Image", use_column_width=False)
            
            start_time = time.time()
            image_resized = image.resize((224, 224))
            # Predict using YOLO
            predictions = predict_with_yolo(image_resized)

            # Display predictions
            if predictions:
                st.write("Predictions:", predictions)
            else:
                st.write("No objects detected.")

            st.write(f"Time taken: {time.time() - start_time:.2f} seconds")
        else:
            st.error("Failed to load image from URL. Please check the URL.")
    except Exception as e:
        st.error(f"Error processing the image: {str(e)}")