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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| - (void)runCnn:(UIImage *)compressedImg { unsigned char *pixels = [self getImagePixel:compressedImg]; int image_channels = 4; tensorflow::Tensor image_tensor( tensorflow::DT_FLOAT, tensorflow::TensorShape( {1, wanted_input_height, wanted_input_width, wanted_input_channels})); auto image_tensor_mapped = image_tensor.tensor<float, 4>(); tensorflow::uint8 *in = pixels; float *out = image_tensor_mapped.data(); for (int y = 0; y < wanted_input_height; ++y) { float *out_row = out + (y * wanted_input_width * wanted_input_channels); for (int x = 0; x < wanted_input_width; ++x) { tensorflow::uint8 *in_pixel = in + (x * wanted_input_width * image_channels) + (y * image_channels); float *out_pixel = out_row + (x * wanted_input_channels); for (int c = 0; c < wanted_input_channels; ++c) { out_pixel[c] = in_pixel[c]; } } }
tensorflow::Tensor style(tensorflow::DT_FLOAT, tensorflow::TensorShape({32})); float *style_data = style.tensor<float, 1>().data(); memset(style_data, 0, sizeof(float) * 32); style_data[currentStyle] = 1;
if (tf_session.get()) { std::vector<tensorflow::Tensor> outputs; tensorflow::Status run_status = tf_session->Run( {{contentNode, image_tensor}, {styleNode, style}}, {outputNode}, {}, &outputs); if (!run_status.ok()) { LOG(ERROR) << "Running model failed:" << run_status; isDone = true; free(pixels); } else { float *styledData = outputs[0].tensor<float,4>().data(); UIImage *styledImg = [self createImage:styledData]; dispatch_async(dispatch_get_main_queue(), ^{ _styleImageView.image = styledImg; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ isDone = true; free(pixels); }); }); } } }
- (unsigned char *)getImagePixel:(UIImage *)image { int width = image.size.width; int height = image.size.height; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char)); NSUInteger bytesPerPixel = 4; NSUInteger bytesPerRow = bytesPerPixel * width; NSUInteger bitsPerComponent = 8; CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace); CGContextDrawImage(context, CGRectMake(0, 0, width, height), image.CGImage); UIImage *ogImg = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)]; dispatch_async(dispatch_get_main_queue(), ^{ _ogImageView.image = ogImg; }); CGContextRelease(context); return rawData; }
- (UIImage *)createImage:(float *)pixels { unsigned char *rawData = (unsigned char*) calloc(wanted_input_height * wanted_input_width * 4, sizeof(unsigned char)); for (int y = 0; y < wanted_input_height; ++y) { unsigned char *out_row = rawData + (y * wanted_input_width * 4); for (int x = 0; x < wanted_input_width; ++x) { float *in_pixel = pixels + (x * wanted_input_width * 3) + (y * 3); unsigned char *out_pixel = out_row + (x * 4); for (int c = 0; c < wanted_input_channels; ++c) { out_pixel[c] = in_pixel[c] * 255; } out_pixel[3] = UINT8_MAX; } } CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); NSUInteger bytesPerPixel = 4; NSUInteger bytesPerRow = bytesPerPixel * wanted_input_width; NSUInteger bitsPerComponent = 8; CGContextRef context = CGBitmapContextCreate(rawData, wanted_input_width, wanted_input_height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace); UIImage *retImg = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)]; CGContextRelease(context); free(rawData); return retImg; }
|