如何解决张量到 JPEG:提高质量
我正在将从 tensorflow 相机返回的张量转换为 jpeg。我已经能够这样做,但 jpeg 图像质量很糟糕。这是一个用 expo 开发的移动 react-native 应用程序。我用来将张量转换为图像的库是 jpeg-js。
这是我用来将张量转换为图像的代码:
const handleCameraStream = async (imageAsTensors) => {
const loop = async () => {
const tensor = await imageAsTensors.next().value;
console.log(tensor)
const [height,width] = tensor.shape
const data = new Buffer(
// concat with an extra alpha channel and slice up to 4 channels to handle 3 and 4 channels tensors
tf.concat([tensor,tf.ones([height,width,1]).mul(255)],[-1])
.slice([0],[height,4])
.dataSync(),)
const rawImageData = {data,height};
const jpegImageData = jpeg.encode(rawImageData,200);
const imgBase64 = tf.util.decodeString(jpegImageData.data,"base64")
const salt = `${Date.Now()}-${Math.floor(Math.random() * 10000)}`
const uri = FileSystem.documentDirectory + `tensor-${salt}.jpg`;
await FileSystem.writeAsstringAsync(uri,imgBase64,{
encoding: FileSystem.EncodingType.Base64,});
setUri(uri)
// return {uri,height}
};
// requestAnimationFrameId = requestAnimationFrame(loop);
!uri ? setTimeout(() => loop(),2000) : null;
}
图像上半部分的图片是相机流。下图下半部分的图片是变换后的张量。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。