-
Notifications
You must be signed in to change notification settings - Fork 0
/
Read_frame_process.py
51 lines (42 loc) · 1.62 KB
/
Read_frame_process.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
from confluent_kafka import Producer, KafkaError
import cv2
import time
from config import *
class KafkaFrameProducer:
def __init__(self, bootstrap_servers=BOOTSTRAP_SERVERS, topic='Frames') -> None:
self.bootstrap_servers = bootstrap_servers
self.topic = topic
self.producer_config = {
'bootstrap.servers': self.bootstrap_servers
}
self.producer = Producer(self.producer_config)
self.count = 0
self.HD = (1080, 720)
self.FHD = (1920, 1080)
self.delay = 0.3
def delivery_report(self, err, msg):
if err is not None:
print('Message delivery failed: {}'.format(err))
else:
print("frame: {}, topic name: {}, partition: {}, offset: {}".format(
self.count, msg.topic(), msg.partition(), msg.offset()))
def send_frame(self, image):
ret, buffer = cv2.imencode('.jpeg', image)
self.producer.produce(
self.topic, value=buffer.tobytes(), callback=self.delivery_report)
self.producer.poll(0) # trigger delivery report callback
def send_video(self, video_path):
vidcap = cv2.VideoCapture(video_path)
while True:
success, image = vidcap.read()
if not success:
break
image = cv2.resize(image, self.FHD)
self.count += 1
self.send_frame(image)
time.sleep(self.delay)
vidcap.release()
self.producer.flush()
if __name__ == "__main__":
kafka_producer = KafkaFrameProducer()
kafka_producer.send_video(video_path='demo_input/video/demo_iai_1.mp4')