Skip to content

Commit

Permalink
Merge pull request #20 from eroniki/fix_#18
Browse files Browse the repository at this point in the history
WIP Add the barebones of the skeleton-person api
  • Loading branch information
eroniki committed Aug 28, 2019
2 parents 41d84a2 + f6942f3 commit 4d88388
Show file tree
Hide file tree
Showing 5 changed files with 365 additions and 3 deletions.
151 changes: 151 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from pose_detection import pose_detection
from utils import utils
from tf.tf import TFTree
from skeleton.skeleton import people, skeleton
import visualization

import logging
from logging.handlers import RotatingFileHandler
Expand Down Expand Up @@ -180,6 +182,19 @@ def create_endpoints(self):
"triangulate",
self.triangulate)

self.app.add_url_rule("/draw_matchstick_frame/<int:exp_id>/<int:cam_id>/<int:frame_id>",
"draw_matchstick_frame",
self.skeletons)

self.app.add_url_rule("/draw_matchsticks/<int:exp_id>/<camera_id>",
"draw_matchsticks",
self.draw_matchsticks)

self.app.add_url_rule("/make_videofrom_matchsticks/<int:exp_id>/<camera_id>/<int:fps>",
"make_videofrom_matchsticks",
self.make_videofrom_matchsticks)


self.app.view_functions['index'] = self.index
self.app.view_functions['video'] = self.video
self.app.view_functions['video_feed'] = self.video_feed
Expand Down Expand Up @@ -209,6 +224,10 @@ def create_endpoints(self):
self.app.view_functions['match_people'] = self.match_people
self.app.view_functions['make_thumbnails'] = self.make_thumbnails
self.app.view_functions['triangulate'] = self.triangulate
self.app.view_functions['draw_matchstick_frame'] = self.skeletons
self.app.view_functions['draw_matchsticks'] = self.draw_matchsticks
self.app.view_functions['make_videofrom_matchsticks'] = self.make_videofrom_matchsticks


def index(self):
"""Render the homepage."""
Expand Down Expand Up @@ -635,6 +654,138 @@ def make_thumbnails(self, exp_id):

return "{n} number of images were processed!".format(n=n)

def make_videofrom_matchsticks(self, exp_id, camera_id, fps):
"""
"""
v = visualization.visualization()
exp = experiment.experiment(new_experiment=False, ts=exp_id)
room_name = exp.metadata["room"]

if room_name.lower() == "cears":
room_id = 1
elif room_name.lower() == "computer_lab":
room_id = 0

devices = self.rooms[room_id]["devices"]
devices.sort()
try:
cam_name = os.path.basename(devices[int(camera_id)])
except Exception as e:
print e
return "No cam found sorry!a"

fcamera_path = os.path.join("/dev/v4l/by-id", cam_name)
nframes = exp.metadata["number_of_images"][fcamera_path]
exp_path = self.um.experiment_path(str(exp_id))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
pathout = os.path.join(exp_path, "output/pose/video")

try:
self.um.create_folder(pathout)
except:
pass

pathout = os.path.join(pathout, cam_name)

try:
self.um.create_folder(pathout)
except:
pass

pathout = os.path.join(pathout, "video.mp4")
print pathout
out = cv2.VideoWriter(pathout, fourcc, fps, (800, 600))

for frame_id in range(nframes):
figure = os.path.join(exp_path,
"output/pose/img",
cam_name,
"matchstick_" + str(frame_id) + ".png")
img_ = cv2.imread(figure)

if img_ is None:

return "I think you forgot to draw the matchsticks!"
cv2.resize(img_, (800, 600))
out.write(img_)

out.release()

return "done"

def draw_matchsticks(self, exp_id, camera_id):
"""
"""
v = visualization.visualization()
exp = experiment.experiment(new_experiment=False, ts=exp_id)
room_name = exp.metadata["room"]

if room_name.lower() == "cears":
room_id = 1
elif room_name.lower() == "computer_lab":
room_id = 0

devices = self.rooms[room_id]["devices"]
devices.sort()
try:
cam_name = os.path.basename(devices[int(camera_id)])
except Exception as e:
print e
return "No cam found sorry!a"

fcamera_path = os.path.join("/dev/v4l/by-id", cam_name)
nframes = exp.metadata["number_of_images"][fcamera_path]
ret_combined = ""
for frame_id in range(nframes):
ret = self.skeletons(exp_id, camera_id, frame_id)
ret_combined += "<br>" + ret
return ret_combined

def skeletons(self, exp_id, cam_id, frame_id):
"""Test skeletons."""
visualizer = visualization.visualization()
exp = experiment.experiment(new_experiment=False, ts=exp_id)
room_name = exp.metadata["room"]
if room_name.lower() == "cears":
room_id = 1
elif room_name.lower() == "computer_lab":
room_id = 0

