Ejemplo n.º 1
0
def import_from_directory(path_to_images):

    connect('eventum')
    creator = User.objects().get(gplus_id='super')

    filenames = os.listdir(path_to_images)
    filenames = [fn for fn in filenames if not fn.startswith('.')]
    failures = []

    for filename in filenames:

        if Image.objects(filename=filename).count() > 0:
            img = Image.objects().get(filename=filename)
            img.delete()

        old_path = os.path.join(path_to_images, filename)
        shutil.copy(old_path, config['UPLOAD_FOLDER'])

        default_path = config['RELATIVE_UPLOAD_FOLDER'] + filename
        image = Image(filename=filename,
                      default_path=default_path,
                      creator=creator)
        try:
            image.save()
        except ValidationError as e:
            failures.append(filename)
            print "FAIL: %s" % filename
            print e

    print "Processed %s images." % len(filenames)
    print "%s success." % (len(filenames) - len(failures))
    print "%s failures." % len(failures)
Ejemplo n.º 2
0
def upload():
    """Upload an image to Eventum

    **Route:** ``/admin/media/upload``

    **Methods:** ``POST``
    """
    form = UploadImageForm(request.form)
    uploaded_from = form.uploaded_from.data
    if form.validate_on_submit():
        f = request.files['image']
        if f and allowed_file(f.filename.lower()):
            filename = create_filename(f, request.form['filename'])
            f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            image = Image(filename=filename,
                          default_path=app.config['RELATIVE_UPLOAD_FOLDER']+filename,
                          creator=g.user)
            image.save()
            return redirect(url_for('.index'))
        flash("Filename {} is invalid".format(f.filename))
    if form.errors:
        flash(form.errors)
    if uploaded_from:
        return redirect(uploaded_from)
    return render_template('admin/media/upload.html', form=form)
Ejemplo n.º 3
0
def import_from_directory(path_to_images):

    connect("eventum")
    creator = User.objects().get(gplus_id="super")

    filenames = os.listdir(path_to_images)
    filenames = [fn for fn in filenames if not fn.startswith(".")]
    failures = []

    for filename in filenames:

        if Image.objects(filename=filename).count() > 0:
            img = Image.objects().get(filename=filename)
            img.delete()

        old_path = os.path.join(path_to_images, filename)
        shutil.copy(old_path, config["UPLOAD_FOLDER"])

        default_path = config["RELATIVE_UPLOAD_FOLDER"] + filename
        image = Image(filename=filename, default_path=default_path, creator=creator)
        try:
            image.save()
        except ValidationError as e:
            failures.append(filename)
            print "FAIL: %s" % filename
            print e

    print "Processed %s images." % len(filenames)
    print "%s success." % (len(filenames) - len(failures))
    print "%s failures." % len(failures)
Ejemplo n.º 4
0
    def post(self, request, *args, **kwargs):
        if len(Item.objects.filter(user=request.user)) > 500:
            return redirect('home')
        form = self.form_class(request.POST)

        if form.is_valid():
            item_data = {
                k: v
                for k, v in form.cleaned_data.items() if 'image' not in k
            }
            item_data['user'] = request.user
            categories = item_data.pop('categories')
            item = Item(**item_data)
            item.save()

            item.categories = categories
            item.save()

            images_data = {
                k: v
                for k, v in form.cleaned_data.items() if 'image' in k
            }
            for key in sorted(k for k, v in images_data.items() if v):
                url = images_data[key]
                image = Image(item=item, url=url)
                image.save()

            send_emails(request, item)
            response = redirect('view_item', item.id)
            response['Location'] += '?new=true'
            return response
        else:
            return render(request, 'app/add_item.html', {'form': form})
Ejemplo n.º 5
0
def get(request):
    likeImgPath = []
    path=''
    if request.method=="POST":
        form = ImageForm(request.POST, request.FILES)
        if form.is_valid():
             img = form.cleaned_data['Img']
             uid=uuid.uuid1()
             imgObj =Image()
             imgObj.uid=uid
             imgObj.path=img
             imgObj.save()
             obj=Image.objects.get(uid=uid)
             path= obj.path.url.encode('utf-8')

             sys.argv = ['compare.py', '/home/ubuntu/face_search_1/face_search_1/app/src/20170512-110547.pb',
                         '/home/ubuntu/face_search_1/face_search_1/'+path]

             args = search.parse_arguments(sys.argv[1:])
             likesImg = search.main(args)

             for i in range(10):
                  filepath = likesImg[i, 0]
                  likeImgPath.append(filepath)
    else:
         form = ImageForm()
    return render_to_response('get.html', {'form': form,'likesImg':likeImgPath,'path':path})
