Models.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // Models.swift
  3. // Demo
  4. //
  5. // Created by Groot on 2025/4/27.
  6. //
  7. import UIKit
  8. import Vision
  9. import CoreLocation
  10. import Photos
  11. /// 分类类型选项集
  12. public enum PhotoImageClassifyType: String, CaseIterable {
  13. case similar, people, screenshot, blurry
  14. public static var all: [PhotoImageClassifyType] {
  15. return Self.allCases
  16. }
  17. public init?(_ rawValue: String) {
  18. switch rawValue.lowercased() {
  19. case "similar":
  20. self = .similar
  21. case "people":
  22. self = .people
  23. case "screenshot":
  24. self = .screenshot
  25. case "blurry":
  26. self = .blurry
  27. default:
  28. return nil
  29. }
  30. }
  31. }
  32. /// 进度更新信息
  33. public struct ClassificationProgress: Codable {
  34. /// 当前处理的批次索引
  35. public let currentBatch: Int
  36. /// 总批次数
  37. public let totalBatches: Int
  38. /// 进度百分比 (0.0-1.0)
  39. public let rate: Double
  40. /// 当前批次处理时间(秒)
  41. public let batchDuration: Double
  42. /// 总耗时(秒)
  43. public let totalDuration: Double
  44. /// 是否为最后一个批次
  45. public let isLastBatch: Bool
  46. public var isCompleted: Bool {
  47. return isLastBatch
  48. }
  49. }
  50. /// 回调数据
  51. public struct PhotoImageClassifiedResult: Identifiable {
  52. public let id: String = UUID().uuidString
  53. public var similarGroups: [ImageGroup]?
  54. public var peopleImages: [ImageItem]?
  55. public var screenshotImages: [ImageItem]?
  56. public var blurryImages: [ImageItem]?
  57. }
  58. /// 图片组
  59. public struct ImageGroup: Identifiable {
  60. public let id = UUID()
  61. public var images: [ImageItem]
  62. public var imageCount: Int {
  63. images.count
  64. }
  65. public var imageSizeCount: Int64 {
  66. images.reduce(0) { $0 + ($1.imageInfo?.fileSize ?? 0) }
  67. }
  68. }
  69. /// 图片项
  70. public struct ImageItem: Identifiable {
  71. public let id = UUID()
  72. public let asset: PHAsset
  73. public var thumbnailImage: UIImage?
  74. public var imageInfo: ImageInfo?
  75. public var classfiyInfo: ClassifyInfo?
  76. public struct ImageInfo {
  77. public let time: Date?
  78. public let locationCoordinate: CLLocationCoordinate2D?
  79. public let fileSize: Int64?
  80. public let pixelSize: CGSize
  81. }
  82. public struct ClassifyInfo {
  83. public var similarFeature: VNFeaturePrintObservation? = nil
  84. public var containsPeople: Bool = false
  85. public var isBlurry: Bool = false
  86. public var isScreenshot: Bool = false
  87. }
  88. public func toDictionary() -> [String: Any] {
  89. var dict: [String: Any] = [
  90. "assetsId": asset.localIdentifier,
  91. "createdAt": imageInfo?.time?.timeIntervalSince1970 ?? 0,
  92. "fileSize": imageInfo?.fileSize ?? 0,
  93. "pixelWidth": Int(imageInfo?.pixelSize.width ?? 0),
  94. "pixelHeight": Int(imageInfo?.pixelSize.height ?? 0)
  95. ]
  96. if let coordinate = imageInfo?.locationCoordinate {
  97. dict["location"] = [
  98. "latitude": coordinate.latitude,
  99. "longitude": coordinate.longitude
  100. ]
  101. }
  102. return dict
  103. }
  104. }