Skip to content

Commit

Permalink
run format & lint
Browse files Browse the repository at this point in the history
  • Loading branch information
gutenye committed May 6, 2024
1 parent 79f6cc1 commit 8b993c4
Show file tree
Hide file tree
Showing 30 changed files with 112 additions and 233 deletions.
14 changes: 13 additions & 1 deletion biome.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"organizeImports": {
"enabled": true
},
"formatter": {
"indentStyle": "space",
"lineWidth": 120
},
"linter": {
"enabled": true,
"rules": {
Expand All @@ -14,5 +18,13 @@
"quoteStyle": "single",
"semicolons": "asNeeded"
}
},
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": true
},
"files": {
"ignore": ["ios", "android"]
}
}
}
Binary file modified bun.lockb
Binary file not shown.
8 changes: 2 additions & 6 deletions examples/browser/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@ function createApp(
) {
const resultTextEl = document.querySelector('#result-text') as HTMLDivElement
const performanceEl = document.querySelector('#performance') as HTMLDivElement
const resultImageEl = document.querySelector(
'#result-image',
) as HTMLImageElement
const inputImageEl = document.querySelector(
'#input-image',
) as HTMLImageElement
const resultImageEl = document.querySelector('#result-image') as HTMLImageElement
const inputImageEl = document.querySelector('#input-image') as HTMLImageElement
inputImageEl.addEventListener('change', async (event) => {
const file = (event.target as HTMLInputElement).files?.[0]
if (!file) {
Expand Down
2 changes: 1 addition & 1 deletion examples/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"dependencies": {
"@gutenye/ocr-browser": "worksapce:*"
}
}
}
2 changes: 1 addition & 1 deletion examples/node/node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import filePath from 'path'
import Ocr from '@gutenye/ocr-node'
import fs from 'fs/promises'
import filePath from 'path'

async function main() {
const ocr = await Ocr.create()
Expand Down
12 changes: 6 additions & 6 deletions examples/node/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "node-example",
"type": "module",
"dependencies": {
"@gutenye/ocr-node": "^1.0.0"
}
}
"name": "node-example",
"type": "module",
"dependencies": {
"@gutenye/ocr-node": "^1.0.0"
}
}
2 changes: 1 addition & 1 deletion examples/react-native/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import camera from 'expo-camera'
import * as ort from 'onnxruntime-react-native'
import { StatusBar } from 'expo-status-bar'
import * as ort from 'onnxruntime-react-native'
import { StyleSheet, Text, View } from 'react-native'
import { SafeAreaProvider, SafeAreaView } from 'react-native-safe-area-context'
import { Ocr } from '../build/ocr'
Expand Down
8 changes: 4 additions & 4 deletions examples/react-native/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = function(api) {
api.cache(true);
module.exports = (api) => {
api.cache(true)
return {
presets: ['babel-preset-expo'],
};
};
}
}
2 changes: 1 addition & 1 deletion examples/react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
"typescript": "^5.1.3"
},
"private": true
}
}
1 change: 0 additions & 1 deletion index.ts

This file was deleted.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"name": "@gutenye/ocr",
"workspaces": [
"packages/*"
],
"workspaces": ["packages/*"],
"devDependencies": {
"@biomejs/biome": "1.7.3",
"@types/bun": "latest",
"tsc-alias": "^1.8.8",
"typescript": "^5.0.0"
}
}
}
9 changes: 2 additions & 7 deletions packages/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,12 @@
"bun": "./src/index.ts",
"default": "./build/index.js"
},
"files": [
"src",
"build",
"tsconfig.json",
"!**/__tests__"
],
"files": ["src", "build", "tsconfig.json", "!**/__tests__"],
"scripts": {
"prepublishOnly": "./ake build"
},
"dependencies": {
"@gutenye/ocr-common": "^1.2.0",
"onnxruntime-web": "^1.17.3"
}
}
}
25 changes: 5 additions & 20 deletions packages/browser/src/ImageRaw.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ImageRawBase } from '@gutenye/ocr-common'
import type { ImageRawData, SizeOption, LineImage } from '@gutenye/ocr-common'
import type { ImageRawData, LineImage, SizeOption } from '@gutenye/ocr-common'
import invariant from 'tiny-invariant'

