Skip to content

Commit

Permalink
v1.0.0更新
Browse files Browse the repository at this point in the history
  • Loading branch information
luoyily committed Feb 9, 2024
1 parent 8e95ba5 commit 2f5b756
Show file tree
Hide file tree
Showing 9 changed files with 385 additions and 158 deletions.
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# ---------------------------------node js---------------------------------
# dependencies
/node_modules
*/node_modules
/.pnp
.pnp.js

Expand Down Expand Up @@ -186,3 +186,12 @@ cython_debug/

# ---------------------------------Custom---------------------------------
*.zip
webui/config/
webui/package-lock.json
webui/scripts
*.woff2
icon.png
test_img
models
settings.json
test.ipynb
54 changes: 49 additions & 5 deletions moe_sr.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

import re
import math
import traceback
from pathlib import Path
import json

import eel
import numpy as np
Expand All @@ -21,6 +23,8 @@ def __init__(self, name, path, scale, algo):
model_list = []
sr_instance = None
port = 10721
last_progress = None
last_progress_set_time = None
# Scan models
model_root = Path('models')
for algo in ['real-esrgan', 'real-hatgan']:
Expand All @@ -44,9 +48,44 @@ def py_get_model_list(algo_name):
models = [m.name for m in model_list if m.algo == algo_name]
return models

@eel.expose
def py_get_settings():
setting_file = open('settings.json','r',encoding='utf-8')
settings = json.load(setting_file)
setting_file.close()
return settings

def progress_setter(data):
eel.handleSetProgress(round(data*100))
@eel.expose
def py_save_settings(new_settings):
setting_file = open('settings.json','w',encoding='utf-8')
settings = json.dumps(new_settings,ensure_ascii=False)
setting_file.write(settings)
setting_file.close()
return 0

def seconds_to_hms(seconds):
hours = seconds // 3600
minutes = (seconds % 3600) // 60
seconds = seconds % 60

return f"{int(hours):0>2d}:{int(minutes):0>2d}:{int(seconds):0>2d}"

def progress_setter(progress,current_time,total_img_num,processed_img_num):
global last_progress,last_progress_set_time
progress_percent = round(progress*100)
total_progress_percent = round((processed_img_num+progress)/total_img_num*100)
etr_str = '--:--:--'
total_etr_str = '--:--:--'
if last_progress_set_time:
etr = (current_time-last_progress_set_time) * (1-last_progress)/(progress-last_progress)
total_etr = (current_time-last_progress_set_time) * (total_img_num-processed_img_num-last_progress)/(progress-last_progress)
etr_str = seconds_to_hms(etr)
total_etr_str = seconds_to_hms(total_etr)
progress_str = f'{progress_percent}% ETR:{etr_str}'
total_progress_str = f'{total_progress_percent}% ETR:{total_etr_str}'
eel.handleSetProgress(progress_percent,progress_str,total_progress_str)
last_progress = progress
last_progress_set_time = current_time


def show_error(error_text):
Expand All @@ -58,7 +97,7 @@ def set_process_state(state):


