123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- import 'dart:convert';
- import 'package:fis_measure/interfaces/process/player/play_controller.dart';
- import 'package:fis_measure/interfaces/process/workspace/application.dart';
- import 'package:fis_measure/process/workspace/measure_data_controller.dart';
- import 'package:fis_measure/view/paint/ai_patint_state.dart';
- import 'package:fis_measure/view/paint/date_structure.dart';
- import 'package:fis_measure/view/player/controller.dart';
- import 'package:fis_measure/view/player/enums.dart';
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- class AiPatintController extends GetxController {
- static const _HAS_VIEW_STATUS_ARR = [VidPlayStatus.play, VidPlayStatus.pause];
- /// 播放器控制器
- final VidPlayerController controller;
- final state = AiPatintState();
- late final application = Get.find<IApplication>();
- /// 测量AI数据
- final measureData = Get.find<MeasureDataController>();
- /// 播放控制器
- final playerController = Get.find<IPlayerController>();
- /// ai结果图展示
- late List<Offset> aiResultsList = [];
- /// 获取病灶大小
- late String lesionSize;
- /// ai结果点集
- late AiDotsResults aiDotsResults;
- /// ai结果点集List
- late List<AiDotsResults> aiDotsResultsList;
- /// ai结果数据
- late List<AIDetectedObject> aiDetectedObject = [];
- /// ai部位
- late DiagnosisOrganEnum diagnosisOrgan = DiagnosisOrganEnum.Null;
- // late double unitsPhysicalPixels =
- // application.currentViewPort.region.width / application.frameData!.width;
- /// 是否是播放状态
- get isPlay => playerController.status == VidPlayStatus.play;
- /// ai json 数据
- late String? aiResults = '';
- /// ai的横纵坐标 四个点
- late Offset p1 = Offset.zero;
- late Offset p2 = Offset.zero;
- late Offset p3 = Offset.zero;
- late Offset p4 = Offset.zero;
- /// 视频时的画面
- double left = -1;
- double top = -1;
- double width = -1;
- double height = -1;
- /// 当前帧数
- int? frameIndex;
- AiPatintController(this.controller);
- @override
- void onInit() {
- _addListenrs();
- super.onInit();
- }
- @override
- void dispose() {
- super.dispose();
- _removeListenrs();
- }
- void onMeasuredAIResultsInfoChanged(Object sender, String e) {
- try {
- if (e.isNotEmpty && e != "[]") {
- aiResults = e;
- final measureDataAIResults = jsonDecode(
- aiResults ?? '',
- );
- state.aiResult.clear();
- for (var element in (measureDataAIResults as List)) {
- if (element != null) {
- try {
- state.aiResult.add(
- AIDiagnosisPerImageDTO.fromJson(
- element,
- ),
- );
- } catch (e) {
- state.aiResult.add(AIDiagnosisPerImageDTO());
- print("AIDiagnosisPerImageDTO.fromJson Failed");
- print(e);
- }
- } else {
- state.aiResult.add(AIDiagnosisPerImageDTO());
- }
- }
- } else {
- aiResults = '';
- state.aiResult.clear();
- }
- } catch (e) {
- print(e);
- }
- }
- /// 单帧图处理
- void onSingleFrameImage(
- List<AIDetectedObject>? detectedObjects,
- ) {
- aiDetectedObject = detectedObjects ?? [];
- final widthScale = Get.find<IApplication>().displayScaleRatio;
- for (int m = 0; m < detectedObjects!.length; m++) {
- final List<AIDiagnosisPoint2D>? contours =
- detectedObjects[m].contours ?? [];
- if ((contours?.isNotEmpty ?? false) &&
- (detectedObjects[m].descriptions?.isNotEmpty ?? false)) {
- var lesionSizeDescription = detectedObjects[m].descriptions?.firstWhere(
- (element) => element.type == DiagnosisDescriptionEnum.LesionSize);
- lesionSize = lesionSizeDescription?.value ?? '';
- final lesionSizeMap = jsonDecode(lesionSize);
- // var carotidInnerDiameterDescription = detectedObjects[m]
- // .descriptions
- // ?.firstWhere((element) =>
- // element.type == DiagnosisDescriptionEnum.CarotidInnerDiameter);
- // final carotidInnerDiameterMap =
- // jsonDecode(carotidInnerDiameterDescription?.value ?? '');
- if (_HAS_VIEW_STATUS_ARR.contains(state.vidStatus)) {
- aiResultsList = [];
- p1 = Offset(lesionSizeMap['HorizontalPoint1']['X'] * widthScale,
- lesionSizeMap['HorizontalPoint1']['Y'] * widthScale);
- p2 = Offset(lesionSizeMap['HorizontalPoint2']['X'] * widthScale,
- lesionSizeMap['HorizontalPoint2']['Y'] * widthScale);
- p3 = Offset(lesionSizeMap['VerticalPoint1']['X'] * widthScale,
- lesionSizeMap['VerticalPoint1']['Y'] * widthScale);
- p4 = Offset(lesionSizeMap['VerticalPoint2']['X'] * widthScale,
- lesionSizeMap['VerticalPoint2']['Y'] * widthScale);
- for (int i = 0; i < contours!.length; i++) {
- aiResultsList.add(Offset(
- contours[i].x * widthScale,
- contours[i].y * widthScale,
- ));
- }
- aiDotsResults = AiDotsResults(
- aiResultsIndex: m,
- aiResultsList: aiResultsList,
- p1: p1,
- p2: p2,
- p3: p3,
- p4: p4,
- );
- aiDotsResultsList.add(aiDotsResults);
- }
- }
- }
- }
- /// ai处理图片结果
- bool onGetAIResultsInfo() {
- double widthScale = Get.find<IApplication>().displayScaleRatio;
- if (state.aiResult.isEmpty) {
- return false;
- } else if (state.aiResult.length == 1) {
- updateFeatures();
- final diagResultsForEachOrgan =
- state.aiResult[0].diagResultsForEachOrgan?[state.aiResultIndex];
- final detectedObjects = diagResultsForEachOrgan!.detectedObjects;
- diagnosisOrgan = diagResultsForEachOrgan.organ;
- if (detectedObjects!.isNotEmpty) {
- onSingleFrameImage(
- detectedObjects,
- );
- return true;
- } else {
- return false;
- }
- } else {
- updateFeatures();
- var diagResultsForEachOrgans =
- state.aiResult[state.frameIndex].diagResultsForEachOrgan ?? [];
- if (diagResultsForEachOrgans.isEmpty) {
- return false;
- }
- final diagResultsForEachOrgan =
- diagResultsForEachOrgans[state.aiResultIndex];
- diagnosisOrgan = diagResultsForEachOrgan.organ;
- final detectedObjects = diagResultsForEachOrgan.detectedObjects;
- if (state.vidStatus == VidPlayStatus.pause) {
- if (detectedObjects == null) {
- return false;
- }
- if (detectedObjects.isNotEmpty) {
- onSingleFrameImage(
- detectedObjects,
- );
- }
- return true;
- } else if (state.vidStatus == VidPlayStatus.play) {
- if (detectedObjects?.isNotEmpty ?? false) {
- for (int m = 0; m < detectedObjects!.length; m++) {
- final organBoundBox =
- detectedObjects[m].boundingBox ?? AIDiagnosisRect();
- bool isLiver = diagnosisOrgan == DiagnosisOrganEnum.Liver;
- bool isThyroid = diagnosisOrgan == DiagnosisOrganEnum.Thyroid;
- // 判断病灶是否是弥漫性
- if (detectedObjects[m].label != 0) {
- if (isLiver) {
- if (detectedObjects[m].label < 5) {
- _horizontalVerticalLine(organBoundBox, widthScale);
- }
- } else if (isThyroid) {
- if (detectedObjects[m].label < 7) {
- _horizontalVerticalLine(organBoundBox, widthScale);
- }
- } else {
- _horizontalVerticalLine(organBoundBox, widthScale);
- }
- }
- }
- return true;
- } else {
- return false;
- }
- }
- return false;
- }
- }
- void updateFeatures() {
- aiDotsResults = AiDotsResults(
- aiResultsIndex: 0,
- aiResultsList: [],
- p1: Offset.zero,
- p2: Offset.zero,
- p3: Offset.zero,
- p4: Offset.zero,
- );
- state.aiResultIndex = 0;
- aiDotsResultsList = [];
- left = 0;
- top = 0;
- width = 0;
- height = 0;
- }
- void _horizontalVerticalLine(
- AIDiagnosisRect organBoundBox,
- double widthScale,
- ) {
- left = organBoundBox.left * widthScale;
- top = organBoundBox.top * widthScale;
- width = organBoundBox.width * widthScale;
- height = organBoundBox.height * widthScale;
- }
- void _addListenrs() async {
- measureData.aiResultsInfoChanged
- .addListener(onMeasuredAIResultsInfoChanged);
- if (measureData.aiResults.isNotEmpty) {
- onMeasuredAIResultsInfoChanged(this, measureData.aiResults);
- }
- }
- void _removeListenrs() {
- measureData.aiResultsInfoChanged
- .removeListener(onMeasuredAIResultsInfoChanged);
- }
- }
|