我们将此用于 CoreMLhelpers,以便将 UIImage 转换为 MultiArray,反之亦然。
链接:https://github.com/hollance/CoreMLHelpers.
我们找到了以下评论参考。
// cat.bin 包含 cat.jpg,保存为 [0,1) 范围内的双精度形状 // (3, 360, 480).将此二进制数据加载到新的 MLMultiArray 对象中。
因此,我们使用自定义图像并尝试创建 .bin 文件,如下所示。 但是我们没有得到想要的输出,我们可以看到在阴影中混合了多种颜色和绿色的猫图像。
任何人都可以在这里抛出一些光来传达什么是正确的吗?
感谢您的回复!
import UIKit
func createBinary() -> MLMultiArray? {
guard let image = UIImage(named: “cat.jpg”) else {
fatalError(“Failed to load image”)
}
// Convert image to pixel data
guard let cgImage = image.cgImage else {
fatalError(“Failed to get CGImage from resized image”)
}
let width = 480
let height = 360
let bytesPerPixel = 4
let bytesPerRow = width * bytesPerPixel
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo.byteOrder32Little.rawValue | CGImageAlphaInfo.premultipliedLast.rawValue
var pixelData = Array<UInt8>(repeating: 0, count: width * height * bytesPerPixel)
guard let context = CGContext(data: &pixelData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else {
fatalError(“Failed to create CGContext”)
}
context.draw(cgImage, in: CGRect(x: 0, y: 0, width: width, height: height))
// Convert pixel data to binary data
var binaryData = NSMutableData()
for y in 0..<height {
for x in 0..<width {
let index = y * width * bytesPerPixel + x * bytesPerPixel
var red = Double(pixelData[index]) / 255.0
var green = Double(pixelData[index + 1]) / 255.0
var blue = Double(pixelData[index + 2]) / 255.0
binaryData.append(UnsafeRawPointer(&red), length: MemoryLayout<Double>.size)
binaryData.append(UnsafeRawPointer(&green), length: MemoryLayout<Double>.size)
binaryData.append(UnsafeRawPointer(&blue), length: MemoryLayout<Double>.size)
}
}
// Verify binary data shape
let expectedShape = (3, 360, 480)
let actualShape = (3, height, width)
guard expectedShape == actualShape else {
fatalError(“Unexpected binary data shape: \(actualShape), expected: \(expectedShape)“)
}
// Done
print(“Binary data size: \(binaryData.count) bytes”)
let ptr = UnsafeMutableRawPointer(mutating: (binaryData as NSData).bytes)
return try? MLMultiArray(dataPointer: ptr,
shape: [3, 360, 480],
dataType: .double,
strides: [NSNumber(value: 360*480), 480, 1])
}