KMDocumentAIManager.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // KMDocumentAIManager.m
  3. // PDF Master
  4. //
  5. // Created by 丁林圭 on 2023/2/2.
  6. //
  7. #import "KMDocumentAIManager.h"
  8. @interface KMDocumentAIManager ()
  9. //@property (nonatomic, assign) COCRModelType modelType;
  10. @property (nonatomic, assign) NSUInteger finishIndex;
  11. @property (nonatomic, retain) NSMutableArray *images;
  12. //@property (nonatomic, retain) COCREngine * ocrEngine;
  13. //@property (nonatomic, retain) CIMEngine * imEngine;
  14. @end
  15. @implementation KMDocumentAIManager
  16. - (id)init {
  17. if (self = [super init]) {
  18. self.images = [NSMutableArray array];
  19. }
  20. return self;
  21. }
  22. + (KMDocumentAIManager *)defaultManager {
  23. static KMDocumentAIManager *singleton = nil;
  24. static dispatch_once_t pred;
  25. dispatch_once(&pred, ^{
  26. singleton = [[KMDocumentAIManager alloc] init];
  27. });
  28. return singleton;
  29. }
  30. //- (void)recognitionImages:(NSArray *)images withModelType:(COCRModelType)modelType {
  31. // self.finishIndex = 0;
  32. // [self.images removeAllObjects];;
  33. // [self.images addObjectsFromArray:images];
  34. //
  35. // self.ocrEngine = [[COCREngine alloc] init];
  36. // [self.ocrEngine setModelWithType:modelType];
  37. // if(!self.ocrEngine) {
  38. // NSError *error = [NSError errorWithDomain:@"PDFReaderBatchErrorDomain" code:-1 userInfo:nil];
  39. // if([self.delegate respondsToSelector:@selector(documentAIManager:didFailureImageAtIndex:error:)]) {
  40. // [self.delegate documentAIManager:self didFailureImageAtIndex:self.finishIndex error:error];
  41. // }
  42. //
  43. // if([self.delegate respondsToSelector:@selector(documentAIDidFinish:)]) {
  44. // [self.delegate documentAIDidFinish:self];
  45. // }
  46. // } else {
  47. //
  48. // if(self.finishIndex < self.images.count) {
  49. // if([self.delegate respondsToSelector:@selector(documentAIManagerDidStart:)]) {
  50. // [self.delegate documentAIManagerDidStart:self];
  51. // }
  52. // [self recognitionImage:self.images[self.finishIndex]];
  53. // }
  54. // }
  55. //}
  56. - (void)recognitionImage:(NSImage *)image {
  57. // NSString *fileName = [NSString stringWithFormat:@"%@.png", @"recognitionImage"];
  58. // NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
  59. // if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
  60. // [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
  61. // }
  62. //
  63. // [image.TIFFRepresentation writeToURL:[NSURL fileURLWithPath:filePath] atomically:YES];
  64. // NSMutableArray * tResults = [[NSMutableArray alloc] init];
  65. //
  66. // COCRErrorType errorType = [self.ocrEngine processWithOCRImageInputPath:filePath andResult:tResults];
  67. // NSMutableArray * results = [[NSMutableArray alloc] init];
  68. // for (COCRResult * cOcrResult in tResults) {
  69. // CGRect rect = cOcrResult.textBounds;
  70. //
  71. // rect.origin.y = image.size.height - cOcrResult.textBounds.origin.y;
  72. // cOcrResult.textBounds = rect;
  73. // [results addObject:cOcrResult];
  74. // }
  75. //
  76. // if(CIMGErrorTypeNone == errorType) {
  77. // if([self.delegate respondsToSelector:@selector(documentAIManager:didFinishOCRImageAtIndex:results:)]) {
  78. // [self.delegate documentAIManager:self didFinishOCRImageAtIndex:self.finishIndex results:results];
  79. // }
  80. // self.finishIndex++;
  81. // if(self.finishIndex < self.images.count) {
  82. // [self recognitionImage:self.images[self.finishIndex]];
  83. // } else {
  84. // if([self.delegate respondsToSelector:@selector(documentAIDidFinish:)]) {
  85. // [self.delegate documentAIDidFinish:self];
  86. // }
  87. // }
  88. // } else {
  89. // NSError *error = [NSError errorWithDomain:@"PDFReaderBatchErrorDomain" code:errorType userInfo:nil];
  90. // if([self.delegate respondsToSelector:@selector(documentAIManager:didFailureImageAtIndex:error:)]) {
  91. // [self.delegate documentAIManager:self didFailureImageAtIndex:self.finishIndex error:error];
  92. // }
  93. // }
  94. }
  95. - (void)increaseImages:(NSArray *)images {
  96. self.finishIndex = 0;
  97. [self.images removeAllObjects];;
  98. [self.images addObjectsFromArray:images];
  99. // self.imEngine = [[CIMEngine alloc] init];
  100. // if(!self.imEngine) {
  101. // NSError *error = [NSError errorWithDomain:@"PDFReaderBatchErrorDomain" code:-1 userInfo:nil];
  102. // if([self.delegate respondsToSelector:@selector(documentAIManager:didFailureImageAtIndex:error:)]) {
  103. // [self.delegate documentAIManager:self didFailureImageAtIndex:self.finishIndex error:error];
  104. // }
  105. //
  106. // if([self.delegate respondsToSelector:@selector(documentAIDidFinish:)]) {
  107. // [self.delegate documentAIDidFinish:self];
  108. // }
  109. // } else {
  110. // if(self.finishIndex < self.images.count) {
  111. // if([self.delegate respondsToSelector:@selector(documentAIManagerDidStart:)]) {
  112. // [self.delegate documentAIManagerDidStart:self];
  113. // }
  114. // [self increaseImage:self.images[self.finishIndex]];
  115. // }
  116. // }
  117. }
  118. - (void)increaseImage:(NSImage *)image {
  119. NSString *fileName = [NSString stringWithFormat:@"%@.png", @"increaseImage"];
  120. NSString *outName = [NSString stringWithFormat:@"%@.jpg", @"increaseOutImage"];
  121. NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
  122. NSString *fileOutPath = [NSTemporaryDirectory() stringByAppendingPathComponent:outName];
  123. if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
  124. [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
  125. }
  126. if ([[NSFileManager defaultManager] fileExistsAtPath:fileOutPath]) {
  127. [[NSFileManager defaultManager] removeItemAtPath:fileOutPath error:nil];
  128. }
  129. // [image.TIFFRepresentation writeToURL:[NSURL fileURLWithPath:filePath] atomically:YES];
  130. // CIMGErrorType errorType = [self.imEngine processWithInputPath:filePath andOutputPath:fileOutPath];
  131. // if(CIMGErrorTypeNone == errorType) {
  132. // if([self.delegate respondsToSelector:@selector(imageEngineManager:didFinishCIImageAtIndex:outImage:)]) {
  133. // [self.delegate imageEngineManager:self didFinishCIImageAtIndex:self.finishIndex outImage:fileOutPath];
  134. // }
  135. // self.finishIndex++;
  136. // if(self.finishIndex < self.images.count) {
  137. // [self increaseImage:self.images[self.finishIndex]];
  138. // } else {
  139. // if([self.delegate respondsToSelector:@selector(documentAIDidFinish:)]) {
  140. // [self.delegate documentAIDidFinish:self];
  141. // }
  142. // }
  143. // } else {
  144. // NSError *error = [NSError errorWithDomain:@"PDFReaderBatchErrorDomain" code:errorType userInfo:nil];
  145. // if([self.delegate respondsToSelector:@selector(documentAIManager:didFailureImageAtIndex:error:)]) {
  146. // [self.delegate documentAIManager:self didFailureImageAtIndex:self.finishIndex error:error];
  147. // }
  148. // }
  149. }
  150. @end