-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdetector.py
121 lines (102 loc) · 14.5 KB
/
detector.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
121
import os
import re
import sys
sys.path.append('.')
import cv2
import math
import time
import scipy
import argparse
import matplotlib
import numpy as np
import pylab as plt
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from collections import OrderedDict
from scipy.ndimage.morphology import generate_binary_structure
from scipy.ndimage.filters import gaussian_filter, maximum_filter
from lib.network.rtpose_vgg import get_model
from evaluate.coco_eval import get_outputs, handle_paf_and_heat
from lib.utils.common import Human, BodyPart, CocoPart, CocoColors, CocoPairsRender, draw_humans
from lib.utils.paf_to_pose import paf_to_pose_cpp
from lib.config import cfg, update_config
CAMERA_CALIBRATION = {"world_ltrb": [-64, -110, 227, 56],"cameras": {"1": {"K": [[0.7991484375, 0.0, 0.5], [0.0, 1.4207083333333332, 0.45833333333333337], [0.0, 0.0, 1.0]], "Tw": [[-0.4867081596720381, 0.47539238874372436, -0.732882831039992, 175.3725308735464], [0.8535129732528981, 0.08006508416056038, -0.514883663352567, 4.474028419622428], [-0.1860934490992496, -0.8761230844024701, -0.44472190094422703, 95.58740326238247], [0.0, 0.0, 0.0, 1.0]], "dist_coeffs": [-0.472161, 0.201042, 0.0, 0.0, 0.0], "look_at": [29.29023020798177, -99.66924774214982, 0.0]}, "10": {"K": [[0.8145546875000002, 0.0, 0.5], [0.0, 1.4480972222222224, 0.45833333333333337], [0.0, 0.0, 1.0]], "Tw": [[0.3459235988301621, -0.6193821704652024, 0.7047713038157865, 3.059610847571051], [-0.9196404140684529, -0.07491358992460301, 0.38555085638163655, -60.57655622641551], [-0.18600637780569187, -0.7815073134362998, -0.5955232543412414, 96.76637602595352], [0.0, 0.0, 0.0, 1.0]], "dist_coeffs": [-0.537589, 0.32519, 0.0, 0.0, 0.0], "look_at": [110.61740078374521, -0.5466358027552807, 0.0]}, "11": {"K": [[0.8053750000000002, 0.0, 0.5], [0.0, 1.431777777777778, 0.45833333333333337], [0.0, 0.0, 1.0]], "Tw": [[-0.8649515047692744, -0.09887198914523854, 0.4920195363599207, -14.849419355680466], [-0.37288982647194907, 0.7827654679979151, -0.49822826036238077, -1.9432845843566984], [-0.3358750834924902, -0.6144123630530791, -0.7139225283015265, 95.08890359183589], [0.0, 0.0, 0.0, 1.0]], "dist_coeffs": [-0.489617, 0.21001, 0.0, 0.0, 0.0], "look_at": [48.7078882475331, -63.72023257813332, 0.0]}, "12": {"K": [[0.8048281250000001, 0.0, 0.5], [0.0, 1.4308055555555557, 0.45833333333333337], [0.0, 0.0, 1.0]], "Tw": [[0.8245575131305252, -0.21108716555582388, 0.5249258195951234, -14.801763743441764], [-0.5237906306312498, -0.6355515922385412, 0.5672015064031237, -56.873139907941805], [0.213888482165173, -0.7426414896440027, -0.6346143199270051, 94.71637918628485], [0.0, 0.0, 0.0, 1.0]], "dist_coeffs": [-0.462601, 0.183489, 0.0, 0.0, 0.0], "look_at": [60.07314850510335, 22.318671540081617, 0.0]}, "2": {"K": [[0.7971171875, 0.0, 0.5], [0.0, 1.4170972222222222, 0.45833333333333337], [0.0, 0.0, 1.0]], "Tw": [[0.45849873510195666, 0.5438310822952696, -0.7028703037114752, 176.21435766822162], [0.8862809971280463, -0.2215571795835648, 0.40671662162332634, -54.73416030918207], [0.06545917842153115, -0.8094196501843351, -0.5835708404776977, 96.0], [0.0, 0.0, 0.0, 1.0]], "dist_coeffs": [-0.489378, 0.212298, 0.0, 0.0, 0.0], "look_at": [67.64998809875672, 8.519714517052721, 0.0]}, "21": {"K": [[0.6972007812500001, 0.0, 0.5], [0.0, 1.2394680555555557, 0.5], [0.0, 0.0, 1.0]], "Tw": [[0.9986558969419876, 0.04409819796838638, -0.027235059003697548, 24.211320385504433], [0.026942669926242835, -0.8905693390959126, -0.45404883525840506, 1.2845049301721136], [-0.044277443921701576, 0.4527047616252891, -0.8905604453160665, 93.97990726828843], [0.0, 0.0, 0.0, 1.0]], "dist_coeffs": [0.0, 0.0, 0.0, 0.0, 0.0], "look_at": [21.337233249616137, -46.63079117049274, 0.0]}, "22": {"K": [[0.7425296875, 0.0, 0.5], [0.0, 1.3200527777777777, 0.5], [0.0, 0.0, 1.0]], "Tw": [[0.9997751587061491, 0.019814977003613232, 0.0075497496939137265, 149.08930242366512], [0.021138780086920606, -0.9033458639941627, -0.42839164555471465, 6.275142561895159], [-0.0016685354450184369, 0.42845491792134427, -0.9035616189826139, 98.98807266863675], [0.0, 0.0, 0.0, 1.0]], "dist_coeffs": [0.0, 0.0, 0.0, 0.0, 0.0], "look_at": [149.9164017112296, -40.656535864812035, 0.0]}, "23": {"K": [[0.7331398437500001, 0.0, 0.5], [0.0, 1.3033597222222222, 0.5], [0.0, 0.0, 1.0]], "Tw": [[0.9997015782188221, 0.023896234573599795, 0.005071930598893826, 108.51214416881247], [0.023693155010345912, -0.8979170310150779, -0.43952660877212374, 3.7369394929837014], [-0.005948858079684205, 0.43951561449654014, -0.8982152502052481, 98.76833801809914], [0.0, 0.0, 0.0, 1.0]], "dist_coeffs": [0.0, 0.0, 0.0, 0.0, 0.0], "look_at": [109.06985698395496, -44.59369467653654, 0.0]}, "25": {"K": [[0.73098125, 0.0, 0.5], [0.0, 1.2995222222222222, 0.5], [0.0, 0.0, 1.0]], "Tw": [[0.9995529158170692, 0.028851584441812073, 0.007845671213581096, 65.76516761622365], [0.029443025396452716, -0.9041403213694741, -0.42621988166838, 3.477239518851242], [-0.005203531214327872, 0.4262603257976291, -0.9045855724660882, 98.78712515405422], [0.0, 0.0, 0.0, 1.0]], "dist_coeffs": [0.0, 0.0, 0.0, 0.0, 0.0], "look_at": [66.62197025345856, -43.06897797026188, 0.0]}, "26": {"K": [[0.7268539062500001, 0.0, 0.5], [0.0, 1.2921847222222222, 0.5], [0.0, 0.0, 1.0]], "Tw": [[0.028250684097942624, -0.9368185543113299, -0.3486730490675009, 177.7180790404277], [-0.9991445727060733, -0.015926015743861377, -0.03816392084842096, -27.387300041052967], [0.0301996966871606, 0.3494529414964994, -0.9364670949902337, 97.25902081149381], [0.0, 0.0, 0.0, 1.0]], "dist_coeffs": [0.0, 0.0, 0.0, 0.0, 0.0], "look_at": [141.50580899156205, -31.350904947089187, 0.0]}, "3": {"K": [[0.8003671875, 0.0, 0.5], [0.0, 1.4228750000000001, 0.45833333333333337], [0.0, 0.0, 1.0]], "Tw": [[-0.9979891728909124, 0.020518643702048645, -0.059971627067648414, 155.1197917265313], [0.06091095708085498, 0.5722013302253762, -0.8178480867470465, 4.3055103887650645], [0.01753471128952393, -0.8198564648454864, -0.5723005425045956, 95.97437991718674], [0.0, 0.0, 0.0, 1.0]], "dist_coeffs": [-0.459687, 0.151974, 0.0, 0.0, 0.0], "look_at": [145.5644068894353, -124.62511135083709, 0.0]}, "4": {"K": [[0.8391796875000002, 0.0, 0.5], [0.0, 1.4918750000000003, 0.45833333333333337], [0.0, 0.0, 1.0]], "Tw": [[0.5468560209068158, -0.5543358241452501, 0.6274235303741567, 126.82327587446932], [-0.8229403147896952, -0.21804872843552708, 0.5246179470066642, -58.94073206203501], [-0.1540056190267141, -0.8032226005889378, -0.5754265576173403, 99.40245472338998], [0.0, 0.0, 0.0, 1.0]], "dist_coeffs": [-0.595662, 0.471424, 0.0, 0.0, 0.0], "look_at": [228.56402601747106, 27.269746492363666, 0.0]}, "5": {"K": [[0.777709375, 0.0, 0.5], [0.0, 1.3825944444444445, 0.45833333333333337], [0.0, 0.0, 1.0]], "Tw": [[-0.15302776026224613, 0.4523010988030582, -0.8786388453799814, 113.00474776887702], [0.9876886090315116, 0.0407963533073184, -0.1510194330085452, 2.2095711040028334], [-0.03246099472472382, -0.890931744623779, -0.45297561771370304, 95.03247954440694], [0.0, 0.0, 0.0, 1.0]], "dist_coeffs": [-0.483463, 0.0766268, 0.0, 0.0, 0.0], "look_at": [-58.31026051801675, -27.456459836002754, 0.0]}, "6": {"K": [[0.7822890625000001, 0.0, 0.5], [0.0, 1.3907361111111112, 0.45833333333333337], [0.0, 0.0, 1.0]], "Tw": [[0.5304066601704497, 0.3994307699355158, -0.7477458357460443, 114.0688719218917], [0.8445086197177807, -0.17197812288172473, 0.5071772042121446, -56.40780611587452], [0.0739862559479681, -0.9004879706463597, -0.4285410699711316, 94.71603028229836], [0.0, 0.0, 0.0, 1.0]], "dist_coeffs": [-0.502934, 0.238446, 0.0, 0.0, 0.0], "look_at": [-38.916066384905776, 47.974686358851336, 0.0]}, "7": {"K": [[0.81859375, 0.0, 0.5], [0.0, 1.4552777777777777, 0.45833333333333337], [0.0, 0.0, 1.0]], "Tw": [[-0.22215586743532476, -0.40347755259611756, 0.8876106326059338, 49.25130075313485], [-0.9747098561313063, 0.06927322825061828, -0.21246627075426788, 2.466020375128444], [0.024237716982969504, -0.91236346068811, -0.4086630013552346, 97.2142182783703], [0.0, 0.0, 0.0, 1.0]], "dist_coeffs": [-0.564746, 0.37875, 0.0, 0.0, 0.0], "look_at": [245.12374297709823, -44.594612827407396, 0.0]}, "8": {"K": [[0.808046875, 0.0, 0.5], [0.0, 1.4365277777777778, 0.45833333333333337], [0.0, 0.0, 1.0]], "Tw": [[0.5201059611497254, -0.34042531852759783, 0.7833264911145947, 52.657768949457456], [-0.8539298460095099, -0.1888610148884503, 0.48490755319903944, -59.4269755345895], [-0.01713497215331038, -0.9211091789580038, -0.38892707950027994, 96.32356900652523], [0.0, 0.0, 0.0, 1.0]], "dist_coeffs": [-0.674432, 0.785755, 0.0, 0.0, 0.0], "look_at": [231.89395162588772, 51.6738475361974, 0.0]}, "9": {"K": [[0.7972890625, 0.0, 0.5], [0.0, 1.4174027777777778, 0.45833333333333337], [0.0, 0.0, 1.0]], "Tw": [[-0.4055977586601728, -0.5847496641620861, 0.7025370370536855, 3.758983533236573], [-0.8868462478026111, 0.06562868132833363, -0.4573801580146797, -1.8743211435043567], [0.2213463144673377, -0.8085547021998151, -0.545201891616578, 95.67532175778733], [0.0, 0.0, 0.0, 1.0]], "dist_coeffs": [-0.519326, 0.284321, 0.0, 0.0, 0.0], "look_at": [119.00021582011026, -78.45914691870706, 0.0]}, "21-depth": {"K": [[0.5035999999999999, 0.0, 0.49707890624999995], [0.0, 0.8952888888888888, 0.48937777777777774], [0.0, 0.0, 1.0]], "Tw": [[0.9982710175756038, 0.04784419904092907, -0.034148714676069246, 24.788389901793014], [0.02692095778944375, -0.8885688533314942, -0.4579521967217341, 1.289556634657642], [-0.05225385795295126, 0.4562409304358952, -0.8883206624276694, 93.92781908446544], [0.0, 0.0, 0.0, 1.0]], "dist_coeffs": [0.0, 0.0, 0.0, 0.0, 0.0], "look_at": [21.832903770510068, -48.517380284940764, 0.0]}, "22-depth": {"K": [[0.50282265625, 0.0, 0.5059484375000001], [0.0, 0.8939069444444445, 0.4977916666666667], [0.0, 0.0, 1.0]], "Tw": [[0.999638690279901, 0.02676813090201524, -0.0022446964416669695, 149.67739400109792], [0.023207668193915575, -0.9027043411844632, -0.4296353679051146, 6.281846179346543], [-0.013526825312936495, 0.4294282643661486, -0.9029997886580358, 98.97086060470605], [0.0, 0.0, 0.0, 1.0]], "dist_coeffs": [0.0, 0.0, 0.0, 0.0, 0.0], "look_at": [148.14040314074387, -41.1458174981043, 0.0]}, "23-depth": {"K": [[0.502890625, 0.0, 0.5000171875], [0.0, 0.8940277777777779, 0.4928513888888889], [0.0, 0.0, 1.0]], "Tw": [[0.9996335038673024, 0.027054590821355157, -0.000818528354538956, 109.09164517225145], [0.02395835634459116, -0.898493463957044, -0.43833237586385587, 3.737344957043877], [-0.012594347544816703, 0.43815223165960127, -0.8988123152351056, 98.74168313353387], [0.0, 0.0, 0.0, 1.0]], "dist_coeffs": [0.0, 0.0, 0.0, 0.0, 0.0], "look_at": [109.02146172846976, -45.3978439025265, 0.0]}, "25-depth": {"K": [[0.5050273437499999, 0.0, 0.501234375], [0.0, 0.8978263888888888, 0.49914027777777775], [0.0, 0.0, 1.0]], "Tw": [[0.999565023328579, 0.029452976711566055, 0.0016346674190862924, 66.33973658495839], [0.027318733788817447, -0.9033813714697915, -0.42796756465876373, 3.4824386063554265], [-0.011128188524637221, 0.42782616832258613, -0.9037924714862736, 98.76482896212589], [0.0, 0.0, 0.0, 1.0]], "dist_coeffs": [0.0, 0.0, 0.0, 0.0, 0.0], "look_at": [66.25443200307116, -43.40958657778623, 0.0]}, "26-depth": {"K": [[0.50450703125, 0.0, 0.4982703125], [0.0, 0.8969013888888889, 0.5000388888888889], [0.0, 0.0, 1.0]], "Tw": [[0.027318377281628788, -0.9364743040730777, -0.3496718665602636, 177.72792002496993], [-0.9992763440261437, -0.016328248471521263, -0.03433982248505669, -27.96271700320617], [0.02644879513401196, 0.35035720076883564, -0.9362428587814324, 97.25150550845231], [0.0, 0.0, 0.0, 1.0]], "dist_coeffs": [0.0, 0.0, 0.0, 0.0, 0.0], "look_at": [141.41704332123152, -31.885854398980747, 0.0]}}}
print("Importing")
parser = argparse.ArgumentParser()
parser.add_argument('--cfg', help='experiment configure file name',
default='./experiments/vgg19_368x368_sgd.yaml', type=str)
parser.add_argument('--weight', type=str,
default='pose_model.pth')
parser.add_argument('opts',
help="Modify config options using the command-line",
default=None,
nargs=argparse.REMAINDER)
args = parser.parse_args()
# update config file
update_config(cfg, args)
model = get_model('vgg19')
model.load_state_dict(torch.load(args.weight))
model = torch.nn.DataParallel(model).cuda()
model.float()
model.eval()
def heatmap2d(arr: np.ndarray):
plt.imshow(arr, cmap='jet')
plt.colorbar()
plt.show()
def detect_keypoints(oriImg):
# Get results of original image
with torch.no_grad():
paf, heatmap, im_scale = get_outputs(oriImg, model, 'rtpose')
# heatmap2d(paf[:,:,1])
print(paf.shape, heatmap.shape, im_scale)
humans = paf_to_pose_cpp(heatmap, paf, cfg)
image_h, image_w = oriImg.shape[:2]
human_keypoints = []
for human in humans:
# draw point
centers = {}
for i in range(CocoPart.Background.value):
if i not in human.body_parts.keys():
continue
body_part = human.body_parts[i]
center = (int(body_part.x * image_w + 0.5), int(body_part.y * image_h + 0.5))
centers[i] = {'center': center, 'score': body_part.score}
human_keypoints.append(centers)
return human_keypoints
def camera_projection(x, y, z, camera):
intrin_data, extrin_data = camera["K"], camera["Tw"]
intrinsics = np.zeros((3, 4))
extrinsics = np.zeros((4, 4))
intrinsics[:, :3] = intrin_data
extrinsics = extrin_data
world_coor = np.array([x, y, z, 1])
camera_coor = np.matmul(intrinsics, np.matmul(extrinsics, world_coor))
print("u v z", camera_coor)
camera_coor = camera_coor #[u, v, 1]
camera_coor = camera_coor / camera_coor[2] * 100
return camera_coor[:2]
def calculate_association(products, targets):
"""
Input:
Output:
List of Association: [Associated Customer]
"""
results = []
for product in products:
x, y, z = get_coordinate(product)
x_camera, y_camera = camera_projection(x, y, z, camera)
target_scores = []
for target in targets:
head_x, head_y, head_z = target['head']
head_x_camera, head_y_camera = camera_projection(head_x, head_y, head_z, camera)
distance = sqrt((head_x_camera - x_camera)^2 + (head_y_camera - y_camera)^2) * 1.0 #confident score of that body part, currently only the head
target_scores.append(distance)
results.append(np.argmax(np.array(target_scores)))
return results
# if __name__ == '__main__':
# test_image = './ski.jpg'
# oriImg = cv2.imread(test_image) # B,G,R order
# human_keypoints = detect_keypoints(oriImg)
# print(human_keypoints)