Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
import cv2
|
| 2 |
-
import csv
|
| 3 |
import tempfile
|
| 4 |
import gradio as gr
|
| 5 |
from ultralytics import YOLO
|
|
|
|
| 6 |
|
| 7 |
def process_video(video_file):
|
| 8 |
# Define colors for each class (8 classes)
|
|
@@ -26,49 +26,62 @@ def process_video(video_file):
|
|
| 26 |
# Open the video file
|
| 27 |
cap = cv2.VideoCapture(video_file)
|
| 28 |
|
| 29 |
-
# Prepare
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
writer = csv.writer(csv_file)
|
| 33 |
-
writer.writerow(["frame", "id", "class", "x", "y", "w", "h"])
|
| 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 |
# Release the video capture
|
| 65 |
cap.release()
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# Create a Gradio interface
|
| 70 |
inputs = gr.Video(label="Input Video")
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
gr.Interface(fn=process_video, inputs=inputs, outputs=
|
| 74 |
|
|
|
|
| 1 |
import cv2
|
|
|
|
| 2 |
import tempfile
|
| 3 |
import gradio as gr
|
| 4 |
from ultralytics import YOLO
|
| 5 |
+
import pandas as pd
|
| 6 |
|
| 7 |
def process_video(video_file):
|
| 8 |
# Define colors for each class (8 classes)
|
|
|
|
| 26 |
# Open the video file
|
| 27 |
cap = cv2.VideoCapture(video_file)
|
| 28 |
|
| 29 |
+
# Prepare DataFrame for storing detection data
|
| 30 |
+
columns = ["frame", "id", "class", "x", "y", "w", "h"]
|
| 31 |
+
df = pd.DataFrame(columns=columns)
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
frame_id = 0
|
| 34 |
|
| 35 |
+
# Loop through the video frames
|
| 36 |
+
while cap.isOpened():
|
| 37 |
+
# Read a frame from the video
|
| 38 |
+
success, frame = cap.read()
|
| 39 |
|
| 40 |
+
if success:
|
| 41 |
+
frame_id += 1
|
| 42 |
|
| 43 |
+
# Run YOLOv8 tracking on the frame, persisting tracks between frames
|
| 44 |
+
results = model.track(frame, persist=True, tracker="insect_tracker.yaml")
|
| 45 |
|
| 46 |
+
for result in results:
|
| 47 |
+
boxes = result.boxes.cpu().numpy()
|
| 48 |
+
confidences = boxes.conf
|
| 49 |
+
class_ids = boxes.cls
|
| 50 |
|
| 51 |
+
for i, box in enumerate(boxes):
|
| 52 |
+
class_id = int(class_ids[i])
|
| 53 |
+
confidence = confidences[i]
|
| 54 |
|
| 55 |
+
# Append detection data to DataFrame
|
| 56 |
+
new_row = pd.DataFrame({
|
| 57 |
+
"frame": [frame_id],
|
| 58 |
+
"id": [box.id],
|
| 59 |
+
"class": [int(box.cls[0])],
|
| 60 |
+
"x": [box.xywh[0][0]],
|
| 61 |
+
"y": [box.xywh[0][1]],
|
| 62 |
+
"w": [box.xywh[0][2]],
|
| 63 |
+
"h": [box.xywh[0][3]]
|
| 64 |
+
})
|
| 65 |
+
df = pd.concat([df, new_row], ignore_index=True)
|
| 66 |
|
| 67 |
+
else:
|
| 68 |
+
break
|
| 69 |
|
| 70 |
# Release the video capture
|
| 71 |
cap.release()
|
| 72 |
|
| 73 |
+
# Save DataFrame to CSV
|
| 74 |
+
csv_path = tempfile.mktemp(suffix=".csv")
|
| 75 |
+
df.to_csv(csv_path, index=False)
|
| 76 |
+
|
| 77 |
+
return df, csv_path
|
| 78 |
|
| 79 |
# Create a Gradio interface
|
| 80 |
inputs = gr.Video(label="Input Video")
|
| 81 |
+
outputs = [
|
| 82 |
+
gr.DataFrame(label="Detection Data"),
|
| 83 |
+
gr.File(label="Download CSV")
|
| 84 |
+
]
|
| 85 |
|
| 86 |
+
gr.Interface(fn=process_video, inputs=inputs, outputs=outputs).launch()
|
| 87 |
|