show image in position

This commit is contained in:
Ruben van de Ven 2020-12-17 18:27:44 +01:00
parent b6b773b4c1
commit 739908c09d
4 changed files with 114 additions and 50 deletions

View file

@ -5,6 +5,7 @@ import pycocotools.coco
import ast import ast
import copy import copy
import svgwrite import svgwrite
import uuid
logger = logging.getLogger('coco.storage') logger = logging.getLogger('coco.storage')
@ -81,6 +82,7 @@ class Annotation:
data = self.__dict__.copy() data = self.__dict__.copy()
del data['storage'] del data['storage']
data['image'] = self.storage.getImage(data['image_id']) data['image'] = self.storage.getImage(data['image_id'])
data['category'] = self.storage.getCategory(self.category_id)
return data return data
def writeToDrawing(self, dwg, **pathSpecs): def writeToDrawing(self, dwg, **pathSpecs):
@ -121,12 +123,19 @@ class Annotation:
(viewbox[2], viewbox[3]), (viewbox[2], viewbox[3]),
fill=bg)) fill=bg))
if clip_image is not False: if clip_image is not False:
clip_id = uuid.uuid4().hex
img = self.storage.getImage(self.image_id) img = self.storage.getImage(self.image_id)
rmpart = len("http://images.cocodataset.org/") rmpart = len("http://images.cocodataset.org/")
href = img['coco_url'][rmpart:] href = img['coco_url'][rmpart:]
if image_dir: if image_dir:
href = os.path.join(image_dir, href) href = os.path.join(image_dir, href)
dwg.add(dwg.image(href, insert=(0,0))) dwg.add(dwg.image(href, insert=(0,0), clip_path=f"url(#{clip_id})"))
clip_path = dwg.defs.add(dwg.clipPath(id=clip_id))
self.writeToDrawing(clip_path)
else:
self.writeToDrawing(dwg) self.writeToDrawing(dwg)
return dwg return dwg

View file

@ -31,6 +31,14 @@
opacity:0; opacity:0;
} }
svg .img{
transition: stroke .2s;
}
svg .img.hide{
opacity:0;
}
#crosshair line{ #crosshair line{
stroke: gray; stroke: gray;
stroke-dasharray: 5px 5px; stroke-dasharray: 5px 5px;

View file

