Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a code snippet for extracting annotations #132

Open
korabelnikov opened this issue Jan 22, 2018 · 10 comments
Open

Provide a code snippet for extracting annotations #132

korabelnikov opened this issue Jan 22, 2018 · 10 comments

Comments

@korabelnikov
Copy link

korabelnikov commented Jan 22, 2018

Now it seem almost impossible to understand how get annotations.

Please explain how do this, because #131 doesn't help

If it will be usefull, someone (or I) can make an PR with something like button 'export annotations'.

Thank you in andvance!

@korabelnikov korabelnikov changed the title Provide a code snipped for extracting annotations Provide a code snippet for extracting annotations Jan 22, 2018
@antingshen
Copy link
Owner

here's an example pseudo-code snippet (unverified). Please give this a try and let me know how it goes :)

First, ./manage.py shell
Then:

from annotator.models import Video
for video in Video.objects.all(): # or Video.objects.filter( ... )
    print(video.annotation)

@korabelnikov
Copy link
Author

Thank you for a quick response,
i will try this right now

@korabelnikov
Copy link
Author

It works!

result for one video is like this:

image

Did I correct understood, that developer should perform interpolation between 'keyframe' by himself?
Do you use Linear interpolation?

@antingshen
Copy link
Owner

antingshen commented Jan 22, 2018

Yes, it's linear interpolation. There's currently no backend tool to do this, but it shouldn't be too difficult.

You can look at the frontend javascript interpolation code for reference.

@korabelnikov
Copy link
Author

And few why questions:
-why field 'frame' has float type? Is it relative to full length of video or image sequence?
-why field 'y' has float type?

@korabelnikov
Copy link
Author

I have a plan to make simple script to exctract annotations. Is it a good idea to add it to your repo?

@antingshen
Copy link
Owner

antingshen commented Jan 22, 2018

frame is float because that's how the frontend HTML5 Video tracks the time.
The coordinates are floats because users can draw on zoomed videos that may be fractional pixels.

Rounding both of these should probably be just fine, but there wasn't a reason to round.

And yeah, happy to merge a script

@clayshieh
Copy link
Collaborator

I have included two methods of downloading video annotations below that would be in a script included in the BeaverDam/scripts/ directory (path matters for the django models method). In both methods the output is a json file named with the video object's id (i.e. video 1's annotations would be outputted in a file named 1.json)

The first method assumes that the server is running locally and just queries the same endpoints that the front end queries for annotations so the video id's must be specified manually.

import requests

base_url = "http://localhost:8000/annotation/{0}/"
video_ids = range(1,2)

for x in video_ids:
	url = base_url.format(str(x))
	response = requests.get(url)
	print "Processing video id: " + str(x)
	with open(str(x)+".json", "wb") as f:
		f.write(response.content)

The second method actually sets up the django environment and queries the Video django model objects themselves and can use the built in django filters to filter objects based on properties. Method assumes that code is being run at BeaverDam/scripts because project_dir needs to be defined as the path to the BeaverDam project directory to setup the django environment.

import os, sys
project_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(project_dir)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "beaverdam.settings")
import django
django.setup()

from annotator.models import Video

for video in Video.objects.all():
	print("Print processing video id: " + str(video.id))
	with open(str(video.id)+".json", "wb") as f:
		f.write(video.annotation)

Example code will be commented step by step with more detail and committed but a more long term solution will be a custom admin action to download selected videos in the admin interface.

@cheind
Copy link
Contributor

cheind commented Feb 25, 2018

Hi everyone,

I've created a new command to export annotations from BeaverDam in a fork of mine.

https://github.com/cheind/BeaverDam

Command implementation can be found here

https://github.com/cheind/BeaverDam/blob/master/annotator/management/commands/export_annotations.py

It supports sparse / dense annotations and writes files in standard JSON format (dense annotations will have a new JSON field named frames). Dense annotations are created between first and last keyframe using interpolation. Since BeaverDam uses html5 currentTime as timestamping mechanism, the command will need to know the fps of the video files. If not specified via command line, video files will automatically be probed using ffmpeg. In theory it should support local, remote video files and image lists.

Type python manage.py export_annotations --help for more info.

A simple annotation viewer using OpenCV can be found here
https://gist.github.com/cheind/9850e35bb08cfe12500942fb8b55531f

@antingshen would be happy to file a PR if desired.

@antingshen
Copy link
Owner

Yeah happy to merge it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants