1 | package imageis;
|
---|
2 |
|
---|
3 | import eu.pharos.kmi.videosegmentation.ContentBasedSearchWSProxyStub;
|
---|
4 | import eu.pharos.kmi.videosegmentation.ContentBasedSearchWSProxyStub.QueryByID;
|
---|
5 | import eu.pharos.kmi.videosegmentation.ContentBasedSearchWSProxyStub.QueryByIDResponse;
|
---|
6 | import eu.pharos.kmi.videosegmentation.VSProcessor;
|
---|
7 | import eu.pharos.kmi.videosegmentation.VSVideoBean;
|
---|
8 | import eu.pharos.kmi.videosegmentation.VideoSegmentation;
|
---|
9 | import java.io.File;
|
---|
10 | import java.io.PrintStream;
|
---|
11 | import mpegMpeg7Schema2001.MediaLocatorType;
|
---|
12 | import mpegMpeg7Schema2001.Mpeg7Document;
|
---|
13 | import mpegMpeg7Schema2001.Mpeg7Document.Factory;
|
---|
14 | import org.apache.commons.io.FileUtils;
|
---|
15 | import org.apache.xmlbeans.XmlObject;
|
---|
16 |
|
---|
17 | public class ImageIS
|
---|
18 | {
|
---|
19 | private final String namespaceDeclaration = "declare namespace mpeg7='urn:mpeg:mpeg7:schema:2001';";
|
---|
20 | private final String query2 = "$this//mpeg7:MediaLocator";
|
---|
21 | private static ContentBasedSearchWSProxyStub cbsStub = null;
|
---|
22 | private static boolean proxy = false;
|
---|
23 | private static String proxyHost = null;
|
---|
24 | private static int proxyPort = 0;
|
---|
25 | private VSProcessor vsp = null;
|
---|
26 |
|
---|
27 | public ImageIS()
|
---|
28 | {
|
---|
29 | if (!(VideoSegmentation.instanceCreated())){
|
---|
30 | VideoSegmentation.getInstance();
|
---|
31 | }
|
---|
32 | String video_endpoint = VideoSegmentation.getCbseWSEndpoint();
|
---|
33 | try {
|
---|
34 | this.cbsStub = new ContentBasedSearchWSProxyStub(video_endpoint);
|
---|
35 | } catch (Exception e) {
|
---|
36 | System.err.println(e);
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public String query(String collection, String id) throws Exception {
|
---|
41 | ContentBasedSearchWSProxyStub.QueryByID req = new ContentBasedSearchWSProxyStub.QueryByID();
|
---|
42 | System.err.println("ImageIS, querying "+collection+":"+id+".jpg");
|
---|
43 | String input = query_xml;
|
---|
44 | input=input.replace("**docid**", collection+":"+id+".jpg");
|
---|
45 | req.setQuery(input);
|
---|
46 | ContentBasedSearchWSProxyStub.QueryByIDResponse res = getCbsStub().queryByID(req);
|
---|
47 | return res.get_return();
|
---|
48 | }
|
---|
49 |
|
---|
50 | public static void main(String[] args)
|
---|
51 | {
|
---|
52 | try {
|
---|
53 |
|
---|
54 | if (args.length != 1) {
|
---|
55 | System.out.println("USAGE: java -jar ImageIS.jar xmlinput.xml");
|
---|
56 | System.exit(0);
|
---|
57 | }
|
---|
58 | String dir = System.getProperty("user.dir");
|
---|
59 |
|
---|
60 | File inputFile = new File(dir + System.getProperty("file.separator") + args[0]);
|
---|
61 |
|
---|
62 | String input = FileUtils.readFileToString(new File(args[0]));
|
---|
63 |
|
---|
64 | ImageIS inst = new ImageIS();
|
---|
65 |
|
---|
66 | if (input.contains("kmi image annotation segment to add")) {
|
---|
67 | System.err.println("adding an image");
|
---|
68 | String imageLocation = inst.getImageLocation(inst.processMpeg7Input(input));
|
---|
69 |
|
---|
70 | //String video_endpoint = VideoSegmentation.getCbseWSEndpoint();
|
---|
71 |
|
---|
72 | //ContentBasedSearchWSProxyStub cbse_stub = new ContentBasedSearchWSProxyStub(video_endpoint);
|
---|
73 | //inst.setCbsStub(cbse_stub);
|
---|
74 | ////inst.setCbsStub(new ContentBasedSearchWSProxyStub(VideoSegmentation.getCbseWSEndpoint()));
|
---|
75 | inst.setVsp(new VSProcessor(VideoSegmentation.getWorkpath(), new VSVideoBean(imageLocation), inst.getCbsStub()));
|
---|
76 | inst.getVsp().processExistingKeyframes();
|
---|
77 | inst.getVsp().addLFImageDB();
|
---|
78 | System.out.println("TRUE");
|
---|
79 | }
|
---|
80 | else if (input.contains("kmi image annotation segment to remove")) {
|
---|
81 | String imageLocation = inst.getImageLocation(inst.processMpeg7Input(input));
|
---|
82 | inst.setCbsStub(new ContentBasedSearchWSProxyStub(VideoSegmentation.getCbseWSEndpoint()));
|
---|
83 | inst.setVsp(new VSProcessor(VideoSegmentation.getWorkpath(), new VSVideoBean(imageLocation), inst.getCbsStub()));
|
---|
84 |
|
---|
85 | inst.getVsp().removeExistingKeyframes(input);
|
---|
86 | System.out.println("TRUE");
|
---|
87 | }
|
---|
88 | else if (input.contains("QueryByMedia")) {
|
---|
89 | inst.setCbsStub(new ContentBasedSearchWSProxyStub(VideoSegmentation.getCbseWSEndpoint()));
|
---|
90 | ContentBasedSearchWSProxyStub.QueryByID req = new ContentBasedSearchWSProxyStub.QueryByID();
|
---|
91 | req.setQuery(input);
|
---|
92 | ContentBasedSearchWSProxyStub.QueryByIDResponse res = inst.getCbsStub().queryByID(req);
|
---|
93 | FileUtils.writeStringToFile(new File(dir + System.getProperty("file.separator") + "image_cbs_result.xml"), res.get_return());
|
---|
94 | }
|
---|
95 | else {
|
---|
96 | throw new RuntimeException("Unsuported action" + input);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | catch (Exception e) {
|
---|
100 | // System.out.println(e);
|
---|
101 | e.printStackTrace();
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | public static boolean isProxy()
|
---|
106 | {
|
---|
107 | return proxy;
|
---|
108 | }
|
---|
109 |
|
---|
110 | public static void setProxy(boolean aProxy)
|
---|
111 | {
|
---|
112 | proxy = aProxy;
|
---|
113 | }
|
---|
114 |
|
---|
115 | public static String getProxyHost()
|
---|
116 | {
|
---|
117 | return proxyHost;
|
---|
118 | }
|
---|
119 |
|
---|
120 | public static void setProxyHost(String aProxyHost)
|
---|
121 | {
|
---|
122 | proxyHost = aProxyHost;
|
---|
123 | }
|
---|
124 |
|
---|
125 | public static int getProxyPort()
|
---|
126 | {
|
---|
127 | return proxyPort;
|
---|
128 | }
|
---|
129 |
|
---|
130 | public static void setProxyPort(int aProxyPort)
|
---|
131 | {
|
---|
132 | proxyPort = aProxyPort;
|
---|
133 | }
|
---|
134 |
|
---|
135 | public ContentBasedSearchWSProxyStub getCbsStub()
|
---|
136 | {
|
---|
137 | return this.cbsStub;
|
---|
138 | }
|
---|
139 |
|
---|
140 | public void setCbsStub(ContentBasedSearchWSProxyStub cbsStub)
|
---|
141 | {
|
---|
142 | this.cbsStub = cbsStub;
|
---|
143 | }
|
---|
144 |
|
---|
145 | private String processMpeg7Input(String mpeg7) throws Exception
|
---|
146 | {
|
---|
147 | Mpeg7Document mpeg7doc = Mpeg7Document.Factory.parse(mpeg7);
|
---|
148 |
|
---|
149 | if (!(mpeg7doc.validate())) {
|
---|
150 | System.out.println("Invalid instance!!!: " + mpeg7);
|
---|
151 | }
|
---|
152 |
|
---|
153 | return getMediaURI(mpeg7doc);
|
---|
154 | }
|
---|
155 |
|
---|
156 | private String getMediaURI(Mpeg7Document mpeg7doc)
|
---|
157 | {
|
---|
158 | XmlObject[] ml = mpeg7doc.selectPath("declare namespace mpeg7='urn:mpeg:mpeg7:schema:2001';$this//mpeg7:MediaLocator");
|
---|
159 | MediaLocatorType mlt = (MediaLocatorType)ml[0];
|
---|
160 | return mlt.getMediaUri().trim();
|
---|
161 | }
|
---|
162 |
|
---|
163 | private String getImageLocation(String input) throws Exception {
|
---|
164 | if (input.startsWith("file://")) {
|
---|
165 | return input.substring("file://".length(), input.length());
|
---|
166 | }
|
---|
167 | return input;
|
---|
168 | }
|
---|
169 |
|
---|
170 | public VSProcessor getVsp()
|
---|
171 | {
|
---|
172 | return this.vsp;
|
---|
173 | }
|
---|
174 |
|
---|
175 | public void setVsp(VSProcessor vsp)
|
---|
176 | {
|
---|
177 | this.vsp = vsp;
|
---|
178 | }
|
---|
179 |
|
---|
180 | private static String query_xml = "<MpegQuery mpqfID='TestId0012-36' xmlns='urn:mpeg:mpqf:schema:2008' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:mpeg7='urn:mpeg:mpeg7:schema:2004' xsi:schemaLocation='urn:mpeg:mpqf:schema:2008 mpqfv08.xsd urn:mpeg:mpeg7:schema:2004 M7v2schema.xsd'><Query><Input><!-- This is used to format the output / Result set --><OutputDescription maxItemCount='10' freeTextUse='true' mediaResourceUse='true' outputNameSpace='urn:mpeg:mpqf:schema:2008'></OutputDescription><QueryCondition><TargetMediaType>video/jpeg</TargetMediaType><Condition xsi:type='QueryByMedia' matchType='similar' thresholdValue='0.8' preferenceValue='1'><MediaResource resourceID='id001'><MediaResource><MediaUri>**docid**</MediaUri></MediaResource></MediaResource></Condition></QueryCondition><ServiceSelection><ServiceID>image-search-mediaid</ServiceID></ServiceSelection></Input></Query></MpegQuery>";
|
---|
181 |
|
---|
182 |
|
---|
183 | }
|
---|
184 |
|
---|