forked from agnsal/BlocksBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFacialEmotionsAgent.py
85 lines (72 loc) · 3.03 KB
/
FacialEmotionsAgent.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
# 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 getFacesAndEmotions(base64Img):
assert isinstance(base64Img, bytes)
key = FacePPConfig['API_KEY']
secret = FacePPConfig['API_SECRET']
httpDetect = FacePPConfig['DETECT_URL']
data = {
'api_key': key,
'api_secret': secret,
'image_base64': base64Img,
'return_attributes': 'emotion',
}
try:
#post data to server
resp = requests.post(httpDetect, data)
#get response
faces = resp.json()
return faces
except HTTPError as e:
print(e.read())
def main():
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()
detectedEmotions = []
imgID = newMsg
img = r.hgetFromRedis(key=imgID, field=RedisConfig['imageHsetB64Field'])
if img:
if not isinstance(img, bytes):
img = base64.b64encode(img)
data = getFacesAndEmotions(img)
if data:
print(data) # Test
if "faces" in data.keys():
faces = data['faces']
for elem in faces:
detectedEmotions.append(elem['attributes']['emotion'])
print("Detected emotions: " + str(detectedEmotions)) # Test
r.rPushToRedisQueue(queue=RedisConfig['FacialQueue'], item=str(detectedEmotions))
r.hsetOnRedis(key=imgID, field=RedisConfig['imageHsetFacialResultField'],
value=str(detectedEmotions))
else:
print("No face detected") # Test
else:
print("No data detected") # Test
if __name__ == '__main__':
main()