@eel.expose
def py_run_process(modelName, tileSize, scale, isSkipAlpha, resizeTo: str, inputType, inputImage, outputPath, gpuid):
def py_run_process(modelName, tileSize, scale, isSkipAlpha, resizeTo: str, inputType, inputImage, outputPath, gpuid,algoName):
global sr_instance
try:
# find model info
Expand All @@ -67,17 +106,18 @@ def py_run_process(modelName, tileSize, scale, isSkipAlpha, resizeTo: str, input
if int(gpuid) >= 0:
provider_options = [{'device_id': int(gpuid)}]
for m in model_list:
if m.name == modelName:
if m.name == modelName and m.algo == algoName:
model = m
break
# init or change sr instance
if not sr_instance:
sr_instance = OnnxSRInfer(model.path, model.scale, model.name,
provider_options=provider_options, progress_setter=progress_setter)
elif sr_instance.name != model.name:
elif sr_instance.model_path != model.path:
del sr_instance
sr_instance = OnnxSRInfer(model.path, model.scale, model.name,
provider_options=provider_options, progress_setter=progress_setter)
print(f'Model Change: {model.path}')
# skip alpha sr
if isSkipAlpha:
sr_instance.alpha_upsampler = 'interpolation'
Expand All @@ -92,6 +132,8 @@ def py_run_process(modelName, tileSize, scale, isSkipAlpha, resizeTo: str, input
imgs_in.append(f)
else:
imgs_in = [inputImage]
sr_instance.total_img_num = len(imgs_in)
sr_instance.processed_img_num = 0
# sr process
for img_in in imgs_in:
img = cv2.imdecode(np.fromfile(img_in,dtype=np.uint8),cv2.IMREAD_UNCHANGED)
Expand Down Expand Up @@ -138,6 +180,7 @@ def py_run_process(modelName, tileSize, scale, isSkipAlpha, resizeTo: str, input
final_output_path = Path(outputPath) / f'{img_in_name}_{img_in_ext}_MoeSR_{model.name}.png'
# cv2.imwrite(str(final_output_path), img_out)
cv2.imencode('.png',img_out)[1].tofile(final_output_path)
sr_instance.processed_img_num += 1
set_process_state('finish')
except Exception as e:
sr_instance = None
Expand All @@ -147,3 +190,4 @@ def py_run_process(modelName, tileSize, scale, isSkipAlpha, resizeTo: str, input


eel.start('index.html', mode='custom', cmdline_args=['electron/electron.exe', 'electron_app/main.js'], port=port)
# eel.start('index.html', mode='custom', cmdline_args=['E:/python/MoeSR/electron/electron.exe', 'webui/main.js'], port=port)
6 changes: 5 additions & 1 deletion onnx_infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import cv2
import numpy as np
import math
import time

class OnnxSRInfer:

Expand All @@ -24,6 +25,9 @@ def __init__(self, model_path,scale,name,
self.scale = scale
self.alpha_upsampler = alpha_upsampler
self.progress_setter = progress_setter
self.model_path = model_path
self.total_img_num = 1
self.processed_img_num = 0

def img_array_norm_expd(self,img):
img = np.array(img).astype(np.float32) / 255.0
Expand Down Expand Up @@ -137,7 +141,7 @@ def tile_process(self, img, tile_size,tile_pad=16):
# put tile into output image
output[output_start_y:output_end_y,output_start_x:output_end_x,:] \
= output_tile[output_start_y_tile:output_end_y_tile,output_start_x_tile:output_end_x_tile,:]
self.progress_setter(tile_idx/tiles_x/tiles_y)
self.progress_setter(tile_idx/tiles_x/tiles_y,time.time(),self.total_img_num,self.processed_img_num)

return output
def rgb_process_pipeline(self, image, tile_size):
Expand Down
32 changes: 26 additions & 6 deletions webui/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import Navbar from './Components/Navbar';
import InferenceUI from './Components/InferenceUI';
import { createTheme, ThemeProvider } from '@mui/material/styles';
import BackGround from './Components/Background';
import { useState } from 'react';
import { useState, useEffect } from 'react';
import Help from './Components/Help';
import About from './Components/About';
import Settings from './Components/Settings';
import translations from './Language';
var webDevMode = false;
const theme = createTheme({
typography: {
fontFamily: 'NotoSerifSC, Arial',
Expand Down Expand Up @@ -38,20 +41,37 @@ const theme = createTheme({
// },
// },
});

async function getSettings(webDevMode) {
if (webDevMode) {
let dummyData = {'language':'English'}
return dummyData
}
else {
return await window.eel.py_get_settings()()
}
}
function App() {
const [navigation, setNavigation] = useState('real-esrgan');

const [lang,SetLang] = useState('English');
const langMap = {'English':'en','简体中文':'zh'};
useEffect(() => {
getSettings(webDevMode).then(result => { SetLang(result['language']) })
console.log('Effect run ' + lang)
}, []);// eslint-disable-line
if (!(lang in langMap)){
SetLang('English')
}
const texts = translations[langMap[lang]]
var content;
// real-esrgan or real-hatgan
if ((navigation === 'real-esrgan') || (navigation === 'real-hatgan')) {
content = <InferenceUI algoName={navigation}></InferenceUI>
content = <InferenceUI algoName={navigation} webDevMode={webDevMode} texts={texts}></InferenceUI>
}
else if (navigation === 'settings') {
content = <p>Todo...</p>
content = <Settings langSetter={SetLang} webDevMode={webDevMode} language={lang}></Settings>
}
else if (navigation === 'help') {
content = <Help></Help>
content = <Help language={lang}></Help>
}
else if (navigation === 'about') {
content = <About></About>
Expand Down
Loading

0 comments on commit 2f5b756

Please sign in to comment.