Last active
December 16, 2015 17:26
-
-
Save mmitou/2927b4a0822e92d95c24 to your computer and use it in GitHub Desktop.
物体検出
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding:utf-8 -*- | |
| from __future__ import print_function, unicode_literals, division | |
| import boto3 | |
| import cv2 | |
| import numpy as np | |
| import urllib | |
| ## test data | |
| # NUM_OF_HISTORY = 50 | |
| # BUCKET_NAME = 'mmitou' | |
| # PREFIX = 'data3/' | |
| # TOPIC_ARN = 'arn:aws:sns:ap-northeast-1:770170371212:test' | |
| # RATIO_THRESHOLD = 10 | |
| # NUM_OF_WAIT = 3 | |
| TABLE_NAME = 'User' | |
| ITEM_ID = 'test' | |
| def get_latest_jpg_keys(bucket_name, prefix, num): | |
| s3 = boto3.resource('s3') | |
| bucket = s3.Bucket(bucket_name) | |
| # s3バケットの指定したprefixを持つ全てのファイルは、 | |
| # 日付.jpgという名前である事を前提としている | |
| keys = sorted([objsummary.key for objsummary in bucket.objects.filter(Prefix=prefix) if objsummary.key.endswith('.jpg')]) | |
| # 後ろからnum個だけ取得する | |
| latest_keys = keys[ (-num) if len(keys) > num else 0 : ] | |
| return latest_keys | |
| def get_file(bucket_name, key): | |
| s3 = boto3.resource('s3') | |
| obj = s3.Object(bucket_name, key) | |
| response = obj.get() | |
| string = response['Body'].read() | |
| return string | |
| def string2img(string): | |
| mat = np.fromstring(string, np.uint8) | |
| img = cv2.imdecode(mat, cv2.IMREAD_COLOR) | |
| return img | |
| def has_sent_notification(): | |
| dynamodb = boto3.session.Session(region_name='ap-northeast-1').resource('dynamodb') | |
| table = dynamodb.Table(TABLE_NAME) | |
| response = table.get_item(Key = {'id':ITEM_ID}) | |
| return response['Item']['has_sent'] | |
| def cancel_notification(): | |
| dynamodb = boto3.session.Session(region_name='ap-northeast-1').resource('dynamodb') | |
| table = dynamodb.Table(TABLE_NAME) | |
| response = table.get_item(Key = {'id':ITEM_ID}) | |
| response['Item']['has_sent'] = False | |
| table.put_item(Item = response['Item']) | |
| def send_notification(bucket_name, key, topic_arn): | |
| sns = boto3.session.Session(region_name='ap-northeast-1').resource('sns') | |
| s3 = boto3.client('s3') | |
| topic = sns.Topic(topic_arn) | |
| url = s3.generate_presigned_url('get_object', Params = {'Bucket': bucket_name, 'Key': key}) | |
| topic.publish(Subject='detected object', Message = url) | |
| dynamodb = boto3.session.Session(region_name='ap-northeast-1').resource('dynamodb') | |
| table = dynamodb.Table(TABLE_NAME) | |
| response = table.get_item(Key = {'id':ITEM_ID}) | |
| response['Item']['has_sent'] = True | |
| table.put_item(Item = response['Item']) | |
| def uploaded_image_key(event): | |
| bucket = event['Records'][0]['s3']['bucket']['name'] | |
| key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8') | |
| return key | |
| def mask_ratio(mask): | |
| w, h = mask.shape | |
| p = cv2.countNonZero(mask) / (w * h) | |
| return int(p * 100) | |
| def has_detected(mask_ratios, ratio_threshold, num_of_wait): | |
| return all([ratio > ratio_threshold for ratio in mask_ratios[- num_of_wait:]]) | |
| def has_detected_other_objects(mask_ratios, ratio_threshold, num_of_wait): | |
| return all([ratio > ratio_threshold and ratio <= mask_ratios[- num_of_wait] for ratio in mask_ratios[- 2 * num_of_wait:]]) | |
| def get_user_item(): | |
| dynamodb = boto3.session.Session(region_name='ap-northeast-1').resource('dynamodb') | |
| table = dynamodb.Table(TABLE_NAME) | |
| response = table.get_item(Key = {'id':ITEM_ID}) | |
| return response['Item'] | |
| def lambda_handler(event, context): | |
| item = get_user_item() | |
| topic_arn = item['topic_arn'].decode() | |
| num_of_wait = int(item['num_of_wait']) | |
| ratio_threshold = int(item['ratio_threshold']) | |
| num_of_history = int(item['num_of_history']) | |
| bucket_name = item['bucket_name'].decode() | |
| prefix = item['prefix'].decode() | |
| keys = get_latest_jpg_keys(bucket_name, prefix, num_of_history) | |
| if len(keys) < num_of_history: | |
| print('there are not required number of images.') | |
| return False | |
| imgs = [string2img(get_file(bucket_name, key)) for key in keys] | |
| substractor = cv2.createBackgroundSubtractorKNN() | |
| masks = [substractor.apply(img) for img in imgs] | |
| ratios = [mask_ratio(mask) for mask in masks[-2*num_of_wait:]] | |
| print(ratios) | |
| if has_sent_notification(): | |
| if has_detected_other_objects(ratios, ratio_threshold, num_of_wait): | |
| print('detect other objects.') | |
| send_notification(bucket_name, keys[-1], topic_arn) | |
| return True | |
| if has_detected(ratios, ratio_threshold, num_of_wait): | |
| print('detected object had been notified.') | |
| return True | |
| print('detected object had gone.') | |
| cancel_notification() | |
| return False | |
| else: | |
| if has_detected(ratios, ratio_threshold, num_of_wait): | |
| print('detect new object') | |
| send_notification(bucket_name, keys[-1], topic_arn) | |
| return True | |
| else: | |
| print('there are no objects.') | |
| return False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| set -eu | |
| if [ $# -ne 2 ]; then | |
| echo "Usage:" | |
| echo "${0} num_of_pics dest_dir" | |
| exit 1 | |
| fi | |
| [ ! -d $2 ] && mkdir ${2} | |
| for i in `seq 0 ${1}`; do | |
| path=${2}/`date +%Y-%m-%d-%H%M%S`.jpg | |
| echo ${i} ${path} | |
| fswebcam -r 640x480 --jpeg 85 -D 60 -S 30 --no-banner ${path} | |
| aws s3 cp ${path} s3://mmitou/${path} --profile s3user | |
| done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| id (S) | bucket_name (S) | has_sent (BOOL) | num_of_history (N) | num_of_wait (N) | prefix (S) | ratio_threshold (N) | topic_arn (S) | |
|---|---|---|---|---|---|---|---|---|
| test | mmitou | false | 250 | 3 | data4/ | 10 | arn:aws:sns:ap-northeast-1:770170371212:test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment