class estUtil():
#封装的关键点检测类
def __init__(self):
super(estUtil, self).__init_ _()
# 使用human_pose_estimation_resnet50_mpii模型
self. module = hub.Module(name= 'human_pose_estimation_resnet50_mpii')
def do_est(self, frame):
res = self. module.keypoint_detection(images=[frame], use_gpu=True)
return res[ 0][ 'data']
def complexres(res, FatherAndSon):
#扩充关键点,但仍然要保持逻辑上的关键点的节点顺序
cres = copy.deepcopy(res)
for key,pos in res.items():
father = FatherAndSon[key]
if father == key:
#当时根节点的时候停止
continue
if key[0] == 'm' or father[0] == 'm':
#子节点第一种命名规则
midkey = 'm'+key+'_'+father
else:
kn = ''
for t in key.split('_'):
kn += t[0]
fn = ''
for t in father.split('_'):
fn += t[0]
#子节点第二种命名规则
midkey = 'm _'+kn+'_'+fn
#计算中点,并把结果按逻辑顺序存到字典中
midvalue = [ (pos[0] + res[ father][ 0]) / 2, (pos[ 1] + res[ father][ 1])/2]
FatherAndSon[key] = midkey
FatherAndSon[midkey] = father
cres[midkey] = midvalue
return cres, FatherAndSon
涂鸦的记录与优化
def linesFilter():
global lines
for line in lines:
linelen = len(line)
sindex = 0
mindex = 1
while mindex < len(line):
eindex = mindex + 1
if eindex >= len(line):
break
d1 = line[ mindex][ 0] - line[ sindex][ 0]
d2 = line[ mindex][ 1] - line[ sindex][ 1]
d3 = line[ eindex][ 0] - line[ sindex][ 0]
d4 = line[ eindex][ 1] - line[ sindex][ 1]
#判断三个点是否在一条直线上
if abs(d1 *d4-d2*d3) <= 1e-6:
line.pop(mindex)
else:
sindex += 1
mindex += 1
def linesCompose():
#防止删除点过多,在每两个点中间插值出一个新的点
global lines
tlines = []
for line in lines:
tlines.append([line[0]])
for i in range(1,len(line)):
l_1 = tlines[-1][-1]
tlines[-1].append(((l_1[0] + line[i][0]) / 2,(l_1[1] + line[i][1]) / 2))
tlines[-1].append((line[i]))
lines = tlines
关键点与涂鸦的绑定
def buildskin(lines, colors, cirRads, nodes):
if lines is None or nodes is None or len(lines) == 0 or len(nodes) == 0:
return []
skins = []
print( "doodle node length", len(nodes))
#将opencv获取的皮肤点列表封装成skinItem类的对象列表
for lineindex in range(len(lines)):
init = True
line = lines[lineindex]
color = colors[lineindex]
cirRad = cirRads[lineindex]
for p in line:
if init:
skins.append(skinItem(p[ 0], p[ 1], True, color, cirRad))
init = False
else:
skins.append(skinItem(p[ 0], p[ 1], False, color, cirRad))
#计算每个skinItem对象最近的四个骨骼点并封装为锚点
for skin in skins:
md = [float( "inf"), float( "inf"), float( "inf"), float( "inf")]
mn = [ None, None, None, None]
mdlen = 0
for key,node in nodes.items():
d = distance(skin.getPos(), node.getPos())
maxi = judge(md)
if d < md[maxi]:
md[maxi] = d
mn[maxi] = node
mdlen += 1
if mdlen < 4:
md = md[:mdlen]
mn = mn[:mdlen]
ws = dist2weight(md)
# 分配每个锚点的权重
for j in range(len(mn)):
th = math.atan2(skin.y-mn[j].y, skin.x-mn[j].x)
r = distance(skin.getPos(), mn[j].getPos())
w = ws[j]
skin.appendAnchor(anchorItem(mn[j], th-mn[j].thabs, r, w))
return skins
涂鸦的更新
def calculateSkin(skins, scale):
for skin in skins:
xw = 0
yw = 0
#根据皮肤点每个锚点的坐标与角度,计算出新的皮肤点的坐标
for anchor in skin.getAnchor():
x = anchor.node.x + math.cos(anchor.th+anchor.node.thabs) * anchor.r * scale
y = anchor.node.y + math.sin(anchor.th+anchor.node.thabs) * anchor.r * scale
xw += x * anchor.w
yw += y * anchor.w
skin.x = xw
skin.y = yw
return skins
目前的一些问题与改进方向
相关推荐
觉得内容不错的话,点个“在看”呗
class estUtil():
#封装的关键点检测类
def __init__(self):
super(estUtil, self).__init_ _()
# 使用human_pose_estimation_resnet50_mpii模型
self. module = hub.Module(name= 'human_pose_estimation_resnet50_mpii')
def do_est(self, frame):
res = self. module.keypoint_detection(images=[frame], use_gpu=True)
return res[ 0][ 'data']
def complexres(res, FatherAndSon):
#扩充关键点,但仍然要保持逻辑上的关键点的节点顺序
cres = copy.deepcopy(res)
for key,pos in res.items():
father = FatherAndSon[key]
if father == key:
#当时根节点的时候停止
continue
if key[0] == 'm' or father[0] == 'm':
#子节点第一种命名规则
midkey = 'm'+key+'_'+father
else:
kn = ''
for t in key.split('_'):
kn += t[0]
fn = ''
for t in father.split('_'):
fn += t[0]
#子节点第二种命名规则
midkey = 'm _'+kn+'_'+fn
#计算中点,并把结果按逻辑顺序存到字典中
midvalue = [ (pos[0] + res[ father][ 0]) / 2, (pos[ 1] + res[ father][ 1])/2]
FatherAndSon[key] = midkey
FatherAndSon[midkey] = father
cres[midkey] = midvalue
return cres, FatherAndSon
涂鸦的记录与优化
def linesFilter():
global lines
for line in lines:
linelen = len(line)
sindex = 0
mindex = 1
while mindex < len(line):
eindex = mindex + 1
if eindex >= len(line):
break
d1 = line[ mindex][ 0] - line[ sindex][ 0]
d2 = line[ mindex][ 1] - line[ sindex][ 1]
d3 = line[ eindex][ 0] - line[ sindex][ 0]
d4 = line[ eindex][ 1] - line[ sindex][ 1]
#判断三个点是否在一条直线上
if abs(d1 *d4-d2*d3) <= 1e-6:
line.pop(mindex)
else:
sindex += 1
mindex += 1
def linesCompose():
#防止删除点过多,在每两个点中间插值出一个新的点
global lines
tlines = []
for line in lines:
tlines.append([line[0]])
for i in range(1,len(line)):
l_1 = tlines[-1][-1]
tlines[-1].append(((l_1[0] + line[i][0]) / 2,(l_1[1] + line[i][1]) / 2))
tlines[-1].append((line[i]))
lines = tlines
关键点与涂鸦的绑定
def buildskin(lines, colors, cirRads, nodes):
if lines is None or nodes is None or len(lines) == 0 or len(nodes) == 0:
return []
skins = []
print( "doodle node length", len(nodes))
#将opencv获取的皮肤点列表封装成skinItem类的对象列表
for lineindex in range(len(lines)):
init = True
line = lines[lineindex]
color = colors[lineindex]
cirRad = cirRads[lineindex]
for p in line:
if init:
skins.append(skinItem(p[ 0], p[ 1], True, color, cirRad))
init = False
else:
skins.append(skinItem(p[ 0], p[ 1], False, color, cirRad))
#计算每个skinItem对象最近的四个骨骼点并封装为锚点
for skin in skins:
md = [float( "inf"), float( "inf"), float( "inf"), float( "inf")]
mn = [ None, None, None, None]
mdlen = 0
for key,node in nodes.items():
d = distance(skin.getPos(), node.getPos())
maxi = judge(md)
if d < md[maxi]:
md[maxi] = d
mn[maxi] = node
mdlen += 1
if mdlen < 4:
md = md[:mdlen]
mn = mn[:mdlen]
ws = dist2weight(md)
# 分配每个锚点的权重
for j in range(len(mn)):
th = math.atan2(skin.y-mn[j].y, skin.x-mn[j].x)
r = distance(skin.getPos(), mn[j].getPos())
w = ws[j]
skin.appendAnchor(anchorItem(mn[j], th-mn[j].thabs, r, w))
return skins
涂鸦的更新
def calculateSkin(skins, scale):
for skin in skins:
xw = 0
yw = 0
#根据皮肤点每个锚点的坐标与角度,计算出新的皮肤点的坐标
for anchor in skin.getAnchor():
x = anchor.node.x + math.cos(anchor.th+anchor.node.thabs) * anchor.r * scale
y = anchor.node.y + math.sin(anchor.th+anchor.node.thabs) * anchor.r * scale
xw += x * anchor.w
yw += y * anchor.w
skin.x = xw
skin.y = yw
return skins
目前的一些问题与改进方向
相关推荐
觉得内容不错的话,点个“在看”呗