export class ImageRaw extends ImageRawBase {
Expand Down Expand Up @@ -39,10 +39,7 @@ export class ImageRaw extends ImageRawBase {
}

async resize({ width, height }: SizeOption) {
invariant(
width !== undefined || height !== undefined,
'both width and height are undefined',
)
invariant(width !== undefined || height !== undefined, 'both width and height are undefined')
const newWidth = width || Math.round((this.width / this.height) * height!)
const newHeight = height || Math.round((this.height / this.width) * width!)
const newCanvas = document.createElement('canvas')
Expand Down Expand Up @@ -92,34 +89,22 @@ export class ImageRaw extends ImageRawBase {
}
}

function canvasDrawImage(
canvas: HTMLCanvasElement,
image: CanvasImageSource,
width?: number,
height?: number,
) {
function canvasDrawImage(canvas: HTMLCanvasElement, image: CanvasImageSource, width?: number, height?: number) {
canvas.width = width || image.width
canvas.height = height || image.height
const ctx = canvas.getContext('2d')!
ctx.drawImage(image, 0, 0, canvas.width, canvas.height)
}

function canvasPutImageData(
canvas: HTMLCanvasElement,
imageData: ImageData,
width?: number,
height?: number,
) {
function canvasPutImageData(canvas: HTMLCanvasElement, imageData: ImageData, width?: number, height?: number) {
const ctx = canvas.getContext('2d')!
canvas.width = width || imageData.width
canvas.height = height || imageData.height
ctx.putImageData(imageData, 0, 0)
}

function canvasGetImageData(canvas: HTMLCanvasElement) {
return canvas
.getContext('2d')!
.getImageData(0, 0, canvas.width, canvas.height)
return canvas.getContext('2d')!.getImageData(0, 0, canvas.width, canvas.height)
}

async function imageFromUrl(url: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Ocr, { registerBackend } from '@gutenye/ocr-common'
import { InferenceSession } from 'onnxruntime-web'
import { FileUtils } from './FileUtils'
import { ImageRaw } from './ImageRaw'
import { InferenceSession } from 'onnxruntime-web'

registerBackend({ FileUtils, ImageRaw, InferenceSession })

Expand Down
10 changes: 2 additions & 8 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@
"default": "./build/use-default-models.node.js"
}
},
"files": [
"assets",
"src",
"build",
"tsconfig.json",
"!**/__tests__"
],
"files": ["assets", "src", "build", "tsconfig.json", "!**/__tests__"],
"scripts": {
"prepublishOnly": "./ake build"
},
Expand All @@ -34,4 +28,4 @@
"js-clipper": "^1.0.1",
"tiny-invariant": "^1.3.3"
}
}
}
8 changes: 3 additions & 5 deletions packages/common/src/Ocr.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Detection, Recognition } from './models'
import invariant from 'tiny-invariant'
import { Detection, Recognition } from './models'

