models.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /// 照片分类类型
  2. enum PhotoImageClassifyType {
  3. similar,
  4. people,
  5. screenshot,
  6. blurry,
  7. }
  8. /// 分类进度信息
  9. class ClassificationProgress {
  10. /// 总批次数
  11. final int totalBatches;
  12. /// 当前批次
  13. final int currentBatch;
  14. /// 是否已完成
  15. final bool isLastBatch;
  16. /// 完成百分比 (0.0-1.0)
  17. final double rate;
  18. /// 当前批次处理时间(秒)
  19. final double batchDuration;
  20. /// 总耗时(秒)
  21. final double totalDuration;
  22. /// 是否已完成
  23. bool get isCompleted => isLastBatch;
  24. ClassificationProgress({
  25. required this.totalBatches,
  26. required this.currentBatch,
  27. required this.isLastBatch,
  28. required this.rate,
  29. required this.batchDuration,
  30. required this.totalDuration,
  31. });
  32. static double _toDouble(dynamic v) => v is int ? v.toDouble() : (v as double);
  33. static ClassificationProgress fromJson(Map<String, dynamic> json) {
  34. return ClassificationProgress(
  35. totalBatches: json['totalBatches'],
  36. currentBatch: json['currentBatch'],
  37. isLastBatch: json['isLastBatch'],
  38. rate: _toDouble(json['rate']),
  39. batchDuration: _toDouble(json['batchDuration']),
  40. totalDuration: _toDouble(json['totalDuration']),
  41. );
  42. }
  43. }
  44. class ClassifiedImageGroup {
  45. final List<ClassifiedImage> images;
  46. int get groupFileSize => images.fold(0, (sum, image) => sum + image.fileSize);
  47. int get groupImageCount => images.length;
  48. ClassifiedImageGroup({
  49. required this.images,
  50. });
  51. static ClassifiedImageGroup fromList(List<ClassifiedImage> images) {
  52. return ClassifiedImageGroup(
  53. images: images,
  54. );
  55. }
  56. }
  57. class ImageLocation {
  58. final double latitude; // 纬度
  59. final double longitude; // 经度
  60. ImageLocation({
  61. required this.latitude,
  62. required this.longitude,
  63. });
  64. static ImageLocation fromJson(Map<String, dynamic> json) {
  65. return ImageLocation(
  66. latitude: json['latitude'],
  67. longitude: json['longitude'],
  68. );
  69. }
  70. }
  71. class ClassifiedImage {
  72. final String assetsId;
  73. final double createdAt;
  74. final ImageLocation? location;
  75. final int fileSize;
  76. final int pixelWidth;
  77. final int pixelHeight;
  78. ClassifiedImage({
  79. required this.assetsId,
  80. required this.createdAt,
  81. this.location,
  82. required this.fileSize,
  83. required this.pixelWidth,
  84. required this.pixelHeight,
  85. });
  86. static ClassifiedImage fromJson(Map<String, dynamic> json) {
  87. return ClassifiedImage(
  88. assetsId: json['assetsId'],
  89. createdAt: json['createdAt'],
  90. location: json['location'] != null ? ImageLocation.fromJson(Map<String, dynamic>.from(json['location'])) : null,
  91. fileSize: json['fileSize'],
  92. pixelWidth: json['pixelWidth'],
  93. pixelHeight: json['pixelHeight'],
  94. );
  95. }
  96. }
  97. class ClassificationResult {
  98. final List<ClassifiedImageGroup>? similarGroups;
  99. final List<ClassifiedImage>? peopleGroups;
  100. final List<ClassifiedImage>? screenshotGroups;
  101. final List<ClassifiedImage>? blurryGroups;
  102. ClassificationResult({
  103. required this.similarGroups,
  104. required this.peopleGroups,
  105. required this.screenshotGroups,
  106. required this.blurryGroups,
  107. });
  108. static ClassificationResult fromJson(Map<String, dynamic> json) {
  109. List<ClassifiedImage> processedImages(dynamic images) {
  110. var imageList = List<Map<dynamic, dynamic>>.from(images ?? []);
  111. return imageList.map((image) => ClassifiedImage.fromJson(Map<String, dynamic>.from(image))).toList();
  112. }
  113. return ClassificationResult(
  114. similarGroups: json['similarGroups']?['groups']?.isEmpty ?? true
  115. ? null
  116. : List<ClassifiedImageGroup>.from((json['similarGroups']?['groups'] ?? []).map((group) =>
  117. ClassifiedImageGroup.fromList(
  118. processedImages(group)
  119. )
  120. )),
  121. peopleGroups: json['peopleImages']?.isEmpty ?? true
  122. ? null
  123. : processedImages(json['peopleImages']),
  124. screenshotGroups: json['screenshotImages']?.isEmpty ?? true
  125. ? null
  126. : processedImages(json['screenshotImages']),
  127. blurryGroups: json['blurryImages']?.isEmpty ?? true
  128. ? null
  129. : processedImages(json['blurryImages']),
  130. );
  131. }
  132. }
  133. /// 分类事件,包含进度和结果
  134. class ClassificationEvent {
  135. /// 分类进度
  136. final ClassificationProgress? progress;
  137. /// 分类结果
  138. final ClassificationResult? result;
  139. ClassificationEvent({
  140. required this.progress,
  141. this.result,
  142. });
  143. }