public RunStatus ExecuteByFile(string file, out string visionResult) { visionResult = ""; //开始计时 totalProcessStopwatch.Restart(); try { if ((VisionFrame == null) || (!VisionFrame.IsInit)) { throw new NullReferenceException("VisionFrame invaild"); } //执行视觉框架 ItemCollection outputs; VisionFrame.ExecuteByFile(file, out outputs); //结果拼接 visionResult = ConvertItemCollectionToString(outputs, MainSeparatorChar, SubSeparatorChar); } catch (Exception ex) { return(new RunStatus(0, EResult.Error, ex.Message)); } finally { //停止计时 totalProcessStopwatch.Stop(); VisionFrame.RunStatus.TotalTime = totalProcessStopwatch.Elapsed.TotalMilliseconds; } return(VisionFrame.RunStatus); }
/// <summary> /// 设置视觉算子文件 /// </summary> /// <param name="file">视觉算子文件</param> public void SetVisionOperaFile(string file) { if (VisionFrame == null) { throw new ArgumentException("VisionFrame cannot be null"); } //还原视觉算子 if (string.IsNullOrEmpty(file) || !File.Exists(file)) { throw new FileNotFoundException("visionOperaFile invalid"); } //确认文件是否本地路径,如果不是,则复制到本地路径下 string dstDirectory = $"VisionPlatform/Scene/{EVisionFrameType}/{Name}/VisionOperation"; string dstFile = $"{dstDirectory}/{Path.GetFileName(file)}"; if (!Directory.Exists(dstDirectory)) { Directory.CreateDirectory(dstDirectory); } if (Path.GetFullPath(dstFile) != file) { try { //如果是dll,则将同级目录下所有的dll都拷贝过来 if (Path.GetExtension(file) == ".dll") { FileInfo[] fileList = new DirectoryInfo(Path.GetDirectoryName(file))?.GetFiles("*.dll", SearchOption.TopDirectoryOnly); foreach (var item in fileList) { File.Copy(item.FullName, $"{dstDirectory}/{Path.GetFileName(item.Name)}", true); } } else { File.Copy(file, dstFile, true); } } catch (IOException) { //如果是同一个文件,则会报IO异常,过滤掉此异常 } } VisionOperaFile = Path.GetFileName(dstFile); VisionFrame.Init(dstFile); //创建默认输入输出参数文件 RecoverInputFile(true); RecoverOutputFile(true); }
private bool disposedValue = false; // 要检测冗余调用 /// <summary> /// 释放资源 /// </summary> /// <param name="disposing"></param> protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { // TODO: 释放托管状态(托管对象)。 } // TODO: 释放未托管的资源(未托管的对象)并在以下内容中替代终结器。 // TODO: 将大型字段设置为 null。 VisionFrame?.Dispose(); VisionFrame = null; disposedValue = true; } }
public RunStatus Execute(int timeout, out string visionResult) { bool isTimeout = false; var runStatus = new RunStatus(); visionResult = ""; try { //参数校验 if (VisionFrame == null) { throw new NullReferenceException("VisionFrame cannot be null"); } if (!IsInit) { throw new Exception($"scene is uninitialized({nameof(IsVisionFrameInit)}:{IsVisionFrameInit} {nameof(IsCameraInit)}:{IsCameraInit})"); } ItemCollection outputs; //开始计时 totalProcessStopwatch.Restart(); //如果使能相机,则调用相机采集 if (VisionFrame.IsEnableCamera && IsCameraInit) { lock (threadLock) { isVisionProcessed = false; } //注册相机采集完成事件 Camera.NewImageEvent -= Camera_NewImageEvent; Camera.NewImageEvent += Camera_NewImageEvent; //配置相机参数 CameraFactory.ConfigurateCamera(CameraSerial, cameraConfigParam); //触发拍照 Camera.TriggerSoftware(); //阻塞等待视觉处理完成 grapTimeoutStopwatch.Restart(); while (true) { lock (threadLock) { if (isVisionProcessed) { grapTimeoutStopwatch.Stop(); break; } } if ((timeout > 0) && (grapTimeoutStopwatch.Elapsed.TotalMilliseconds > timeout)) { isTimeout = true; grapTimeoutStopwatch.Stop(); throw new TimeoutException("grab image timeout"); } System.Threading.Thread.Sleep(2); } //执行视觉框架 VisionFrame.ExecuteByImageInfo(imageInfo, out outputs); //结果拼接 visionResult = ConvertItemCollectionToString(outputs, MainSeparatorChar, SubSeparatorChar); } else { //执行视觉框架 VisionFrame.Execute(timeout, out outputs); //结果拼接 visionResult = ConvertItemCollectionToString(outputs, MainSeparatorChar, SubSeparatorChar); } } catch (Exception ex) { return(new RunStatus(0, EResult.Error, ex.Message)); } finally { //停止计时 totalProcessStopwatch.Stop(); VisionFrame.RunStatus.TotalTime = totalProcessStopwatch.Elapsed.TotalMilliseconds; if (!isTimeout) { //释放图像资源,否则可能会导致资源泄露 imageInfo.DisposeImageIntPtr?.Invoke(imageInfo.ImagePtr); } } return(VisionFrame.RunStatus); }
public void Init() { try { //还原视觉框架 if (EVisionFrameType == EVisionFrameType.Unknown) { throw new ArgumentException("VisionFrameName invalid"); } if (VisionFrame == null) { VisionFrame = VisionFrameFactory.CreateInstance(EVisionFrameType); } //还原视觉算子 if (string.IsNullOrEmpty(VisionOperaFile)) { throw new ArgumentException("VisionOperaFile cannot be null"); } string visionOperaFilePath = $"{System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase}/VisionPlatform/Scene/{EVisionFrameType}/{Name}/VisionOperation/{VisionOperaFile}"; VisionFrame.Init(visionOperaFilePath); //还原输入参数 RecoverInputFile(); //还原输出参数 RecoverOutputFile(); //还原相机 if (VisionFrame.IsEnableCamera) { if (!string.IsNullOrEmpty(CameraSerial)) { //若相机无效,则不报异常 try { SetCamera(CameraSerial); } catch (InvalidOperationException) { //若打开相机失败,不抛异常 } ////配置相机参数 //if (CameraFactory.DefaultCameraSdkType != ECameraSdkType.VirtualCamera) //{ // SetCameraConfigFile(CameraConfigFile); //} ////配置标定信息 //SetCameraCalibrationFile(CalibrationFile); } } } catch (Exception) { throw; } }