[PYTHON] Einfache Verwaltungs-App für heruntergeladene Bilder und Gesichtsbilder

Einführung

Überblick

oberste Seite

image.png

Bild und Gesichtsbild herunterladen

image.png

Bibliothek

oberste Seite

image_viewer.py


@app.route('/')
def index():
    """Top Page."""
    return render_template('index.html', items=CLASSES)

templates/index.html


    <div class="container-fluid">
      {% for item in items %}
      <div class="row">
        <div class="col">
          {{ loop.index }} {{ item }}
        </div>
        <div class="col-11">
          <a class="btn btn-primary" href="/download_and_face/{{ item }}" role="button">Bild herunterladen, Gesichtsbild</a>
          <a class="btn btn-secondary" href="/predict/train/{{ item }}" role="button">Vorhersageergebnis des Trainingsbildes</a>
          <a class="btn btn-success" href="/predict/test/{{ item }}" role="button">Vorhersageergebnis des Testbildes</a>
        </div>
      </div>
      <br />
      {% endfor %}
    </div>

Bild und Gesichtsbild herunterladen

GET-Verarbeitung

image_viewer.py


@app.route('/download_and_face/<item>', methods=['GET', 'POST'])
def download_and_face(item):
    """Bild herunterladen, Gesichtsbild."""

    download_list = glob.glob(os.path.join(DOWNLOAD_PATH, item, '*.jpeg'))
    download_list = sorted([os.path.basename(filename) for filename in download_list])
    face_list = glob.glob(os.path.join(FACE_PATH, item, '*.jpeg'))
    face_list = sorted([os.path.basename(filename) for filename in face_list])

image_viewer.py


    rows = []
    for download in download_list:
        row = [download]
        key = download.split('.')[0] + '-'
        for face in face_list:
            if face.startswith(key):
                row.append(face)
        rows.append(row)

    return render_template('download_and_face.html', item=item, rows=rows)

templates/download_and_face.html


          <tbody>
            {% for row in rows %}
            <tr>
              <td>
                {{ loop.index }}
              </td>
              <td>
                <figure class="figure">
                  <img src="/data/download/{{ item }}/{{ row[0] }}?size=200" />
                  <figcaption class="figure-caption">{{ row[0] }}</figcaption>
                </figure>
              </td>
              <td>
                {% for filename in row[1:] %}
                <figure class="figure">
                  <label class="image-checkbox">
                  <img src="/data/face/{{ item }}/{{ filename }}?size=200" />
                  <input type="checkbox" name="filename" value="{{ filename }}" />
                  <i class="fa fa-trash-o d-none"></i>
                  </label>
                  <figcaption class="figure-caption">{{ filename }}</figcaption>
                </figure>
                {% endfor %}
              </td>
            </tr>
            {% endfor %}
          </tbody>

static/download_and_face.css


.image-checkbox {
  cursor: pointer;
  border: 2px solid transparent;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  position: relative;
}

.image-checkbox input[type="checkbox"] {
  display: none;
}

.image-checkbox-checked {
  border-color: #d9534f;
}

.image-checkbox .fa {
  color: #ffffff;
  background-color: #d9534f;
  font-size: 20px;
  padding: 4px;
  position: absolute;
  right: 0;
  top: 0;
}

.image-checkbox-checked .fa {
  display: block !important;
}

static/download_and_face.js


// image gallery
// init the state from the input
$(".image-checkbox").each(function () {
  if ($(this).find('input[type="checkbox"]').first().attr("checked")) {
    $(this).addClass('image-checkbox-checked');
  }
  else {
    $(this).removeClass('image-checkbox-checked');
  }
});

// sync the state to the input
$(".image-checkbox").on("click", function (e) {
  $(this).toggleClass('image-checkbox-checked');
  var $checkbox = $(this).find('input[type="checkbox"]');
  $checkbox.prop("checked",!$checkbox.prop("checked"))

  e.preventDefault();
});

Bildgröße anpassen

image_viewer.py


@app.route('/data/<folder>/<item>/<filename>')
def get_image(folder, item, filename):
    """Skalieren Sie mit der Bildantwortgröße."""

    if folder not in ['download', 'face']:
        abort(404)

    filename = os.path.join(DATA_PATH, folder, item, filename)

image_viewer.py


    try:
        image = Image.open(filename)
    except Exception as err:
        pprint.pprint(err)
        abort(404)

image_viewer.py


    if 'size' in request.args:
        height = int(request.args.get('size'))
        width = int(image.size[0] * height / image.size[1])
        image = image.resize((width, height), Image.LANCZOS)

image_viewer.py


    data = io.BytesIO()
    image.save(data, 'jpeg', optimize=True, quality=95)
    response = make_response()
    response.data = data.getvalue()
    response.mimetype = 'image/jpeg'

    return response

Nachbearbeitung

image_viewer.py


@app.route('/download_and_face/<item>', methods=['GET', 'POST'])
def download_and_face(item):
    """Bild herunterladen, Gesichtsbild."""

    if request.method == 'POST' and request.form.get('action') == 'delete':
        for filename in request.form.getlist('filename'):
            filename = os.path.join(FACE_PATH, item, filename)
            if os.path.isfile(filename):
                os.remove(filename)
                print('delete face image: {}'.format(filename))

abschließend

Recommended Posts

Einfache Verwaltungs-App für heruntergeladene Bilder und Gesichtsbilder
Laden Sie Bilder mit Falcon hoch und laden Sie sie herunter
Vorsichtsmaßnahmen für den Umgang mit PNG- und JPG-Bildern
Laden Sie Profilbilder (Symbole) für alle von Slack herunter