-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumer.py
More file actions
executable file
·101 lines (86 loc) · 3.3 KB
/
Copy pathconsumer.py
File metadata and controls
executable file
·101 lines (86 loc) · 3.3 KB
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
import sys
from argparse import ArgumentParser, FileType
from configparser import ConfigParser
from confluent_kafka import Consumer, OFFSET_END, OFFSET_BEGINNING, TopicPartition
import numpy as np;
import json
import time
class KafkaConsumer:
def __init__(self, groupId, topics, onAssign = None):
config_parser = ConfigParser()
config_parser.read("config.ini")
self.config = dict(config_parser['default'])
self.config.update(config_parser['consumer'])
self.config.update({"group.id": groupId})
self.consumer = Consumer(self.config)
if onAssign != None:
self.consumer.subscribe([topics], on_assign = onAssign)
else:
self.consumer.subscribe([topics])
class SetupConsumer(KafkaConsumer):
def __init__(self, groupId):
super().__init__(groupId = groupId, topics = "setup")
self.topic = TopicPartition("setup", partition = 0)
def receive_setup_value(self):
msg = self.consumer.poll(0)
if msg is None:
pass
elif msg.error():
print("ERROR: %s".format(msg.error()))
else:
return True, int(msg.value())
return False, 0
class FrameConsumer(KafkaConsumer):
def __init__(self, groupId):
super().__init__(groupId = groupId, topics = "frame")
def receive_frame(self):
msg = self.consumer.poll(0)
if msg is None:
pass
elif msg.error():
print("ERROR: %s".format(msg.error()))
else:
return True, {
"offset": int(msg.key()),
"data": np.array(list(msg.value())).reshape(480, 640, 3).astype(np.uint8)
}, msg.offset()
return False, 0, 0
class LatestFrameConsumer(KafkaConsumer):
def __init__(self, groupId):
super().__init__(groupId = groupId, topics = "latest_frame")
self.topic = TopicPartition("latest_frame", partition = 0)
def receive_latest_frame(self):
msg = self.consumer.poll(0.15)
topic_with_latest_offset = TopicPartition("latest_frame", partition = 0, offset = OFFSET_END)
self.consumer.assign([topic_with_latest_offset])
if msg is None:
pass
elif msg.error():
print("ERROR: %s".format(msg.error()))
else:
return True, np.array(list(msg.value())).reshape(480, 640, 3).astype(np.uint8), msg.offset()
return False, 0, 0
class BoundingBoxConsumer(KafkaConsumer):
def __init__(self, groupId):
super().__init__(groupId = groupId, topics = "bounding_box")
def receive_bounding_box(self):
msg = self.consumer.poll(0)
if msg is None:
pass
elif msg.error():
print("ERROR: %s".format(msg.error()))
else:
return True, json.loads(msg.value()), msg.offset()
return False, 0, 0
class KeypointsConsumer(KafkaConsumer):
def __init__(self, groupId):
super().__init__(groupId = groupId, topics = "keypoints")
def receive_keypoints(self):
msg = self.consumer.poll(0)
if msg is None:
pass
elif msg.error():
print("ERROR: %s".format(msg.error()))
else:
return True, json.loads(msg.value()), msg.offset()
return False, 0, 0