# base64-to-tensor

> a pure js convert a base64 to a tensor object for node

Latest version **1.2.16** (published 2023-08-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install base64-to-tensor
pnpm add base64-to-tensor
yarn add base64-to-tensor
bun add base64-to-tensor
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.16 |
| Published | 2023-08-05 |
| First published | 2022-06-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 119.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jeff Mendez |
| Maintainers | jeffmendez |
| Keywords | tensorflow, image-recognition |

## Links

- npm: https://www.npmjs.com/package/base64-to-tensor
- Repository: https://github.com/A11yWatch/base64-to-tensor
- Homepage: https://github.com/A11yWatch/base64-to-tensor#readme
- Issues: https://github.com/A11yWatch/base64-to-tensor/issues
- npm.io page: https://npm.io/package/base64-to-tensor

## Alternatives

- [exif-parser](https://npm.io/package/exif-parser.md) — 3.8M weekly downloads
- [vite-plugin-compression](https://npm.io/package/vite-plugin-compression.md) — 569.5K weekly downloads
- [pica](https://npm.io/package/pica.md) — 442.4K weekly downloads
- [@reportportal/client-javascript](https://npm.io/package/@reportportal/client-javascript.md) — 408.8K weekly downloads
- [@tldraw/state](https://npm.io/package/@tldraw/state.md) — 316.0K weekly downloads

## Recent versions

- 1.2.16 (latest) — 2023-08-05
- 1.2.15 — 2023-08-05
- 1.2.14 — 2023-08-05
- 1.2.13 — 2023-08-05
- 1.2.12 — 2023-08-05
- 1.2.11 — 2023-08-05
- 1.2.10 — 2023-08-05
- 1.2.9 — 2023-08-04
- 1.2.8 — 2022-07-22
- 1.2.7 — 2022-07-22
- 1.2.6 — 2022-07-22
- 1.2.5 — 2022-06-19
- 1.2.4 — 2022-06-19
- 1.2.3 — 2022-06-19
- 1.2.2 — 2022-06-19
- … 21 more at https://npm.io/package/base64-to-tensor/versions

## README

# base64-to-tensor

[![Build and lint](https://github.com/A11yWatch/base64-to-tensor/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/A11yWatch/base64-to-tensor/actions/workflows/tests.yml)

pure js convert a base64 image to tensor

## Installation

```
npm i base64-to-tensor --save
```

## Getting Started

Make sure to have `@tensorflow/tfjs-core` installed and a valid tensorflow backend set.
You also need to pick between [sync package jpeg-js](https://github.com/jpeg-js/jpeg-js) or [async package sharp](https://github.com/lovell/sharp).

```sh
# pure js full sync blocking installation
npm i @tensorflow/tfjs-core jpeg-js
# if going to use async non blocking
npm i @tensorflow/tfjs-core sharp
```

View the [convert.test.ts](./__tests__/convert.test.ts) file for an example setup.

```ts
import { convert, convertAsync } from "base64-to-tensor";
import { setBackend } from "@tensorflow/tfjs-core";
import "@tensorflow/tfjs-backend-wasm";

await setBackend("wasm");

const tensor = convert(mybase64); // The base64 must be a valid jpeg image.
// or use native sharp for increased performance 2x [Expiremental]
const tensor = await convertAsync(mybase64);
// output example
// {
//       kept: false,
//       isDisposedInternal: false,
//       shape: [189, 300, 3],
//       dtype: "int32",
//       size: 170100,
//       strides: [900, 3],
//       dataId: { id: 1 },
//       id: 1,
//       rankType: "3",
//     }
```

## Why

The benefits of using pure js to calc the image is in a couple areas:

1. size and portablity required is drastically less since you do not need `cairo` or any of the native img dev converters.
1. speed is also faster since the calcs are done at hand without needing to bridge any calls.
1. can use tensors in worker threads - allows for properly using Tensorflow wasm backends in an API service 🥳.

## Benchmarks

Examples of some test ran on a mac m1(64gb):

| Name | chars | size     | sync  | async |
| ---- | ----- | -------- | ----- | ----- |
| jpeg | 26791 | 26.16 KB | 100ms | 50ms  |

---
_Source: https://npm.io/package/base64-to-tensor · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