Ejemplo n.º 6
0
    def save(self):
        super(ImageFile, self).save()
        img = Image()
        img.file_id = self.id
        img.image   = self.base + '.' + self.ext

        img.save()
        return self.base
Ejemplo n.º 7
0
    def save(self):
        super(ImageFile, self).save()
        img = Image()
        img.file_id = self.id
        img.image = self.base + '.' + self.ext

        img.save()
        return self.base
Ejemplo n.º 8
0
	def take_frame(self):
		now = datetime.now()
		fileName = filePath + now.strftime('%y%m%d_%H%M%S') + '.png'
		print (fileName)
		cv2.imwrite(fileName, self.frame)

		db = Image(image_name=now.strftime('%y%m%d_%H%M%S'), pub_date=timezone.now())
		db.save()
Ejemplo n.º 9
0
def create_images(num_images, superuser, printer):
    """Creates ``num_images`` image objects in the database.  It will download
    sample images from http://lorempixel.com, and add database entries.

    :param int num_images: The number of images to create
    :param superuser: The superuser object to associate with the images.
    :type superuser: :class:`~app.models.User`
    :param printer: The object to manage progress printing.
    :type printer: :class:`~script.cli.ProgressPrinter`

    :returns: A list of images that now exist.
    :rtype: list(:class:`~app.models.Image`)
    """
    print "Generating images..."
    printer.line()

    successes = []
    failures = []
    skips = []
    for width in range(400, 1600, (1600 - 400) / num_images):
        height = width / 2
        filename = BASE_FILENAME.format(width, height)
        path = config['UPLOAD_FOLDER'] + filename
        url = BASE_URL.format(width, height)

        printer.begin_status_line(filename)

        # Download image if it doesn't exist already
        if not exists(path):
            try:
                urllib.urlretrieve(url, path)
            except IOError:
                failures.append((filename, ''))
                printer.status_fail()
                continue  # Failed to download, move on to the next image.

        # Insert or fetch image from database
        if Image.objects(filename=filename).count() == 0:
            image = Image(filename=filename,
                          default_path=path,
                          creator=superuser)
            image.save()
            successes.append((filename, path))
            printer.status_success()
        else:
            skips.append((filename, path))
            printer.status_skip()

    printer.line()
    printer.results(len(successes), len(skips), len(failures))
    return successes + skips
Ejemplo n.º 10
0
def register(request):
    if request.method =="POST":
        form=ImageForm(request.POST,request.FILES)
        if form.is_valid():
             img = form.cleaned_data['Img']
             imgObj =Image()
             imgObj.uid=uuid.uuid1()
             imgObj.path=img

             imgObj.save()
             return  HttpResponse('ok')
    else:
        form=ImageForm()
    return render_to_response('index.html',{'form':form})
Ejemplo n.º 11
0
def update_image_storage():
    """This operation should clear DB if run multiple times on the same DB

    8 Points: 3 for xpath (1 each), 1 for correct resource GET, 2 for correct DB entry handling,
              2 for correct regex, 2 for Transaction explanation
    -0.5 for small mistakes (return value...)

    :return: json if successful or not
    """

    # TODO get BASE_URL_DATASET, we would suggest requests for it (already installed) Think about error handling.
    answer = requests.get(BASE_URL_DATASET)
    # Error handling

    # TODO Please explain what this line is doing. Why is it needed? In which case? (directly here as comment)
    with database_holder.database.transaction():
        # Empty databases
        Image.delete().execute()  # pylint: disable=no-value-for-parameter
        Caption.delete().execute()  # pylint: disable=no-value-for-parameter

        # TODO We encourage you to use the html.fromstring method provided by the lxml package (already installed).
        tree = None

        # TODO After parsing the XML tree, please use the xpath method to iterate over all elements
        for pictureTree in tree.xpath(''):

            # TODO get image src by xpath method, you can check lxml documentation or use a debugger to find attributes
            src = None

            # TODO parse category by appling a regex to src, probably check out regex101.com
            # check out re docs of Python3
            category = None

            # save Image in DB, nothing magical here
            imageDb = Image(src=src, category=category)
            imageDb.save()

            # TODO iterate over all captions by using xpath method. Try to make the xpath expression as short as
            # possible
            for captionTree in []:
                caption_text = ''
                Caption(text=caption_text, image=imageDb).save()

    return json.dumps({'status': 'finished'}), 200
