diff --git a/python/paddle_serving_app/reader/__init__.py b/python/paddle_serving_app/reader/__init__.py index 93e2cd76102d93f52955060055afda34f9576ed8..44a9f51f435d8e4108d008ab5d23baf63d8d8bcc 100644 --- a/python/paddle_serving_app/reader/__init__.py +++ b/python/paddle_serving_app/reader/__init__.py @@ -11,12 +11,12 @@ # 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 .audio_reader import AudioFeatureOp from .chinese_bert_reader import ChineseBertReader +from .frame_reader import FrameExtractOp from .image_reader import ImageReader, File2Image, URL2Image, Sequential, Normalize from .image_reader import CenterCrop, Resize, Transpose, Div, RGB2BGR, BGR2RGB, ResizeByFactor from .image_reader import RCNNPostprocess, SegPostprocess, PadStride -from .image_reader import DBPostProcess, FilterBoxes, GetRotateCropImage, SortedBoxes +from .image_reader import DBPostProcess, FilterBoxes from .lac_reader import LACReader from .senta_reader import SentaReader -from .imdb_reader import IMDBDataset -from .ocr_reader import OCRReader diff --git a/python/paddle_serving_app/reader/audio_reader.py b/python/paddle_serving_app/reader/audio_reader.py new file mode 100644 index 0000000000000000000000000000000000000000..bce9327db7f2c932755f4b3a6b15078e6f80b181 --- /dev/null +++ b/python/paddle_serving_app/reader/audio_reader.py @@ -0,0 +1,50 @@ +# Copyright (c) 2020 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. +# pylint: disable=doc-string-missing +""" +audio feature op +""" + +import ffmpeg +import numpy +import logging +import cv2 +import adpredictor as adpredictor + +try: + from paddle_serving_server.pipeline import Op +except Exception as e: + from paddle_serving_server_gpu.pipeline import Op + +_LOGGER = logging.getLogger() + + +class AudioFeatureOp(Op): + """ + audio feature op class + """ + + def extract_audio_from_video(self, local_video_path): + """ + extract audios from video + Params: + local_video_path, string + Returns: + audio pcm s16 le raw data for success, None for failed + """ + out, _ = (ffmpeg.input(local_video_path, ).output( + '-', format='s16le', acodec='pcm_s16le', ac=1, ar='16k') + .overwrite_output().run(capture_stdout=True)) + result = numpy.frombuffer(out, dtype=numpy.short) + return result \ No newline at end of file diff --git a/python/paddle_serving_app/reader/frame_extractor.py b/python/paddle_serving_app/reader/frame_extractor.py new file mode 100644 index 0000000000000000000000000000000000000000..59846c89c4cfd4435c601d2f432a22bb41bf9a38 --- /dev/null +++ b/python/paddle_serving_app/reader/frame_extractor.py @@ -0,0 +1,105 @@ +""" +frame extractor +""" +import sys +import numpy as np +from paddle_serving_client import Client + +import logging +import hwextract as hwextract +from multiprocessing.pool import ThreadPool + +_LOGGER = logging.getLogger(__name__) +class FrameData(object): + """ + store frame data in python + """ + def __init__(self): + """ + constructor + """ + self._width = 0 + self._height = 0 + self._img = None + + def width(self): + """ + return width + """ + return self._width + + def height(self): + """ + return height + """ + return self._height + + def img(self): + """ + return img array + """ + return self._img + + def set_width(self, width): + """ + set width + """ + self._width = width + + def set_height(self, height): + """ + set height + """ + self._height = height + + def set_img(self, img): + """ + set_img + """ + self._img = img + + def set_data(self, frame): + """ + set all data using frame + """ + self._width = frame.width() + self._height = frame.height() + self._img = np.array(frame, copy=True) + +class FrameExt(object): + """ + extract and call tns + """ + def __init__(self): + """ + constructor + """ + + self._handler = None + + def init_handler(self, card_idx): + """ + init handler + """ + + self._handler = hwextract.HwExtractFrameJpeg(card_idx) + result = self._handler.init_handler() + return result + + def extract_frame(self, file_name): + """ + hardware extract video + """ + frame_list = self._handler.extract_frame(file_name, 1) + result_list = [] + for item in frame_list: + tmp_frame = FrameData() + tmp_frame.set_data(item) + result_list.append(tmp_frame) + item.free_memory() + return result_list + + def chunks(self, lst, n): + """Yield successive n-sized chunks from lst.""" + for i in range(0, len(lst), n): + yield lst[i:i + n] diff --git a/python/paddle_serving_app/reader/frame_reader.py b/python/paddle_serving_app/reader/frame_reader.py new file mode 100644 index 0000000000000000000000000000000000000000..52f1cab2b6fda4d6a4aeaaccb80f742052beb16e --- /dev/null +++ b/python/paddle_serving_app/reader/frame_reader.py @@ -0,0 +1,71 @@ +# Copyright (c) 2020 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. +# pylint: disable=doc-string-missing +""" +frame extract op +""" + +import logging +import sys +import frame_extractor as frame_ext +try: + from paddle_serving_server.pipeline import Op +except Exception as e: + from paddle_serving_server_gpu.pipeline import Op + +_LOGGER = logging.getLogger(__name__) + + +class FrameExtractOp(Op): + """ frame extract op """ + + def init_op(self): + """ + overwrite init_op function to load custom resources. + + note: + since only one frame_extractor instance can run on + each gpu card, this op can only start one single thread. + """ + pipeline_config = util.load_config(sys.argv[1]) + self.is_thread = pipeline_config["dag"]["is_thread_op"] + op_config = util.load_config(pipeline_config["op_config"]) + + self.frame_ext = frame_ext.FrameExt() + self.frame_ext.init_handler(op_config["frame_extract_card"]) + + def preprocess(self, input_dict): + """ extract frame """ + (_, input_data), = input_dict.items() + video_data = input_data.get("video_data") + is_pcm_raw_data = input_data.get("is_pcm_raw_data") + seq = input_data.get("seq", None) + if is_pcm_raw_data: + raise Exception("not support pcm_raw_data") + + frame_list = self.frame_ext.extract_frame(video_data) + frame_batches = self.frame_ext.chunks(frame_list, 8) + + if not self.is_thread: + # TODO: object hwextract.HwFrameResult could + # not be serialized correctly. + frame_batches = list(frame_batches) + #for frame_list in frame_batches: + # width = frame_list[0].width() + # height = frame_list[0].height() + # _LOGGER.error("[FrameExt]({}) width: {}, height: {}" + # .format(type(frame_list[0]), width, height)) + # break + output_dict = {"frame_batches": frame_batches, "seq": None} + return output_dict diff --git a/python/paddle_serving_app/utils/__init__.py b/python/paddle_serving_app/utils/__init__.py index 847ddc47ac89114f2012bc6b9990a69abfe39fb3..56551b939a509fbbe59d6ac8f49ad6955e139a81 100644 --- a/python/paddle_serving_app/utils/__init__.py +++ b/python/paddle_serving_app/utils/__init__.py @@ -11,3 +11,5 @@ # 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 .arr2image import Arr2Image \ No newline at end of file diff --git a/python/paddle_serving_app/utils/arr2image.py b/python/paddle_serving_app/utils/arr2image.py new file mode 100644 index 0000000000000000000000000000000000000000..bbaad99497d68b1f60bc675a638b98b0116573e0 --- /dev/null +++ b/python/paddle_serving_app/utils/arr2image.py @@ -0,0 +1,31 @@ +# Copyright (c) 2020 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 cv2 +import yaml + +class Arr2Image(object): + """ + from numpy array image(jpeg) to cv::Mat image + """ + + def __init__(self): + pass + + def __call__(self, img_arr): + img = cv2.imdecode(img_arr, cv2.IMREAD_COLOR) + return img + + def __repr__(self): + return self.__class__.__name__ + "()" \ No newline at end of file diff --git a/python/paddle_serving_server_gpu/__init__.py b/python/paddle_serving_server_gpu/__init__.py index 1d94bf3093e6d76b260f53acd0c799080627c0ab..becfbb20090799aaf40d79973964e497cf599436 100644 --- a/python/paddle_serving_server_gpu/__init__.py +++ b/python/paddle_serving_server_gpu/__init__.py @@ -235,15 +235,11 @@ class Server(object): self.bin_path = os.environ["SERVING_BIN"] def check_cuda(self): - cuda_flag = False - r = os.popen("ldd {} | grep cudart".format(self.bin_path)) - r = r.read().split("=") - if len(r) >= 2 and "cudart" in r[1] and os.system( - "ls /dev/ | grep nvidia > /dev/null") == 0: - cuda_flag = True - if not cuda_flag: + if os.system("ls /dev/ | grep nvidia > /dev/null") == 0: + pass + else: raise SystemExit( - "CUDA not found, please check your environment or use cpu version by \"pip install paddle_serving_server\"" + "GPU not found, please check your environment or use cpu version by \"pip install paddle_serving_server\"" ) def set_gpuid(self, gpuid=0):