ElodieA commited on
Commit
0f6c01e
·
verified ·
1 Parent(s): 20d4bc8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -30
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 CSV file for writing
30
- csv_path = tempfile.mktemp(suffix=".csv")
31
- with open(csv_path, mode='w', newline='') as csv_file:
32
- writer = csv.writer(csv_file)
33
- writer.writerow(["frame", "id", "class", "x", "y", "w", "h"])
34
 
35
- frame_id = 0
36
 
37
- # Loop through the video frames
38
- while cap.isOpened():
39
- # Read a frame from the video
40
- success, frame = cap.read()
41
 
42
- if success:
43
- frame_id += 1
44
 
45
- # Run YOLOv8 tracking on the frame, persisting tracks between frames
46
- results = model.track(frame, persist=True, tracker="insect_tracker.yaml")
47
 
48
- for result in results:
49
- boxes = result.boxes.cpu().numpy()
50
- confidences = boxes.conf
51
- class_ids = boxes.cls
52
 
53
- for i, box in enumerate(boxes):
54
- class_id = int(class_ids[i])
55
- confidence = confidences[i]
56
 
57
- # Write detection data to CSV
58
- writer.writerow([frame_id, box.id, int(box.cls[0]), box.xywh[0][0], box.xywh[0][1],
59
- box.xywh[0][2], box.xywh[0][3]])
 
 
 
 
 
 
 
 
60
 
61
- else:
62
- break
63
 
64
  # Release the video capture
65
  cap.release()
66
 
67
- return csv_path
 
 
 
 
68
 
69
  # Create a Gradio interface
70
  inputs = gr.Video(label="Input Video")
71
- output = gr.File(label="CSV File")
 
 
 
72
 
73
- gr.Interface(fn=process_video, inputs=inputs, outputs=output).launch()
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