@ -1,49 +1,86 @@
import { Flywheel } from "./effects.js"; import { Flywheel } from "./effects.js";
import { getPathShapeBetweenPoints } from "./svg.js"; import { getPathShapeBetweenPoints } from "./svg.js";
export function init(svgEl, crosshairXEl, crosshairYEl) { export class Game {
let flywheel = new Flywheel();
constructor(svgEl, crosshairXEl, crosshairYEl) {
this.flywheel = new Flywheel();
this.svgEl = svgEl;
// canvas position relative to mouse/screen // canvas position relative to mouse/screen
// TODO: update on resize: // TODO: update on resize:
svgEl.setAttribute('viewBox', '0 0 ' + window.innerWidth + ' ' + window.innerHeight); svgEl.setAttribute('viewBox', '0 0 ' + window.innerWidth + ' ' + window.innerHeight);
let rect = svgEl.getBoundingClientRect(); this.rect = svgEl.getBoundingClientRect();
// move crosshairs // move crosshairs
window.addEventListener('mousemove', function (mousemoveEv) { window.addEventListener('mousemove', function (mousemoveEv) {
let x = mousemoveEv.clientX - rect.left; let x = mousemoveEv.clientX - this.rect.left;
let y = mousemoveEv.clientY - rect.top; let y = mousemoveEv.clientY - this.rect.top;
crosshairXEl.setAttribute('x1', x) crosshairXEl.setAttribute('x1', x)
crosshairXEl.setAttribute('x2', x) crosshairXEl.setAttribute('x2', x)
crosshairYEl.setAttribute('y1', y) crosshairYEl.setAttribute('y1', y)
crosshairYEl.setAttribute('y2', y) crosshairYEl.setAttribute('y2', y)
}); }.bind(this));
this.loadAnnotation();
}
svgEl.addEventListener('mousedown', function (mousedownEv) { loadAnnotation() {
let json_request = new Request('/annotation.json');
fetch(json_request).then(function (response) {
return response.json();
}).then((annotation) => {
let svg_request = new Request('/annotation.svg?id='+annotation.id);
fetch(svg_request).then(function (response) {
return response.text()
}).then((text) => {
this.addAnnotation(annotation, text);
})
}).catch((e) => console.error(e));
}
addAnnotation(annotation, svgText) {
//determine position
const maxX = this.rect.width - annotation.bbox[2];
const maxY = this.rect.height - annotation.bbox[3];
const posX = Math.random() * maxX;
const posY = Math.random() * maxY;
let imgEl = document.createElementNS('http://www.w3.org/2000/svg', 'g');
imgEl.innerHTML = svgText;
imgEl.classList.add('img');
imgEl.setAttribute('transform', `translate(${posX} ${posY})`);
this.svgEl.appendChild(imgEl);
let startTrace = function (mousedownEv) {
// create shape to draw // create shape to draw
let shapeEl = document.createElementNS('http://www.w3.org/2000/svg', 'path'); let shapeEl = document.createElementNS('http://www.w3.org/2000/svg', 'path');
shapeEl.classList.add('rect'); shapeEl.classList.add('rect');
let p1 = { 'x': mousedownEv.clientX - rect.left, 'y': mousedownEv.clientY - rect.top } let p1 = { 'x': mousedownEv.clientX - this.rect.left, 'y': mousedownEv.clientY - this.rect.top }
let corners = flywheel.effects.shape.getAngles(); let corners = this.flywheel.effects.shape.getAngles();
let d = getPathShapeBetweenPoints(p1, p1, corners); let d = getPathShapeBetweenPoints(p1, p1, corners);
svgEl.appendChild(shapeEl); this.svgEl.appendChild(shapeEl);
shapeEl.setAttribute('d', d); shapeEl.setAttribute('d', d);
let mouseMoveEv = function (mousemoveEv) { let mouseMoveEv = function (mousemoveEv) {
let p2 = { 'x': mousemoveEv.clientX - rect.left, 'y': mousemoveEv.clientY - rect.top } let p2 = { 'x': mousemoveEv.clientX - this.rect.left, 'y': mousemoveEv.clientY - this.rect.top }
let d = getPathShapeBetweenPoints(p1, p2, corners); let d = getPathShapeBetweenPoints(p1, p2, corners);
shapeEl.setAttribute('d', d); shapeEl.setAttribute('d', d);
}; }.bind(this);
let mouseUpEv = function () { let mouseUpEv = function () {
console.log('up'); console.log('up');
window.removeEventListener('mousemove', mouseMoveEv); // remove itself. window.removeEventListener('mousemove', mouseMoveEv); // remove itself.
window.removeEventListener('mouseup', mouseUpEv); // remove itself. window.removeEventListener('mouseup', mouseUpEv); // remove itself.
this.svgEl.removeEventListener('mousedown', startTrace); // remove itself.
shapeEl.classList.add('locked'); shapeEl.classList.add('locked');
//TODO: calculate points //TODO: calculate points
flywheel.add(); this.flywheel.add();
this.loadAnnotation();
// fade out // fade out box
setTimeout(function () { setTimeout(function () {
shapeEl.classList.add('hide'); shapeEl.classList.add('hide');
// remove shape // remove shape
@ -51,10 +88,20 @@ export function init(svgEl, crosshairXEl, crosshairYEl) {
shapeEl.parentNode.removeChild(shapeEl); shapeEl.parentNode.removeChild(shapeEl);
}, 1000); }, 1000);
}, 1000); }, 1000);
}; // fade out img
setTimeout(function () {
imgEl.classList.add('hide');
// remove shape
setTimeout(function () {
imgEl.parentNode.removeChild(imgEl);
}, 500);
}, 500);
}.bind(this);
window.addEventListener('mousemove', mouseMoveEv); window.addEventListener('mousemove', mouseMoveEv);
window.addEventListener('mouseup', mouseUpEv); window.addEventListener('mouseup', mouseUpEv);
}) }.bind(this);
this.svgEl.addEventListener('mousedown', startTrace)
return this; }
} }

View file

@ -1,9 +1,9 @@
import { init } from "./game.js"; import { Game } from "./game.js";
let svgEl = document.getElementById('canvas'); let svgEl = document.getElementById('canvas');
let crosshairXEl = document.getElementById('crosshair_x'); let crosshairXEl = document.getElementById('crosshair_x');
let crosshairYEl = document.getElementById('crosshair_y'); let crosshairYEl = document.getElementById('crosshair_y');
var game = init(svgEl, crosshairXEl, crosshairYEl); var game = new Game(svgEl, crosshairXEl, crosshairYEl);
console.log('Initialised game', game); console.log('Initialised game', game);