export class Ocr {
static DEFAULT_DETECTION_PATH: string
Expand All @@ -17,11 +17,9 @@ export class Ocr {
} = {}) {
const detectionPath = this.DEFAULT_DETECTION_PATH || models?.detectionPath
invariant(detectionPath, 'detectionPath is required')
const recognitionPath =
this.DEFAULT_RECOGINTION_PATH || models?.recognitionPath
const recognitionPath = this.DEFAULT_RECOGINTION_PATH || models?.recognitionPath
invariant(recognitionPath, 'recognitionPath is required')
const dictionaryPath =
this.DEFAULT_DICTIONARY_PATH || models?.dictionaryPath
const dictionaryPath = this.DEFAULT_DICTIONARY_PATH || models?.dictionaryPath
invariant(dictionaryPath, 'dictionaryPath is required')

const detection = await Detection.create({
Expand Down
3 changes: 1 addition & 2 deletions packages/common/src/backend/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import type {

let FileUtils: FileUtilsType = undefined as unknown as FileUtilsType
let ImageRaw: ImageRawType = undefined as unknown as ImageRawType
let InferenceSession: InferenceSessionType =
undefined as unknown as InferenceSessionType
let InferenceSession: InferenceSessionType = undefined as unknown as InferenceSessionType

export function registerBackend(backend: {
FileUtils: FileUtilsType
Expand Down
16 changes: 5 additions & 11 deletions packages/common/src/models/Detection.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Tensor } from 'onnxruntime-common'
import { ImageRaw, InferenceSession } from '#/backend'
import { splitIntoLineImages } from '#/splitIntoLineImages'
import { Tensor } from 'onnxruntime-common'
import type { ModelBaseConstructorArgs, Size } from '#/types'
import { ModelBase } from './ModelBase'
import type { Size, ModelBaseConstructorArgs } from '#/types'

const BASE_SIZE = 32

Expand All @@ -25,7 +25,7 @@ export class Detection extends ModelBase {
// - image width and height must be a multiple of 32
// - bigger image -> more accurate result, but takes longer time
// inputImage = await Image.resize(image, multipleOfBaseSize(image, { maxSize: 960 }))
let inputImage = await image.resize(multipleOfBaseSize(image))
const inputImage = await image.resize(multipleOfBaseSize(image))
this.debugImage(inputImage, './output/out1-multiple-of-base-size.jpg')

// Covert image data to model data
Expand Down Expand Up @@ -59,10 +59,7 @@ export class Detection extends ModelBase {
}
}

function multipleOfBaseSize(
image: ImageRaw,
{ maxSize }: { maxSize?: number } = {},
): Size {
function multipleOfBaseSize(image: ImageRaw, { maxSize }: { maxSize?: number } = {}): Size {
let width = image.width
let height = image.height
if (maxSize && Math.max(width, height) > maxSize) {
Expand All @@ -76,10 +73,7 @@ function multipleOfBaseSize(
Math.ceil(width / BASE_SIZE) * BASE_SIZE,
BASE_SIZE,
)
const newHeight = Math.max(
Math.ceil(height / BASE_SIZE) * BASE_SIZE,
BASE_SIZE,
)
const newHeight = Math.max(Math.ceil(height / BASE_SIZE) * BASE_SIZE, BASE_SIZE)
return { width: newWidth, height: newHeight }
}

Expand Down
20 changes: 3 additions & 17 deletions packages/common/src/models/ModelBase.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { Tensor } from 'onnxruntime-common'
import type {
ModelData,
InferenceSession,
ImageRaw,
ReshapeOptions,
ModelBaseConstructorArgs,
} from '#/types'
import type { ImageRaw, InferenceSession, ModelBaseConstructorArgs, ModelData, ReshapeOptions } from '#/types'

export class ModelBase {
isDebug: boolean
Expand All @@ -27,18 +21,10 @@ export class ModelBase {

#prepareInput(modelData: ModelData) {
const input = Float32Array.from(modelData.data)
return new Tensor('float32', input, [
1,
3,
modelData.height,
modelData.width,
])
return new Tensor('float32', input, [1, 3, modelData.height, modelData.width])
}

imageToInput(
image: ImageRaw,
{ mean = [0, 0, 0], std = [1, 1, 1] }: ReshapeOptions,
): ModelData {
imageToInput(image: ImageRaw, { mean = [0, 0, 0], std = [1, 1, 1] }: ReshapeOptions): ModelData {
const R: number[] = []
const G: number[] = []
const B: number[] = []
Expand Down
Loading

0 comments on commit 8b993c4

Please sign in to comment.