Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
1,019
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. import numpy as np
  2. import json
  3. from json.decoder import JSONDecodeError
  4. from os import listdir
  5. from os.path import join, isfile
  6.  
  7. def read_json(file):
  8. try:
  9. with open(file, 'r') as j:
  10. return json.loads(j.read())
  11. except (OSError, UnicodeDecodeError, JSONDecodeError) as e:
  12. print(f'Error while reading json file: {file}')
  13. print(e)
  14. return
  15.  
  16.  
  17. def write_json(j, dst):
  18. with open(dst, 'w') as f:
  19. json.dump(j, f)
  20.  
  21.  
  22. def distance(p1, p2):
  23. x1, y1 = center_of_mass(p1)
  24. x2, y2 = center_of_mass(p2)
  25.  
  26. if not any([x for x in [x1, x2, y1, y2] if x is np.NaN]):
  27. return np.sqrt(np.power(x1 - x2, 2) + np.power(y1 - y2, 2))
  28. else:
  29. return np.inf
  30.  
  31.  
  32. def center_of_mass(p):
  33. k = np.array([float(c) for c in p['pose_keypoints_2d']])
  34. coords = [c for c in zip(k[::3], k[1::3], k[2::3])]
  35. threshold = 0.1
  36.  
  37. x = np.array([c[0] for c in coords if c[2] > threshold]).mean()
  38. y = np.array([c[1] for c in coords if c[2] > threshold]).mean()
  39.  
  40. return x, y
  41.  
  42.  
  43. def find_closest(p, prevs):
  44. ids = set([pi['person_id'] for pi in prevs])
  45. id_weights = dict([(i, []) for i in ids])
  46. for pi in prevs:
  47. d = distance(p, pi)
  48. id_weights[pi['person_id']].append(d)
  49.  
  50. for i in ids:
  51. id_weights[i] = np.mean(id_weights[i])
  52.  
  53. if len(id_weights) > 0:
  54. selected_id = min(id_weights, key=id_weights.get)
  55. selected_weight = id_weights[selected_id]
  56. else:
  57. selected_id = 0
  58. selected_weight = 0
  59. return selected_id, selected_weight
  60.  
  61.  
  62. def match_frames(ps, prevs):
  63. def gen_id(ps):
  64. ids = [p['person_id'] for p in ps]
  65. i = 0
  66. while i in ids:
  67. i += 1
  68. return i
  69.  
  70. found = {}
  71. for p in ps:
  72. new_id, d_new = find_closest(p, prevs)
  73. if new_id in found:
  74. p_old, d_old = found[new_id]
  75. generated_id = gen_id(prevs + [v[0] for v in found.values()])
  76. if d_old < d_new:
  77. new_id = generated_id
  78. else:
  79. p_old['person_id'] = generated_id
  80. found[generated_id] = (p_old, d_old)
  81. p['person_id'] = new_id
  82. p['person_id'] = new_id
  83. found[new_id] = (p, d_new)
  84.  
  85.  
  86. def set_person_id(json_src, n=5, print_interval=10000):
  87. file_names = [f for f in listdir(json_src) if isfile(join(json_src, f)) and f.endswith('json')]
  88.  
  89. def src_path(i):
  90. return join(json_src, file_names[i])
  91.  
  92. def dst_path(i):
  93. return join(json_src, file_names[i])
  94.  
  95. jsons = [read_json(src_path(0))]
  96. for idx, p in enumerate(jsons[0]['people']):
  97. p['person_id'] = idx
  98. write_json(jsons[0], dst_path(0))
  99.  
  100. for i in range(1, len(file_names)):
  101. jsons.append(read_json(src_path(i)))
  102. people = jsons[i]['people']
  103.  
  104. prevs = []
  105. for j in range(min(n, i)):
  106. for p in jsons[j]['people']:
  107. prevs.append(p)
  108.  
  109. match_frames(people, prevs)
  110.  
  111. write_json(jsons[i], dst_path(i))
  112.  
  113. if i % print_interval == 0:
  114. print(f'frame: {i}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement