-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathStyleTransferModel.mm
51 lines (38 loc) · 1.38 KB
/
StyleTransferModel.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#import "StyleTransferModel.h"
#import "../../utils/ImageProcessor.h"
#import "opencv2/opencv.hpp"
@implementation StyleTransferModel {
cv::Size originalSize;
}
- (cv::Size)getModelImageSize {
NSArray *inputShape = [module getInputShape:@0];
NSNumber *widthNumber = inputShape.lastObject;
NSNumber *heightNumber = inputShape[inputShape.count - 2];
int height = [heightNumber intValue];
int width = [widthNumber intValue];
return cv::Size(height, width);
}
- (NSArray *)preprocess:(cv::Mat &)input {
self->originalSize = cv::Size(input.cols, input.rows);
cv::Size modelImageSize = [self getModelImageSize];
cv::Mat output;
cv::resize(input, output, modelImageSize);
NSArray *modelInput = [ImageProcessor matToNSArray:output];
return modelInput;
}
- (cv::Mat)postprocess:(NSArray *)output {
cv::Size modelImageSize = [self getModelImageSize];
cv::Mat processedImage = [ImageProcessor arrayToMat:output
width:modelImageSize.width
height:modelImageSize.height];
cv::Mat processedOutput;
cv::resize(processedImage, processedOutput, originalSize);
return processedOutput;
}
- (cv::Mat)runModel:(cv::Mat &)input {
NSArray *modelInput = [self preprocess:input];
NSArray *result = [self forward:modelInput];
input = [self postprocess:result[0]];
return input;
}
@end