-
Notifications
You must be signed in to change notification settings - Fork 24
/
pt3-webcam3D.py
120 lines (100 loc) · 3.45 KB
/
pt3-webcam3D.py
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
"""
This serve as our base openGL class.
"""
import numpy as np
import pyqtgraph.opengl as gl
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import sys
import cv2
import time
import os
from estimator import TfPoseEstimator
from networks import get_graph_path, model_wh
from lifting.prob_model import Prob3dPose
import common
class Terrain(object):
def __init__(self):
"""
Initialize the graphics window and mesh surface
"""
# setup the view window
self.app = QtGui.QApplication(sys.argv)
self.window = gl.GLViewWidget()
self.window.setWindowTitle('Terrain')
self.window.setGeometry(0, 110, 1920, 1080)
self.window.setCameraPosition(distance=30, elevation=12)
self.window.show()
gx = gl.GLGridItem()
gy = gl.GLGridItem()
gz = gl.GLGridItem()
gx.rotate(90, 0, 1, 0)
gy.rotate(90, 1, 0, 0)
gx.translate(-10, 0, 0)
gy.translate(0, -10, 0)
gz.translate(0, 0, -10)
self.window.addItem(gx)
self.window.addItem(gy)
self.window.addItem(gz)
model = 'mobilenet_thin_432x368'
camera = 0
w, h = model_wh(model)
self.e = TfPoseEstimator(get_graph_path(model), target_size=(w, h))
self.cam = cv2.VideoCapture(camera)
ret_val, image = self.cam.read()
self.poseLifting = Prob3dPose('./src/lifting/models/prob_model_params.mat')
keypoints = self.mesh(image)
self.points = gl.GLScatterPlotItem(
pos=keypoints,
color=pg.glColor((0, 255, 0)),
size=15
)
self.window.addItem(self.points)
def mesh(self, image):
image_h, image_w = image.shape[:2]
width = 640
height = 480
pose_2d_mpiis = []
visibilities = []
humans = self.e.inference(image, scales=[None])
for human in humans:
pose_2d_mpii, visibility = common.MPIIPart.from_coco(human)
pose_2d_mpiis.append(
[(int(x * width + 0.5), int(y * height + 0.5)) for x, y in pose_2d_mpii]
)
visibilities.append(visibility)
pose_2d_mpiis = np.array(pose_2d_mpiis)
visibilities = np.array(visibilities)
transformed_pose2d, weights = self.poseLifting.transform_joints(pose_2d_mpiis, visibilities)
pose_3d = self.poseLifting.compute_3d(transformed_pose2d, weights)
keypoints = pose_3d[0].transpose()
return keypoints / 80
def update(self):
"""
update the mesh and shift the noise each time
"""
ret_val, image = self.cam.read()
try:
keypoints = self.mesh(image)
except AssertionError:
print('body not in image')
else:
self.points.setData(pos=keypoints)
def start(self):
"""
get the graphics window open and setup
"""
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
def animation(self, frametime=10):
"""
calls the update method to run in a loop
"""
timer = QtCore.QTimer()
timer.timeout.connect(self.update)
timer.start(frametime)
self.start()
if __name__ == '__main__':
os.chdir('..')
t = Terrain()
t.animation()