Browse Source

Fix `LoadStreams()` dataloader frame skip issue (#3833)

* Update datasets.py to read every 4th frame of streams

* Update datasets.py

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
modifyDataloader
Feras Oughali GitHub 3 years ago
parent
commit
7d6af69638
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 24 deletions
  1. +7
    -24
      utils/datasets.py

+ 7
- 24
utils/datasets.py View File

import hashlib import hashlib
import json import json
import logging import logging
import math
import os import os
import random import random
import shutil import shutil
from threading import Thread from threading import Thread


import cv2 import cv2
import math
import numpy as np import numpy as np
import torch import torch
import torch.nn.functional as F import torch.nn.functional as F
def __init__(self, pipe='0', img_size=640, stride=32): def __init__(self, pipe='0', img_size=640, stride=32):
self.img_size = img_size self.img_size = img_size
self.stride = stride self.stride = stride

if pipe.isnumeric():
pipe = eval(pipe) # local camera
# pipe = 'rtsp://192.168.1.64/1' # IP camera
# pipe = 'rtsp://username:password@192.168.1.64/1' # IP camera with login
# pipe = 'http://wmccpinetop.axiscam.net/mjpg/video.mjpg' # IP golf camera

self.pipe = pipe
self.cap = cv2.VideoCapture(pipe) # video capture object
self.pipe = eval(pipe) if pipe.isnumeric() else pipe
self.cap = cv2.VideoCapture(self.pipe) # video capture object
self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 3) # set buffer size self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 3) # set buffer size


def __iter__(self): def __iter__(self):
raise StopIteration raise StopIteration


# Read frame # Read frame
if self.pipe == 0: # local camera
ret_val, img0 = self.cap.read()
img0 = cv2.flip(img0, 1) # flip left-right
else: # IP camera
n = 0
while True:
n += 1
self.cap.grab()
if n % 30 == 0: # skip frames
ret_val, img0 = self.cap.retrieve()
if ret_val:
break
ret_val, img0 = self.cap.read()
img0 = cv2.flip(img0, 1) # flip left-right


# Print # Print
assert ret_val, f'Camera Error {self.pipe}' assert ret_val, f'Camera Error {self.pipe}'


def update(self, i, cap): def update(self, i, cap):
# Read stream `i` frames in daemon thread # Read stream `i` frames in daemon thread
n, f = 0, self.frames[i]
n, f, read = 0, self.frames[i], 1 # frame number, frame array, inference every 'read' frame
while cap.isOpened() and n < f: while cap.isOpened() and n < f:
n += 1 n += 1
# _, self.imgs[index] = cap.read() # _, self.imgs[index] = cap.read()
cap.grab() cap.grab()
if n % 4: # read every 4th frame
if n % read == 0:
success, im = cap.retrieve() success, im = cap.retrieve()
self.imgs[i] = im if success else self.imgs[i] * 0 self.imgs[i] = im if success else self.imgs[i] * 0
time.sleep(1 / self.fps[i]) # wait time time.sleep(1 / self.fps[i]) # wait time

Loading…
Cancel
Save