示例#1
0
def bench(completeFilename):
    random_id = uuid.uuid4.__str__()
    aws_filename = random_id + '.jpg'
    amazon_start = datetime.now()
    awsFilename = aws.uploadData(completeFilename, aws_filename)
    amazon_end = datetime.now()
    length_aws = amazon_end - amazon_start

    # Save the image into redis
    redis_start = datetime.now()
    file = open(completeFilename, "rb")
    data = file.read()
    file.close()
    rediscache.__setCache(random_id, data, 3600)
    redis_end = datetime.now()
    length_redis = redis_end - redis_start
    logger.warning("AWS/REDIS:{}/{}".format(length_aws, length_redis))
示例#2
0
def image():
    try:
        if (postgres.__checkHerokuLogsTable()):
            postgres.__saveLogEntry(request)
        logger.debug(utils.get_debug_all(request))
        imageid = request.args['id']
        i = request.files['fileToUpload']  # get the image
        f = ('%s.jpeg' % (imageid))
        i.save('%s/%s' % (PATH_TO_TEST_IMAGES_DIR, f))
        completeFilename = '%s/%s' % (PATH_TO_TEST_IMAGES_DIR, f)
        # now upload
        logger.debug(completeFilename)
        awsFilename = aws.uploadData(completeFilename, f)

        return "File received, thanks for sharing!  : You can review it here : " + awsFilename, 200
    except Exception as e:
        import traceback
        traceback.print_exc()
        return "An error occured, check logDNA for more information", 200
示例#3
0
文件: aws.py 项目: touvok/uglydemo
def AWS_upload(file, request):
    from PIL import Image, ExifTags
    from libs import aws

    PATH_TO_TEST_IMAGES_DIR = './images'
    i = file  # get the image
    imageid = uuid.uuid4().__str__()
    f = ('%s.jpeg' % (imageid))
    i.save('%s/%s' % (PATH_TO_TEST_IMAGES_DIR, f))
    completeFilename = '%s/%s' % (PATH_TO_TEST_IMAGES_DIR, f)
    try:
        filepath = completeFilename
        image = Image.open(filepath)

        img_width = image.size[0]
        img_height = image.size[1]

        for orientation in ExifTags.TAGS.keys():
            if ExifTags.TAGS[orientation] == 'Orientation':
                break
        try:
            exif = dict(image._getexif().items())
            logger.debug(exif[orientation])
            if exif[orientation] == 3:
                image = image.rotate(180, expand=True)
                image.save(
                    filepath,
                    quality=50,
                    subsampling=0,
                )
            elif exif[orientation] == 6:
                image = image.rotate(270, expand=True)
                image.save(
                    filepath,
                    quality=50,
                    subsampling=0,
                )
            elif exif[orientation] == 8:
                image = image.rotate(90, expand=True)
                image.save(
                    filepath,
                    quality=50,
                    subsampling=0,
                )

            img_width = image.size[0]
            img_height = image.size[1]
        except Exception as e:
            import traceback
            traceback.print_exc()

        image.close()

        remotefilename = imageid + ".jpg"
        logger.info("RemoteFilename = " + remotefilename)
        logger.info("completeFilename = " + completeFilename)
        awsFilename = aws.uploadData(completeFilename, remotefilename)
        os.remove(completeFilename)
        logger.info("File saved in AWS as " + awsFilename)

        rabbitdata = {
            'id': imageid,
            'user-agent': request.headers['User-Agent'],
            'url': request.url,
            'image_width': img_width,
            "image_height": img_height,
            'cookie': ""
        }

        return awsFilename, rabbitdata
    except Exception as e:
        import traceback
        traceback.print_exc()
        return "", {}