devices = self.rooms[room_id]["devices"]
devices.sort()
try:
cam_name = os.path.basename(devices[int(cam_id)])
except:
return "No cam found sorry!"

exp_path = self.um.experiment_path(str(exp_id))
pose_detection_result = "output/pose"
json = "pose"
img = "img"
fname = os.path.join(exp_path,
pose_detection_result,
json,
cam_name,
str(frame_id)+".png.json")


output_fname = os.path.join(exp_path,
pose_detection_result,
img,
cam_name,
"matchstick_" + str(frame_id) + ".png")
print fname

if self.um.check_file_exists(fname):
json_data = self.um.read_json(fname)
people_in_frame = people(json_data, frame_id)
visualizer.draw_matchsticks(people_in_frame, output_fname)
npeople = str(len(people_in_frame.list))
else:
npeople = 0

return "Number of people drawn: {n}".format(n=npeople)

def triangulate(self, exp_id):
"""Triangulate people's locations."""
n = 99999
Expand Down
1 change: 1 addition & 0 deletions skeleton/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""This class implements stuff about the skeleton data structure."""
157 changes: 157 additions & 0 deletions skeleton/skeleton.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
"""This class implements stuff about the skeleton data structure."""
from __future__ import division
import numpy as np


class people(object):
"""docstring for people."""
def __init__(self, json, frame=None):
super(people, self).__init__()
self.json = json
self.json = np.asarray(self.json).reshape(-1, 25, 3)
self.list = list()
for person_id, person in enumerate(self.json):
self.list.append(skeleton(person, person_id=person_id, frame=frame))


class skeleton(object):
"""This class implements stuff about the skeleton data structure."""

def __init__(self, joint_data, person_id=None, frame=None):
"""Initialize the class."""
super(skeleton, self).__init__()
self.n_joint = 25
self.frame = frame
self.person_id = person_id
self.joint_locs = np.array(joint_data)
self.joint_locs = self.joint_locs.reshape(-1, 3)
self.bone_list = np.array([[0, 16],
[0, 15],
[1, 0],
[1, 8],
[1, 5],
[1, 2],
[2, 17],
[2, 3],
[3, 4],
[5, 6],
[5, 18],
[6, 7],
[8, 9],
[8, 12],
[9, 10],
[10, 11],
[11, 22],
[11, 24],
[12, 13],
[13, 14],
[14, 19],
[14, 21],
[15, 17],
[16, 18],
[19, 20],
[22, 23]])
self.bone_colors = np.random.rand(len(self.bone_list), 3, 1)

self.names = ["Nose", "Neck", "RShoulder", "RElbow", "RWrist",
"LShoulder", "LElbow", "LWrist", "MidHip",
"RHip", "RKnee", "RAnkle", "LHip", "LKnee", "LAnkle",
"REye", "LEye", "REar", "LEar", "LBigToe",
"LSmallToe", "LHeel", "RBigToe", "RSmallToe",
"RHeel", "Background"]

self.nose = joint(joint_id=0, name=self.names[0],
loc2d=self.joint_locs[0, 0:2],
conf=self.joint_locs[0, 2])
self.neck = joint(joint_id=1, name=self.names[1],
loc2d=self.joint_locs[1, 0:2],
conf=self.joint_locs[1, 2])
self.rshoulder = joint(joint_id=2, name=self.names[2],
loc2d=self.joint_locs[2, 0:2],
conf=self.joint_locs[2, 2])
self.relbow = joint(joint_id=3, name=self.names[3],
loc2d=self.joint_locs[3, 0:2],
conf=self.joint_locs[3, 2])
self.rwrist = joint(joint_id=4, name=self.names[4],
loc2d=self.joint_locs[4, 0:2],
conf=self.joint_locs[4, 2])
self.lshoulder = joint(joint_id=5, name=self.names[5],
loc2d=self.joint_locs[5, 0:2],
conf=self.joint_locs[5, 2])
self.lelbow = joint(joint_id=6, name=self.names[6],
loc2d=self.joint_locs[6, 0:2],
conf=self.joint_locs[6, 2])
self.lwrist = joint(joint_id=7, name=self.names[7],
loc2d=self.joint_locs[7, 0:2],
conf=self.joint_locs[7, 2])
self.midhip = joint(joint_id=8, name=self.names[8],
loc2d=self.joint_locs[8, 0:2],
conf=self.joint_locs[8, 2])
self.rhip = joint(joint_id=9, name=self.names[9],
loc2d=self.joint_locs[9, 0:2],
conf=self.joint_locs[9, 2])
self.rknee = joint(joint_id=10, name=self.names[10],
loc2d=self.joint_locs[10, 0:2],
conf=self.joint_locs[10, 2])
self.rankle = joint(joint_id=11, name=self.names[11],
loc2d=self.joint_locs[11, 0:2],
conf=self.joint_locs[11, 2])
self.lhip = joint(joint_id=12, name=self.names[12],
loc2d=self.joint_locs[12, 0:2],
conf=self.joint_locs[12, 2])
self.lknee = joint(joint_id=13, name=self.names[13],
loc2d=self.joint_locs[13, 0:2],
conf=self.joint_locs[13, 2])
self.lankle = joint(joint_id=14, name=self.names[14],
loc2d=self.joint_locs[14, 0:2],
conf=self.joint_locs[14, 2])
self.reye = joint(joint_id=15, name=self.names[15],
loc2d=self.joint_locs[15, 0:2],
conf=self.joint_locs[15, 2])
self.leye = joint(joint_id=16, name=self.names[16],
loc2d=self.joint_locs[16, 0:2],
conf=self.joint_locs[16, 2])
self.rear = joint(joint_id=17, name=self.names[17],
loc2d=self.joint_locs[17, 0:2],
conf=self.joint_locs[17, 2])
self.lear = joint(joint_id=18, name=self.names[18],
loc2d=self.joint_locs[18, 0:2],
conf=self.joint_locs[18, 2])
self.lbigtoe = joint(joint_id=19, name=self.names[19],
loc2d=self.joint_locs[19, 0:2],
conf=self.joint_locs[19, 2])
self.lsmalltoe = joint(joint_id=20, name=self.names[20],
loc2d=self.joint_locs[20, 0:2],
conf=self.joint_locs[20, 2])
self.lheel = joint(joint_id=21, name=self.names[21],
loc2d=self.joint_locs[21, 0:2],
conf=self.joint_locs[21, 2])
self.rbigtoe = joint(joint_id=22, name=self.names[22],
loc2d=self.joint_locs[22, 0:2],
conf=self.joint_locs[22, 2])
self.rsmalltoe = joint(joint_id=23, name=self.names[23],
loc2d=self.joint_locs[23, 0:2],
conf=self.joint_locs[23, 2])
self.rheel = joint(joint_id=24, name=self.names[24],
loc2d=self.joint_locs[24, 0:2],
conf=self.joint_locs[24, 2])
# self.background = joint(joint_id=25, name=self.names[25],
# loc2d=self.joint_locs[25, 0:2],
# conf=self.joint_locs[25, 2])


class joint(object):
"""This class implements stuff about the joint data structure."""

def __init__(self, joint_id, name, loc2d=None, conf=None, loc3d=None):
"""Initialize the class."""
super(joint, self).__init__()
self.id = joint_id
self.name = name
self.loc2d = loc2d
self.conf = conf
self.loc3d = loc3d


if __name__ == '__main__':
pass
16 changes: 13 additions & 3 deletions templates/experiment.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,25 @@ <h1>Administration</h1>
<p><a href={{ url_for('clone_experiment', id=user.timestamp) }} target="_blank">Move data to cloud</a></p>
<hr>
<h2>Pose Detection</h2>
</p>
{% for camera_id in range(user.camera): %}
<p>
Camera {{ camera_id }}:<a href={{ url_for('pose_cam', exp_id=user.timestamp, camera_id=camera_id ) }} target="_blank">
{{ user.pose_detection_processed_images[camera_id] }}</a>
{% endfor %}
<p>
<a href={{ url_for('pose_exp', exp_id=user.timestamp) }} target="_blank">Pose Detection for the Whole Experiment</a>
</p>
<hr>
<h2>Draw Matchstick-People</h2>
{% for camera_id in range(user.camera): %}
Camera {{ camera_id }}: <a href={{ url_for('draw_matchsticks', exp_id=user.timestamp, camera_id=camera_id ) }} target="_blank">Do it!</a>
{% endfor %}

<h2>Make Video from Drawings</h2>
{% for camera_id in range(user.camera): %}
Camera {{ camera_id }}: <a href={{ url_for('make_videofrom_matchsticks', exp_id=user.timestamp, camera_id=camera_id, fps=10) }} target="_blank">Do it!</a>
{% endfor %}
<p><a href={{ url_for('pose_exp', exp_id=user.timestamp) }} target="_blank">Pose Detection for the Whole Experiment</a></p>
<hr>

<h2>Computer Vision</h2>
<p><a href={{ url_for('make_thumbnails', exp_id=user.timestamp) }} target="_blank">Make Thumbnails and Extract
Features</a></p>
Expand Down

0 comments on commit 4d88388

Please sign in to comment.