use of org.vitrivr.cineast.core.data.entities.MediaObjectMetadataDescriptor in project cineast by vitrivr.
the class FindObjectMetadataPostHandler method performPost.
@Override
public MediaObjectMetadataQueryResult performPost(OptionallyFilteredIdList ids, Context ctx) {
if (ids == null || ids.getIds().length == 0) {
return new MediaObjectMetadataQueryResult("", new ArrayList<>(0));
}
final MetadataRetrievalService service = new MetadataRetrievalService();
List<MediaObjectMetadataDescriptor> descriptors = service.lookupMultimediaMetadata(ids.getIdList());
if (ids.hasFilters()) {
final List<AbstractMetadataFilterDescriptor> filters = ids.getFilterList();
for (AbstractMetadataFilterDescriptor filter : filters) {
descriptors = descriptors.stream().filter(filter).collect(Collectors.toList());
}
}
return new MediaObjectMetadataQueryResult("", descriptors);
}
use of org.vitrivr.cineast.core.data.entities.MediaObjectMetadataDescriptor in project cineast by vitrivr.
the class AbstractQueryMessageHandler method loadAndWriteObjectMetadata.
/**
* Performs a lookup for {@link MediaObjectMetadataReader} identified by the provided segment IDs.
*
* @param session The WebSocket session to write the data to.
* @param queryId The current query id used for transmitting data back.
* @param objectIds List of object IDs for which to lookup metadata.
* @param metadataAccessSpec if this parameter is null, no metadata is fetched
*/
protected synchronized List<Thread> loadAndWriteObjectMetadata(Session session, String queryId, List<String> objectIds, Collection<String> objectIdsForWhichMetadataIsFetched, List<MetadataAccessSpecification> metadataAccessSpec) {
if (objectIds.isEmpty()) {
return new ArrayList<>();
}
objectIds.removeAll(objectIdsForWhichMetadataIsFetched);
objectIdsForWhichMetadataIsFetched.addAll(objectIds);
if (objectIds.size() > 100_000) {
return Lists.partition(objectIds, 100_000).stream().map(list -> loadAndWriteObjectMetadata(session, queryId, list, objectIdsForWhichMetadataIsFetched, metadataAccessSpec)).flatMap(Collection::stream).collect(Collectors.toList());
}
Thread thread = new Thread(() -> {
final List<MediaObjectMetadataDescriptor> objectMetadata = this.objectMetadataReader.findBySpec(objectIds, metadataAccessSpec);
if (objectMetadata.isEmpty()) {
return;
}
/*
* Partition metadata list so that message size does not get too big, and then wait until all metadata has been sent
*/
List<CompletableFuture<Void>> futures = new ArrayList<>();
Lists.partition(objectMetadata, 100_000).forEach(list -> futures.add(this.write(session, new MediaObjectMetadataQueryResult(queryId, list))));
futures.forEach(CompletableFuture::join);
});
thread.setName("metadata-retrieval-objects");
thread.start();
return Collections.singletonList(thread);
}
use of org.vitrivr.cineast.core.data.entities.MediaObjectMetadataDescriptor in project cineast by vitrivr.
the class MetadataTest method objMetadataForID.
List<MediaObjectMetadataDescriptor> objMetadataForID(int i) {
List<MediaObjectMetadataDescriptor> descriptors = new ArrayList<>();
for (int j = 0; j < METADATA_PER_ELEMENT_COUNT; j++) {
descriptors.add(new MediaObjectMetadataDescriptor(String.valueOf(i), i < ELEMENT_COUNT / 2 ? "one" : "two", String.valueOf(j), j, true));
}
descriptors.add(new MediaObjectMetadataDescriptor(String.valueOf(i), i < ELEMENT_COUNT / 2 ? "one" : "two", "id", i, true));
return descriptors;
}
use of org.vitrivr.cineast.core.data.entities.MediaObjectMetadataDescriptor in project cineast by vitrivr.
the class CliUtils method printInfoForObject.
public static void printInfoForObject(String objectId, DBSelector selector) {
System.out.println("= Retrieving object information for " + objectId + " =");
MediaObjectReader objectReader = new MediaObjectReader(selector);
System.out.println(objectReader.lookUpObjectById(objectId));
System.out.println("= Retrieving object metadata for =");
MediaObjectMetadataReader reader = new MediaObjectMetadataReader(selector);
List<MediaObjectMetadataDescriptor> metadataDescriptors = reader.lookupMultimediaMetadata(objectId);
metadataDescriptors.forEach(System.out::println);
}
use of org.vitrivr.cineast.core.data.entities.MediaObjectMetadataDescriptor in project cineast by vitrivr.
the class GenericExtractionItemHandler method run.
@Override
@SuppressWarnings("unchecked")
public void run() {
LOGGER.info("Starting extraction");
this.executorService.execute(pipeline);
final ObjectIdGenerator generator = this.context.objectIdGenerator();
Pair<ExtractionItemContainer, MediaType> pair = null;
/* Initalize all Metadata Extractors */
for (MetadataExtractor extractor : this.metadataExtractors) {
LOGGER.debug("Initializing metadata extractor {}", extractor.getClass().getSimpleName());
if (extractor instanceof MetadataFeatureModule) {
((MetadataFeatureModule<?>) extractor).init(this.context.persistencyWriter());
} else {
extractor.init();
}
}
/* Process until there's nothing left*/
while ((pair = this.nextItem()) != null) {
try {
LOGGER.debug("Processing path {} and mediatype {}", pair.getLeft(), pair.getRight());
if (handlerCache.get(pair.getRight()) == null) {
LOGGER.error("Unknown mediatype {}, exiting extraction", pair.getRight());
break;
}
/* Clear non-reusable segmenter */
handlerCache.compute(pair.getRight(), (mediaType, cache) -> {
Decoder decoder = null;
if (cache.getLeft() != null) {
if (cache.getLeft().canBeReused()) {
decoder = cache.getLeft();
}
}
return ImmutablePair.of(decoder, null);
});
/* Put a new decoder in the cache if there's not one already there */
if (handlerCache.get(pair.getRight()).getLeft() == null) {
Decoder decoder = handlers.get(pair.getRight()).getLeft().get();
handlerCache.compute(pair.getRight(), (mediaType, cache) -> new ImmutablePair<>(decoder, cache.getRight()));
}
/* Put a new segmenter in the cache if there's not one already there */
if (handlerCache.get(pair.getRight()).getRight() == null) {
Segmenter segmenter = handlers.get(pair.getRight()).getRight().get();
handlerCache.compute(pair.getRight(), (mediaType, cache) -> new ImmutablePair<>(cache.getLeft(), segmenter));
}
Decoder decoder = handlerCache.get(pair.getRight()).getLeft();
Segmenter segmenter = handlers.get(pair.getRight()).getRight().get();
if (decoder.init(pair.getLeft().getPathForExtraction(), Config.sharedConfig().getDecoders().get(pair.getRight()), Config.sharedConfig().getCache())) {
/* Create / lookup MediaObjectDescriptor for new file. */
final MediaObjectDescriptor descriptor = this.fetchOrCreateMultimediaObjectDescriptor(generator, pair.getLeft(), pair.getRight());
if (!this.checkAndPersistMultimediaObject(descriptor)) {
continue;
}
final String objectId = descriptor.getObjectId();
int segmentNumber = 1;
segmenter.init(decoder, descriptor);
this.executorService.execute(segmenter);
while (!segmenter.complete()) {
try {
final SegmentContainer container = segmenter.getNext();
if (container != null) {
/* Create segment-descriptor and try to persist it. */
MediaSegmentDescriptor mediaSegmentDescriptor;
if (container.getId() != null) {
mediaSegmentDescriptor = this.fetchOrCreateSegmentDescriptor(objectId, container.getId(), segmentNumber, container.getStart(), container.getEnd(), container.getAbsoluteStart(), container.getAbsoluteEnd());
/* Special case; segment ID is determined by container (image sequences only) */
} else {
mediaSegmentDescriptor = this.fetchOrCreateSegmentDescriptor(objectId, segmentNumber, container.getStart(), container.getEnd(), container.getAbsoluteStart(), container.getAbsoluteEnd());
}
container.setId(mediaSegmentDescriptor.getSegmentId());
container.setSuperId(mediaSegmentDescriptor.getObjectId());
if (!this.checkAndPersistSegment(mediaSegmentDescriptor)) {
continue;
}
int emissionTimeout = 1000;
while (!this.pipeline.emit(container, emissionTimeout)) {
LOGGER.debug("ExtractionPipeline is full - deferring emission of segment. Consider increasing the thread-pool count for the extraction pipeline.");
Thread.sleep(emissionTimeout);
// emissionTimeout += 500;
}
segmentNumber += 1;
}
} catch (InterruptedException e) {
LOGGER.log(Level.ERROR, "Thread was interrupted while the extraction process was running. Aborting...");
break;
}
}
List<MediaObjectMetadataDescriptor> metadata = pair.getLeft().getMetadata().stream().map(el -> MediaObjectMetadataDescriptor.fromExisting(el, objectId)).collect(Collectors.toList());
this.metadataWriter.write(metadata);
/* Extract metadata. */
this.extractAndPersistMetadata(pair.getLeft(), objectId);
/* Force flush the segment, object and metadata information. */
this.mediaSegmentWriter.flush();
this.objectWriter.flush();
this.metadataWriter.flush();
} else {
LOGGER.error("Failed to initialize decoder. File is being skipped...");
}
/* Increment the files counter. */
this.count_processed += 1;
/* Create new decoder pair for a new file if the decoder reports that it cannot be reused.*/
if (!decoder.canBeReused()) {
decoder.close();
final MediaType type = pair.getRight();
handlerCache.compute(type, (mediaType, cache) -> ImmutablePair.of(handlers.get(type).getLeft().get(), cache.getRight()));
}
for (ExtractionCompleteListener completeListener : this.completeListeners) {
completeListener.onCompleted(pair.getLeft());
}
/*
* Trigger garbage collection once in a while. This is specially relevant when many small files are processed, since unused allocated memory could accumulate and trigger swapping.
*/
if (this.count_processed % 50 == 0) {
System.gc();
}
} catch (Throwable t) {
LOGGER.error("Exception while processing path {}, {}", pair.getLeft(), t.getMessage());
t.printStackTrace();
}
}
shutdown();
}
Aggregations