Browse Source

lazy import decord (#2776)

There is a confict with `paddle` when import `decord` globally.
Tingquan Gao 10 months ago
parent
commit
4d4c52630f
2 changed files with 11 additions and 8 deletions
  1. 10 7
      paddlex/inference/utils/io/readers.py
  2. 1 1
      requirements.txt

+ 10 - 7
paddlex/inference/utils/io/readers.py

@@ -25,15 +25,10 @@ import soundfile
 import decord
 import random
 import platform
+import importlib
 
 from ....utils import logging
 
-if not platform.machine().startswith("arm"):
-    import decord
-else:
-    logging.warning(
-        "Please install `decord` manually on ARM machine. Otherwise, the related model cannot work."
-    )
 
 __all__ = [
     "ReaderType",
@@ -345,6 +340,14 @@ class DecordVideoReaderBackend(_VideoReaderBackend):
         self.valid_mode = True
         self._fps = 0
 
+        # XXX(gaotingquan): There is a confict with `paddle` when import `decord` globally.
+        try:
+            self.decord_module = importlib.import_module("decord")
+        except ModuleNotFoundError():
+            raise Exception(
+                "Please install `decord` manually, otherwise, the related model cannot work. It can be automatically installed only on `x86_64`. Refers: `https://github.com/dmlc/decord`."
+            )
+
     def set_pos(self, pos):
         self._pos = pos
 
@@ -385,7 +388,7 @@ class DecordVideoReaderBackend(_VideoReaderBackend):
 
     def read_file(self, in_path):
         """read vidio file from path"""
-        self._cap = decord.VideoReader(in_path)
+        self._cap = self.decord_module.VideoReader(in_path)
         frame_len = len(self._cap)
         if self.sample_type == "uniform":
             sample_video = self.sample(frame_len, self._cap)

+ 1 - 1
requirements.txt

@@ -36,7 +36,7 @@ erniebot-agent == 0.5.0
 unstructured
 networkx
 faiss-cpu
-decord==0.6.0; platform_machine != 'arm64'
+decord==0.6.0; platform_machine == 'x86_64'
 ######## For NLP Tokenizer #######
 jieba
 sentencepiece