File size: 2,056 Bytes
7457453
 
 
 
 
49b5bca
7457453
49b5bca
7457453
 
 
 
 
 
 
4c94976
73f6d3a
4c94976
 
7457453
7ac00fa
24b5566
 
7457453
 
 
24b5566
7457453
 
 
 
 
 
 
24b5566
02796cf
49b5bca
 
02796cf
7457453
4c94976
 
 
7457453
240b902
 
 
 
 
 
 
96115ce
240b902
 
 
 
24b5566
 
240b902
 
24b5566
240b902
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
import streamlit as st
from PIL import Image
from ultralytics import YOLO

# Load the YOLO model
@st.cache_resource
def load_model():
    model = YOLO('best (5).pt')  # Use the path to your trained model
    return model

# Prediction function
def predict(model, image):
    results = model(image)
    return results

# Load and display the header image
header_image = Image.open('historicalPIC.png')
st.image(header_image, use_column_width=True)

# Streamlit UI
st.title("Historical Places")

# File uploader for users to upload images
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])

if uploaded_file is not None:
    # Display the uploaded image
    image = Image.open(uploaded_file)
    st.image(image, caption='Uploaded Image.', use_column_width=True)
    
    # Convert the image to RGB format if needed
    if image.mode != 'RGB':
        image = image.convert('RGB')
    
    # Load the model
    model = load_model()
    
    # Run YOLO prediction
    results = predict(model, image)

    # Access class names
    class_names = model.names

    # Process and display results
    detected_objects = []
    for result in results:
        # Iterate over each detected object
        for bbox in result.boxes:
            x1, y1, x2, y2 = bbox.xyxy[0].tolist()
            conf = bbox.conf.item()
            cls = int(bbox.cls.item())
            detected_objects.append(f"Detected {class_names[cls]}")
    
    # Display detected objects' names and confidence scores
    st.subheader("Detection Results")
    if detected_objects:
        for obj in detected_objects:
            st.write(obj)
    else:
        st.write("No objects detected.")

    # Render the image with bounding boxes
    if results:
        try:
            results.render()  # Modify the image with bounding boxes
            img_with_boxes = Image.fromarray(results.imgs[0])
            st.image(img_with_boxes, caption='Detected Objects', use_column_width=True)
        except Exception:
            pass  # Ignore any errors in rendering