updates
This commit is contained in:
parent
360a32811c
commit
e926afd02b
66
detect.py
66
detect.py
|
@ -7,36 +7,30 @@ from utils.datasets import *
|
||||||
from utils.utils import *
|
from utils.utils import *
|
||||||
|
|
||||||
|
|
||||||
def detect(cfg,
|
def detect(save_txt=False,
|
||||||
data,
|
|
||||||
weights,
|
|
||||||
images='data/samples', # input folder
|
|
||||||
output='output', # output folder
|
|
||||||
fourcc='mp4v', # video codec
|
|
||||||
img_size=416,
|
|
||||||
conf_thres=0.5,
|
|
||||||
nms_thres=0.5,
|
|
||||||
save_txt=False,
|
|
||||||
save_images=True):
|
save_images=True):
|
||||||
|
out = opt.output
|
||||||
|
img_size = opt.img_size
|
||||||
|
|
||||||
# Initialize
|
# Initialize
|
||||||
device = torch_utils.select_device(force_cpu=ONNX_EXPORT)
|
device = torch_utils.select_device(force_cpu=ONNX_EXPORT)
|
||||||
torch.backends.cudnn.benchmark = False # set False for reproducible results
|
torch.backends.cudnn.benchmark = False # set False for reproducible results
|
||||||
if os.path.exists(output):
|
if os.path.exists(out):
|
||||||
shutil.rmtree(output) # delete output folder
|
shutil.rmtree(out) # delete output folder
|
||||||
os.makedirs(output) # make new output folder
|
os.makedirs(out) # make new output folder
|
||||||
|
|
||||||
# Initialize model
|
# Initialize model
|
||||||
if ONNX_EXPORT:
|
if ONNX_EXPORT:
|
||||||
s = (320, 192) # (320, 192) or (416, 256) or (608, 352) onnx model image size (height, width)
|
s = (320, 192) # (320, 192) or (416, 256) or (608, 352) onnx model image size (height, width)
|
||||||
model = Darknet(cfg, s)
|
model = Darknet(opt.cfg, s)
|
||||||
else:
|
else:
|
||||||
model = Darknet(cfg, img_size)
|
model = Darknet(opt.cfg, img_size)
|
||||||
|
|
||||||
# Load weights
|
# Load weights
|
||||||
if weights.endswith('.pt'): # pytorch format
|
if opt.weights.endswith('.pt'): # pytorch format
|
||||||
model.load_state_dict(torch.load(weights, map_location=device)['model'])
|
model.load_state_dict(torch.load(opt.weights, map_location=device)['model'])
|
||||||
else: # darknet format
|
else: # darknet format
|
||||||
_ = load_darknet_weights(model, weights)
|
_ = load_darknet_weights(model, opt.weights)
|
||||||
|
|
||||||
# Fuse Conv2d + BatchNorm2d layers
|
# Fuse Conv2d + BatchNorm2d layers
|
||||||
# model.fuse()
|
# model.fuse()
|
||||||
|
@ -61,22 +55,22 @@ def detect(cfg,
|
||||||
save_images = False
|
save_images = False
|
||||||
dataloader = LoadWebcam(img_size=img_size, half=opt.half)
|
dataloader = LoadWebcam(img_size=img_size, half=opt.half)
|
||||||
else:
|
else:
|
||||||
dataloader = LoadImages(images, img_size=img_size, half=opt.half)
|
dataloader = LoadImages(opt.input, img_size=img_size, half=opt.half)
|
||||||
|
|
||||||
# Get classes and colors
|
# Get classes and colors
|
||||||
classes = load_classes(parse_data_cfg(data)['names'])
|
classes = load_classes(parse_data_cfg(opt.data)['names'])
|
||||||
colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(classes))]
|
colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(classes))]
|
||||||
|
|
||||||
# Run inference
|
# Run inference
|
||||||
t0 = time.time()
|
t0 = time.time()
|
||||||
for i, (path, img, im0, vid_cap) in enumerate(dataloader):
|
for path, img, im0, vid_cap in dataloader:
|
||||||
t = time.time()
|
t = time.time()
|
||||||
save_path = str(Path(output) / Path(path).name)
|
save_path = str(Path(out) / Path(path).name)
|
||||||
|
|
||||||
# Get detections
|
# Get detections
|
||||||
img = torch.from_numpy(img).unsqueeze(0).to(device)
|
img = torch.from_numpy(img).unsqueeze(0).to(device)
|
||||||
pred, _ = model(img)
|
pred, _ = model(img)
|
||||||
det = non_max_suppression(pred.float(), conf_thres, nms_thres)[0]
|
det = non_max_suppression(pred.float(), opt.conf_thres, opt.nms_thres)[0]
|
||||||
|
|
||||||
if det is not None and len(det) > 0:
|
if det is not None and len(det) > 0:
|
||||||
# Rescale boxes from 416 to true image size
|
# Rescale boxes from 416 to true image size
|
||||||
|
@ -101,7 +95,7 @@ def detect(cfg,
|
||||||
print('Done. (%.3fs)' % (time.time() - t))
|
print('Done. (%.3fs)' % (time.time() - t))
|
||||||
|
|
||||||
if opt.webcam: # Show live webcam
|
if opt.webcam: # Show live webcam
|
||||||
cv2.imshow(weights, im0)
|
cv2.imshow(opt.weights, im0)
|
||||||
|
|
||||||
if save_images: # Save image with detections
|
if save_images: # Save image with detections
|
||||||
if dataloader.mode == 'images':
|
if dataloader.mode == 'images':
|
||||||
|
@ -115,13 +109,13 @@ def detect(cfg,
|
||||||
fps = vid_cap.get(cv2.CAP_PROP_FPS)
|
fps = vid_cap.get(cv2.CAP_PROP_FPS)
|
||||||
width = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
width = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
||||||
height = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
height = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
||||||
vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*fourcc), fps, (width, height))
|
vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*opt.fourcc), fps, (width, height))
|
||||||
vid_writer.write(im0)
|
vid_writer.write(im0)
|
||||||
|
|
||||||
if save_images:
|
if save_images:
|
||||||
print('Results saved to %s' % os.getcwd() + os.sep + output)
|
print('Results saved to %s' % os.getcwd() + os.sep + out)
|
||||||
if platform == 'darwin': # macos
|
if platform == 'darwin': # MacOS
|
||||||
os.system('open ' + output + ' ' + save_path)
|
os.system('open ' + out + ' ' + save_path)
|
||||||
|
|
||||||
print('Done. (%.3fs)' % (time.time() - t0))
|
print('Done. (%.3fs)' % (time.time() - t0))
|
||||||
|
|
||||||
|
@ -131,24 +125,16 @@ if __name__ == '__main__':
|
||||||
parser.add_argument('--cfg', type=str, default='cfg/yolov3-spp.cfg', help='cfg file path')
|
parser.add_argument('--cfg', type=str, default='cfg/yolov3-spp.cfg', help='cfg file path')
|
||||||
parser.add_argument('--data', type=str, default='data/coco.data', help='coco.data file path')
|
parser.add_argument('--data', type=str, default='data/coco.data', help='coco.data file path')
|
||||||
parser.add_argument('--weights', type=str, default='weights/yolov3-spp.weights', help='path to weights file')
|
parser.add_argument('--weights', type=str, default='weights/yolov3-spp.weights', help='path to weights file')
|
||||||
parser.add_argument('--images', type=str, default='data/samples', help='path to images')
|
parser.add_argument('--input', type=str, default='data/samples', help='input folder') # input folder
|
||||||
|
parser.add_argument('--output', type=str, default='output', help='output folder') # output folder
|
||||||
parser.add_argument('--img-size', type=int, default=416, help='inference size (pixels)')
|
parser.add_argument('--img-size', type=int, default=416, help='inference size (pixels)')
|
||||||
parser.add_argument('--conf-thres', type=float, default=0.3, help='object confidence threshold')
|
parser.add_argument('--conf-thres', type=float, default=0.3, help='object confidence threshold')
|
||||||
parser.add_argument('--nms-thres', type=float, default=0.5, help='iou threshold for non-maximum suppression')
|
parser.add_argument('--nms-thres', type=float, default=0.5, help='iou threshold for non-maximum suppression')
|
||||||
parser.add_argument('--fourcc', type=str, default='mp4v', help='fourcc output video codec (verify ffmpeg support)')
|
parser.add_argument('--fourcc', type=str, default='mp4v', help='output video codec (verify ffmpeg support)')
|
||||||
parser.add_argument('--output', type=str, default='output', help='specifies the output path for images and videos')
|
|
||||||
parser.add_argument('--half', action='store_true', help='half precision FP16 inference')
|
parser.add_argument('--half', action='store_true', help='half precision FP16 inference')
|
||||||
parser.add_argument('--webcam', action='store_true', help='use webcam')
|
parser.add_argument('--webcam', action='store_true', help='use webcam')
|
||||||
opt = parser.parse_args()
|
opt = parser.parse_args()
|
||||||
print(opt)
|
print(opt)
|
||||||
|
|
||||||
with torch.no_grad():
|
with torch.no_grad():
|
||||||
detect(opt.cfg,
|
detect()
|
||||||
opt.data,
|
|
||||||
opt.weights,
|
|
||||||
images=opt.images,
|
|
||||||
img_size=opt.img_size,
|
|
||||||
conf_thres=opt.conf_thres,
|
|
||||||
nms_thres=opt.nms_thres,
|
|
||||||
fourcc=opt.fourcc,
|
|
||||||
output=opt.output)
|
|
||||||
|
|
4
test.py
4
test.py
|
@ -195,15 +195,15 @@ def test(cfg,
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
parser = argparse.ArgumentParser(prog='test.py')
|
parser = argparse.ArgumentParser(prog='test.py')
|
||||||
parser.add_argument('--batch-size', type=int, default=16, help='size of each image batch')
|
|
||||||
parser.add_argument('--cfg', type=str, default='cfg/yolov3-spp.cfg', help='cfg file path')
|
parser.add_argument('--cfg', type=str, default='cfg/yolov3-spp.cfg', help='cfg file path')
|
||||||
parser.add_argument('--data', type=str, default='data/coco.data', help='coco.data file path')
|
parser.add_argument('--data', type=str, default='data/coco.data', help='coco.data file path')
|
||||||
parser.add_argument('--weights', type=str, default='weights/yolov3-spp.weights', help='path to weights file')
|
parser.add_argument('--weights', type=str, default='weights/yolov3-spp.weights', help='path to weights file')
|
||||||
|
parser.add_argument('--batch-size', type=int, default=16, help='size of each image batch')
|
||||||
|
parser.add_argument('--img-size', type=int, default=416, help='inference size (pixels)')
|
||||||
parser.add_argument('--iou-thres', type=float, default=0.5, help='iou threshold required to qualify as detected')
|
parser.add_argument('--iou-thres', type=float, default=0.5, help='iou threshold required to qualify as detected')
|
||||||
parser.add_argument('--conf-thres', type=float, default=0.001, help='object confidence threshold')
|
parser.add_argument('--conf-thres', type=float, default=0.001, help='object confidence threshold')
|
||||||
parser.add_argument('--nms-thres', type=float, default=0.5, help='iou threshold for non-maximum suppression')
|
parser.add_argument('--nms-thres', type=float, default=0.5, help='iou threshold for non-maximum suppression')
|
||||||
parser.add_argument('--save-json', action='store_true', help='save a cocoapi-compatible JSON results file')
|
parser.add_argument('--save-json', action='store_true', help='save a cocoapi-compatible JSON results file')
|
||||||
parser.add_argument('--img-size', type=int, default=416, help='inference size (pixels)')
|
|
||||||
opt = parser.parse_args()
|
opt = parser.parse_args()
|
||||||
print(opt)
|
print(opt)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue