7
7
import numpy as np
8
8
import tensorflow as tf
9
9
import time
10
-
10
+ import json
11
+ import os
11
12
from tf_pose import common
12
13
from tf_pose .common import CocoPart
13
14
from tf_pose .tensblur .smoother import Smoother
@@ -303,6 +304,7 @@ class TfPoseEstimator:
303
304
def __init__ (self , graph_path , target_size = (320 , 240 ), tf_config = None ):
304
305
self .target_size = target_size
305
306
307
+
306
308
# load graph
307
309
logger .info ('loading graph from %s(default size=%dx%d)' % (graph_path , target_size [0 ], target_size [1 ]))
308
310
with tf .gfile .GFile (graph_path , 'rb' ) as f :
@@ -378,12 +380,14 @@ def _quantize_img(npimg):
378
380
return npimg_q
379
381
380
382
@staticmethod
381
- def draw_humans (npimg , humans , imgcopy = False ):
383
+ def draw_humans (npimg , humans , imgcopy = False , frame = 0 , output_json_dir = None ):
382
384
if imgcopy :
383
385
npimg = np .copy (npimg )
384
386
image_h , image_w = npimg .shape [:2 ]
387
+ dc = {"people" :[]}
385
388
centers = {}
386
- for human in humans :
389
+ for n , human in enumerate (humans ):
390
+ flat = [0.0 for i in range (36 )]
387
391
# draw point
388
392
for i in range (common .CocoPart .Background .value ):
389
393
if i not in human .body_parts .keys ():
@@ -392,7 +396,11 @@ def draw_humans(npimg, humans, imgcopy=False):
392
396
body_part = human .body_parts [i ]
393
397
center = (int (body_part .x * image_w + 0.5 ), int (body_part .y * image_h + 0.5 ))
394
398
centers [i ] = center
395
- cv2 .circle (npimg , center , 3 , common .CocoColors [i ], thickness = 3 , lineType = 8 , shift = 0 )
399
+ #add x
400
+ flat [i * 2 ] = center [0 ]
401
+ #add y
402
+ flat [i * 2 + 1 ] = center [1 ]
403
+ cv2 .circle (npimg , center , 8 , common .CocoColors [i ], thickness = 3 , lineType = 8 , shift = 0 )
396
404
397
405
# draw line
398
406
for pair_order , pair in enumerate (common .CocoPairsRender ):
@@ -402,6 +410,13 @@ def draw_humans(npimg, humans, imgcopy=False):
402
410
# npimg = cv2.line(npimg, centers[pair[0]], centers[pair[1]], common.CocoColors[pair_order], 3)
403
411
cv2 .line (npimg , centers [pair [0 ]], centers [pair [1 ]], common .CocoColors [pair_order ], 3 )
404
412
413
+ dc ["people" ].append ({"pose_keypoints_2d" : flat })
414
+
415
+ if output_json_dir :
416
+ with open (os .path .join (output_json_dir , '{0}_keypoints.json' .format (str (frame ).zfill (12 ))), 'w' ) as outfile :
417
+ json .dump (dc , outfile )
418
+
419
+
405
420
return npimg
406
421
407
422
def _get_scaled_img (self , npimg , scale ):
0 commit comments