-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to convert Matft array to OpenCV mat array #53
Comments
@Eumentis-Jayashree void MfarrayToMat(const MfArray array, cv::Mat& m) {
// you should assert array is row contiguous
Int cols = array.shape[0], rows = array.shape[1], channel = array.shape[2];
if (channel == 1)
{
m.create(rows, cols, CV_32FC1);
memcpy(array.mfdata.data_real, m.data, array.storedSize);
}
else if (channel == 3)
{
m.create(rows, cols, CV_32FC4);
memcpy(array.mfdata.data_real, m.data, array.storedSize);
}
else
{
m.create(rows, cols, CV_32FC4);
memcpy(array.mfdata.data_real, m.data, array.storedSize);
}
} |
Hey @jjjkkkjjj thank you for the response. Actually, I am getting the result by the ONNX model. So the output I get is in ORTValue form, which has this shape [1, 359, 8400].
So, according to your solution, if I print the shape of the arr, then I only get Shape: [3015600], which is equal to 359 * 8400. |
The reason of the shape: So, when you want MfArray with shape: [1, 359, 8400], you should reshape it by; let arr = MfArray(floatValuesForOutput, shape: [1, -1, 8400]) Caution: To prevent this, you should manage not only data but also shape and strides. Does |
Oh, I'm sorry, the above code is invalid due to not supporting negative dimension in init... let arr = MfArray(floatValuesForOutput).reshape([1, -1, 8400]) |
No, ORTValue doesn't have strides property. It only has tensorData(), typeInfo(), tensorTypeAndShapeInfo() and tensorStringData() properties. |
I see, then you can get same data by above code. |
@Eumentis-Jayashree |
Yeah, I solved it. But I am not sure if it's right approach. I have converted the Matft array to a Swift array and then converted it into an OpenCV mat array. |
Here I get rawOutputData from ORTValue using |
@jjjkkkjjj The output which I get is in shape of [1, 359, 8400]. Here 359 are rows and 8400 are columns. There is no channel like image. |
OK, but it is not efficient due to coping the data twice as you mentioned.
That's why we should use same data or copy it once. Fortunately, I implemented borrowing data system by this; let shape = // get from ORTValue
let size = // calculate the size from product all dimension = 1*359*8400
let floatValuesForOutput = rawOutputData.withUnsafeBytes {
(buffer: UnsafeRawBufferPointer) -> MfArray in
// copy data automatically
let mfdata = MfData(source: nil, data_real_ptr: buffer.baseAddress!, data_imag_ptr: nil, storedSize: size, mftype: .Float, offset: 0)
let mfstructure = MfStructure(shape: shape)
return MfArray(mfdata: mfdata, mfstructure: mfstructure)
} If the |
@jjjkkkjjj Thank you :). But the this constructor is define as internal, I am unable to access it. |
OMG! |
@Eumentis-Jayashree |
This one is no-copy (share version) extension ORTValue: MfDataBasable{
}
let rawOutputData // ORTValue
let shape = // get from ORTValue
let size = // calculate the size from product all dimension = 1*359*8400
let floatValuesForOutput = rawOutputData.withUnsafeBytes {
(buffer: UnsafeRawBufferPointer) -> MfArray in
// share data
let mfdata = MfData(source: rawOutputData, data_real_ptr: buffer.baseAddress!, data_imag_ptr: nil, storedSize: size, mftype: .Float, offset: 0)
let mfstructure = MfStructure(shape: shape)
return MfArray(mfdata: mfdata, mfstructure: mfstructure)
} |
I am getting error Cannot convert value of type 'UnsafeRawPointer' to expected argument type 'UnsafeMutableRawPointer' at data_real_ptr: buffer.baseAddress! , because constructor accepts this data type data_real_ptr: UnsafeMutableRawPointer |
@Eumentis-Jayashree let mfdata = MfData(source: nil, data_real_ptr: UnsafeMutableRawPointer(mutating: buffer.baseAddress!), data_imag_ptr: nil, storedSize: size, mftype: .Float, offset: 0) |
I have converted UnsafeRawBufferPointer to UnsafeMutableRawPointer it's giving multiple errors at the time of build. Showing All Messages ../Matft/core/util/typeconversion.swift:73:40: Cannot convert value of type 'UnsafePointer' to expected argument type 'T' ../Matft/core/util/typeconversion.swift:134:24: Incorrect argument label in call (have 'from:count:', expected 'repeating:count:') ../Matft/core/util/typeconversion.swift:134:45: Cannot convert value of type 'UnsafeMutablePointer' to expected argument type 'Float' ../Matft/core/util/typeconversion.swift:144:18: Value of type 'UnsafeMutablePointer' has no member 'moveUpdate' ../Matft/core/util/typeconversion.swift:154:24: Incorrect argument label in call (have 'from:count:', expected 'repeating:count:') ../Matft/core/util/typeconversion.swift:154:72: Cannot convert value of type 'UnsafePointer' to expected argument type 'Float' ../Matft/core/util/typeconversion.swift:234:18: Value of type 'UnsafeMutablePointer' has no member 'moveUpdate' ../Matft/core/util/typeconversion.swift:241:24: Incorrect argument label in call (have 'from:count:', expected 'repeating:count:') ../Matft/core/util/typeconversion.swift:241:45: Cannot convert value of type 'UnsafeMutablePointer' to expected argument type 'Double' ../Matft/core/util/typeconversion.swift:256:24: Incorrect argument label in call (have 'from:count:', expected 'repeating:count:') ../Matft/core/util/typeconversion.swift:256:73: Cannot convert value of type 'UnsafePointer' to expected argument type 'Double' ../Matft/core/util/typeconversion.swift:269:27: Value of type 'UnsafeMutablePointer' has no member 'moveUpdate' |
Which swift version was used? |
Xcode -> target -> Build Settings -> Swift Compiler Language -> Swift Language Version: swift 5 from terminal I get: |
Hmm, It's strange. let d: [Float] = [2, 3] // I didn't install ORTValue. So I use a simple array as workaround
let size = 2
let shape = [2,1]
let floatValuesForOutput = d.withUnsafeBytes {
(buffer: UnsafeRawBufferPointer) -> MfArray in
// copy data automatically
let mfdata = MfData(source: nil, data_real_ptr: UnsafeMutableRawPointer(mutating: buffer.baseAddress!), data_imag_ptr: nil, storedSize: size, mftype: .Float, offset: 0)
let mfstructure = MfStructure(shape: shape, mforder: .Row)
return MfArray(mfdata: mfdata, mfstructure: mfstructure)
} |
@Eumentis-Jayashree |
@Eumentis-Jayashree |
@jjjkkkjjj It's working on the |
I have created Matft array from float array
let matftArray = MfArray(floatArrayValues)
now I want to convert this array to OpenCV mat array so that I can use this array for processing.The text was updated successfully, but these errors were encountered: