Browse Source

Auto-UTF handling (#4594)

modifyDataloader
Glenn Jocher GitHub 3 years ago
parent
commit
e5e5ebc799
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 8 deletions
  1. +4
    -3
      detect.py
  2. +5
    -4
      models/common.py
  3. +1
    -1
      utils/general.py

+ 4
- 3
detect.py View File



from models.experimental import attempt_load from models.experimental import attempt_load
from utils.datasets import LoadStreams, LoadImages from utils.datasets import LoadStreams, LoadImages
from utils.general import check_img_size, check_requirements, check_imshow, colorstr, non_max_suppression, \
from utils.general import check_img_size, check_requirements, check_imshow, colorstr, is_ascii, non_max_suppression, \
apply_classifier, scale_coords, xyxy2xywh, strip_optimizer, set_logging, increment_path, save_one_box apply_classifier, scale_coords, xyxy2xywh, strip_optimizer, set_logging, increment_path, save_one_box
from utils.plots import colors, Annotator
from utils.plots import Annotator, colors
from utils.torch_utils import select_device, load_classifier, time_sync from utils.torch_utils import select_device, load_classifier, time_sync




output_details = interpreter.get_output_details() # outputs output_details = interpreter.get_output_details() # outputs
int8 = input_details[0]['dtype'] == np.uint8 # is TFLite quantized uint8 model int8 = input_details[0]['dtype'] == np.uint8 # is TFLite quantized uint8 model
imgsz = check_img_size(imgsz, s=stride) # check image size imgsz = check_img_size(imgsz, s=stride) # check image size
ascii = is_ascii(names) # names are ascii (use PIL for UTF-8)


# Dataloader # Dataloader
if webcam: if webcam:
s += '%gx%g ' % img.shape[2:] # print string s += '%gx%g ' % img.shape[2:] # print string
gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh
imc = im0.copy() if save_crop else im0 # for save_crop imc = im0.copy() if save_crop else im0 # for save_crop
annotator = Annotator(im0, line_width=line_thickness, pil=False)
annotator = Annotator(im0, line_width=line_thickness, pil=not ascii)
if len(det): if len(det):
# Rescale boxes from img_size to im0 size # Rescale boxes from img_size to im0 size
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round()

+ 5
- 4
models/common.py View File

from torch.cuda import amp from torch.cuda import amp


from utils.datasets import exif_transpose, letterbox from utils.datasets import exif_transpose, letterbox
from utils.general import colorstr, non_max_suppression, make_divisible, scale_coords, increment_path, xyxy2xywh, \
save_one_box
from utils.plots import colors, Annotator
from utils.general import colorstr, increment_path, is_ascii, make_divisible, non_max_suppression, save_one_box, \
scale_coords, xyxy2xywh
from utils.plots import Annotator, colors
from utils.torch_utils import time_sync from utils.torch_utils import time_sync


LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
self.imgs = imgs # list of images as numpy arrays self.imgs = imgs # list of images as numpy arrays
self.pred = pred # list of tensors pred[0] = (xyxy, conf, cls) self.pred = pred # list of tensors pred[0] = (xyxy, conf, cls)
self.names = names # class names self.names = names # class names
self.ascii = is_ascii(names) # names are ascii (use PIL for UTF-8)
self.files = files # image filenames self.files = files # image filenames
self.xyxy = pred # xyxy pixels self.xyxy = pred # xyxy pixels
self.xywh = [xyxy2xywh(x) for x in pred] # xywh pixels self.xywh = [xyxy2xywh(x) for x in pred] # xywh pixels
n = (pred[:, -1] == c).sum() # detections per class n = (pred[:, -1] == c).sum() # detections per class
str += f"{n} {self.names[int(c)]}{'s' * (n > 1)}, " # add to string str += f"{n} {self.names[int(c)]}{'s' * (n > 1)}, " # add to string
if show or save or render or crop: if show or save or render or crop:
annotator = Annotator(im, pil=False)
annotator = Annotator(im, pil=not self.ascii)
for *box, conf, cls in reversed(pred): # xyxy, confidence, class for *box, conf, cls in reversed(pred): # xyxy, confidence, class
label = f'{self.names[int(cls)]} {conf:.2f}' label = f'{self.names[int(cls)]} {conf:.2f}'
if crop: if crop:

+ 1
- 1
utils/general.py View File



def is_ascii(s=''): def is_ascii(s=''):
# Is string composed of all ASCII (no UTF) characters? # Is string composed of all ASCII (no UTF) characters?
s = str(s) # convert to str() in case of None, etc.
s = str(s) # convert list, tuple, None, etc. to str
return len(s.encode().decode('ascii', 'ignore')) == len(s) return len(s.encode().decode('ascii', 'ignore')) == len(s)





Loading…
Cancel
Save