Ejemplo n.º 12
0
def update_image_storage():
    """This operation should clear DB if run multiple times on the same DB

    8 Points: 3 for xpath (1 each), 1 for correct resource GET, 2 for correct DB handling,
              1 for correct regex, 1 for Transaction explanation
    -0.5 for small mistakes (return value...)

    :return: json if successful or not
    """

    answer = requests.get(BASE_URL_DATASET)
    # Error handling
    answer.raise_for_status()

    # This line starts a new transaction and automatically commits it at the end of the with-clause
    # It is needed because database operations can fail. Then, the transaction would have to be aborted.
    # The with-clause also takes care of this and issues a rollback.
    with database_holder.database.transaction():
        # Empty databases
        Image.delete().execute()  # pylint: disable=no-value-for-parameter
        Caption.delete().execute()  # pylint: disable=no-value-for-parameter

        tree = html.fromstring(answer.text)

        # for every picture (corresponds to tr)
        for pictureTree in tree.xpath('/html/body/table/tr'):
            # get source and category
            src = pictureTree.xpath('td/img/@src')[0]
            category = re.match(r'(\w+)\/', src).group(1)

            # save Image in DB, nothing magical here
            imageDb = Image(src=src, category=category)
            imageDb.save()

            #  get all captions and save them
            for captionTree in pictureTree.xpath('td//td'):
                caption_text = captionTree.text[1:]
                Caption(text=caption_text, image=imageDb).save()

    return json.dumps({'status': 'finished'}), 200
Ejemplo n.º 13
0
def upload():
    """Upload an image to Eventum

    :returns: A JSON containing the status of the file upload, or error
              messages, if any.
    :rtype: json
    """
    form = UploadImageForm(request.form)
    if form.validate_on_submit():
        f = request.files['image']
        if f:
            filename = create_filename(f, request.form['filename'])
            f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            default_path = app.config['RELATIVE_UPLOAD_FOLDER'] + filename
            image = Image(filename=filename,
                          default_path=default_path,
                          creator=g.user)
            image.save()
            return jsonify({"status": "true"})
    if form.errors:
        return jsonify(form.errors)
    return jsonify({"status": "error"})
