-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.py
187 lines (145 loc) · 5.9 KB
/
detect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import os
import argparse
import wandb
import cv2
from typing import Optional, Iterator, Tuple
from utils.sahi_batched import Yolov8BatchedDetectionModel
from utils.sahi_batched import get_sliced_prediction
from utils.filevideostream import FileVideoStream
from tqdm import tqdm
from pathlib import Path
from core.detections import Detections, save_video_detections
from utils.annotators import annotate_frame
from utils.artifacts import download_artifact
def detect_video(video_path, model_path, max_frames=None):
sahi_model = Yolov8BatchedDetectionModel(
model_path=model_path,
confidence_threshold=0.5,
image_size=640,
device="cuda:0", # or 'cuda:0'
)
video_dir, video_name = os.path.split(video_path)
video_dir = video_dir or os.getcwd()
if not video_name:
print("error no video name")
return
elif not os.path.exists(video_path):
print(f"video path {video_path} does not exist")
return
# Open the video file
cap = FileVideoStream(path=video_path, queue_size=128, transform=None).start()
# get the number of frames
video_frame_count = int(cap.stream.get(cv2.CAP_PROP_FRAME_COUNT))
current_frame_num = 0
total_frame_num = (max_frames or video_frame_count)
pbar = tqdm(total=total_frame_num)
frame_detections = []
# Loop through the video frames
while cap.more() and (current_frame_num < total_frame_num):
# Read a frame from the video
frame = cap.read()
if frame is not None:
# Getting prediction using model
object_prediction_list = get_sliced_prediction(
frame,
sahi_model,
slice_height=640,
slice_width=640,
overlap_height_ratio=0.2,
overlap_width_ratio=0.2,
perform_standard_pred=False,
verbose=0,
)
detections = Detections.from_sahi_batched(object_prediction_list)
frame_detections.append(detections)
pbar.update(1)
current_frame_num = current_frame_num + 1
# Release the video capture object and close the display window
if cap.running():
cap.stop()
_, detections_path = save_video_detections(frame_detections, video_path, module="detect")
return detections_path
def detect_frame(frame_path, model_path):
sahi_model = Yolov8BatchedDetectionModel(
model_path=model_path,
confidence_threshold=0.3,
image_size=640,
device="cuda:0",
)
frame_dir, frame_name = os.path.split(frame_path)
frame_dir = frame_dir or os.getcwd()
if not frame_name:
print("error no frame name")
return
elif not os.path.exists(frame_path):
print(f"frame path {frame_path} does not exist")
return
detections_name = frame_name
detections_dir = os.path.join(frame_dir, "detect", "")
detections_path = os.path.join(detections_dir, detections_name)
Path(detections_dir).mkdir(parents=True, exist_ok=True)
frame = cv2.imread(frame_path) # image is in BGR format
# Getting prediction using model
object_prediction_list = get_sliced_prediction(
frame,
sahi_model,
slice_height=640,
slice_width=640,
overlap_height_ratio=0.2,
overlap_width_ratio=0.2,
perform_standard_pred=False,
verbose=0,
)
detections = Detections.from_sahi_batched(object_prediction_list)
annotate_frame(frame, detections=detections)
cv2.imwrite(detections_path, frame)
return detections_path
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--model', help='Path to the model to use', type=str, default="latest")
parser.add_argument('--video-frames', help='Max number of video frames to detect', type=int)
parser.add_argument('--folder', help='Folder to detect', type=str)
parser.add_argument('--wandb', help='Log to wandb', action='store_true')
parser.add_argument('remainder', nargs=argparse.REMAINDER, help='All other arguments')
args = parser.parse_args()
video_list = []
frame_list = []
if args.folder:
for file in os.listdir(args.folder):
if file.endswith(".mp4"):
video_list.append(os.path.join(args.folder, file))
elif file.endswith((".jpg", ".png", ".jpeg", ".tiff")):
frame_list.append(os.path.join(args.folder, file))
if args.remainder:
for file in args.remainder:
if file.endswith(".mp4"):
video_list.append(file)
elif file.endswith((".jpg", ".png", ".jpeg", ".tiff")):
frame_list.append(file)
if args.wandb:
logged_in = wandb.login(timeout=1)
assert(logged_in)
#wandb.setup().settings.update(mode="online", login_timeout=None)
run = wandb.init(project = "YOLOv8", job_type="detect", config = {})
detections_artifact = wandb.Artifact(f"vacocam_detect", "detect")
else:
run = None
detections_artifact = None
if os.path.exists(args.model):
model_path = args.model
else:
artifact, artifact_location = download_artifact(f"vacocam_model:{args.model}", run=run)
model_path = Path(artifact_location) / "best.pt"
for video_path in video_list:
detections_path = detect_video(video_path, model_path, args.video_frames)
if detections_artifact is not None and detections_path is not None:
detections_artifact.add_file(detections_path)
for frame_path in frame_list:
detections_path = detect_frame(frame_path, model_path)
if detections_artifact is not None and detections_path is not None:
detections_artifact.add_file(detections_path)
if run is not None:
if detections_artifact is not None:
run.log_artifact(detections_artifact)
run.finish()
print("finnyssh detect")