示例#4
0
def image():
    try:

        logger.debug(utils.get_debug_all(request))

        i = request.files['fileToUpload']  # get the image
        imageid = uuid.uuid4().__str__()
        f = ('%s.jpeg' % (imageid))
        i.save('%s/%s' % (PATH_TO_TEST_IMAGES_DIR, f))
        completeFilename = '%s/%s' % (PATH_TO_TEST_IMAGES_DIR, f)

        try:
            filepath = completeFilename
            image = Image.open(filepath)

            img_width = image.size[0]
            img_height = image.size[1]

            for orientation in ExifTags.TAGS.keys():
                if ExifTags.TAGS[orientation] == 'Orientation':
                    break
            exif = dict(image._getexif().items())
            logger.debug(exif[orientation])
            if exif[orientation] == 3:
                image = image.rotate(180, expand=True)
                image.save(
                    filepath,
                    quality=50,
                    subsampling=0,
                )
            elif exif[orientation] == 6:
                image = image.rotate(270, expand=True)
                image.save(
                    filepath,
                    quality=50,
                    subsampling=0,
                )
            elif exif[orientation] == 8:
                image = image.rotate(90, expand=True)
                image.save(
                    filepath,
                    quality=50,
                    subsampling=0,
                )

            img_width = image.size[0]
            img_height = image.size[1]
            image.close()
        except Exception as e:
            import traceback
            traceback.print_exc()

        # ok the entry is correct let's add it in our db
        cookie, cookie_exists = utils.getCookie()
        if (postgres.__checkHerokuLogsTable()):
            postgres.__saveLogEntry(request, cookie)

        # now upload
        logger.debug(completeFilename)

        #prepare rabbitmq data
        rabbitdata = {
            'id': imageid,
            'user-agent': request.headers['User-Agent'],
            'url': request.url,
            'image_width': img_width,
            "image_height": img_height,
            'cookie': cookie
        }

        if (UPLOAD_IN_REDIS == True):
            # Save the image into redis
            file = open(completeFilename, "rb")
            data = file.read()
            file.close()
            rediscache.__setCache(imageid, data, 3600)
            os.remove(completeFilename)
            logger.info("File saved in Redis")
            rabbitdata['UPLOAD_IN_REDIS'] = True
        else:
            # saves into AWS
            rabbitdata['UPLOAD_IN_REDIS'] = False
            remotefilename = imageid + ".jpg"
            awsFilename = aws.uploadData(completeFilename, remotefilename)
            os.remove(completeFilename)
            logger.info("File saved in AWS")
            rabbitdata['remote_url'] = awsFilename

        # Sends data to RabbitMQ
        logger.debug(rabbitdata)
        rabbitmq.sendMessage(ujson.dumps(rabbitdata), rabbitmq.CLOUDAMQP_QUEUE)
        #awsFilename = aws.uploadData(completeFilename, f)

        key = {
            'url': request.url,
            'status_upload': 'Thanks for participating',
            'error_upload': None
        }

        tmp_dict = None
        #data_dict = None
        tmp_dict = rediscache.__getCache(key)
        if ((tmp_dict == None) or (tmp_dict == '')):
            logger.info("Data not found in cache")
            data = render_template(RENDER_ROOT_PHOTO,
                                   status_upload="Thanks for participating",
                                   error_upload=None,
                                   FA_APIKEY=utils.FOLLOWANALYTICS_API_KEY,
                                   userid=cookie,
                                   PUSHER_KEY=notification.PUSHER_KEY)
            #rediscache.__setCache(key, data, 60)
        else:
            logger.info("Data found in redis, using it directly")
            data = tmp_dict

        return utils.returnResponse(data, 200, cookie, cookie_exists)

    except Exception as e:
        import traceback
        cookie, cookie_exists = utils.getCookie()
        data = render_template(
            RENDER_ROOT_PHOTO,
            status_upload=None,
            error_upload=
            "An error occured while saving your file, please try again",
            FA_APIKEY=utils.FOLLOWANALYTICS_API_KEY,
            userid=cookie,
            PUSHER_KEY=notification.PUSHER_KEY)

        return utils.returnResponse(data, 200, cookie, cookie_exists)
