Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,8 @@ import tempfile
|
|
| 3 |
import gradio as gr
|
| 4 |
from ultralytics import YOLO
|
| 5 |
import pandas as pd
|
| 6 |
-
import
|
|
|
|
| 7 |
|
| 8 |
# Define the label mapping
|
| 9 |
label_mapping = {
|
|
@@ -17,18 +18,6 @@ label_mapping = {
|
|
| 17 |
}
|
| 18 |
|
| 19 |
def process_video(video_file):
|
| 20 |
-
# Define colors for each class (8 classes)
|
| 21 |
-
colors = [
|
| 22 |
-
(255, 0, 0), # Class 0 - Blue
|
| 23 |
-
(50, 205, 50), # Class 1 - Green
|
| 24 |
-
(0, 0, 255), # Class 2 - Red
|
| 25 |
-
(255, 255, 0), # Class 3 - Cyan
|
| 26 |
-
(255, 0, 255), # Class 4 - Magenta
|
| 27 |
-
(255, 140, 0), # Class 5 - Orange
|
| 28 |
-
(128, 0, 128), # Class 6 - Purple
|
| 29 |
-
(0, 128, 128) # Class 7 - Teal
|
| 30 |
-
]
|
| 31 |
-
|
| 32 |
# Load the YOLOv8 model
|
| 33 |
model = YOLO("insect_detection4.pt")
|
| 34 |
|
|
@@ -100,37 +89,41 @@ def process_video(video_file):
|
|
| 100 |
# Read the DataFrame from the CSV file
|
| 101 |
df_from_csv = pd.read_csv(csv_path)
|
| 102 |
|
| 103 |
-
# Create the plot from the CSV data
|
| 104 |
-
|
| 105 |
-
for insect_id in df_from_csv['insect_id'].unique():
|
| 106 |
-
frames = df_from_csv[df_from_csv['insect_id'] == insect_id]['frame']
|
| 107 |
-
insect_class = label_mapping[df_from_csv[df_from_csv['insect_id'] == insect_id]['class'].values[0]]
|
| 108 |
-
plt.plot(frames, [insect_id] * len(frames), 'o-', label=f'{insect_class} {insect_id}')
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
|
| 121 |
gallery_items = [(crop_path, f'{label} {insect_id}') for insect_id, (crop_path, label) in unique_insect_crops.items()]
|
| 122 |
|
| 123 |
-
return
|
| 124 |
|
| 125 |
# Create a Gradio interface
|
| 126 |
-
example_video = "
|
| 127 |
|
| 128 |
inputs = gr.Video(label="Input Insect Trap Video", value=example_video)
|
| 129 |
outputs = [
|
| 130 |
-
gr.
|
| 131 |
-
gr.Gallery(label="
|
| 132 |
gr.File(label="Download CSV")
|
| 133 |
]
|
| 134 |
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from ultralytics import YOLO
|
| 5 |
import pandas as pd
|
| 6 |
+
import plotly.graph_objects as go
|
| 7 |
+
import numpy as np
|
| 8 |
|
| 9 |
# Define the label mapping
|
| 10 |
label_mapping = {
|
|
|
|
| 18 |
}
|
| 19 |
|
| 20 |
def process_video(video_file):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# Load the YOLOv8 model
|
| 22 |
model = YOLO("insect_detection4.pt")
|
| 23 |
|
|
|
|
| 89 |
# Read the DataFrame from the CSV file
|
| 90 |
df_from_csv = pd.read_csv(csv_path)
|
| 91 |
|
| 92 |
+
# Create the interactive plot from the CSV data
|
| 93 |
+
fig = go.Figure()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
for insect_id, group in df_from_csv.groupby('insect_id'):
|
| 96 |
+
class_name = label_mapping[group.iloc[0]['class']]
|
| 97 |
+
color = 'rgb({}, {}, {})'.format(*np.random.randint(0, 256, 3))
|
| 98 |
+
hover_text = group.apply(lambda row: f'Insect ID: {int(row["insect_id"])}, Class: {class_name}, Frame: {int(row["frame"])}', axis=1)
|
| 99 |
+
fig.add_trace(go.Scatter(x=group['frame'], y=group['insect_id'], mode='markers', marker=dict(color=color), name=f'{class_name} {insect_id}',
|
| 100 |
+
hoverinfo='text', hovertext=hover_text))
|
| 101 |
|
| 102 |
+
fig.update_layout(title='Temporal distribution of insects',
|
| 103 |
+
xaxis_title='Frame',
|
| 104 |
+
yaxis_title='Insect ID',
|
| 105 |
+
hovermode='closest')
|
| 106 |
|
| 107 |
gallery_items = [(crop_path, f'{label} {insect_id}') for insect_id, (crop_path, label) in unique_insect_crops.items()]
|
| 108 |
|
| 109 |
+
return fig, gallery_items, csv_path
|
| 110 |
|
| 111 |
# Create a Gradio interface
|
| 112 |
+
example_video = "insect_trap_video_exemple.mp4" # Replace with the actual path to your example video
|
| 113 |
|
| 114 |
inputs = gr.Video(label="Input Insect Trap Video", value=example_video)
|
| 115 |
outputs = [
|
| 116 |
+
gr.Plot(label="Insect Detection Plot"),
|
| 117 |
+
gr.Gallery(label="Insect Gallery"), # Added a gallery to display insect crops with labels
|
| 118 |
gr.File(label="Download CSV")
|
| 119 |
]
|
| 120 |
|
| 121 |
+
description = """
|
| 122 |
+
Uncover the Secret Lives of Insects! 🐝🦋🕷️
|
| 123 |
+
|
| 124 |
+
Upload your video now to track, visualize, and explore insect activity with our cutting-edge detection tool. You can get started with the example video.
|
| 125 |
+
"""
|
| 126 |
+
|
| 127 |
+
gr.Interface(fn=process_video, inputs=inputs, outputs=outputs, title= 'InsectSpy 🕵️♂️🦗', description=description, examples=[example_video]).launch()
|
| 128 |
+
|
| 129 |
|