# Path: ISeeNN/image_server/models.py
# class DBImage(Document):
#     server = fields.ObjectIdField(required=True)
#     path = fields.StringField(required=True)
#     mime_type = fields.StringField(required=True)
#     width = fields.IntField(required=True)
#     height = fields.IntField(required=True)
#     source = fields.StringField()
#     meta = {
#         'indexes': [
#             {
#                 'fields': ['path'],
#                 'unique': True
#             },
#             '#source'
#         ]
#     }
# 
#     def get_thumbnail(self):
#         try:
#             my_thumbnail = DBImageThumbnail.objects.get(DBImage=self.pk)
#             return my_thumbnail
#         except DBImageThumbnail.DoesNotExist:
#             im = Image.open(self.path)
#             thumbnail_height = 200
#             thumbnail_width = thumbnail_height * im.width / im.height
#             im.thumbnail((thumbnail_width, thumbnail_height), Image.ANTIALIAS)
#             io = StringIO()
#             im.save(io, im.format)
#             io.seek(0)
# 
#             my_thumbnail = DBImageThumbnail(DBImage=self.pk,
#                                             file=io,
#                                             width=thumbnail_width,
#                                             height=thumbnail_height,
#                                             mime_type=self.mime_type)
#             my_thumbnail.save()
#             return my_thumbnail
# 
# 
#     def __str__(self):
#         return "[%s](%s)" % (self.server, self.path)
# 
# class DBImageThumbnail(Document):
#     DBImage = fields.ObjectIdField(required=True)
#     file = fields.FileField(required=True)
#     width = fields.IntField(required=True)
#     height = fields.IntField(required=True)
#     mime_type = fields.StringField(required=True)
#     meta = {
#         'max_documents': 200000,
#         'indexes': [
#             {
#                 'fields': ['DBImage'],
#                 'unique': True
#             }
#         ]
#     }
# 
# class ImageServer(Document):
#     server_name = fields.StringField(required=True)
#     server_ip = fields.StringField(required=True)
# 
#     def __str__ (self):
#         return "%s(%s)" % (self.server_name, self.server_ip)


import sys
import os
import personal_settings as ps
import mongoengine
from PIL import Image
from feature_extractor import ResizeExtractor, RootNormalizer
from ISeeNN.image_server.models import DBImage, DBImageThumbnail, ImageServer
from models import Feature, AestheticInfo
    file_id = elements[1]
    scores = 0.0
    nums = 0
    for x in range(1,11):
        scores += float(elements[x+1])*x
        nums += int(elements[x+1])
    score = scores / nums
    tag = []
    for x in range(12,14):
        if elements[x] != '0':
            tag.append(tags[elements[x]])
    return (file_id, score, tag)


with open(AVA_file, 'r') as f:
    for line in f:
        print(parse_ava_line(line))
        (file_id, score, tag) = parse_ava_line(line)
        img_filename = os.path.join(dir_name, file_id+'.jpg')
        if not os.path.isfile(img_filename):
            continue
        file_name, file_extension = os.path.splitext(img_filename)
        print(img_filename)
        try:
            db_image = DBImage.objects.get(path=img_filename)
            image_id = db_image.pk
        except DBImage.DoesNotExist:
            im = Image.open(img_filename)
            # if im.format != 'JPEG':
            #     continue

# Path: ISeeNN/image_server/models.py
# class DBImage(Document):
#     server = fields.ObjectIdField(required=True)
#     path = fields.StringField(required=True)
#     mime_type = fields.StringField(required=True)
#     width = fields.IntField(required=True)
#     height = fields.IntField(required=True)
#     source = fields.StringField()
#     meta = {
#         'indexes': [
#             {
#                 'fields': ['path'],
#                 'unique': True
#             },
#             '#source'
#         ]
#     }
# 
#     def get_thumbnail(self):
#         try:
#             my_thumbnail = DBImageThumbnail.objects.get(DBImage=self.pk)
#             return my_thumbnail
#         except DBImageThumbnail.DoesNotExist:
#             im = Image.open(self.path)
#             thumbnail_height = 200
#             thumbnail_width = thumbnail_height * im.width / im.height
#             im.thumbnail((thumbnail_width, thumbnail_height), Image.ANTIALIAS)
#             io = StringIO()
#             im.save(io, im.format)
#             io.seek(0)
# 
#             my_thumbnail = DBImageThumbnail(DBImage=self.pk,
#                                             file=io,
#                                             width=thumbnail_width,
#                                             height=thumbnail_height,
#                                             mime_type=self.mime_type)
#             my_thumbnail.save()
#             return my_thumbnail
# 
# 
#     def __str__(self):
#         return "[%s](%s)" % (self.server, self.path)
# 
# class DBImageThumbnail(Document):
#     DBImage = fields.ObjectIdField(required=True)
#     file = fields.FileField(required=True)
#     width = fields.IntField(required=True)
#     height = fields.IntField(required=True)
#     mime_type = fields.StringField(required=True)
#     meta = {
#         'max_documents': 200000,
#         'indexes': [
#             {
#                 'fields': ['DBImage'],
#                 'unique': True
#             }
#         ]
#     }
# 
# class ImageServer(Document):
#     server_name = fields.StringField(required=True)
#     server_ip = fields.StringField(required=True)
# 
#     def __str__ (self):
#         return "%s(%s)" % (self.server_name, self.server_ip)


# Path: Indexer/ava_indexer.py
import sys
import os
import personal_settings as ps
import mongoengine
from PIL import Image
from feature_extractor import ResizeExtractor, RootNormalizer
from ISeeNN.image_server.models import DBImage, DBImageThumbnail, ImageServer
from models import Feature, AestheticInfo

    file_id = elements[1]
    scores = 0.0
    nums = 0
    for x in range(1,11):
        scores += float(elements[x+1])*x
        nums += int(elements[x+1])
    score = scores / nums
    tag = []
    for x in range(12,14):
        if elements[x] != '0':
            tag.append(tags[elements[x]])
    return (file_id, score, tag)


with open(AVA_file, 'r') as f:
    for line in f:
        print(parse_ava_line(line))
        (file_id, score, tag) = parse_ava_line(line)
        img_filename = os.path.join(dir_name, file_id+'.jpg')
        if not os.path.isfile(img_filename):
            continue
        file_name, file_extension = os.path.splitext(img_filename)
        print(img_filename)
        try:
            db_image = DBImage.objects.get(path=img_filename)
            image_id = db_image.pk
        except DBImage.DoesNotExist:
            im = Image.open(img_filename)
            # if im.format != 'JPEG':
            #     continue

            db_image = DBImage(