-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
author : justniub #212
Open
xiguadong
wants to merge
4
commits into
RangiLyu:main
Choose a base branch
from
unknown repository
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
author : justniub #212
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,19 +12,19 @@ | |
image_ext = ['.jpg', '.jpeg', '.webp', '.bmp', '.png'] | ||
video_ext = ['mp4', 'mov', 'avi', 'mkv'] | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('demo', default='image', help='demo type, eg. image, video and webcam') | ||
parser.add_argument('--config', help='model config file path') | ||
parser.add_argument('--model', help='model file path') | ||
parser.add_argument('--path', default='./demo', help='path to images or video') | ||
parser.add_argument('--camid', type=int, default=0, help='webcam demo camera id') | ||
parser.add_argument('--save_result', action='store_true', help='whether to save the inference result of image/video') | ||
parser.add_argument('--save_result', default=False,action='store_true', | ||
help='whether to save the inference result of image/video') | ||
parser.add_argument('--save_path', type=str,help='path to save the inference result of image/video') | ||
args = parser.parse_args() | ||
return args | ||
|
||
|
||
class Predictor(object): | ||
def __init__(self, cfg, model_path, logger, device='cuda:0'): | ||
self.cfg = cfg | ||
|
@@ -64,10 +64,9 @@ def inference(self, img): | |
def visualize(self, dets, meta, class_names, score_thres, wait=0): | ||
time1 = time.time() | ||
result_img = self.model.head.show_result(meta['raw_img'], dets, class_names, score_thres=score_thres, show=True) | ||
print('viz time: {:.3f}s'.format(time.time()-time1)) | ||
print('viz time: {:.3f}s'.format(time.time() - time1)) | ||
return result_img | ||
|
||
|
||
|
||
def get_image_list(path): | ||
image_names = [] | ||
for maindir, subdir, file_name_list in os.walk(path): | ||
|
@@ -78,29 +77,35 @@ def get_image_list(path): | |
image_names.append(apath) | ||
return image_names | ||
|
||
|
||
def main(): | ||
args = parse_args() | ||
torch.backends.cudnn.enabled = True | ||
torch.backends.cudnn.benchmark = True | ||
|
||
load_config(cfg, args.config) | ||
logger = Logger(-1, use_tensorboard=False) | ||
predictor = Predictor(cfg, args.model, logger, device='cuda:0') | ||
logger.log('Press "Esc", "q" or "Q" to exit.') | ||
current_time = time.localtime() | ||
|
||
if args.demo == 'image': | ||
if os.path.isdir(args.path): | ||
files = get_image_list(args.path) | ||
else: | ||
files = [args.path] | ||
files.sort() | ||
if args.save_result: | ||
save_folder = args.save_path if args.save_path else os.path.join(cfg.save_dir, | ||
time.strftime("%Y_%m_%d_%H_%M_%S", | ||
current_time)) | ||
if not os.path.exists(save_folder): | ||
mkdir(save_folder) | ||
logger.info(fr"make the image save folder: {save_folder}") | ||
|
||
for image_name in files: | ||
meta, res = predictor.inference(image_name) | ||
result_image = predictor.visualize(res, meta, cfg.class_names, 0.35) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Too many unnecessary line breaks. |
||
if args.save_result: | ||
save_folder = os.path.join(cfg.save_dir, time.strftime("%Y_%m_%d_%H_%M_%S", current_time)) | ||
mkdir(save_folder) | ||
save_file_name = os.path.join(save_folder, os.path.basename(image_name)) | ||
cv2.imwrite(save_file_name, result_image) | ||
ch = cv2.waitKey(0) | ||
|
@@ -111,11 +116,16 @@ def main(): | |
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) # float | ||
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) # float | ||
fps = cap.get(cv2.CAP_PROP_FPS) | ||
save_folder = os.path.join(cfg.save_dir, time.strftime("%Y_%m_%d_%H_%M_%S", current_time)) | ||
mkdir(save_folder) | ||
save_path = os.path.join(save_folder, args.path.split('/')[-1]) if args.demo == 'video' else os.path.join(save_folder, 'camera.mp4') | ||
print(f'save_path is {save_path}') | ||
vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (int(width), int(height))) | ||
|
||
if args.save_result: | ||
save_folder = args.save_path if args.save_path else os.path.join(cfg.save_dir, | ||
time.strftime("%Y_%m_%d_%H_%M_%S", current_time)) | ||
if not os.path.exists(save_folder): | ||
mkdir(save_folder) | ||
save_path = os.path.join(save_folder, args.path.split('/')[-1]) if args.demo == 'video' else os.path.join( | ||
save_folder, 'camera.mp4') | ||
vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (int(width), int(height))) | ||
logger.info(fr"make the video save file: {save_path}") | ||
while True: | ||
ret_val, frame = cap.read() | ||
if ret_val: | ||
|
@@ -129,6 +139,5 @@ def main(): | |
else: | ||
break | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use mkdir function in nanodet.util