-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlisten.py
38 lines (33 loc) · 1.06 KB
/
listen.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
"""
listens to a FIFO queue that's updated by `reality`
"""
import json
import fasteners
from time import sleep
from faces import extract_faces
from objects import extract_objects
from util import hash
def on_article(article):
print(article['url'])
im = hash(article['image'])
path = '../reality/data/_images/{}'.format(im)
faces = extract_faces(path)
objects = extract_objects(path)
print(' detected {} faces, {} objects'.format(
len(faces), len(objects)))
if __name__ == '__main__':
while True:
fifo = 'fifo'
try:
with fasteners.InterProcessLock('/tmp/{}.lock'.format(hash(fifo))):
try:
with open(fifo, 'r+') as f:
for l in f:
article = json.loads(l.strip())
on_article(article)
open(fifo, 'w').close()
except FileNotFoundError:
print('no fifo')
sleep(10)
except (KeyboardInterrupt, SystemExit):
break