-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoutputkeypoint.py
More file actions
166 lines (118 loc) · 3.78 KB
/
outputkeypoint.py
File metadata and controls
166 lines (118 loc) · 3.78 KB
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
"""
Example script using PyOpenPose.
"""
import PyOpenPose as OP
import time
import cv2
import numpy as np
import os
import sys
import logging as lg
OPENPOSE_ROOT = os.environ["OPENPOSE_ROOT"]
def showHeatmaps(hm):
for idx, h in enumerate(hm):
cv2.imshow("HeatMap "+str(idx), h)
def showPAFs(PAFs, startIdx=0, endIdx=16):
allpafs = []
for idx in range(startIdx, endIdx):
X = PAFs[idx*2]
Y = PAFs[idx*2+1]
tmp = np.dstack((X, Y, np.zeros_like(X)))
allpafs.append(tmp)
pafs = np.mean(allpafs, axis=0)
cv2.imshow("PAF", pafs)
def writeKeyPoints( pose, frameCounter, outputFile):
frameNo = "Frame No." + str(frameCounter) + '\n'
outputFile.write( frameNo )
lg.info( frameNo )
totalpeople = 'Total people:' + str(len(pose)) + '\n'
outputFile.write( totalpeople )
lg.info( totalpeople )
for i in range( 0, len(pose) ):
pplNo = 'No.' + str(i+1) + '\n'
outputFile.write( pplNo )
lg.info( pplNo )
for p in pose[i]:
outputFile.write(str(p))
lg.info( str(p) )
outputFile.write('\n')
outputFile.write('\n\n')
lg.info('\n\n')
def run( _filename, _show, _standard):
cap = cv2.VideoCapture(0)
fileName = _filename
is_open = cap.open(fileName)
if _show:
lg.getLogger().setLevel( lg.INFO)
lg.info( ' Media read:' + str(is_open) )
outputFileName = ('output_' + fileName + '.txt')
outputFile = open( str(outputFileName), 'w' )
lg.info( ' ' + str(outputFile) )
download_heatmaps = False
op = OP.OpenPose((320, 240), (240, 240), (640, 480), "COCO", OPENPOSE_ROOT + os.sep + "models" + os.sep, 0, download_heatmaps)
actual_fps = 0
paused = False
delay = {True: 0, False: 1}
lg.info(" Entering main Loop.")
ret, frame = cap.read()
frameCounter = 1
while True:
start_time = time.time()
try:
ret, frame = cap.read()
rgb = frame
except Exception as e:
print("Failed to grab", e)
break
t = time.time()
try:
op.detectPose(rgb)
except Exception as e:
break
t = time.time() - t
op_fps = 1.0 / t
res = op.render(rgb)
cv2.putText(res, 'UI FPS = %f, OP FPS = %f' % (actual_fps, op_fps), (20, 20), 0, 0.5, (0, 0, 255))
persons = op.getKeypoints(op.KeypointType.POSE)
if persons[0] is not None:
pose = persons[0]
score = persons[1]
result_pose = []
result_score = []
i = 0
for sc in score:
if sc > _standard:
result_pose.append( pose[i] )
result_score.append( sc )
i = i + 1
# end of for-loop
if len(result_score) > 0:
writeKeyPoints( result_pose, frameCounter, outputFile )
if _show:
cv2.imshow("OpenPose result", res)
key = cv2.waitKey(delay[paused])
if key & 255 == ord('p'):
paused = not paused
if key & 255 == ord('q'):
break
actual_fps = 1.0 / (time.time() - start_time)
frameCounter = frameCounter + 1
outputFile.close()
lg.info( outputFile )
if __name__ == '__main__':
if sys.argv[1] == None:
filename = input('Enter the file name with extension:\n')
else:
filename = str(sys.argv[1])
if sys.argv[2] == None:
show = True
else:
if sys.argv[2] == 'False':
show = False
else:
show = True
if sys.argv[3] == None:
standard = 0.5
else:
standard = float(sys.argv[3])
run( filename, show, standard)