Utils reorganization (#1392)
* Utils reorganization * Add new utils files * cleanup * simplify * reduce datasets.py * remove evolve.sh * loadWebcam cleanup
This commit is contained in:
1 parent
379396e896
commit
fe341fa44d
14 files changed
+890
-988
No files matched your search
+12
-63
@@ -1,3 +1,5 @@
|
||||
# Dataset utils and dataloaders
|
||||
|
||||
import glob
|
||||
import math
|
||||
import os
|
||||
@@ -16,8 +18,10 @@ from PIL import Image, ExifTags
|
||||
from torch.utils.data import Dataset
|
||||
from tqdm import tqdm
|
||||
|
||||
from utils.general import xyxy2xywh, xywh2xyxy, torch_distributed_zero_first
|
||||
from utils.general import xyxy2xywh, xywh2xyxy
|
||||
from utils.torch_utils import torch_distributed_zero_first
|
||||
|
||||
# Parameters
|
||||
help_url = 'https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data'
|
||||
img_formats = ['.bmp', '.jpg', '.jpeg', '.png', '.tif', '.tiff', '.dng']
|
||||
vid_formats = ['.mov', '.avi', '.mp4', '.mpg', '.mpeg', '.m4v', '.wmv', '.mkv']
|
||||
@@ -50,7 +54,7 @@ def exif_size(img):
|
||||
|
||||
def create_dataloader(path, imgsz, batch_size, stride, opt, hyp=None, augment=False, cache=False, pad=0.0, rect=False,
|
||||
rank=-1, world_size=1, workers=8):
|
||||
# Make sure only the first process in DDP process the dataset first, and the following others can use the cache.
|
||||
# Make sure only the first process in DDP process the dataset first, and the following others can use the cache
|
||||
with torch_distributed_zero_first(rank):
|
||||
dataset = LoadImagesAndLabels(path, imgsz, batch_size,
|
||||
augment=augment, # augment images
|
||||
@@ -75,9 +79,9 @@ def create_dataloader(path, imgsz, batch_size, stride, opt, hyp=None, augment=Fa
|
||||
|
||||
|
||||
class InfiniteDataLoader(torch.utils.data.dataloader.DataLoader):
|
||||
""" Dataloader that reuses workers.
|
||||
""" Dataloader that reuses workers
|
||||
|
||||
Uses same syntax as vanilla DataLoader.
|
||||
Uses same syntax as vanilla DataLoader
|
||||
"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
@@ -94,7 +98,7 @@ class InfiniteDataLoader(torch.utils.data.dataloader.DataLoader):
|
||||
|
||||
|
||||
class _RepeatSampler(object):
|
||||
""" Sampler that repeats forever.
|
||||
""" Sampler that repeats forever
|
||||
|
||||
Args:
|
||||
sampler (Sampler)
|
||||
@@ -177,7 +181,6 @@ class LoadImages: # for inference
|
||||
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
|
||||
img = np.ascontiguousarray(img)
|
||||
|
||||
# cv2.imwrite(path + '.letterbox.jpg', 255 * img.transpose((1, 2, 0))[:, :, ::-1]) # save letterbox image
|
||||
return path, img, img0, self.cap
|
||||
|
||||
def new_video(self, path):
|
||||
@@ -190,23 +193,15 @@ class LoadImages: # for inference
|
||||
|
||||
|
||||
class LoadWebcam: # for inference
|
||||
def __init__(self, pipe=0, img_size=640):
|
||||
def __init__(self, pipe='0', img_size=640):
|
||||
self.img_size = img_size
|
||||
|
||||
if pipe == '0':
|
||||
pipe = 0 # local camera
|
||||
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 = 'rtsp://170.93.143.139/rtplive/470011e600ef003a004ee33696235daa' # IP traffic camera
|
||||
# pipe = 'http://wmccpinetop.axiscam.net/mjpg/video.mjpg' # IP golf camera
|
||||
|
||||
# https://answers.opencv.org/question/215996/changing-gstreamer-pipeline-to-opencv-in-pythonsolved/
|
||||
# pipe = '"rtspsrc location="rtsp://username:password@192.168.1.64/1" latency=10 ! appsink' # GStreamer
|
||||
|
||||
# https://answers.opencv.org/question/200787/video-acceleration-gstremer-pipeline-in-videocapture/
|
||||
# https://stackoverflow.com/questions/54095699/install-gstreamer-support-for-opencv-python-package # install help
|
||||
# pipe = "rtspsrc location=rtsp://root:root@192.168.0.91:554/axis-media/media.amp?videocodec=h264&resolution=3840x2160 protocols=GST_RTSP_LOWER_TRANS_TCP ! rtph264depay ! queue ! vaapih264dec ! videoconvert ! appsink" # GStreamer
|
||||
|
||||
self.pipe = pipe
|
||||
self.cap = cv2.VideoCapture(pipe) # video capture object
|
||||
self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 3) # set buffer size
|
||||
@@ -895,52 +890,6 @@ def cutout(image, labels):
|
||||
return labels
|
||||
|
||||
|
||||
def reduce_img_size(path='path/images', img_size=1024): # from utils.datasets import *; reduce_img_size()
|
||||
# creates a new ./images_reduced folder with reduced size images of maximum size img_size
|
||||
path_new = path + '_reduced' # reduced images path
|
||||
create_folder(path_new)
|
||||
for f in tqdm(glob.glob('%s/*.*' % path)):
|
||||
try:
|
||||
img = cv2.imread(f)
|
||||
h, w = img.shape[:2]
|
||||
r = img_size / max(h, w) # size ratio
|
||||
if r < 1.0:
|
||||
img = cv2.resize(img, (int(w * r), int(h * r)), interpolation=cv2.INTER_AREA) # _LINEAR fastest
|
||||
fnew = f.replace(path, path_new) # .replace(Path(f).suffix, '.jpg')
|
||||
cv2.imwrite(fnew, img)
|
||||
except:
|
||||
print('WARNING: image failure %s' % f)
|
||||
|
||||
|
||||
def recursive_dataset2bmp(dataset='path/dataset_bmp'): # from utils.datasets import *; recursive_dataset2bmp()
|
||||
# Converts dataset to bmp (for faster training)
|
||||
formats = [x.lower() for x in img_formats] + [x.upper() for x in img_formats]
|
||||
for a, b, files in os.walk(dataset):
|
||||
for file in tqdm(files, desc=a):
|
||||
p = a + '/' + file
|
||||
s = Path(file).suffix
|
||||
if s == '.txt': # replace text
|
||||
with open(p, 'r') as f:
|
||||
lines = f.read()
|
||||
for f in formats:
|
||||
lines = lines.replace(f, '.bmp')
|
||||
with open(p, 'w') as f:
|
||||
f.write(lines)
|
||||
elif s in formats: # replace image
|
||||
cv2.imwrite(p.replace(s, '.bmp'), cv2.imread(p))
|
||||
if s != '.bmp':
|
||||
os.system("rm '%s'" % p)
|
||||
|
||||
|
||||
def imagelist2folder(path='path/images.txt'): # from utils.datasets import *; imagelist2folder()
|
||||
# Copies all the images in a text file (list of images) into a folder
|
||||
create_folder(path[:-4])
|
||||
with open(path, 'r') as f:
|
||||
for line in f.read().splitlines():
|
||||
os.system('cp "%s" %s' % (line, path[:-4]))
|
||||
print(line)
|
||||
|
||||
|
||||
def create_folder(path='./new'):
|
||||
# Create folder
|
||||
if os.path.exists(path):
|
||||
|
||||
Reference in new issue
Block a user