From e37f51d022a1ee16c8c2f37897c444113373bc27 Mon Sep 17 00:00:00 2001 From: oakhtar147 Date: Fri, 18 Sep 2020 16:24:11 +0500 Subject: [PATCH 1/8] add contrast to conf/class against bbox rectangle color --- utils/general.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/general.py b/utils/general.py index b3253b112dec..5305c3abc4fb 100755 --- a/utils/general.py +++ b/utils/general.py @@ -985,7 +985,7 @@ def plot_one_box(x, img, color=None, label=None, line_thickness=None): t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled - cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA) + cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [(255 - c) for c in color], thickness=tf, lineType=cv2.LINE_AA) # add contrast of conf/class with bbox rectangle def plot_wh_methods(): # from utils.general import *; plot_wh_methods() From bc16152fccf7860f68c1b822cbfdb3e1e74c0e6e Mon Sep 17 00:00:00 2001 From: oakhtar147 Date: Fri, 18 Sep 2020 20:48:50 +0500 Subject: [PATCH 2/8] added feature to save conf in txt labels --- detect.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/detect.py b/detect.py index fd2c14d1ba2e..7072aac17ab7 100644 --- a/detect.py +++ b/detect.py @@ -19,8 +19,8 @@ def detect(save_img=False): - out, source, weights, view_img, save_txt, imgsz = \ - opt.output, opt.source, opt.weights, opt.view_img, opt.save_txt, opt.img_size + out, source, weights, view_img, save_txt, imgsz, save_conf = \ + opt.output, opt.source, opt.weights, opt.view_img, opt.save_txt, opt.img_size, opt.save_conf webcam = source.isnumeric() or source.startswith('rtsp') or source.startswith('http') or source.endswith('.txt') # Initialize @@ -106,7 +106,10 @@ def detect(save_img=False): if save_txt: # Write to file xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh with open(txt_path + '.txt', 'a') as f: - f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format + if save_conf: + f.write(('%g ' * 6 + '\n') % (cls, conf, *xywh)) # label format includes conf + else: + f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format does not include conf if save_img or view_img: # Add bbox to image label = '%s %.2f' % (names[int(cls)], conf) @@ -161,6 +164,7 @@ def detect(save_img=False): parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS') parser.add_argument('--augment', action='store_true', help='augmented inference') parser.add_argument('--update', action='store_true', help='update all models') + parser.add_argument('--save-conf', action='store_true', help='put confidence score next to class in label*.txt') opt = parser.parse_args() print(opt) From 5c9815268a9ce69641093378630dfdea5c19e92a Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Tue, 20 Oct 2020 13:19:34 +0200 Subject: [PATCH 3/8] Update general.py --- utils/general.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/general.py b/utils/general.py index 5305c3abc4fb..b3253b112dec 100755 --- a/utils/general.py +++ b/utils/general.py @@ -985,7 +985,7 @@ def plot_one_box(x, img, color=None, label=None, line_thickness=None): t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled - cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [(255 - c) for c in color], thickness=tf, lineType=cv2.LINE_AA) # add contrast of conf/class with bbox rectangle + cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA) def plot_wh_methods(): # from utils.general import *; plot_wh_methods() From f81d5c305802fb1d196e56ef5fdfaf470e478508 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Tue, 20 Oct 2020 13:20:29 +0200 Subject: [PATCH 4/8] Update detect.py --- detect.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detect.py b/detect.py index 7072aac17ab7..71669a5b651a 100644 --- a/detect.py +++ b/detect.py @@ -164,7 +164,7 @@ def detect(save_img=False): parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS') parser.add_argument('--augment', action='store_true', help='augmented inference') parser.add_argument('--update', action='store_true', help='update all models') - parser.add_argument('--save-conf', action='store_true', help='put confidence score next to class in label*.txt') + parser.add_argument('--save-conf', action='store_true', help='output confidences in --save-txt labels') opt = parser.parse_args() print(opt) From ae9c5d42d54d34cafb94f97b069cc0ae4513d0d2 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Tue, 20 Oct 2020 13:20:51 +0200 Subject: [PATCH 5/8] Update detect.py --- detect.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detect.py b/detect.py index 71669a5b651a..ad10c3a94e90 100644 --- a/detect.py +++ b/detect.py @@ -160,11 +160,11 @@ def detect(save_img=False): parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') parser.add_argument('--view-img', action='store_true', help='display results') parser.add_argument('--save-txt', action='store_true', help='save results to *.txt') + parser.add_argument('--save-conf', action='store_true', help='output confidences in --save-txt labels') parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --class 0, or --class 0 2 3') parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS') parser.add_argument('--augment', action='store_true', help='augmented inference') parser.add_argument('--update', action='store_true', help='update all models') - parser.add_argument('--save-conf', action='store_true', help='output confidences in --save-txt labels') opt = parser.parse_args() print(opt) From 1ff288ed653f8d7d37e1c458972825accd83c92b Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Tue, 20 Oct 2020 13:34:35 +0200 Subject: [PATCH 6/8] Update detect.py save_conf variable removed as only used once, so we can use opt.save_conf instead. --- detect.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/detect.py b/detect.py index b2b88f1b722f..b21a2368e96a 100644 --- a/detect.py +++ b/detect.py @@ -18,8 +18,8 @@ def detect(save_img=False): - out, source, weights, view_img, save_txt, imgsz, save_conf = \ - opt.output, opt.source, opt.weights, opt.view_img, opt.save_txt, opt.img_size, opt.save_conf + out, source, weights, view_img, save_txt, imgsz = \ + opt.output, opt.source, opt.weights, opt.view_img, opt.save_txt, opt.img_size webcam = source.isnumeric() or source.startswith(('rtsp://', 'rtmp://', 'http://')) or source.endswith('.txt') # Initialize @@ -104,12 +104,10 @@ def detect(save_img=False): for *xyxy, conf, cls in reversed(det): if save_txt: # Write to file xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh + line = (cls, conf, *xywh) if opt.save_conf else (cls, *xywh) # label format with open(txt_path + '.txt', 'a') as f: - if save_conf: - f.write(('%g ' * 6 + '\n') % (cls, conf, *xywh)) # label format includes conf - else: - f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format does not include conf - + f.write(('%g ' * len(line) + '\n') % line) + if save_img or view_img: # Add bbox to image label = '%s %.2f' % (names[int(cls)], conf) plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3) From e54303ca15c414e623d7521a48f6ec57f703e345 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Tue, 20 Oct 2020 13:35:08 +0200 Subject: [PATCH 7/8] Update detect.py --- detect.py | 1 - 1 file changed, 1 deletion(-) diff --git a/detect.py b/detect.py index b21a2368e96a..10d3a2c53d6d 100644 --- a/detect.py +++ b/detect.py @@ -107,7 +107,6 @@ def detect(save_img=False): line = (cls, conf, *xywh) if opt.save_conf else (cls, *xywh) # label format with open(txt_path + '.txt', 'a') as f: f.write(('%g ' * len(line) + '\n') % line) - if save_img or view_img: # Add bbox to image label = '%s %.2f' % (names[int(cls)], conf) plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3) From 5be93325719ca7baa08cd3b9759bc0375a53a3cd Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Tue, 20 Oct 2020 13:37:24 +0200 Subject: [PATCH 8/8] Update detect.py --- detect.py | 1 + 1 file changed, 1 insertion(+) diff --git a/detect.py b/detect.py index 10d3a2c53d6d..807dee3cce35 100644 --- a/detect.py +++ b/detect.py @@ -107,6 +107,7 @@ def detect(save_img=False): line = (cls, conf, *xywh) if opt.save_conf else (cls, *xywh) # label format with open(txt_path + '.txt', 'a') as f: f.write(('%g ' * len(line) + '\n') % line) + if save_img or view_img: # Add bbox to image label = '%s %.2f' % (names[int(cls)], conf) plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)