You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by Apache Wiki <wi...@apache.org> on 2017/04/30 22:11:50 UTC

[Tika Wiki] Update of "TikaAndVisionVideo" by ChrisMattmann

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Tika Wiki" for change notification.

The "TikaAndVisionVideo" page has been changed by ChrisMattmann:
https://wiki.apache.org/tika/TikaAndVisionVideo

New page:
= Tika and Computer Vision for Videos =

<<TableOfContents(4)>>

This page describes how to make use of Object (Visual) Recognition capability of Apache Tika for videos.
TIKA-2322 introduced a new parser to perform object recognition on videos.
Visit [[https://issues.apache.org/jira/browse/TIKA-2322 | TIKA-2322 issue on Jira ]] or [[ https://github.com/apache/tika/pull/168 | pull request on Github]] to read the related conversation. Continue reading to get Tika up and running for object recognition in videos.

Currently, Tika utilizes [[https://arxiv.org/abs/1602.07261|Inception-V4]] model from [[https://www.tensorflow.org/|Tensorflow]] for recognizing objects in the videos.


== Tika and Tensorflow Video Recognition ==

=== 1. Tensorflow Using REST Server  ===

This is the recommended way for utilizing visual recognition capability of Tika.
This approach uses Tensorflow over REST API. 
To get this working, we are going to start a python flask based REST API server and tell tika to connect to it. 
All these dependencies and setup complexities are isolated in docker image.


Requirements :
  Docker --  Visit [[https://www.docker.com/| Docker.com]] and install latest version of Docker. (Note: tested on docker v17.03.1)

==== Step 1. Setup REST Server ====
You can either start the REST server in an isolated docker container or natively on the host that runs tensorflow.

===== a. Using docker (Recommended) =====
{{{#!highlight bash

cd tika-parsers/src/main/resources/org/apache/tika/parser/recognition/tf/ 
# alternatively, if you do not have tika's source code, you may simply wget the 'InceptionVideoRestDockerfile' from github link 
docker build -f InceptionVideoRestDockerfile -t inception-video-rest-tika .
docker run -p 8764:8764 -it inception-video-rest-tika
}}}


Once it is done, test the setup by visiting [[http://localhost:8764/inception/v4/classify/video?topk=2&url=http://dev.exiv2.org/attachments/344/MOV_0234.mp4&mode=fixed&ext=.m4v]] in your web browser.


'''Sample output from API:'''
{{{#!json
{
"confidence": [
0.29871075600385666,
0.271069885417819
],
"classnames": [
"screen, CRT screen",
"hand-held computer, hand-held microcomputer"
],
"classids": [
783,
591
],
"time": {
"read": 1,
"units": "ms",
"classification": 18842
}
}
}}}

Note: MAC USERS:
      If you are using an older version, say, 'Docker toolbox' instead of the newer 'Docker for Mac',
you need to add port forwarding rules in your Virtual Box default machine. 

  1. Open the Virtual Box Manager.
  2. Select your Docker Machine Virtual Box image.
  3. Open Settings -> Network -> Advanced -> Port Forwarding.
  4. Add an appname,Host IP 127.0.0.1 and set both ports to 8764.

===== b. Without Using docker =====
If you chose to setup REST server without a docker container, you are free to manually install all the required tools specified in the [[ https://github.com/apache/tika/blob/master/tika-parsers/src/main/resources/org/apache/tika/parser/recognition/tf/InceptionVideoRestDockerfile | docker file]].

Note: docker file has setup instructions for Ubuntu, you will have to transform those commands for your environment.

{{{#!highlight bash
   python tika-parsers/src/main/resources/org/apache/tika/parser/recognition/tf/inceptionapi.py  --port 8764
}}}



==== Step 2. Create a Tika-Config XML to enable Tensorflow Video parser. ====
 A sample config can be found in Tika source code at [[https://raw.githubusercontent.com/apache/tika/master/tika-parsers/src/test/resources/org/apache/tika/parser/recognition/tika-config-tflow-video-rest.xml|tika-parsers/src/test/resources/org/apache/tika/parser/recognition/tika-config-tflow-video-rest.xml]]

'''Here is an example:'''
{{{#!highlight xml
<properties>
    <parsers>
        <parser class="org.apache.tika.parser.recognition.ObjectRecognitionParser">
            <mime>video/mp4</mime>
            <params>
                <param name="topN" type="int">4</param>
                <param name="minConfidence" type="double">0.015</param>
                <param name="class" type="string">org.apache.tika.parser.recognition.tf.TensorflowRESTVideoRecogniser</param>
                <param name="healthUri" type="uri">http://localhost:8764/inception/v4/ping</param>
                <param name="apiUri" type="uri">http://localhost:8764/inception/v4/classify/video?mode=fixed</param>
            </params>
        </parser>
    </parsers>
</properties>
}}}

'''Description of parameters :'''
{{{#!csv
  Param Name, Type, Meaning, Range, Example
  topN, int, Number of object names to output, a non-zero positive integer, 1 to receive top 1 object name
  minConfidence, double, Minimum confidence required to output the name of detected objects, [0.0 to 1.0] inclusive, 0.9 for outputting object names iff at least 90% confident
  class, string, Class that implements object recognition functionality, constant string, org.apache.tika.parser.recognition.tf.TensorflowRESTVideoRecogniser
  healthUri, uri, The URI link to the ping service to ensure that the Video classification service is up and running.
  apiUri, uri, The URI link to the Python OpenCV/ImageNet based Video classification service.
}}}


==== Step 3: Demo ====
To use the vision capability via Tensorflow, just supply the above configuration to Tika.


For example, to use in Tika App (Assuming you have ''tika-app'' JAR and it is ready to run and you are running from the Tika src checkout dir):

{{{#!bash 
      $ java -jar tika-app/target/tika-app-1.15-SNAPSHOT.jar \
        --config=tika-parsers/src/test/resources/org/apache/tika/parser/recognition/tika-config-tflow-video-rest.xml \ 
       ./tika-parsers/src/test/resources/test-documents/testVideoMp4.mp4
 }}}

The input video is from: [[http://www.sample-videos.com/ | Sample Videos.com]].

[[https://raw.githubusercontent.com/apache/tika/master/tika-parsers/src/test/resources/test-documents/testVideoMp4.mp4|Big Rabbit coming out of his house]]

And, the top 4 detections are:
{{{#!highlight xml
...
<meta name="OBJECT" content="king penguin, Aptenodytes patagonica (0.19076)"/>
<meta name="OBJECT" content="hare (0.13538)"/>
<meta name="OBJECT" content="wallaby, brush kangaroo (0.09441)"/>
<meta name="OBJECT" content="ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus (0.09350)"/>
...
}}}


==== Changing the default topN, API port or URL ====
To change the defaults, update the parameters in config XML file accordingly

'''Here is an example scenario:'''

Run REST API on port 3030, and get top 4 object names if the confidence is above 10%. You may also change host to something else than 'localhost' if required.

'''Example Config File'''
{{{#!xml
<properties>
    <parsers>
        <parser class="org.apache.tika.parser.recognition.ObjectRecognitionParser">
            <mime>image/jpeg</mime>
            <params>
                <param name="topN" type="int">4</param>
                <param name="minConfidence" type="double">0.1</param>
                <param name="class" type="string">org.apache.tika.parser.recognition.tf.TensorflowRESTRecogniser</param>
       		<param name="healthUri" type="uri">http://localhost:3030/inception/v4/ping</param>
       		<param name="apiUri" type="uri">http://localhost:3030/inception/v4/classify/video?topk=4</param>
            </params>
        </parser>
    </parsers>
</properties>
}}}

'''To Start the service on port 3030:'''

Using Docker:

  {{{docker run -it -p 3030:8764 inception-video-rest-tika}}}


Without Using Docker:

{{{python tika-parsers/src/main/resources/org/apache/tika/parser/recognition/tf/inceptionapi.py  --port 3030}}}




----

=== Questions / Suggestions / Improvements / Feedback ? ===

  1. If it was useful, let us know on twitter by mentioning [[https://twitter.com/ApacheTika|@ApacheTika]]
  2. If you have questions, let us know by [[https://tika.apache.org/mail-lists.html | using Mailing Lists]]
  3. If you find any bugs, [[https://issues.apache.org/jira/browse/TIKA/| use Jira to report them]]