Created
March 5, 2019 15:42
-
-
Save piercus/0efbc2a0d0bd09ba15521d8bb9df86d8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// i'm using opencv4nodejs to load a file into a tensorflow tensor | |
const cv = require('opencv4nodejs'); | |
const mat = cv.imread('./tmp/lenna.png') | |
const size = {w: mat.cols, h: mat.rows} | |
const mat3 = new cv.Mat(rect.h, rect.w, cv.CV_8UC3); | |
mat.getRegion(new cv.Rect(rect.x, rect.y, rect.w, rect.h)).copyTo(mat3) | |
cv.imwrite('./tmp/cv.png', mat3) | |
const tf = require('@tensorflow/tfjs-node'); | |
// I want to crop this rect | |
const rect = { | |
x: 171, | |
y: 422, | |
w: 9, | |
h: 8 | |
} | |
// this is the address in the image of the pixel value followed through the tensors calculation | |
const address = [ | |
0, | |
422+1, | |
171+4, | |
0 | |
] | |
// Some function to be able to see what is happening inside tensors | |
const fact = function (array) { | |
if (array.length === 0) { | |
return 1; | |
} | |
return array[0] * fact(array.slice(1)); | |
}; | |
const addressToIndex = function (address, shape) { | |
return address.map((a, index) => a * fact(shape.slice(index + 1))).reduce((a, b) => a + b); | |
}; | |
const logAddress = function(tensor, address){ | |
console.log(address, getPixelValue(address, tensor.dataSync()[addressToIndex(address, shape)])) | |
} | |
const intTensor = tf.tensor4d(mat.getData(), [1, size.h, size.w, 3]) | |
const floatTensor = intTensor.div(255); | |
// if i'm logging | |
logAddress(address, floatTensor) | |
const cropped = tf.image.cropAndResize(floatTensor, [[rect.y/size.h, rect.x/size.w, (rect.y+rect.h)/size.h, (rect.x+rect.w)/size.w]], [0], [rect.h, rect.w], 'nearest'); | |
logAddress(address, floatTensor) | |
const croppedValues = cropped.mul(255).toInt().dataSync(); | |
const d = new Uint8Array(croppedValues); | |
const mat2 = new cv.Mat(d, rect.h, rect.w, cv.CV_8UC3); | |
const mat3 = new cv.Mat(rect.h, rect.w, cv.CV_8UC3); | |
mat.getRegion(new cv.Rect(rect.x, rect.y, rect.w, rect.h)).copyTo(mat3) | |
cv.imwrite('./tmp/cv.png', mat3) | |
cv.imwrite('./tmp/tf.png', mat2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment