Skip to content

Commit

Permalink
Rename functions to remove conflict with open pose editor
Browse files Browse the repository at this point in the history
  • Loading branch information
jexom committed Mar 2, 2023
1 parent 53595cc commit 2228af2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 51 deletions.
43 changes: 22 additions & 21 deletions javascript/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ fabric.Object.prototype.borderColor = '#108ce6';
fabric.Object.prototype.cornerSize = 10;
fabric.Object.prototype.lockRotation = false;

let count = 0;
let executed = false;
let depth_obj = {
// width, height
resolution: [512, 512]
}

let lockMode = false;
const undo_history = [];
function gradioApp() {
let depth_executed = false;
function depth_gradioApp() {
const elems = document.getElementsByTagName('gradio-app')
const gradioShadowRoot = elems.length == 0 ? null : elems[0].shadowRoot
return !!gradioShadowRoot ? gradioShadowRoot : document;
}

function calcResolution(resolution){
function depth_calcResolution(resolution){
const width = resolution[0]
const height = resolution[1]
const viewportWidth = window.innerWidth / 2.25;
Expand All @@ -24,11 +25,11 @@ function calcResolution(resolution){
return {width: width * ratio, height: height * ratio}
}

function resizeCanvas(width, height){
function depth_resizeCanvas(width, height){
const elem = depth_lib_elem;
const canvas = depth_lib_canvas;

let resolution = calcResolution([width, height])
let resolution = depth_calcResolution([width, height])

canvas.setWidth(width);
canvas.setHeight(height);
Expand All @@ -40,7 +41,7 @@ function resizeCanvas(width, height){
elem.parentElement.style.height = resolution["height"] + "px"
}

function addImg(path){
function depth_addImg(path){
const canvas = depth_lib_canvas;
fabric.Image.fromURL(path, function(oImg) {
canvas.add(oImg);
Expand All @@ -50,7 +51,7 @@ function addImg(path){
canvas.requestRenderAll();
}

function initCanvas(elem){
function depth_initCanvas(elem){
const canvas = window.depth_lib_canvas = new fabric.Canvas(elem, {
backgroundColor: '#000',
// selection: false,
Expand All @@ -60,16 +61,16 @@ function initCanvas(elem){
window.depth_lib_elem = elem


resizeCanvas(...openpose_obj.resolution)
depth_resizeCanvas(...depth_obj.resolution)
}

function resetCanvas(){
function depth_resetCanvas(){
const canvas = depth_lib_canvas;
canvas.clear()
canvas.backgroundColor = "#000"
}

function savePNG(){
function depth_savePNG(){
if (depth_lib_canvas.backgroundImage) depth_lib_canvas.backgroundImage.opacity = 0
depth_lib_canvas.discardActiveObject();
depth_lib_canvas.renderAll()
Expand All @@ -90,7 +91,7 @@ function savePNG(){
return
}

function addBackground(){
function depth_addBackground(){
const input = document.createElement("input");
input.type = "file"
input.accept = "image/*"
Expand All @@ -109,7 +110,7 @@ function addBackground(){
input.click()
}

function sendImage(){
function depth_sendImage(){
if (depth_lib_canvas.backgroundImage) depth_lib_canvas.backgroundImage.opacity = 0
depth_lib_canvas.discardActiveObject();
depth_lib_canvas.renderAll()
Expand All @@ -118,7 +119,7 @@ function sendImage(){
const dt = new DataTransfer();
dt.items.add(file);
const list = dt.files
gradioApp().querySelector("#txt2img_script_container").querySelectorAll("span.transition").forEach((elem) => {
depth_gradioApp().querySelector("#txt2img_script_container").querySelectorAll("span.transition").forEach((elem) => {
if (elem.previousElementSibling.textContent === "ControlNet"){
switch_to_txt2img()
elem.className.includes("rotate-90") && elem.parentElement.click();
Expand All @@ -143,14 +144,14 @@ function sendImage(){

window.addEventListener('DOMContentLoaded', () => {
const observer = new MutationObserver((m) => {
if(!executed && gradioApp().querySelector('#depth_lib_canvas')){
executed = true;
initCanvas(gradioApp().querySelector('#depth_lib_canvas'))
// gradioApp().querySelectorAll("#tabs > div > button").forEach((elem) => {
if(!depth_executed && depth_gradioApp().querySelector('#depth_lib_canvas')){
depth_executed = true;
depth_initCanvas(depth_gradioApp().querySelector('#depth_lib_canvas'))
// depth_gradioApp().querySelectorAll("#tabs > div > button").forEach((elem) => {
// if (elem.innerText === "OpenPose Editor") elem.click()
// })
observer.disconnect();
}
})
observer.observe(gradioApp(), { childList: true, subtree: true })
observer.observe(depth_gradioApp(), { childList: true, subtree: true })
})
38 changes: 8 additions & 30 deletions scripts/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,12 @@

from basicsr.utils.download_util import load_file_from_url

body_estimation = None

def pil2cv(in_image):
out_image = np.array(in_image, dtype=np.uint8)

if out_image.shape[2] == 3:
out_image = cv2.cvtColor(out_image, cv2.COLOR_RGB2BGR)
return out_image

def candidate2li(li):
res = []
for x, y, *_ in li:
res.append([x, y])
return res

def subset2li(li):
res = []
for r in li:
for c in r:
res.append(c)
return res

class Script(scripts.Script):
def __init__(self) -> None:
super().__init__()

def title(self):
return "Hand Poser"
return "Depth Library"

def show(self, is_img2img):
return scripts.AlwaysVisible
Expand Down Expand Up @@ -70,13 +48,13 @@ def on_ui_tabs():
send_output = gr.Button(value="Send to ControlNet")


width.change(None, [width, height], None, _js="(w, h) => {resizeCanvas(w, h)}")
height.change(None, [width, height], None, _js="(w, h) => {resizeCanvas(w, h)}")
png_output.click(None, [], None, _js="savePNG")
bg_input.click(None, [], None, _js="addBackground")
add.click(None, [png_input_area], None, _js="(path) => {addImg(path)}")
send_output.click(None, [], None, _js="sendImage")
reset_btn.click(None, [], None, _js="resetCanvas")
width.change(None, [width, height], None, _js="(w, h) => {depth_resizeCanvas(w, h)}")
height.change(None, [width, height], None, _js="(w, h) => {depth_resizeCanvas(w, h)}")
png_output.click(None, [], None, _js="depth_savePNG")
bg_input.click(None, [], None, _js="depth_addBackground")
add.click(None, [png_input_area], None, _js="(path) => {depth_addImg(path)}")
send_output.click(None, [], None, _js="depth_sendImage")
reset_btn.click(None, [], None, _js="depth_resetCanvas")

return [(depth_lib, "Depth Library", "depth_lib")]

Expand Down

0 comments on commit 2228af2

Please sign in to comment.