示例#5
0
def FACE_API_CALLBACK(ch, method, properties, body):
    try:
        # transforms body into dict
        body_dict = ujson.loads(body)
        logger.info(body_dict)
        logger.info(" [x] Received id=%r" % (body_dict['id']))
        # gets the id of the image to retrieve in Redis
        image_id = body_dict['id']
        if (body_dict['UPLOAD_IN_REDIS'] == True):
            image_binary_data = rediscache.__getCache(image_id)
            # write binary data   into a file
            logger.debug("Writing file to disk")
            localfilename = '%s/%s' % (PATH_TO_TEST_IMAGES_DIR,
                                       "/rab_" + image_id + ".jpg")
            remotefilename = image_id + ".jpg"
            file = open(localfilename, "wb")
            file.write(image_binary_data)
            file.close()
            # sends data to AWS
            logger.debug("Starting AWS Upload")
            awsFilename = aws.uploadData(localfilename, remotefilename)
            logger.debug("uploaded file to amazon : {}".format(awsFilename))
            # deletes from redis
            rediscache.__delCache(image_id)
            # deletes local file
            os.remove(localfilename)
        else:
            awsFilename = body_dict['remote_url']
        # now detection !!
        logger.debug("Starting Face API")
        result, code = faceapi.face_http(awsFilename)
        logger.debug("Face API Result : {}".format(result))
        if (code != 200):
            logger.error("Can't  treat entry.")
            return
        else:
            img_width = body_dict['image_width']
            img_height = body_dict["image_height"]

            # now treats each result
            for entry in result:
                try:

                    personid = uuid.uuid4().__str__()
                    data = {
                        'MediaId__c':
                        image_id,
                        'image__c':
                        awsFilename,
                        'UserAgent__c':
                        body_dict['user-agent'],
                        'URL__c':
                        body_dict['url'],
                        'Name':
                        personid,
                        'PersonId__c':
                        personid,
                        'Gender__c':
                        entry['faceAttributes']['gender'],
                        'Age__c':
                        int(entry['faceAttributes']['age']),
                        'Smile_value__c':
                        entry['faceAttributes']['smile'],
                        'eyemakeup__c':
                        entry['faceAttributes']['makeup']['eyeMakeup'],
                        'lipmakeup__c':
                        entry['faceAttributes']['makeup']['lipMakeup'],
                        'Emotion_Value__c':
                        0.000
                    }
                    # now let's treat things in the right order ..
                    floatToBool(entry['faceAttributes'], 'smile', data,
                                'Smile__c', 0.5)
                    stringToBool(entry['faceAttributes'], 'glasses', data,
                                 'Glasses__c', ["ReadingGlasses"])
                    floatToBool(entry['faceAttributes']['hair'], 'bald', data,
                                'Bald__c', 0.5)

                    # face square
                    data['ImageWidth__c'] = img_width
                    data['ImageHeight__c'] = img_height
                    data['FaceTop__c'] = int(
                        (entry['faceRectangle']['top'] / img_height) * 100)
                    data['FaceLeft__c'] = int(
                        (entry['faceRectangle']['left'] / img_width) * 100)
                    data['FaceWidth__c'] = int(
                        (entry['faceRectangle']['width'] / img_width) * 100)
                    data['FaceHeight__c'] = int(
                        (entry['faceRectangle']['height'] / img_height) * 100)

                    if (entry['faceAttributes']['hair']['bald'] > 0.49):
                        data['Hair__c'] = "bald"

                    data['Haircolor__c'] = ''
                    if ('hairColor' in entry['faceAttributes']['hair']):
                        if (len(entry['faceAttributes']['hair']['hairColor'])
                                >= 1):
                            data['Haircolor__c'] = entry['faceAttributes'][
                                'hair']['hairColor'][0]['color']
                            data['Hair__c'] = entry['faceAttributes']['hair'][
                                'hairColor'][0]['color']
                        else:
                            data['Hair__c'] = "bald"
                    else:
                        data['Hair__c'] = "bald"

                    FacialHair = "None"
                    if (entry['faceAttributes']['facialHair']['moustache'] >
                            0.49):
                        FacialHair = 'moustache'
                    if (entry['faceAttributes']['facialHair']['beard'] > 0.49):
                        if (FacialHair != "None"):
                            FacialHair += ' or beard'
                        else:
                            FacialHair += 'beard'
                    data['FacialHair__c'] = FacialHair

                    data['Emotion__c'] = "neutral"
                    floatToBool(entry['faceAttributes']['facialHair'],
                                'moustache', data, 'Moustache__c', 0.5)
                    floatToBool(entry['faceAttributes']['facialHair'], 'beard',
                                data, 'Beard__c', 0.5)

                    floatToBool(entry['faceAttributes']['emotion'], 'anger',
                                data, 'Anger__c', 0.01)
                    floatToBool(entry['faceAttributes']['emotion'], 'contempt',
                                data, 'Contempt__c', 0.01)
                    floatToBool(entry['faceAttributes']['emotion'], 'disgust',
                                data, 'Disgust__c', 0.01)
                    floatToBool(entry['faceAttributes']['emotion'], 'fear',
                                data, 'Fear__c', 0.01)
                    floatToBool(entry['faceAttributes']['emotion'],
                                'happiness', data, 'Happiness__c', 0.01)
                    floatToBool(entry['faceAttributes']['emotion'], 'neutral',
                                data, 'Neutral__c', 0.01)
                    floatToBool(entry['faceAttributes']['emotion'], 'sadness',
                                data, 'Sadness__c', 0.01)
                    floatToBool(entry['faceAttributes']['emotion'], 'surprise',
                                data, 'Surprise__c', 0.01)

                    data['Emotion_Value__c'] = 0.0
                    data['Emotion__c'] = 'neutral'

                    floatToString(entry['faceAttributes']['emotion'], 'anger',
                                  data['Emotion_Value__c'], data, 'Emotion__c',
                                  'Emotion_Value__c')
                    floatToString(entry['faceAttributes']['emotion'],
                                  'contempt', data['Emotion_Value__c'], data,
                                  'Emotion__c', 'Emotion_Value__c')
                    floatToString(entry['faceAttributes']['emotion'],
                                  'disgust', data['Emotion_Value__c'], data,
                                  'Emotion__c', 'Emotion_Value__c')
                    floatToString(entry['faceAttributes']['emotion'], 'fear',
                                  data['Emotion_Value__c'], data, 'Emotion__c',
                                  'Emotion_Value__c')
                    floatToString(entry['faceAttributes']['emotion'],
                                  'happiness', data['Emotion_Value__c'], data,
                                  'Emotion__c', 'Emotion_Value__c')
                    floatToString(entry['faceAttributes']['emotion'],
                                  'neutral', data['Emotion_Value__c'], data,
                                  'Emotion__c', 'Emotion_Value__c')
                    floatToString(entry['faceAttributes']['emotion'],
                                  'sadness', data['Emotion_Value__c'], data,
                                  'Emotion__c', 'Emotion_Value__c')
                    floatToString(entry['faceAttributes']['emotion'],
                                  'surprise', data['Emotion_Value__c'], data,
                                  'Emotion__c', 'Emotion_Value__c')

                    data['Description__c'] = ujson.dumps(entry)

                    logger.debug(data)

                    postgres.__saveImageAnalysisEntry(data)
                except Exception as e:
                    import traceback
                    traceback.print_exc()
                    logger.error("Error treating entry, going to the next")
            logger.info(" [x] Finished id=%r" % (body_dict['id']))
    except Exception as e:
        import traceback
        traceback.print_exc()