def image_register(request):
    params = request.GET if request.method == "GET" else request.POST

    if not ("url" in params):
        content = {"message": u"パラメータ`url`が指定されていません"}
        return Response(content, status=status.HTTP_400_BAD_REQUEST)

    try:
        image = Images.objects.get(url=params["url"])
    except ObjectDoesNotExist:
        image = Images(url=params["url"], adult_flag=False, grotesque_flag=False)

    descriptor = factory.descriptor(filepath=image.local_path)
    if descriptor == None:
        content = {"message": u"ローカルに画像が見つかりません"}
        return Response(content, status=status.HTTP_412_PRECONDITION_FAILED)
    else:
        tmp = TemporaryFile()
        try:
            np.save(tmp, descriptor)
            tmp.seek(0)

            image.description = tmp.read()
            image.save()
        finally:
            tmp.close()

        return Response(ImageMapper(image).as_dict())
def image_detect(request):
    params = request.GET if request.method == "GET" else request.POST

    if not ("url" in params):
        content = {"message": u"パラメータ`url`が指定されていません"}
        return Response(content, status=status.HTTP_400_BAD_REQUEST)

    image = Images.objects.get(url=params["url"])
    query = factory.descriptor(filepath=image.local_path)

    # アダルト判定
    _descriptors = [desc.description for desc in Images.objects.filter(adult_flag=True)]
    descriptors = []
    for d in _descriptors:
        tmp = NamedTemporaryFile()
        try:
            tmp.write(d)
            tmp.seek(0)
            data = np.load(tmp.name)
            descriptors.append(data)
        finally:
            tmp.close()
    image.adult_flag = 10 if factory.harmful_check(query, descriptors) else -1
    print "adult: ", image.adult_flag

    # グロテスク判定
    _descriptors = [desc.description for desc in Images.objects.filter(adult_flag=True)]
    descriptors = []
    for d in _descriptors:
        tmp = NamedTemporaryFile()
        try:
            tmp.write(d)
            tmp.seek(0)
            data = np.load(tmp.name)
            descriptors.append(data)
        finally:
            tmp.close()
    image.grotesque_flag = 10 if factory.harmful_check(query, descriptors) else -1
    print "gro: ", image.grotesque_flag

    # descriptionの保存
    tmp = TemporaryFile()
    try:
        np.save(tmp, query)
        tmp.seek(0)

        image.description = tmp.read()
        image.save()
    finally:
        tmp.close()

    return Response(ImageMapper(image).as_dict())