index.html
<view class="container page">
<view>
<canvas type="2d" id="canvas" style="width: {{width}}px; height: {{height}}px"></canvas>
</view>
<view class="btn-cnt" style="position: fixed; top: 0; left: 0;">
<button type="primary" bindtap="chooseMedia">选择图片</button>
<button type="primary" disabled="{{!bodyImgUrl}}" style="margin-top: 20px;" bindtap="detectbody">开始检测</button>
</view>
</view>
index.js
const app = getApp()
Page({
data: {
canvas:null,
width:1,
height:1,
anchor2DList:[],
bodyImgUrl:"",
bodyImgWidth:0,
bodyImgHeight:0,
bodyImgOriginWidth:0,
bodyImgOriginHeight:0
},
onReady() {
wx.createSelectorQuery()
.select('#canvas')
.node()
.exec(res => {
this.canvas = res[0].node
this.calcSize()
this.initVK()
})
},
calcSize(){
const info = wx.getSystemInfoSync()
let width = info.windowWidth
let height = info.windowHeight
this.canvas.width = width
this.canvas.height = height
this.setData({
width,
height,
})
},
initVK() {
const session = this.session = wx.createVKSession({
track: {
body: {
mode: 2
}
},
version: 'v1'
})
session.start(err => {
if (err) return console.error('VK error: ', err)
const canvas = this.canvas
session.on('resize', () => {
this.calcSize()
})
session.on('addAnchors', anchors => {
this.setData({
anchor2DList: anchors.map(anchor => ({
points: anchor.points,
origin: anchor.origin,
size: anchor.size
})),
})
})
session.on('updateAnchors', anchors => {
console.log("anchors : ",anchors)
this.data.anchor2DList = []
this.setData({
anchor2DList: anchors.map(anchor => ({
points: anchor.points,
origin: anchor.origin,
size: anchor.size
})),
})
})
session.on('removeAnchors', anchors => {
this.setData({
anchor2DList: [],
})
this.data.anchor2DList = []
})
})
},
chooseMedia() {
wx.chooseMedia({
count: 1,
mediaType: ['image'],
success: res => {
const imgUrl = res.tempFiles[0].tempFilePath
wx.getImageInfo({
src: imgUrl,
success: res => {
const fixWidth = this.data.width
const {
width,
height
} = res
this.setData({
bodyImgUrl: imgUrl,
bodyImgWidth: fixWidth,
bodyImgHeight: (fixWidth / width) * height,
bodyImgOriginWidth: width,
bodyImgOriginHeight: height
})
},
fail: res => {
console.error(res)
}
})
},
fail: res => {
console.error(res)
}
})
},
async detectbody() {
if (this.data.bodyImgUrl) {
// const canvas = uni.createOffscreenCanvas({
// type: '2d',
// width: this.data.bodyImgOriginWidth,
// height: this.data.bodyImgOriginHeight,
// })
// const context = canvas.getContext('2d')
const canvas = this.canvas
const context = canvas.getContext('2d')
const img = canvas.createImage()
await new Promise(resolve => {
img.onload = resolve
img.src = this.data.bodyImgUrl
})
context.clearRect(0, 0, this.data.bodyImgOriginWidth, this.data.bodyImgOriginHeight)
context.drawImage(img, 0, 0, this.data.bodyImgOriginWidth, this.data.bodyImgOriginHeight)
this.imgData = context.getImageData(0, 0, this.data.bodyImgOriginWidth, this.data.bodyImgOriginHeight)
console.log("imgData : ",this.imgData.data.buffer)
this.session.detectBody({
frameBuffer: this.imgData.data.buffer,
width: this.data.bodyImgOriginWidth,
height: this.data.bodyImgOriginHeight,
scoreThreshold: 0.5,
sourceType: 1
})
}
},
})
静态图片
控制台打印信息
微信版本 8.0.40
基础库版本 3.0.1
请问解决了吗?我现在也被这个问题困扰
谢邀