forked from agnsal/BlocksBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoseEmotionsAgent.py
87 lines (72 loc) · 3.05 KB
/
PoseEmotionsAgent.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
# coding : utf-8
'''
Copyright 2020-2021 Agnese Salutari.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License
'''
from urllib.request import HTTPError
import requests
import base64
from RedisManager import RedisManager
import Yamler
FacePPConfig = Yamler.getConfigDict("Configs/FacePlusPlusConfig.yaml")
RedisConfig = Yamler.getConfigDict("Configs/RedisConfig.yaml")
def getBodies(base64Img):
key = FacePPConfig['API_KEY']
secret = FacePPConfig['API_SECRET']
httpSkeleton = FacePPConfig['SKELETON_URL']
data = {
'api_key': key,
'api_secret': secret,
'image_base64': base64Img,
}
try:
#post data to server
resp = requests.post(httpSkeleton, data)
#get response
bodies = resp.json()
return bodies
except HTTPError as e:
print(e.read())
def getAttitude(body, errorThreshold):
attitude = None
if (body['landmark']['right_hand']['score'] > errorThreshold and
body['landmark']['left_hand']['score'] > errorThreshold):
RHandX = body['landmark']['right_hand']['x']
LHandX = body['landmark']['left_hand']['x']
bodyWidth = body['body_rectangle']['width']
attitude = abs(RHandX - LHandX) / bodyWidth
return attitude
def main():
errorThreshold = 0.50
r = RedisManager(host=RedisConfig['host'], port=RedisConfig['port'], db=RedisConfig['db'],
password=RedisConfig['password'], decodedResponses=RedisConfig['decodedResponses'])
sub = r.getRedisPubSub()
sub.subscribe(RedisConfig['newImagePubSubChannel'])
for item in sub.listen():
print(item) # Test
if item['type'] == 'message':
newMsg = item['data']
print("New Msg: " + str(newMsg)) # Test
if not isinstance(newMsg, str):
newMsg = newMsg.decode()
detectedAttitudes = []
imgID = newMsg
img = r.hgetFromRedis(key=imgID, field=RedisConfig['imageHsetB64Field'])
if img:
if not isinstance(img, bytes):
img = base64.b64encode(img)
data = getBodies(img)
bodies = data['skeletons']
for elem in bodies:
detectedAttitudes.append(getAttitude(elem, errorThreshold))
r.rPushToRedisQueue(queue=RedisConfig['PoseQueue'], item=str(detectedAttitudes))
r.hsetOnRedis(key=imgID, field=RedisConfig['imageHsetPoseResultField'], value=str(detectedAttitudes))
print("Detected Attitudes: " + str(detectedAttitudes)) # Test
if __name__ == '__main__':
main()