Skip to content

Commit 9f29dc1

Browse files
committed
Adds image capturer
1 parent d8281ad commit 9f29dc1

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
node_modules/
22
.DS_Store
33
kafka_2.11-0.10.0.0/
4+
cv.py
5+
cv2.so
6+
tmp.jpg

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ ullala is a demo that showcase using kafka as a data pipeline and log aggregatio
88
2. Run Kafka, Zookeeper, Elasticsearch and Logstash: `docker-compose up`
99
3. Set kafka on your host file to point to the running kafka instance (docker-machine ip default on mac)
1010
3. Setup topics `scripts/setup-topics.sh`
11-
11+
4. Install OpenCV for capturer, `brew install opencv` on mac.
12+
5. Symlink opencv into capturer, for mac you can run `scripts/symlink-opencv-bew.sh`
13+
6. Install capturer dependencies `pip install -r capturer/requirements.txt`
14+
6. Run Capturer: `KAFKA_HOSTS=KAFKA:9092 capturer/capturer.py

capturer/capture.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from __future__ import print_function
2+
3+
import io
4+
import sys
5+
import logging
6+
import calendar
7+
from os import getenv
8+
from time import sleep
9+
from datetime import datetime
10+
11+
import avro.io
12+
import avro.schema
13+
from SimpleCV import Camera
14+
from kafka import KafkaProducer
15+
16+
def get_timestamp():
17+
timestamp = datetime.now()
18+
return '{0}:{1}:{2}'.format(timestamp.hour, timestamp.minute, timestamp.second)
19+
20+
def run_capturer(kafka_hosts, fps=24):
21+
producer = KafkaProducer(bootstrap_servers=kafka_hosts)
22+
cam = Camera()
23+
while True:
24+
img = cam.getImage()
25+
img.drawText(get_timestamp(), fontsize=160)
26+
img.save('tmp.jpg')
27+
with open('tmp.jpg', mode='rb') as file:
28+
content = file.read()
29+
producer.send('CAMERA_FEED', pack_image(content))
30+
print('Got an image')
31+
sleep(0.4)
32+
33+
def pack_image(image):
34+
schema_string = """
35+
{
36+
"namespace": "ullala",
37+
"type": "record",
38+
"name": "Image",
39+
"fields": [
40+
{"name": "image", "type": "bytes"},
41+
{"name": "capture_timestamp", "type": "int"}
42+
]
43+
}
44+
"""
45+
schema = avro.schema.parse(schema_string)
46+
capture_timestamp = calendar.timegm(datetime.utcnow().utctimetuple())
47+
48+
writer = avro.io.DatumWriter(schema)
49+
50+
bytes_writer = io.BytesIO()
51+
encoder = avro.io.BinaryEncoder(bytes_writer)
52+
53+
writer.write({"image": image, "capture_timestamp": capture_timestamp}, encoder)
54+
55+
return bytes_writer.getvalue()
56+
57+
58+
if __name__ == '__main__':
59+
logging.getLogger().setLevel(logging.DEBUG)
60+
kafka_hosts = getenv('KAFKA_HOSTS')
61+
if not kafka_hosts:
62+
print('You need to set $KAFKA_HOSTS environment variable', file=sys.stderr)
63+
exit(1)
64+
run_capturer(kafka_hosts, fps=24)

capturer/requirements.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
scipy
2+
simplecv
3+
pygame
4+
kafka-python
5+
avro

scripts/symlink-opencv-brew.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#! /bin/sh
2+
3+
ln -s /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv2.so `pwd`/capturer/cv2.so
4+
ln -s /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv.py `pwd`/capturer/cv.py

0 commit comments

Comments
 (0)