Beispiel #1
0
def match_star_by_url(image_url):
    # print('match_star_by_url')
    # start_time = time.time()

    try:
        resp = request.urlopen(image_url)
        image = np.asarray(bytearray(resp.read()), dtype='uint8')
        image = cv2.imdecode(image, cv2.IMREAD_COLOR)
    except Exception as e:
        # print('asdfasdf')
        # traceback.print_exc()
        response = utils.respond_failure(str(e))
        print(json.dumps(response))
        return

    try:
        result = match_star(image)
        response = utils.respond_success(result)
        print(json.dumps(response))
    except Exception as e:
        print('asdfasdf')
        traceback.print_exc()
        response = utils.respond_failure(str(e))
        print(json.dumps(response))
        return
Beispiel #2
0
def put(event, context):
    data = json.loads(event.get('body'))
    table = dynamodb.Table(TABLE_NAME)
    id = str(uuid.uuid4())
    response = table.put_item(Item={'id': id, 'task': data['task']})
    if response["ResponseMetadata"]["HTTPStatusCode"] == 200:
        return utils.respond_success({"result": "succesfully added."})
    else:
        return utils.respond_not_found({"error": "ToDo could not be found."})
Beispiel #3
0
def match_star_by_file(image_file, save_image=False, verbose=False):
    # print('match_star_by_file')
    try:
        start_time = time.time()
        image = face_recognition.load_image_file(image_file)
        if verbose:
            duration = time.time() - start_time
            print('load image file duration=%.4fs' % (duration))
    except Exception as e:
        message = traceback.format_exc()
        response = utils.respond_failure(message)
        # print(json.dumps(response))
        # return
        return response

    try:
        # start_time = time.time()
        # stdout.write('file=%s' % (path.basename(image_file)))
        result = match_star(image, save_image=save_image, verbose=verbose)

        dist_list = [star_dist['distance'] for _, star_dist in result.items()]
        dist = sum(dist_list)

        # print('file=%s dist=%.4f' % (path.basename(image_file), ))
        utils.create_pardir(config.dist_filepath)
        fout = open(config.dist_filepath, 'a+')
        fout.write('%.4f\t%s\n' % (dist, image_file))
        fout.close()

        if dist <= 1.8500:
            result['number'] = 5
        elif dist <= 2.3750:
            result['number'] = 4
        elif dist <= 2.6250:
            result['number'] = 3
        elif dist <= 2.9960:
            result['number'] = 2
        else:
            result['number'] = 1

        response = utils.respond_success(result)
        # if verbose:
        #   duration = time.time() - start_time
        #   print('match star duration=%.4fs' % (duration))
        # print(json.dumps(response))
        # return response
    except Exception as e:
        message = traceback.format_exc()
        response = utils.respond_failure(message)
        # print(message)
        # print(json.dumps(response))
    # print(response)
    return response
Beispiel #4
0
def store(event, context):
    data = json.loads(event.get('body'))
    response = demo_table.put_item(
        Item={
            'id': str(uuid.uuid4()),
            'title': data.get('title'),
            'description': data.get('description')
        },
        ConditionExpression='attribute_not_exists(#id)',
        ExpressionAttributeNames={'#id': 'id'},
        ReturnConsumedCapacity='TOTAL')
    return utils.respond_success(response)
Beispiel #5
0
def delete(event, context):
    table = dynamodb.Table(TABLE_NAME)
    id = event.get('pathParameters').get('todoId')
    try:
        response = table.delete_item(Key={'id': id})
    except ClientError as e:
        return utils.respond_error()
    else:
        if response["ResponseMetadata"]["HTTPStatusCode"] == 200:
            return utils.respond_success({"result": "succesfully deleted."})
        else:
            return utils.respond_not_found(
                {"error": "ToDo could not be found."})
Beispiel #6
0
def get(event, context):
    table = dynamodb.Table(TABLE_NAME)
    id = event.get('pathParameters').get('todoId')
    try:
        response = table.get_item(Key={'id': id})
    except ClientError as e:
        return utils.respond_error()
    else:
        if 'Item' in response:
            return utils.respond_success(response['Item'])
        else:
            return utils.respond_not_found(
                {"error": "ToDo could not be found."})
Beispiel #7
0
def update(event, context):
    data = json.loads(event.get('body'))
    response = demo_table.update_item(
        Key={'id': event.get('pathParameters').get('item')},
        UpdateExpression='SET #title = :title, #description = :description',
        ExpressionAttributeValues={
            ':title': data.get('title'),
            ':description': data.get('description')
        },
        ExpressionAttributeNames={
            '#title': 'title',
            '#description': 'description'
        },
        ReturnConsumedCapacity='TOTAL')
    return utils.respond_success(response)
Beispiel #8
0
def greet(event, context):
    response = f"Hi!!! Greetings from CDK. You have hit {event.get('path')} at {datetime.now().isoformat()}"
    return utils.respond_success({"message": response})
Beispiel #9
0
def delete(event, context):
    response = demo_table.delete_item(
        Key={'id': event.get('pathParameters').get('item')},
        ReturnConsumedCapacity='TOTAL')
    return utils.respond_success(response)
Beispiel #10
0
def index(event, context):
    response = demo_table.scan(Limit=10, ReturnConsumedCapacity='TOTAL')
    return utils.respond_success(response)
Beispiel #11
0
def get(event, context):
    table = dynamodb.Table(TABLE_NAME)
    response = table.scan()

    return utils.respond_success(response['Items'])    
def greet(event, context):
    response = "Hi! This is simple ToDo API. You have hit {}".format(
        event.get('path'))
    return utils.respond_success(response)