-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Deblurring - Denoising - Deraining - Dehazing - Indoor - Outdoor - Low Light Enhancement - Retouching
- Loading branch information
1 parent
b278cee
commit 3599678
Showing
319 changed files
with
3,818 additions
and
132 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
models |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { dependencies, devDependencies } from '../package.json'; | ||
import { loadPackageModels } from './utils'; | ||
|
||
export const AVAILABLE_PACKAGES = Object.keys({ | ||
...dependencies, | ||
...devDependencies, | ||
}).reduce((set, key) => { | ||
if (key.includes('@upscalerjs')) { | ||
const packageName = key.split('@upscalerjs/').pop(); | ||
if (packageName) { | ||
set.add(packageName); | ||
} | ||
} | ||
return set; | ||
}, new Set<string>()); | ||
|
||
export const loadAvailableModels = async (packageName: string) => { | ||
const exports = await loadPackageModels(packageName); | ||
return Object.keys(exports); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,58 @@ | ||
export const makeImg = (path: string, label: string) => { | ||
export const getCanvas = (img: HTMLImageElement) => { | ||
const canvas = document.createElement('canvas'); | ||
canvas.width = img.width; | ||
canvas.height = img.height; | ||
canvas.getContext('2d')?.drawImage(img, 0, 0, canvas.width, canvas.height); | ||
return canvas; | ||
}; | ||
|
||
const scaleCanvas = (canvas: HTMLCanvasElement, scale: number) => { | ||
const scaledCanvas = document.createElement('canvas'); | ||
scaledCanvas.width = canvas.width * scale; | ||
scaledCanvas.height = canvas.height * scale; | ||
|
||
scaledCanvas | ||
.getContext('2d') | ||
?.drawImage(canvas, 0, 0, scaledCanvas.width, scaledCanvas.height); | ||
|
||
return scaledCanvas; | ||
}; | ||
|
||
export const resizeImage = (img: HTMLImageElement, scale: number) => { | ||
const canvas = getCanvas(img); | ||
const scaledCanvas = scaleCanvas(canvas, scale); | ||
return scaledCanvas; | ||
}; | ||
|
||
const loadImage = (path: string) => new Promise<HTMLImageElement>((resolve, reject) => { | ||
const img = new Image(); | ||
img.src = path; | ||
img.onload = () => { | ||
const divEl = document.createElement('div'); | ||
const imgEl = document.createElement('img'); | ||
const labelEl = document.createElement('label'); | ||
labelEl.innerText = label; | ||
imgEl.src = path; | ||
imgEl.width = img.width; | ||
imgEl.height = img.height; | ||
imgEl.appendChild(img); | ||
|
||
divEl.appendChild(labelEl); | ||
divEl.appendChild(imgEl); | ||
divEl.appendChild(document.createElement('hr')); | ||
|
||
document.body.appendChild(divEl); | ||
return imgEl; | ||
resolve(img); | ||
}; | ||
img.onerror = reject; | ||
}); | ||
|
||
export const makeImg = async (path: string, label: string, scale?: number): Promise<HTMLImageElement | HTMLCanvasElement> => { | ||
let img: HTMLImageElement | HTMLCanvasElement = await loadImage(path); | ||
|
||
if (scale) { | ||
img = resizeImage(img, scale); | ||
} | ||
|
||
const divEl = document.createElement('div'); | ||
const imgEl = document.createElement('img'); | ||
const labelEl = document.createElement('label'); | ||
labelEl.innerText = label; | ||
imgEl.src = path; | ||
imgEl.width = img.width; | ||
imgEl.height = img.height; | ||
imgEl.appendChild(img); | ||
|
||
divEl.appendChild(labelEl); | ||
divEl.appendChild(imgEl); | ||
divEl.appendChild(document.createElement('hr')); | ||
|
||
document.body.appendChild(divEl); | ||
return imgEl; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,21 @@ | ||
label { | ||
display: block; | ||
} | ||
|
||
#description { | ||
display: flex; | ||
gap: 12px; | ||
padding: 20px; | ||
padding-bottom: 10px; | ||
margin-bottom: 10px; | ||
border-bottom: 1px solid rgba(0,0,0,0.2); | ||
align-items: flex-end; | ||
} | ||
|
||
#description p { | ||
margin: 0; | ||
} | ||
|
||
select { | ||
padding: 10px; | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,80 @@ | ||
import Upscaler from '../../../packages/upscalerjs/src/index'; | ||
import model from '../../../models/esrgan-legacy/src/gans'; | ||
import flower from '../../../models/esrgan-legacy/test/__fixtures__/fixture.png'; | ||
import * as tf from '@tensorflow/tfjs'; | ||
// import { ModelDefinitionFn, } from '../../../packages/core/src/index'; | ||
import { makeImg } from './image'; | ||
const MODEL = '/node_modules/@upscalerjs/esrgan-legacy/models/gans/model.json'; | ||
import { AVAILABLE_PACKAGES, loadAvailableModels } from './filters'; | ||
// import Upscaler, { ModelDefinition } from 'upscaler'; | ||
import Upscaler, { ModelDefinition } from '../../../packages/upscalerjs/src/index'; | ||
import { getFixture, getModelPath, getRoot, importModel, loadPackageJSON } from './utils'; | ||
|
||
const status = document.getElementById('status')!; | ||
const packages = document.getElementById('packages') as HTMLSelectElement; | ||
const models = document.getElementById('models') as HTMLSelectElement; | ||
const status = document.getElementById('status') as HTMLDivElement; | ||
|
||
const getModel = (path: string) => { | ||
const { packageInformation, ...rest } = model(tf); | ||
return { | ||
...rest, | ||
path: path, | ||
}; | ||
const addOptions = (target: Element, arr: Set<string> | string[], includeFirstBlank = true) => { | ||
if (includeFirstBlank) { | ||
const option = document.createElement('option'); | ||
target.appendChild(option); | ||
} | ||
|
||
arr.forEach(value => { | ||
const option = document.createElement('option'); | ||
option.innerHTML = value; | ||
target.appendChild(option); | ||
}); | ||
} | ||
|
||
(async () => { | ||
makeImg(flower, 'Original'); | ||
const model = getModel(MODEL); | ||
addOptions(packages, AVAILABLE_PACKAGES); | ||
|
||
packages.addEventListener('change', async (e) => { | ||
models.innerHTML = ''; | ||
const target = e.target as HTMLSelectElement; | ||
const packageName = target.value; | ||
const availableModels = await loadAvailableModels(packageName); | ||
|
||
addOptions(models, availableModels, availableModels.length !== 1); | ||
if (availableModels.length === 1) { | ||
const modelName = availableModels[0]; | ||
await loadModel(packages.value, modelName); | ||
} | ||
}); | ||
|
||
models.addEventListener('change', async (e) => { | ||
const target = e.target as HTMLSelectElement; | ||
const modelName = target.value; | ||
await loadModel(packages.value, modelName); | ||
}); | ||
|
||
const loadModel = async (packageName: string, modelName: string) => { | ||
console.log(packageName, modelName); | ||
const importedModel = await importModel(packageName, modelName); | ||
const modelDefinition = (importedModel).default; | ||
const { packageInformation, ...modelJSON }= typeof modelDefinition === 'function' ? modelDefinition(tf) : modelDefinition; | ||
|
||
const fixture = await getFixture(packageName, modelName); | ||
|
||
const img = await makeImg(fixture, `Original: ${packageName}/${modelName}`, .25); | ||
const modelPath = getModelPath(packageName, modelJSON.path); | ||
const upscaledImg = await upscaleImage({ | ||
...modelJSON, | ||
path: modelPath, | ||
}, img); | ||
await makeImg(upscaledImg, `Upscaled: ${packageName}/${modelName}`); | ||
}; | ||
|
||
|
||
const upscaleImage = async (model: ModelDefinition, img: HTMLImageElement | HTMLCanvasElement, patchSize: undefined | number = 64) => { | ||
status.innerHTML = 'Starting'; | ||
const upscaler = new Upscaler({ | ||
model, | ||
}); | ||
status.innerHTML = 'Upscaling...'; | ||
const upscaledImg = await upscaler.upscale(flower, { | ||
patchSize: 8, | ||
padding: 1, | ||
progressOutput: 'tensor', | ||
progress: (rate, slice, row, col) => { | ||
console.log(rate, slice.shape.slice(0, 2), row, col); | ||
}, | ||
const start = performance.now(); | ||
const upscaledImg = await upscaler.upscale(img, { | ||
patchSize, | ||
progress: console.log, | ||
}); | ||
console.log(`Duration: ${((performance.now() - start) / 1000).toFixed(2)}s`); | ||
status.innerHTML = 'Image upscaled'; | ||
makeImg(upscaledImg, 'Upscaled'); | ||
status.innerHTML = 'Image printed'; | ||
})(); | ||
return upscaledImg; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
export const getRoot = (packageName: string) => `/node_modules/@upscalerjs/${packageName}`; | ||
export const loadPackageJSON = async (packageName: string) => { | ||
const r = await fetch(`${getRoot(packageName)}/package.json?ts=${new Date().getTime()}`); | ||
return r.json(); | ||
}; | ||
|
||
export const loadPackageModels = async (packageName: string) => { | ||
const { exports } = await loadPackageJSON(packageName); | ||
return exports; | ||
} | ||
|
||
const loadPackageModel = async (packageName: string, modelName: string) => { | ||
const exports = await loadPackageModels(packageName); | ||
const modelSourceFiles = exports[modelName]; | ||
return modelSourceFiles; | ||
} | ||
|
||
export const importModel = async (packageName: string, modelName = '.') => { | ||
const modelSourceFiles = await loadPackageModel(packageName, modelName); | ||
const importPath = `${getRoot(packageName)}/${modelSourceFiles.import}?ts=${new Date().getTime()}`; | ||
return import(importPath); | ||
}; | ||
|
||
export const getModelPath = (packageName: string, modelPath: string) => { | ||
return `${getRoot(packageName)}/${modelPath}`; | ||
} | ||
|
||
export const getFixture = async (packageName: string, modelName = '.') => { | ||
const packageJSON = await loadPackageJSON(packageName); | ||
if (packageJSON['@upscalerjs']?.assets) { | ||
const fixture = packageJSON['@upscalerjs'].assets[modelName]; | ||
if (!fixture) { | ||
throw new Error(`NO fixture found for ${packageName}/${modelName}`) | ||
} | ||
return `${getRoot(packageName)}/${fixture}?ts=${new Date().getTime()}`; | ||
} | ||
return `${getRoot(packageName)}/assets/fixture.png?ts=${new Date().getTime()}`; | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"compilerOptions": { | ||
"resolveJsonModule": true | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
import { defineConfig, } from 'vite'; | ||
import path from 'path'; | ||
|
||
// const ROOT = path.resolve(__dirname, '../'); | ||
|
||
export default defineConfig({ | ||
// root: path.resolve(ROOT, './dev'), | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
link-workspace-packages=false |
This file contains 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
Empty file.
File renamed without changes
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
dist | ||
node_modules | ||
*.generated.ts | ||
/models |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
src | ||
yarn-error.log | ||
node_modules | ||
test |
Oops, something went wrong.