Skip to content

Commit 1ffe1fb

Browse files
authored
Added Makefile and automatic tests (#11)
1 parent e64523c commit 1ffe1fb

File tree

17 files changed

+414
-58
lines changed

17 files changed

+414
-58
lines changed

.circleci/config.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
version: 2.1
2+
3+
jobs:
4+
test:
5+
machine:
6+
enabled: true
7+
image: ubuntu-1604:201903-01
8+
steps:
9+
- checkout
10+
- run:
11+
name: System Setup
12+
command: |
13+
sudo apt-get -qq update -y
14+
sudo apt-get -q install -y curl ca-certificates wget make
15+
(cd ..; git clone https://github.com/antirez/redis.git; cd redis; git checkout 5.0.8; make -j$(nproc); sudo make install)
16+
SUDO=sudo make setup
17+
- run:
18+
name: Test
19+
command: |
20+
make test VERBOSE=1
21+
22+
workflows:
23+
version: 2
24+
build_and_package:
25+
jobs:
26+
- test:
27+
filters:
28+
tags:
29+
only: /.*/
30+
branches:
31+
only: /.*/

Makefile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
start:
3+
@docker-compose up -d
4+
5+
stop:
6+
@docker-compose down
7+
8+
build:
9+
@docker-compose build
10+
11+
test:
12+
@./tests/cats-n-dogs.sh
13+
14+
ifdef REDIS
15+
CAMERA_ARG=-u $(REDIS)
16+
endif
17+
18+
camera:
19+
ifneq ($(VENV),0)
20+
python2 -m virtualenv venv
21+
. ./venv/bin/activate; pip install -r camera/requirements.txt
22+
. ./venv/bin/activate; python camera/read_camera.py $(CAMERA_ARG)
23+
else
24+
python2 camera/read_camera.py $(CAMERA_ARG)
25+
endif
26+
27+
setup:
28+
@$(SUDO) curl -s -L https://github.com/docker/compose/releases/download/1.25.4/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose ;\
29+
$(SUDO) chmod +x /usr/local/bin/docker-compose
30+
@wget -q https://github.com/git-lfs/git-lfs/releases/download/v2.10.0/git-lfs-linux-amd64-v2.10.0.tar.gz -O /tmp/git-lfs.tar.gz ;\
31+
cd /tmp; tar xf git-lfs.tar.gz; $(SUDO) ./install.sh
32+
@git lfs pull
33+
34+
.PHONY: start stop build test camera setup

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[![CircleCI](https://circleci.com/gh/RedisGears/AnimalRecognitionDemo/tree/master.svg?style=svg)](https://circleci.com/gh/RedisGears/AnimalRecognitionDemo/tree/master)
2+
13
# AnimalRecognitionDemo
24

35
This demo combines several [Redis](https://redis.io) data structures and [Redis Modules](https://redis.io/topics/modules-intro)

app/Dockerfile

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
# FROM python:3.7.3
2-
FROM debian:buster
1+
# OSNICK=stretch|bionic|buster
2+
ARG OSNICK=buster
3+
4+
#----------------------------------------------------------------------------------------------
5+
FROM redisfab/redisedgevision-${OSNICK}:0.2.0
6+
7+
# This is due on the following error on ARMv8:
8+
# /usr/lib/aarch64-linux-gnu/libgomp.so.1: cannot allocate memory in static TLS block
9+
# Something is exausting TLS, causing libgomp to fail. Preloading it as a workaround helps.
10+
# ENV LD_PRELOAD /usr/lib/aarch64-linux-gnu/libgomp.so.1
311

412
ENV DEBIAN_FRONTEND=noninteractive
5-
ENV RUNTIME_DEPS "python3 python3-setuptools ca-certificates curl python3-opencv"
613

7-
RUN set -ex ;\
8-
apt-get -qq update ;\
9-
apt-get -q install -y --no-install-recommends $RUNTIME_DEPS
14+
RUN apt-get -qq update
1015

1116
RUN set -ex ;\
12-
curl -q -s https://bootstrap.pypa.io/get-pip.py -o /tmp/get-pip.py ;\
17+
apt-get install -y wget python3-distutils ;\
18+
wget -q https://bootstrap.pypa.io/get-pip.py -O /tmp/get-pip.py ;\
1319
python3 /tmp/get-pip.py
1420

1521
WORKDIR /app

app/gear.py

Lines changed: 43 additions & 22 deletions
Large diffs are not rendered by default.

camera/Dockerfile

Lines changed: 0 additions & 7 deletions
This file was deleted.

camera/Dockerfile.debian

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
1-
FROM python:2
1+
# OSNICK=stretch|bionic|buster
2+
ARG OSNICK=buster
23

3-
RUN set -ex;\
4-
apt-get -qq update;\
5-
apt-get install -y python-opencv
4+
#----------------------------------------------------------------------------------------------
5+
FROM redisfab/redisedgevision-${OSNICK}:0.2.0
6+
7+
# This is due on the following error on ARMv8:
8+
# /usr/lib/aarch64-linux-gnu/libgomp.so.1: cannot allocate memory in static TLS block
9+
# Something is exausting TLS, causing libgomp to fail. Preloading it as a workaround helps.
10+
# ENV LD_PRELOAD /usr/lib/aarch64-linux-gnu/libgomp.so.1
11+
12+
ENV DEBIAN_FRONTEND=noninteractive
13+
14+
RUN apt-get -qq update
15+
16+
RUN set -x; \
17+
apt-get install -y wget python3-distutils ;\
18+
wget -q https://bootstrap.pypa.io/get-pip.py -O /tmp/get-pip.py ;\
19+
python3 /tmp/get-pip.py
20+
21+
RUN pip install redis==3.2.1
622

723
WORKDIR /usr/src/app
24+
825
ADD read_camera.py ./
9-
RUN pip install redis
26+
ADD *.jpg ./
1027

11-
CMD [ "python", "./read_camera.py" ]
28+
CMD [ "python3", "./read_camera.py" ]

camera/cat.jpg

152 KB
Loading

camera/dog.jpg

81.7 KB
Loading

camera/read_camera.py

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1+
12
import argparse
23
import cv2
34
import redis
5+
import time
6+
import sys
7+
import os
8+
49
try:
510
import urllib.parse
611
except ImportError:
712
import urllib.parse as urlparse
813

14+
IMAGE_WIDTH = 640
15+
IMAGE_HEIGHT = 480
16+
17+
MAX_IMAGES = 1000 # 5
18+
919
class Webcam:
1020
def __init__(self, infile=0, fps=15.0):
1121
self.cam = cv2.VideoCapture(infile)
1222
self.cam.set(cv2.CAP_PROP_FPS, fps)
13-
self.cam.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
14-
self.cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
23+
self.cam.set(cv2.CAP_PROP_FRAME_WIDTH, IMAGE_WIDTH)
24+
self.cam.set(cv2.CAP_PROP_FRAME_HEIGHT, IMAGE_HEIGHT)
1525

1626
def __iter__(self):
1727
self.count = -1
@@ -41,24 +51,49 @@ def __len__(self):
4151
parser.add_argument('--fmt', help='Frame storage format', type=str, default='.jpg')
4252
parser.add_argument('--fps', help='Frames per second (webcam)', type=float, default=15.0)
4353
parser.add_argument('--maxlen', help='Maximum length of output stream', type=int, default=1000)
54+
parser.add_argument('--test', help='transmit image instead of reading webcam', action="store_true")
4455
args = parser.parse_args()
4556

4657
# Set up Redis connection
4758
url = urllib.parse.urlparse(args.url)
4859
conn = redis.Redis(host=url.hostname, port=url.port)
4960
if not conn.ping():
5061
raise Exception('Redis unavailable')
62+
print('Connected to Redis')
63+
sys.stdout.flush()
5164

52-
if args.infile is None:
53-
loader = Webcam(infile=0, fps=args.fps)
54-
else:
55-
loader = Webcam(infile=int(args.infile), fps=args.fps)
65+
if args.test is None:
66+
print('Operating in camera mode')
67+
sys.stdout.flush()
68+
if args.infile is None:
69+
loader = Webcam(infile=0, fps=args.fps)
70+
else:
71+
loader = Webcam(infile=int(args.infile), fps=args.fps)
5672

57-
for (count, img) in loader:
73+
for (count, img) in loader:
74+
_, data = cv2.imencode(args.fmt, img)
75+
msg = {
76+
'count': count,
77+
'image': data.tobytes()
78+
}
79+
_id = conn.execute_command('xadd', args.output, 'MAXLEN', '~', str(MAX_IMAGES), '*', 'count', msg['count'], 'img', msg['image'])
80+
print('count: {} id: {}'.format(count, _id))
81+
sys.stdout.flush()
82+
else:
83+
image_file = os.environ['ANIMAL'] + '.jpg'
84+
print('Operating in test mode with image ' + image_file)
85+
sys.stdout.flush()
86+
img0 = cv2.imread(image_file)
87+
img = cv2.resize(img0, (IMAGE_WIDTH, IMAGE_HEIGHT))
5888
_, data = cv2.imencode(args.fmt, img)
59-
msg = {
60-
'count': count,
61-
'image': data.tobytes()
62-
}
63-
_id = conn.execute_command('xadd', args.output, 'MAXLEN', '~', '1000', '*', 'count', msg['count'], 'img', msg['image'])
64-
print(('count: {} id: {}'.format(count, _id)))
89+
count = 1
90+
while True:
91+
msg = {
92+
'count': count,
93+
'image': data.tobytes()
94+
}
95+
_id = conn.execute_command('xadd', args.output, 'MAXLEN', '~', str(MAX_IMAGES), '*', 'count', msg['count'], 'img', msg['image'])
96+
# print('count: {} rc: {} id: {}'.format(count, rc, _id))
97+
# sys.stdout.flush()
98+
count += 1
99+
time.sleep(0.1)

0 commit comments

Comments
 (0)