File size: 5,925 Bytes
ea35075 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
import { getImage } from '../../utils/image'
import {
CopyTextToClipboard,
uploadImage,
uploadJson,
} from '../../utils/transfer'
import { DetectPosefromImage } from '../../utils/detect'
import { BodyControlor } from '../../body'
import { GetLoading } from '../../components/Loading'
import { BodyEditor } from '../../editor'
import i18n from '../../i18n'
import { Oops } from '../../components/Oops'
import assets from '../../assets'
import { ShowToast } from '../../components/Toast'
import { GetRandomPose, LoadPosesLibrary } from '../../pose-library'
export class Helper {
editor: BodyEditor
constructor(editor: BodyEditor) {
this.editor = editor
}
async DetectFromImage(onChangeBackground: (url: string) => void) {
const body = await this.editor.GetBodyToSetPose()
if (!body) {
ShowToast({ title: i18n.t('Please select a skeleton!!') })
return
}
const loading = GetLoading(500)
try {
const dataUrl = await uploadImage()
if (!dataUrl) return
const image = await getImage(dataUrl)
onChangeBackground(dataUrl)
loading.show({ title: i18n.t('Downloading MediaPipe Pose Model') })
const result = await DetectPosefromImage(image)
loading.hide()
if (result) {
if (!result.poseWorldLandmarks)
throw new Error(JSON.stringify(result))
const positions: [number, number, number][] =
result.poseWorldLandmarks.map(({ x, y, z }) => [
x * 100,
-y * 100,
-z * 100,
])
// this.drawPoseData(
// result.poseWorldLandmarks.map(({ x, y, z }) =>
// new THREE.Vector3().fromArray([x * 100, -y * 100, -z * 100])
// )
// )
await this.editor.SetBlazePose(positions)
return
}
} catch (error) {
loading.hide()
Oops(
i18n.t(
'If you try to detect anime characters, you may get an error. Please try again with photos.'
) +
'\n' +
error
)
console.error(error)
return null
}
}
async CopyKeypointToClipboard() {
const body = await this.editor.GetBodyToSetPose()
if (!body) {
ShowToast({ title: i18n.t('Please select a skeleton!!') })
return
}
try {
const data = new BodyControlor(body).Get18keyPointsData()
await CopyTextToClipboard(JSON.stringify(data, null, 4))
ShowToast({ title: i18n.t('Copied to Clipboard') })
} catch (error) {
Oops(error)
console.error(error)
return null
}
}
async SaveGesture() {
const hand = await this.editor.getSelectedHand()
if (!hand) {
ShowToast({ title: i18n.t('Please select a hand!!') })
return
}
try {
this.editor.SaveGesture()
} catch (error) {
Oops(error)
console.error(error)
return null
}
}
async LoadGesture() {
const hand = await this.editor.getSelectedHand()
if (!hand) {
ShowToast({ title: i18n.t('Please select a hand!!') })
return
}
const rawData = await uploadJson()
if (!rawData) return
try {
this.editor.RestoreGesture(rawData)
} catch (error) {
Oops(error)
console.error(error)
return null
}
}
async GenerateSceneURL() {
try {
const d = encodeURIComponent(
JSON.stringify(this.editor.GetSceneData())
)
const url_base = location.href.replace(/#$/, '')
const url = `${url_base}#${d}`
await CopyTextToClipboard(url)
ShowToast({ title: i18n.t('Copied to Clipboard') })
} catch (error) {
Oops(error)
console.error(error)
}
}
async SetRandomPose() {
const body = await this.editor.GetBodyToSetPose()
if (!body) {
ShowToast({ title: i18n.t('Please select a skeleton!!') })
return
}
const loading = GetLoading(500)
try {
let poseData = GetRandomPose()
if (poseData) {
await this.editor.SetPose(poseData)
return
}
loading.show({ title: i18n.t('Downloading Poses Library') })
await LoadPosesLibrary(assets['src/poses/data.bin'])
loading.hide()
poseData = GetRandomPose()
if (poseData) {
await this.editor.SetPose(poseData)
return
}
} catch (error) {
loading.hide()
Oops(error)
console.error(error)
return
}
}
async CopySkeleton() {
const body = this.editor.getSelectedBody()
if (!body) {
ShowToast({ title: i18n.t('Please select a skeleton!!') })
return
}
this.editor.CopySelectedBody()
}
async RemoveSkeleton() {
const body = this.editor.getSelectedBody()
if (!body) {
ShowToast({ title: i18n.t('Please select a skeleton!!') })
return
}
this.editor.RemoveBody()
}
FeedbackByQQ() {
window.open('https://jq.qq.com/?_wv=1027&k=N6j4nigd')
}
FeedbackByGithub() {
window.open(
'https://github.com/nonnonstop/sd-webui-3d-open-pose-editor/issues/new/choose'
)
}
}
|