| import streamlit as st |
| from PIL import Image |
| from ultralytics import YOLO |
|
|
| |
| detection_model = YOLO('detection.pt') |
| segmentation_model = YOLO('segmentation.pt') |
|
|
| st.title('Protein Crystallization Detection and Segmentation') |
| st.write('Upload an image to detect or segment crystals.') |
|
|
| |
| mode = st.selectbox( |
| "Choose the model mode", |
| ("Detection", "Segmentation") |
| ) |
|
|
| |
| if mode == "Detection": |
| st.info("Detection is useful for images with distinct and separated crystals.") |
| else: |
| st.info("Segmentation is better for images with many crystals, especially overlaying ones.") |
|
|
| uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png"]) |
|
|
| if uploaded_file is not None: |
| |
| image = Image.open(uploaded_file).convert("RGB") |
| st.image(image, caption='Uploaded Image', use_column_width=True) |
|
|
| |
| if mode == "Detection": |
| model = detection_model |
| st.write("Running Detection...") |
| else: |
| model = segmentation_model |
| st.write("Running Segmentation...") |
|
|
| |
| results = model.predict(image) |
|
|
| |
| results_image = results[0].plot() |
|
|
| |
| results_image = Image.fromarray(results_image) |
|
|
| |
| st.image(results_image, caption='Processed Image', use_column_width=True) |
|
|
| |
| with st.expander("Show Results", expanded=True): |
| inference_time = (results[0].speed['inference']) / 1000 |
| st.write(f"Inference Time: {inference_time:.2f} s") |
| |
| st.write("Detection Results:" if mode == "Detection" else "Segmentation Results:") |
| for i, box in enumerate(results[0].boxes): |
| x1, y1, x2, y2 = box.xyxy[0] |
| conf = box.conf[0] |
| cls = box.cls[0] |
| st.write(f"{results[0].names[int(cls)]} {i + 1}:") |
| st.write(f" - Bounding Box: ({x1:.2f}, {y1:.2f}, {x2:.2f}, {y2:.2f})") |
| st.write(f" - Confidence: {conf:.2f}") |