123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- {% extends "layout.html" %} {% block title %}
- <title>YOLOv5 Drag & Drop Demo</title> {% endblock %} {% block header %}
- <script
- src="https://code.jquery.com/jquery-3.6.0.min.js"
- integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
- crossorigin="anonymous"
- ></script>
- {% endblock %} {% block content %}
- <div style="overflow-x: hidden">
- <div class="row">
- <div class="col-auto m-2">
- <label for="model_name" class="form-label"
- ><b>Select YOLOv5 Model</b></label
- >
- <select class="form-select" id="model_name" name="model_name">
- {% for selection in model_selection_options %}
- <option value="{{ selection }}">{{ selection }}</option>
- {% endfor %}
- </select>
- <label for="img_size" class="form-label"
- ><b>Model Inference Size</b></label
- >
- <input
- type="text"
- class="form-control"
- id="img_size"
- name="img_size"
- value="1824"
- />
- <input
- type="checkbox"
- id="multi_scale"
- name="multi_scale"
- />
- <label for="multi_scale" class="form-label">
- <b>Multi-scale Inference</b>
- </label>
- </div>
- <div class="col">
-
- <div
- class="m-2"
- id="drop-region"
- style="border: 3px dashed limegreen; height: 150px"
- >
- <div class="container-fluid">
- <div class="d-flex justify-content-center align-items-center h-100">
- <b>Drag & Drop Images Or Click To Upload</b>
- </div>
- </div>
- </div>
- </div>
- </div>
-
- <canvas id="canvas" style="max-width: 99%; height: auto"></canvas>
- </div>
- <script type="text/javascript">
-
- const canvas = document.getElementById('canvas')
- const ctx = canvas.getContext('2d')
- var image = new Image(60, 45)
- image.onload = drawImageWithBBoxes
- var colormap = {
- car: [255, 216, 0],
- person: [255, 0, 0],
- truck: [255, 0, 255],
- }
- var data
- function drawImageWithBBoxes() {
-
- canvas.width = this.naturalWidth
- canvas.height = this.naturalHeight
-
- ctx.drawImage(this, 0, 0)
-
- ctx.lineWidth = 2
- ctx.font = '24px arial'
- ctx.strokeStyle = 'yellow'
- textboxlocations = []
- FILL_ALPHA = 1
- const randomBetween = (min, max) =>
- min + Math.floor(Math.random() * (max - min + 1))
- for (const item of data) {
- if (item['class_name'] in colormap) {
- rgb = colormap[item['class_name']]
- } else {
-
- rgb = [
- randomBetween(0, 255),
- randomBetween(0, 255),
- randomBetween(0, 255),
- ]
- colormap[item['class_name']] = rgb
- }
- ctx.fillStyle = `rgba(${rgb[0]},${rgb[1]},${rgb[2]},${FILL_ALPHA})`
- ctx.strokeStyle = `rgba(${rgb[0]},${rgb[1]},${rgb[2]},${FILL_ALPHA})`
-
- ctx.strokeRect(
- item['bbox'][0],
- item['bbox'][1],
- item['bbox'][2] - item['bbox'][0],
- item['bbox'][3] - item['bbox'][1]
- )
- let label = `${item['class_name']} ${do_rounding(item['confidence'])}`
- let textMeasures = ctx.measureText(label)
- let textHeight =
- textMeasures.actualBoundingBoxAscent +
- textMeasures.actualBoundingBoxDescent
- let padding = 2
- let x = item['bbox'][0]
- let y = item['bbox'][1] - textHeight - 2 * padding
- let w = textMeasures.width + 2 * padding
- let h = textHeight + 2 * padding
-
- while (
- textboxlocations.some(box => IOU(box, [x, y, x + w, y + h]) > 0.01)
- ) {
-
- y -= h + 3
- }
-
- if (y <= 0) {
- y = 0
- }
- textboxlocations.push([x - 1, y - 1, x + w + 1, y + h + 1])
-
- ctx.fillRect(x, y, w, h)
-
- if (rgb[0] + rgb[1] + rgb[2] > (255 * 3) / 2) {
- ctx.fillStyle = 'black'
- } else {
- ctx.fillStyle = 'white'
- }
- ctx.fillText(label, x + padding, y + h - padding)
-
- ctx.beginPath()
- ctx.moveTo(x, y)
- ctx.lineTo(x, item['bbox'][1])
- ctx.stroke()
- }
- }
- function IOU(boxA, boxB, isPixel = 0) {
-
-
- xA = Math.max(boxA[0], boxB[0])
- yA = Math.max(boxA[1], boxB[1])
- xB = Math.min(boxA[2], boxB[2])
- yB = Math.min(boxA[3], boxB[3])
- if (xA >= xB || yA >= yB) {
- return 0
- }
-
- interArea = (xB - xA + isPixel) * (yB - yA + isPixel)
-
- boxAArea = (boxA[2] - boxA[0] + isPixel) * (boxA[3] - boxA[1] + isPixel)
- boxBArea = (boxB[2] - boxB[0] + isPixel) * (boxB[3] - boxB[1] + isPixel)
-
-
- iou = interArea / (boxAArea + boxBArea - interArea)
- return iou
- }
- function do_rounding(num, places = 2) {
- return Math.round((num + Number.EPSILON) * 10 ** places) / 10 ** places
- }
- </script>
- <script type="text/javascript">
-
-
- var dropRegion = document.getElementById('drop-region')
-
- var fakeInput = document.createElement('input')
- fakeInput.type = 'file'
- fakeInput.accept = 'image/*'
- fakeInput.multiple = false
- dropRegion.addEventListener('click', function () {
- fakeInput.click()
- })
- function validateImage(image) {
-
- var validTypes = ['image/jpeg', 'image/png', 'image/gif']
- if (validTypes.indexOf(image.type) === -1) {
- alert('Invalid File Type')
- return false
- }
-
- var maxSizeInBytes = 10e6
- if (image.size > maxSizeInBytes) {
- alert('File too large')
- return false
- }
- return true
- }
- function handleFiles(files) {
- for (var i = 0, len = files.length; i < len; i++) {
- if (validateImage(files[i])) previewAnduploadImage(files[i])
- }
- }
- fakeInput.addEventListener('change', function () {
- var files = fakeInput.files
- handleFiles(files)
- })
- function preventDefault(e) {
- e.preventDefault()
- e.stopPropagation()
- }
- dropRegion.addEventListener('dragenter', preventDefault, false)
- dropRegion.addEventListener('dragleave', preventDefault, false)
- dropRegion.addEventListener('dragover', preventDefault, false)
- dropRegion.addEventListener('drop', preventDefault, false)
- function handleDrop(e) {
- var data = e.dataTransfer,
- files = data.files
- handleFiles(files)
- }
- dropRegion.addEventListener('drop', handleDrop, false)
- function handleDrop(e) {
- var data = e.dataTransfer,
- files = data.files
- handleFiles(files)
- }
- dropRegion.addEventListener('drop', handleDrop, false)
- function handleDrop(e) {
- var dt = e.dataTransfer,
- files = dt.files
- if (files.length) {
- handleFiles(files)
- } else {
-
- var html = dt.getData('text/html'),
- match = html && /\bsrc="?([^"\s]+)"?\s*/.exec(html),
- url = match && match[1]
- if (url) {
- uploadImageFromURL(url)
- return
- }
- }
- function uploadImageFromURL(url) {
- var img = new Image()
- var c = document.createElement('canvas')
- var ctx = c.getContext('2d')
- img.onload = function () {
- c.width = this.naturalWidth
- c.height = this.naturalHeight
- ctx.drawImage(this, 0, 0)
- c.toBlob(function (blob) {
-
-
- handleFiles([blob])
- }, 'image/png')
- }
- img.onerror = function () {
- alert('Error in uploading')
- }
- img.crossOrigin = ''
- img.src = url
- }
- }
- function previewAnduploadImage(img) {
-
-
- var reader = new FileReader()
- reader.onload = function (e) {
- image.src = e.target.result
- }
-
- var formData = new FormData()
- formData.append('file_list', img)
- formData.append('model_name', $('#model_name').val())
- formData.append('img_size', $('#img_size').val())
- formData.append('multi_scale', $('#multi_scale').val())
- $.ajax({
- url: '/detect',
- data: formData,
- processData: false,
- contentType: false,
- type: 'POST',
- success: function (json_result_data) {
- json_result_data = json_result_data.replaceAll("'", '"')
- data = JSON.parse(json_result_data)[0]
-
- reader.readAsDataURL(img)
- },
- error: function (xhr, ajaxOptions, thrownError) {
- alert(
- 'Error code ' +
- xhr.status +
- ': ' +
- thrownError +
- '\nMessage: ' +
- JSON.parse(xhr.responseText)['message']
- )
- },
- })
- }
- </script>
- {% endblock %}
|