antingshen / BeaverDam

Video annotation tool for deep learning training labels
BSD 2-Clause "Simplified" License
202 stars 100 forks source link

Provide a code snippet for extracting annotations #132

Open korabelnikov opened 6 years ago

korabelnikov commented 6 years ago

Now it seem almost impossible to understand how get annotations.

Please explain how do this, because https://github.com/antingshen/BeaverDam/issues/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!

antingshen commented 6 years ago

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 commented 6 years ago

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

korabelnikov commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

antingshen commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

Yeah happy to merge it