From d5289b54c49cb0690cafcf48a3f91da388b6f23c Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Thu, 17 Dec 2020 17:20:20 -0800 Subject: [PATCH] clean_str() function addition (#1674) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * clean_str() function addition * cleanup * add euro symbol € * add closing exclamation (spanish) * cleanup --- detect.py | 9 +++++---- utils/datasets.py | 4 ++-- utils/general.py | 7 ++++++- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/detect.py b/detect.py index 5935367d3..45dbdb7fa 100644 --- a/detect.py +++ b/detect.py @@ -81,12 +81,13 @@ def detect(save_img=False): # Process detections for i, det in enumerate(pred): # detections per image if webcam: # batch_size >= 1 - p, s, im0, frame = Path(path[i]), '%g: ' % i, im0s[i].copy(), dataset.count + p, s, im0, frame = path[i], '%g: ' % i, im0s[i].copy(), dataset.count else: - p, s, im0, frame = Path(path), '', im0s, getattr(dataset, 'frame', 0) + p, s, im0, frame = path, '', im0s, getattr(dataset, 'frame', 0) - save_path = str(save_dir / p.name) - txt_path = str(save_dir / 'labels' / p.stem) + ('' if dataset.mode == 'image' else f'_{frame}') + p = Path(p) # to Path + save_path = str(save_dir / p.name) # img.jpg + txt_path = str(save_dir / 'labels' / p.stem) + ('' if dataset.mode == 'image' else f'_{frame}') # img.txt s += '%gx%g ' % img.shape[2:] # print string gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh if len(det): diff --git a/utils/datasets.py b/utils/datasets.py index 7d10d1c0d..e2b139fa6 100755 --- a/utils/datasets.py +++ b/utils/datasets.py @@ -19,7 +19,7 @@ from PIL import Image, ExifTags from torch.utils.data import Dataset from tqdm import tqdm -from utils.general import xyxy2xywh, xywh2xyxy +from utils.general import xyxy2xywh, xywh2xyxy, clean_str from utils.torch_utils import torch_distributed_zero_first # Parameters @@ -267,7 +267,7 @@ class LoadStreams: # multiple IP or RTSP cameras n = len(sources) self.imgs = [None] * n - self.sources = sources + self.sources = [clean_str(x) for x in sources] # clean source names for later for i, s in enumerate(sources): # Start the thread to read frames from the video stream print('%g/%g: %s... ' % (i + 1, n, s), end='') diff --git a/utils/general.py b/utils/general.py index 1ea7d40de..12249059c 100755 --- a/utils/general.py +++ b/utils/general.py @@ -2,6 +2,7 @@ import glob import logging +import math import os import platform import random @@ -11,7 +12,6 @@ import time from pathlib import Path import cv2 -import math import numpy as np import torch import torchvision @@ -97,6 +97,11 @@ def make_divisible(x, divisor): return math.ceil(x / divisor) * divisor +def clean_str(s): + # Cleans a string by replacing special characters with underscore _ + return re.sub(pattern="[|@#!¡·$€%&()=?¿^*;:,¨´><+]", repl="_", string=s) + + def labels_to_class_weights(labels, nc=80): # Get class weights (inverse frequency) from training labels if labels[0] is None: # no labels loaded