def update_image_storage():
    """This operation should clear DB if run multiple times on the same DB

    8 Points: 3 for xpath (1 each), 1 for correct resource GET, 1 for correct DB cleaning, 1 for correct DB saving,
              2 for correct regex, 2 for Transaction explanation
    -0.5 for small mistakes (return value...)

    :return: json if successful or not
    """

    # TODO get BASE_URL_DATASET, we would suggest requests for it (already installed) Think about error handling.
    try:
        page = requests.get(BASE_URL_DATASET)
    except requests.exceptions.Timeout:
        print('A timeout occured.')
        # Maybe set up for a retry, or continue in a retry loop
    except requests.exceptions.TooManyRedirects:
        print('Too many redirects were made.')
        # Tell the user their URL was bad and try a different one
    except requests.exceptions.RequestException as e:
        print('An error occured', e)
        # catastrophic error. bail.
        sys.exit(1)


    # TODO Please explain what this line is doing. Why is it needed? In which case? (directly here as comment)
    # The `with` keyword guarantees that some cleanup routine for the to-be-executed routine is implicitly run
    # after the scope exits. In this particular case, the clean-up-routine is the return statement, such that
    # the 'status': 'finished' value is guaranteed to be sent together with the 200 status code.
    with database_holder.database.transaction():
        # Empty databases
        Image.delete().execute()  # pylint: disable=no-value-for-parameter
        Caption.delete().execute()  # pylint: disable=no-value-for-parameter

        # TODO We encourage you to use the html.fromstring method provided by the lxml package (already installed).
        tree = html.fromstring(page.text)
        
        # "status": "/html/body/table/tr[1000]/td[2]/table/tr[5]/td"
        
        pictureTrees = tree.xpath('/html/body/table/tr');

        # TODO After parsing the XML tree, please use the xpath method to iterate over all elements
        for index, pictureTree in enumerate(pictureTrees, start=1):
            
            # print('processing pictureTree #', index);
                   
            # Extract the source attribute
            src = next(iter(pictureTree.xpath('td[1]/img/@src')), None)
            if src == None:
                continue  # skip entry if no image is in row

            # print('src is ', src);

            # Take only substring with category descriptor
            category = re.match('^(\w.*)\/', src).group(1)
            if category == None:
                continue  # skip entry if category could can't be extracted
            
            # print('category is ', category);

            # Save Image in DB, nothing magical here
            imageDb = Image(src=src, category=category)
            imageDb.save()

            # print('saved image entry!');

            # Save the captions additionally
            for captionTree in pictureTree.xpath('td[2]/table/*/td/text()'):
                # Remove whitespaces on edges
                caption_text = captionTree.strip()
                Caption(text=caption_text, image=imageDb).save()
                
                # print('Added caption', caption_text);

    return json.dumps({'status': 'finished'}), 200
Ejemplo n.º 15
0
def upload_image():
    if request.method == 'POST':
        file = request.files.getlist('file')
        image = Image()
        image.save(file[0])
    return render_template('upload.html')
Ejemplo n.º 16
0
def create_image():
    image = Image(name=get_word())
    image.save()
    return image
Ejemplo n.º 17
0
 def images_handler():
     if request.method == "POST":
         url = str(request.data.get('url', ''))
         diff = int(request.data.get('diff', ''))
         if url and diff:
             image = Image(url=url)
             image.save()
             pieces = cropper(diff, url)
             if diff == 4:
                 for item in pieces:
                     piece = PieceBeginner(img_id=image.id,
                                           value=item['value'],
                                           url=item['url'])
                     piece.save()
             if diff == 9:
                 for item in pieces:
                     piece = PieceIntermediate(img_id=image.id,
                                               value=item['value'],
                                               url=item['url'])
                     piece.save()
             if diff == 16:
                 for item in pieces:
                     piece = PieceHard(img_id=image.id,
                                       value=item['value'],
                                       url=item['url'])
                     piece.save()
             beginner_pieces = PieceBeginner.query.filter_by(
                 img_id=image.id)
             bpieces = []
             for piece in beginner_pieces:
                 obj = {'value': piece.value, 'url': piece.url}
                 bpieces.append(obj)
             intermediate_pieces = PieceIntermediate.query.filter_by(
                 img_id=image.id)
             ipieces = []
             for piece in intermediate_pieces:
                 obj = {'value': piece.value, 'url': piece.url}
                 ipieces.append(obj)
             hard_pieces = PieceHard.query.filter_by(img_id=image.id)
             hpieces = []
             for piece in hard_pieces:
                 obj = {'value': piece.value, 'url': piece.url}
                 hpieces.append(obj)
             response = jsonify({
                 'id': image.id,
                 'url': image.url,
                 'date_created': image.date_created,
                 'date_modified': image.date_modified,
                 'beginner_pieces': bpieces,
                 'intermediate_pieces': ipieces,
                 'hard_pieces': hpieces
             })
             response.status_code = 201
             return response
     else:
         # GET
         images = Image.get_all()
         results = []
         for image in images:
             obj = {
                 'id': image.id,
                 'url': image.url,
                 'date_created': image.date_created,
                 'date_modified': image.date_modified
             }
             results.append(obj)
         response = jsonify(results)
         response.status_code = 200
         return response
Ejemplo n.º 18
0
 def post(self, request):
     new_img = Image(image=request.FILES.get('editormd-image-file'), )
     new_img.save()
     self.data['success'] = 1
     self.data['url'] = new_img.image.url
     return JsonResponse(self.data)