|
import streamlit as st |
|
from PIL import Image |
|
import cv2 |
|
from ultralytics import YOLO |
|
|
|
|
|
with st.sidebar: |
|
st.title("Control panel") |
|
file = st.file_uploader("Choose an image or a video", type=["png", "jpg", "jpeg", "mp4"]) |
|
radio_button1 = st.radio("Model", ["model_train_17", "model_train_15"]) |
|
radio_button2=st.radio("Visualize",["No","Yes"]) |
|
|
|
st.header("Palm Tree Detection") |
|
st.write( |
|
'<p style="font-family: Arial, sans-serif; font-size: px; color: black; font-style: italic;">Counting the number of palm and coconut trees</p>', |
|
unsafe_allow_html=True |
|
) |
|
|
|
status_placeholder = st.empty() |
|
if radio_button1 == "model_train_17": |
|
model = YOLO('train_17_best.pt') |
|
elif radio_button1 == "model_train_15": |
|
model = YOLO('train_15_best.pt') |
|
|
|
|
|
|
|
|
|
def count_objects(results, class_names): |
|
"""Count objects detected for each class.""" |
|
class_counts = {name: 0 for name in class_names.values()} |
|
for box in results[0].boxes: |
|
cls_idx = int(box.cls[0]) |
|
class_name = class_names.get(cls_idx, None) |
|
|
|
if class_name: |
|
class_counts[class_name] += 1 |
|
else: |
|
st.warning(f"Unknown class index detected: {cls_idx}") |
|
return class_counts |
|
|
|
|
|
def run_inference(file): |
|
file_type = file.type.split('/')[0] |
|
|
|
if file_type == 'image': |
|
image = Image.open(file) |
|
st.image(image, caption="Uploaded Image", use_container_width=True) |
|
status_placeholder.write("Processing...Please wait....") |
|
results = model.predict(source=image, save=False) |
|
|
|
class_names = model.names |
|
counts = count_objects(results, class_names) |
|
st.write("Detected objects:") |
|
for obj, count in counts.items(): |
|
st.write(f"{obj}: {count}") |
|
status_placeholder.empty() |
|
|
|
if(radio_button2=="Yes"): |
|
status_placeholder.write("Processing...") |
|
st.image(results[0].plot(), caption="Detected Objects", use_container_width=True) |
|
status_placeholder.empty() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if file is not None: |
|
run_inference(file) |
|
|