1818from opencv_tools import DIRECTORY_IMAGES ,DIRECTORY_ANNOTATIONS ,DIRECTORY_PREANNOTATIONS
1919import base64
2020
21-
2221## opencv读取中文路径图片
2322def imread_chinese_path (image_path ):
2423 image = cv2 .imdecode (np .fromfile (image_path , dtype = np .uint8 ), - 1 )
@@ -770,6 +769,7 @@ def __init__(self,video_path,camera_type,use_gpu_decode,camera_reopen_times=30,J
770769 self .device = device
771770 self .JadeLog = JadeLog
772771 self .show_window = show_window
772+ self .capture = None
773773 super (VideoCaptureBaseProcess , self ).__init__ ()
774774
775775 def download_frame (self ,frame ):
@@ -838,14 +838,29 @@ def open_gpu_capture(self):
838838 def opencv_cpu_capture (self ):
839839 if self .device == "ascend" :
840840 from acllite import videocapture
841- import acl
842- ret = acl .rt .set_device (0 )
843- self .capture = videocapture .VideoCapture (self .video_path )
844- self .JadeLog .INFO ("相机类型为:{},使用Ascend芯片解码,准备打开相机" .format (self .camera_type ))
841+ if self .capture is None :
842+ import acl
843+ from acllite import acllite_utils as utils
844+ self ._context , ret = acl .rt .create_context (0 )
845+ utils .check_ret ("acl.rt.create_context" , ret )
846+ else :
847+ self .JadeLog .DEBUG ("相机类型为:{},释放相机资源" .format (self .camera_type ))
848+ try :
849+ self .capture = videocapture .VideoCapture (self .video_path )
850+ self .JadeLog .INFO ("相机类型为:{},使用Ascend芯片解码,准备打开相机" .format (self .camera_type ))
851+ except Exception as e :
852+ self .JadeLog .ERROR ("相机类型为:{},相机打开失败,失败原因为:{}" .format (self .camera_type ,e ))
853+ self .capture = None
854+ self .capture = videocapture .VideoCapture (self .video_path )
845855 else :
856+ if self .capture is None :
857+ pass
858+ else :
859+ self .JadeLog .DEBUG ("相机类型为:{},释放相机资源" .format (self .camera_type ))
860+ self .capture .release ()
846861 self .capture = cv2 .VideoCapture (self .video_path )
862+ self .capture .release ()
847863 self .JadeLog .INFO ("相机类型为:{},使用CPU解码,准备打开相机" .format (self .camera_type ))
848-
849864 if self .device == "ascend" :
850865 ret ,frame = self .capture .read ()
851866 if ret == 0 and frame is not None :
@@ -895,7 +910,8 @@ def capture_reader(self):
895910 ret = True
896911 else :
897912 ret = False
898- if ret is False or frame is None :
913+
914+ if ret is False :
899915 self .JadeLog .WARNING (
900916 "相机类型为:{},相机中途断开,等待{}s,尝试重连" .format (self .camera_type , self .camera_reopen_times ))
901917 time .sleep (self .camera_reopen_times )
@@ -905,7 +921,7 @@ def capture_reader(self):
905921 if self .history_status :
906922 time .sleep (0.04 )
907923 except Exception as e :
908- self .JadeLog .WARNING (
924+ self .JadeLog .ERROR (
909925 "相机类型为:{},相机解码失败,失败原因为:{},发生异常文件{},发生异常所在的行数{},等待{}s,尝试重连" .format (self .camera_type ,e ,
910926 e .__traceback__ .tb_frame .f_globals ["__file__" ],e .__traceback__ .tb_lineno ,self .camera_reopen_times ))
911927 time .sleep (self .camera_reopen_times )
0 commit comments