如何解决从张量到jpeg的tfjs-react-native
decodeJpeg
中有@tensorflow/tfjs-react-native
,但没有encodeJpeg
。然后如何将张量写入本地jpeg文件中?
import * as tf from '@tensorflow/tfjs';
import * as FileSystem from 'expo-file-system';
import * as jpeg from 'jpeg-js';
export const encoderJpeg = async (tensor,name) => {
// add alpha channel if missing
const shape = [...tensor.shape]
shape.pop()
shape.push(4)
const tensorWithAlpha = tf.concat([tensor,tensor],[-1]).slice([0],shape)
const array = new Uint8Array(tensorWithAlpha.dataSync())
const rawImageData = {
data: array.buffer,width: shape[1],height: shape[0],};
const jpegImageData = jpeg.encode(rawImageData,50);
const imgBase64 = tf.util.decodeString(jpegImageData.data,"base64")
const uri = FileSystem.documentDirectory + name;
await FileSystem.writeAsstringAsync(uri,imgBase64,{
encoding: FileSystem.EncodingType.Base64,});
return uri
}
但是当我显示带有<Image />
的图像时,我看到所有的都是纯绿色。
解决方法
您可以将imgBase64
直接用于图像组件,如下所示:
<Image source={{uri: 'data:image/jpeg;base64,' + imgBase64}} />
这是我这样做的最终工具:
import * as tf from '@tensorflow/tfjs';
import * as FileSystem from 'expo-file-system';
import * as jpeg from 'jpeg-js';
export const encodeJpeg = async (tensor) => {
const height = tensor.shape[0]
const width = tensor.shape[1]
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,100);
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,});
return {uri,height}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。