diff --git a/examples/vision/detection/paddledetection/python/infer_ppyoloe.py b/examples/vision/detection/paddledetection/python/infer_ppyoloe.py index 4f3b398b3..ff9e2724d 100644 --- a/examples/vision/detection/paddledetection/python/infer_ppyoloe.py +++ b/examples/vision/detection/paddledetection/python/infer_ppyoloe.py @@ -1,7 +1,9 @@ -import fastdeploy as fd import cv2 import os +import fastdeploy as fd +import fastdeploy.utils + def parse_arguments(): import argparse @@ -9,10 +11,10 @@ def parse_arguments(): parser = argparse.ArgumentParser() parser.add_argument( "--model_dir", - required=True, + default=None, help="Path of PaddleDetection model directory") parser.add_argument( - "--image", required=True, help="Path of test image file.") + "--image", default=None, help="Path of test image file.") parser.add_argument( "--device", type=str, @@ -39,9 +41,14 @@ def build_option(args): args = parse_arguments() -model_file = os.path.join(args.model_dir, "model.pdmodel") -params_file = os.path.join(args.model_dir, "model.pdiparams") -config_file = os.path.join(args.model_dir, "infer_cfg.yml") +if args.model_dir is None: + model_dir = fd.download_model(name='ppyoloe_crn_l_300e_coco') +else: + model_dir = args.model_dir + +model_file = os.path.join(model_dir, "model.pdmodel") +params_file = os.path.join(model_dir, "model.pdiparams") +config_file = os.path.join(model_dir, "infer_cfg.yml") # 配置runtime,加载模型 runtime_option = build_option(args) @@ -49,7 +56,11 @@ model = fd.vision.detection.PPYOLOE( model_file, params_file, config_file, runtime_option=runtime_option) # 预测图片检测结果 -im = cv2.imread(args.image) +if args.image is None: + image = fd.utils.get_detection_test_image() +else: + image = args.image +im = cv2.imread(image) result = model.predict(im.copy()) print(result) diff --git a/examples/vision/detection/yolor/python/infer.py b/examples/vision/detection/yolor/python/infer.py index ca0c9f453..85123420f 100644 --- a/examples/vision/detection/yolor/python/infer.py +++ b/examples/vision/detection/yolor/python/infer.py @@ -1,15 +1,17 @@ -import fastdeploy as fd import cv2 +import fastdeploy as fd +import fastdeploy.utils + def parse_arguments(): import argparse import ast parser = argparse.ArgumentParser() parser.add_argument( - "--model", required=True, help="Path of yolor onnx model.") + "--model", default=None, help="Path of yolor onnx model.") parser.add_argument( - "--image", required=True, help="Path of test image file.") + "--image", default=None, help="Path of test image file.") parser.add_argument( "--device", type=str, @@ -39,10 +41,20 @@ args = parse_arguments() # 配置runtime,加载模型 runtime_option = build_option(args) -model = fd.vision.detection.YOLOR(args.model, runtime_option=runtime_option) +if args.model is None: + model = fd.download_model(name='YOLOR-W6') +else: + model = args.model + +model = fd.vision.detection.YOLOR(model, runtime_option=runtime_option) # 预测图片检测结果 -im = cv2.imread(args.image) +if args.image is None: + image = fd.utils.get_detection_test_image() +else: + image = args.image + +im = cv2.imread(image) result = model.predict(im.copy()) print(result) diff --git a/python/fastdeploy/download.py b/python/fastdeploy/download.py index 01c382ec5..0b14ccf8e 100644 --- a/python/fastdeploy/download.py +++ b/python/fastdeploy/download.py @@ -245,5 +245,6 @@ def download_model(name: str, except FileExistsError: pass print('Successfully download model at path: {}'.format(fullpath)) + return fullpath else: print('ERROR: Could not find a model named {}'.format(name)) diff --git a/python/fastdeploy/utils/__init__.py b/python/fastdeploy/utils/__init__.py index 7e1abd0b1..374afbad4 100644 --- a/python/fastdeploy/utils/__init__.py +++ b/python/fastdeploy/utils/__init__.py @@ -11,4 +11,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + from .profile import profile +from .example_resource import get_detection_test_image diff --git a/python/fastdeploy/utils/example_resource.py b/python/fastdeploy/utils/example_resource.py new file mode 100644 index 000000000..4bfc4f2d8 --- /dev/null +++ b/python/fastdeploy/utils/example_resource.py @@ -0,0 +1,24 @@ +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License" +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import fastdeploy.download as download +import fastdeploy.utils.hub_env as hubenv + + +def get_detection_test_image(path=None): + if path is None: + path = hubenv.RESOURCE_HOME + fullpath = download.download( + url='https://bj.bcebos.com/paddlehub/fastdeploy/example/detection_test_image.jpg', + path=path) + return fullpath diff --git a/python/fastdeploy/utils/hub_env.py b/python/fastdeploy/utils/hub_env.py index 0a81bc953..1db53372e 100644 --- a/python/fastdeploy/utils/hub_env.py +++ b/python/fastdeploy/utils/hub_env.py @@ -52,3 +52,4 @@ USER_HOME = _get_user_home() HUB_HOME = _get_hub_home() MODEL_HOME = _get_sub_home('models') CONF_HOME = _get_sub_home('conf') +RESOURCE_HOME = _get_sub_home('resources')