Skip to content

Commit

Permalink
Debugging #1
Browse files Browse the repository at this point in the history
  • Loading branch information
rafie committed Oct 27, 2019
1 parent 1f59ecd commit b6c0fef
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 36 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![CircleCI](https://circleci.com/gh/RedisGears/AnimalRecognitionDemo/tree/master.svg?style=svg)](https://circleci.com/gh/RedisGears/AnimalRecognitionDemo/tree/master)

# AnimalRecognitionDemo

This demo combines several [Redis](https://redis.io) data structures and [Redis Modules](https://redis.io/topics/modules-intro)
Expand Down
8 changes: 5 additions & 3 deletions app/gear.py

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions camera/Dockerfile.debian
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
FROM python:2
FROM debian:buster

RUN set -ex;\
apt-get -qq update;\
apt-get install -y python-opencv
RUN apt-get -qq update
RUN apt-get -q install -y python wget
RUN set -x; \
wget -q https://bootstrap.pypa.io/get-pip.py -O /tmp/get-pip.py ;\
python /tmp/get-pip.py
RUN apt-get -q install -y python-opencv

WORKDIR /usr/src/app
ADD read_camera.py ./
ADD *.jpg ./
RUN pip install redis

CMD [ "python", "./read_camera.py" ]
12 changes: 8 additions & 4 deletions camera/read_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import redis
import time
import sys
import os

try:
import urlparse
Expand All @@ -12,6 +13,8 @@
IMAGE_WIDTH = 640
IMAGE_HEIGHT = 480

MAX_IMAGES = 5 # 1000

class Webcam:
def __init__(self, infile=0, fps=15.0):
self.cam = cv2.VideoCapture(infile)
Expand Down Expand Up @@ -47,7 +50,7 @@ def __len__(self):
parser.add_argument('--fmt', help='Frame storage format', type=str, default='.jpg')
parser.add_argument('--fps', help='Frames per second (webcam)', type=float, default=15.0)
parser.add_argument('--maxlen', help='Maximum length of output stream', type=int, default=1000)
parser.add_argument('--test', help='transmit image instead of reading webcam', type=str)
parser.add_argument('--test', help='transmit image instead of reading webcam', action="store_true")
args = parser.parse_args()

# Set up Redis connection
Expand Down Expand Up @@ -76,9 +79,10 @@ def __len__(self):
print('count: {} id: {}'.format(count, _id))
sys.stdout.flush()
else:
print('Operating in test mode')
image_file = os.environ['ANIMAL'] + '.jpg'
print('Operating in test mode with image ' + image_file)
sys.stdout.flush()
img0 = cv2.imread(args.test)
img0 = cv2.imread(image_file)
img = cv2.resize(img0, (IMAGE_WIDTH, IMAGE_HEIGHT))
rc, data = cv2.imencode(args.fmt, img)
count = 1
Expand All @@ -87,7 +91,7 @@ def __len__(self):
'count': count,
'image': data.tobytes()
}
_id = conn.execute_command('xadd', args.output, 'MAXLEN', '~', '1000', '*', 'count', msg['count'], 'img', msg['image'])
_id = conn.execute_command('xadd', args.output, 'MAXLEN', '~', str(MAX_IMAGES), '*', 'count', msg['count'], 'img', msg['image'])
# print('count: {} rc: {} id: {}'.format(count, rc, _id))
# sys.stdout.flush()
count += 1
Expand Down
66 changes: 45 additions & 21 deletions tests/cats-n-dogs.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

[[ $VERBOSE == 1 ]] && set -x
# [[ $VERBOSE == 1 ]] && set -x

HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
cd $HERE/..
Expand All @@ -14,53 +14,77 @@ fi
PROJECT=catsndogs
DOCKER_LOG=/tmp/cats-n-dogs.log
SPEC="--no-ansi -p $PROJECT -f docker-compose.yaml -f tests/cats-n-dogs.yaml"
[[ $REBUILD == 1 ]] && BUILD_ARG=--build

rm -f $DOCKER_LOG

start() {
if [[ $VERBOSE == 1 ]]; then
ANIMAL=$1 docker-compose $SPEC up -d
docker-compose $SPEC up $BUILD_ARG -d
else
ANIMAL=$1 docker-compose $SPEC up -d >> $DOCKER_LOG 2>&1
docker-compose $SPEC up $BUILD_ARG -d >> $DOCKER_LOG 2>&1
fi
BUILD_ARG=''
}

stop() {
if [[ $VERBOSE == 1 ]]; then
ANIMAL=$1 docker-compose $SPEC down -v --remove-orphans
docker-compose $SPEC down -v --remove-orphans
else
ANIMAL=$1 docker-compose $SPEC down -v --remove-orphans >> $DOCKER_LOG 2>&1
docker-compose $SPEC down -v --remove-orphans >> $DOCKER_LOG 2>&1
fi
}

build() {
if [[ $VERBOSE == 1 ]]; then
docker-compose $SPEC build
else
docker-compose $SPEC build >> $DOCKER_LOG 2>&1
fi
}

show_logs() {
docker-compose $SPEC logs $*
}

cats_demo() {
echo "Testing ${1}s ..."
# local PROJECT=${PROJECT}_$1
start $1
echo "Testing ${ANIMAL}s ..."
start
sleep 3
num_cats=$(redis-cli $host_arg xlen cats | cat)
stop $1
if [[ $VERBOSE == 1 ]]; then
echo "num_cats=$num_cats"
show_logs
else
echo "num_cats=$num_cats" >> $DOCKER_LOG
show_logs >> $DOCKER_LOG
fi
stop
}

if [[ $1 == help ]]; then
echo "$0: [start ANIMAL|stop|help]"
cmd=$1
shift
if [[ $cmd == help ]]; then
echo "[ANIMAL=cat|dog] [VERBOSE=0|1] [REBUILD=0|1] $0 [start|stop|build|logs|help]"
exit 0
elif [[ $1 == start ]]; then
shift
VERBOSE=1 start $1
elif [[ $1 == stop ]]; then
shift
VERBOSE=1 stop $1
elif [[ $cmd == start ]]; then
start
elif [[ $cmd == stop ]]; then
stop
elif [[ $cmd == build ]]; then
build
elif [[ $cmd == logs ]]; then
show_logs $*
else
cats_demo cat
if [[ $num_cats == 0 ]]; then
ANIMAL=cat cats_demo
if [[ -z $num_cats || $num_cats == 0 ]]; then
echo "cats: FAIL"
exit 1
fi
echo "cats: OK"

cats_demo dog
if [[ $num_cats != 0 ]]; then
ANIMAL=dog cats_demo
if [[ -z $num_cats || $num_cats != 0 ]]; then
echo "dogs: FAIL"
exit 1
fi
Expand Down
8 changes: 5 additions & 3 deletions tests/cats-n-dogs.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
version: '3'
services:
camera:
build: camera
build:
context: camera
dockerfile: Dockerfile.debian
depends_on:
- redis
environment:
- ANIMAL=cat
command: ['python', './read_camera.py', '-u', 'redis://redis:6379', '--test', '${ANIMAL}.jpg']
- ANIMAL
command: python ./read_camera.py -u redis://redis:6379 --test
2 changes: 1 addition & 1 deletion tests/show-log.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ else
host_arg="-h $(echo $DOCKER_HOST|cut -d: -f1)"
fi

redis-cli $host_arg lrange log 0 -1 | cat
redis-cli $host_arg xrange log - + | cat
16 changes: 16 additions & 0 deletions tests/show-stream-stat.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

if [[ -z $DOCKER_HOST ]]; then
host_arg=""
else
host_arg="-h $(echo $DOCKER_HOST|cut -d: -f1)"
fi

stream_size() {
echo $(redis-cli $host_arg xlen $1|cat)
}

echo camera: $(stream_size camera:0)
echo all: $(stream_size all)
echo cats: $(stream_size cats)
echo log: $(stream_size log)

0 comments on commit b6c0fef

Please sign in to comment.