Skip to content

Instantly share code, notes, and snippets.

@kmhoran
Created March 21, 2018 19:09
Show Gist options
  • Select an option

  • Save kmhoran/96a578cb6bdd6894343a328c21f6e341 to your computer and use it in GitHub Desktop.

Select an option

Save kmhoran/96a578cb6bdd6894343a328c21f6e341 to your computer and use it in GitHub Desktop.
Video Producer for Kafka Stream
import sys
import time
import cv2
from kafka import KafkaProducer
topic = "distributed-video1"
def publish_video(video_file):
"""
Publish given video file to a specified Kafka topic.
Kafka Server is expected to be running on the localhost. Not partitioned.
:param video_file: path to video file <string>
"""
# Start up producer
producer = KafkaProducer(bootstrap_servers='localhost:9092')
# Open file
video = cv2.VideoCapture(video_file)
print('publishing video...')
while(video.isOpened()):
success, frame = video.read()
# Ensure file was read successfully
if not success:
print("bad read!")
break
# Convert image to png
ret, buffer = cv2.imencode('.jpg', frame)
# Convert to bytes and send to kafka
producer.send(topic, buffer.tobytes())
time.sleep(0.2)
video.release()
print('publish complete')
def publish_camera():
"""
Publish camera video stream to specified Kafka topic.
Kafka Server is expected to be running on the localhost. Not partitioned.
"""
# Start up producer
producer = KafkaProducer(bootstrap_servers='localhost:9092')
camera = cv2.VideoCapture(0)
try:
while(True):
success, frame = camera.read()
ret, buffer = cv2.imencode('.jpg', frame)
producer.send(topic, buffer.tobytes())
# Choppier stream, reduced load on processor
time.sleep(0.2)
except:
print("\nExiting.")
sys.exit(1)
camera.release()
if __name__ == '__main__':
"""
Producer will publish to Kafka Server a video file given as a system arg.
Otherwise it will default by streaming webcam feed.
"""
if(len(sys.argv) > 1):
video_path = sys.argv[1]
publish_video(video_path)
else:
print("publishing feed!")
publish_camera()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment