Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize-for-streams-detection #2538

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


def detect(save_img=False):
source, weights, view_img, save_txt, imgsz = opt.source, opt.weights, opt.view_img, opt.save_txt, opt.img_size
source, weights, view_img, save_txt, imgsz, skip_step = opt.source, opt.weights, opt.view_img, opt.save_txt, opt.img_size, opt.skip_step
webcam = source.isnumeric() or source.endswith('.txt') or source.lower().startswith(
('rtsp://', 'rtmp://', 'http://'))

Expand Down Expand Up @@ -47,7 +47,7 @@ def detect(save_img=False):
if webcam:
view_img = check_imshow()
cudnn.benchmark = True # set True to speed up constant image size inference
dataset = LoadStreams(source, img_size=imgsz, stride=stride)
dataset = LoadStreams(source, img_size=imgsz, stride=stride, skip_step=skip_step)
else:
save_img = True
dataset = LoadImages(source, img_size=imgsz, stride=stride)
Expand All @@ -60,7 +60,15 @@ def detect(save_img=False):
if device.type != 'cpu':
model(torch.zeros(1, 3, imgsz, imgsz).to(device).type_as(next(model.parameters()))) # run once
t0 = time.time()

prev_img = [None] * dataset.count if webcam else [None]
for path, img, im0s, vid_cap in dataset:
# Optimize repeated img for steams
if webcam:
if (prev_img == img).all():
continue
prev_img = img

img = torch.from_numpy(img).to(device)
img = img.half() if half else img.float() # uint8 to fp16/32
img /= 255.0 # 0 - 255 to 0.0 - 1.0
Expand Down Expand Up @@ -149,6 +157,7 @@ def detect(save_img=False):
parser.add_argument('--weights', nargs='+', type=str, default='yolov5s.pt', help='model.pt path(s)')
parser.add_argument('--source', type=str, default='data/images', help='source') # file/folder, 0 for webcam
parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
parser.add_argument('--skip-step', type=int, default=4, help='default read every 4th frame for streams')
parser.add_argument('--conf-thres', type=float, default=0.25, help='object confidence threshold')
parser.add_argument('--iou-thres', type=float, default=0.45, help='IOU threshold for NMS')
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
Expand Down
10 changes: 6 additions & 4 deletions utils/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,11 @@ def __len__(self):


class LoadStreams: # multiple IP or RTSP cameras
def __init__(self, sources='streams.txt', img_size=640, stride=32):
def __init__(self, sources='streams.txt', img_size=640, stride=32, skip_step=4):
self.mode = 'stream'
self.img_size = img_size
self.stride = stride
self.skip_step = skip_step

if os.path.isfile(sources):
with open(sources, 'r') as f:
Expand All @@ -270,6 +271,7 @@ def __init__(self, sources='streams.txt', img_size=640, stride=32):
sources = [sources]

n = len(sources)
self.count = n
self.imgs = [None] * n
self.sources = [clean_str(x) for x in sources] # clean source names for later
for i, s in enumerate(sources):
Expand All @@ -281,7 +283,7 @@ def __init__(self, sources='streams.txt', img_size=640, stride=32):
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS) % 100
_, self.imgs[i] = cap.read() # guarantee first frame
thread = Thread(target=self.update, args=([i, cap]), daemon=True)
thread = Thread(target=self.update, args=([i, cap, self.skip_step]), daemon=True)
print(f' success ({w}x{h} at {fps:.2f} FPS).')
thread.start()
print('') # newline
Expand All @@ -292,14 +294,14 @@ def __init__(self, sources='streams.txt', img_size=640, stride=32):
if not self.rect:
print('WARNING: Different stream shapes detected. For optimal performance supply similarly-shaped streams.')

def update(self, index, cap):
def update(self, index, cap, skip_step):
# Read next stream frame in a daemon thread
n = 0
while cap.isOpened():
n += 1
# _, self.imgs[index] = cap.read()
cap.grab()
if n == 4: # read every 4th frame
if n == skip_step: # read every {skip_step}th frame
success, im = cap.retrieve()
self.imgs[index] = im if success else self.imgs[index] * 0
n = 0
Expand Down