Search in sources :

Example 1 with SegmentContainer

use of org.vitrivr.cineast.core.data.segments.SegmentContainer in project cineast by vitrivr.

the class SpatialDistance method getSimilar.

/**
 * Returns similar <i>objects</i> to the feature data contained in the given segment container.
 */
@Override
public List<ScoreElement> getSimilar(SegmentContainer sc, ReadableQueryConfig qc) {
    final QueryConfig mqc = QueryConfig.clone(qc);
    if (sc instanceof ParameterisedLocationQueryTermContainer) {
        if (((ParameterisedLocationQueryTermContainer) sc).getParameter().isPresent()) {
            final String param = ((ParameterisedLocationQueryTermContainer) sc).getParameter().get();
            mqc.setCorrespondenceFunctionIfEmpty(CorrespondenceFunction.hyperbolic(parseAndEvaluateHalfSimilarityDistance(param)));
        }
    }
    return this.extractFeature(sc).map(ReadableFloatVector::toArray).map(array -> this.getSimilar(array, new ReadableQueryConfig(mqc))).orElse(Collections.emptyList());
}
Also used : ParameterisedLocationQueryTermContainer(org.vitrivr.cineast.core.data.query.containers.ParameterisedLocationQueryTermContainer) MetadataFeatureModule(org.vitrivr.cineast.core.features.abstracts.MetadataFeatureModule) CorrespondenceFunction(org.vitrivr.cineast.core.data.CorrespondenceFunction) ReadableFloatVector(org.vitrivr.cineast.core.data.ReadableFloatVector) QueryConfig(org.vitrivr.cineast.core.config.QueryConfig) ScoreElement(org.vitrivr.cineast.core.data.score.ScoreElement) MediaObjectMetadataDescriptor(org.vitrivr.cineast.core.data.entities.MediaObjectMetadataDescriptor) ReadableQueryConfig(org.vitrivr.cineast.core.config.ReadableQueryConfig) DoubleEvaluator(com.fathzer.soft.javaluator.DoubleEvaluator) LinkedHashMap(java.util.LinkedHashMap) GpsData(org.vitrivr.cineast.core.data.GpsData) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ImmutableList(com.google.common.collect.ImmutableList) SegmentContainer(org.vitrivr.cineast.core.data.segments.SegmentContainer) Optional(java.util.Optional) Distance(org.vitrivr.cineast.core.config.ReadableQueryConfig.Distance) Path(java.nio.file.Path) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) Location(org.vitrivr.cineast.core.data.Location) ReadableQueryConfig(org.vitrivr.cineast.core.config.ReadableQueryConfig) QueryConfig(org.vitrivr.cineast.core.config.QueryConfig) ReadableQueryConfig(org.vitrivr.cineast.core.config.ReadableQueryConfig) ParameterisedLocationQueryTermContainer(org.vitrivr.cineast.core.data.query.containers.ParameterisedLocationQueryTermContainer) ReadableFloatVector(org.vitrivr.cineast.core.data.ReadableFloatVector)

Example 2 with SegmentContainer

use of org.vitrivr.cineast.core.data.segments.SegmentContainer in project cineast by vitrivr.

the class HPCPShingle method getFeatures.

/**
 * Returns a list of feature vectors given a SegmentContainer.
 *
 * @param segment SegmentContainer for which to calculate the feature vectors.
 * @return List of HPCP Shingle feature vectors.
 */
private List<float[]> getFeatures(SegmentContainer segment) {
    /* Create STFT; If this fails, return empty list. */
    Pair<Integer, Integer> parameters = FFTUtil.parametersForDuration(segment.getSamplingrate(), WINDOW_SIZE);
    STFT stft = segment.getSTFT(parameters.first, (parameters.first - 2 * parameters.second) / 2, parameters.second, new HanningWindow());
    if (stft == null) {
        return new ArrayList<>(0);
    }
    HPCP hpcps = new HPCP(this.resolution, this.min_frequency, this.max_frequency);
    hpcps.addContribution(stft);
    int vectors = Math.max(hpcps.size() - SHINGLE_SIZE, 1);
    final SummaryStatistics statistics = new SummaryStatistics();
    List<Pair<Double, float[]>> features = new ArrayList<>(vectors);
    for (int n = 0; n < vectors; n++) {
        Pair<Double, float[]> feature = this.getHPCPShingle(hpcps, n);
        features.add(feature);
        statistics.addValue(feature.first);
    }
    final double threshold = 0.25 * statistics.getGeometricMean();
    return features.stream().filter(f -> (f.first > threshold)).map(f -> f.second).collect(Collectors.toList());
}
Also used : StagedFeatureModule(org.vitrivr.cineast.core.features.abstracts.StagedFeatureModule) MathHelper(org.vitrivr.cineast.core.util.math.MathHelper) CorrespondenceFunction(org.vitrivr.cineast.core.data.CorrespondenceFunction) QueryConfig(org.vitrivr.cineast.core.config.QueryConfig) FloatVectorImpl(org.vitrivr.cineast.core.data.FloatVectorImpl) HashMap(java.util.HashMap) ScoreElement(org.vitrivr.cineast.core.data.score.ScoreElement) ReadableQueryConfig(org.vitrivr.cineast.core.config.ReadableQueryConfig) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) FFTUtil(org.vitrivr.cineast.core.util.dsp.fft.FFTUtil) STFT(org.vitrivr.cineast.core.util.dsp.fft.STFT) HPCP(org.vitrivr.cineast.core.util.audio.HPCP) SummaryStatistics(org.apache.commons.math3.stat.descriptive.SummaryStatistics) List(java.util.List) SegmentContainer(org.vitrivr.cineast.core.data.segments.SegmentContainer) Pair(org.vitrivr.cineast.core.data.Pair) SegmentDistanceElement(org.vitrivr.cineast.core.data.distance.SegmentDistanceElement) DistanceElement(org.vitrivr.cineast.core.data.distance.DistanceElement) HanningWindow(org.vitrivr.cineast.core.util.dsp.fft.windows.HanningWindow) ArrayList(java.util.ArrayList) STFT(org.vitrivr.cineast.core.util.dsp.fft.STFT) SummaryStatistics(org.apache.commons.math3.stat.descriptive.SummaryStatistics) HPCP(org.vitrivr.cineast.core.util.audio.HPCP) HanningWindow(org.vitrivr.cineast.core.util.dsp.fft.windows.HanningWindow) Pair(org.vitrivr.cineast.core.data.Pair)

Example 3 with SegmentContainer

use of org.vitrivr.cineast.core.data.segments.SegmentContainer 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();
}
Also used : MetadataFeatureModule(org.vitrivr.cineast.core.features.abstracts.MetadataFeatureModule) MimeTypeHelper(org.vitrivr.cineast.core.util.MimeTypeHelper) Level(org.apache.logging.log4j.Level) MediaObjectDescriptor(org.vitrivr.cineast.core.data.entities.MediaObjectDescriptor) StringUtils(org.apache.commons.lang3.StringUtils) ExtractionContextProvider(org.vitrivr.cineast.core.extraction.ExtractionContextProvider) Pair(org.apache.commons.lang3.tuple.Pair) MediaObjectReader(org.vitrivr.cineast.core.db.dao.reader.MediaObjectReader) SegmentContainer(org.vitrivr.cineast.core.data.segments.SegmentContainer) Map(java.util.Map) Path(java.nio.file.Path) MediaSegmentDescriptor(org.vitrivr.cineast.core.data.entities.MediaSegmentDescriptor) ImageSequenceDecoder(org.vitrivr.cineast.core.extraction.decode.image.ImageSequenceDecoder) IdConfig(org.vitrivr.cineast.core.config.IdConfig) MediaObjectMetadataWriter(org.vitrivr.cineast.core.db.dao.writer.MediaObjectMetadataWriter) ExtractionPipeline(org.vitrivr.cineast.standalone.runtime.ExtractionPipeline) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) List(java.util.List) Logger(org.apache.logging.log4j.Logger) MediaSegmentWriter(org.vitrivr.cineast.core.db.dao.writer.MediaSegmentWriter) Optional(java.util.Optional) LogHelper(org.vitrivr.cineast.core.util.LogHelper) ModularMeshDecoder(org.vitrivr.cineast.core.extraction.decode.m3d.ModularMeshDecoder) MediaType(org.vitrivr.cineast.core.data.MediaType) ImageSequenceSegmenter(org.vitrivr.cineast.core.extraction.segmenter.image.ImageSequenceSegmenter) Config(org.vitrivr.cineast.standalone.config.Config) ReflectionHelper(org.vitrivr.cineast.core.util.ReflectionHelper) Model3DSegment(org.vitrivr.cineast.core.data.segments.Model3DSegment) DBSelector(org.vitrivr.cineast.core.db.DBSelector) HashMap(java.util.HashMap) Decoder(org.vitrivr.cineast.core.extraction.decode.general.Decoder) MediaObjectMetadataDescriptor(org.vitrivr.cineast.core.data.entities.MediaObjectMetadataDescriptor) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Mesh(org.vitrivr.cineast.core.data.m3d.Mesh) PersistencyWriter(org.vitrivr.cineast.core.db.PersistencyWriter) FFMpegVideoDecoder(org.vitrivr.cineast.core.extraction.decode.video.FFMpegVideoDecoder) ExecutorService(java.util.concurrent.ExecutorService) Segmenter(org.vitrivr.cineast.core.extraction.segmenter.general.Segmenter) Files(java.nio.file.Files) MediaSegmentReader(org.vitrivr.cineast.core.db.dao.reader.MediaSegmentReader) ConstantLengthAudioSegmenter(org.vitrivr.cineast.core.extraction.segmenter.audio.ConstantLengthAudioSegmenter) MetadataExtractor(org.vitrivr.cineast.core.extraction.metadata.MetadataExtractor) PassthroughSegmenter(org.vitrivr.cineast.core.extraction.segmenter.general.PassthroughSegmenter) ObjectIdGenerator(org.vitrivr.cineast.core.extraction.idgenerator.ObjectIdGenerator) IngestConfig(org.vitrivr.cineast.standalone.config.IngestConfig) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) TimeUnit(java.util.concurrent.TimeUnit) Paths(java.nio.file.Paths) FFMpegAudioDecoder(org.vitrivr.cineast.core.extraction.decode.audio.FFMpegAudioDecoder) MediaObjectWriter(org.vitrivr.cineast.core.db.dao.writer.MediaObjectWriter) DefaultImageDecoder(org.vitrivr.cineast.core.extraction.decode.image.DefaultImageDecoder) VideoHistogramSegmenter(org.vitrivr.cineast.core.extraction.segmenter.video.VideoHistogramSegmenter) LogManager(org.apache.logging.log4j.LogManager) ImageSegmenter(org.vitrivr.cineast.core.extraction.segmenter.image.ImageSegmenter) ObjectIdGenerator(org.vitrivr.cineast.core.extraction.idgenerator.ObjectIdGenerator) MediaObjectDescriptor(org.vitrivr.cineast.core.data.entities.MediaObjectDescriptor) MetadataFeatureModule(org.vitrivr.cineast.core.features.abstracts.MetadataFeatureModule) ImageSequenceSegmenter(org.vitrivr.cineast.core.extraction.segmenter.image.ImageSequenceSegmenter) Segmenter(org.vitrivr.cineast.core.extraction.segmenter.general.Segmenter) ConstantLengthAudioSegmenter(org.vitrivr.cineast.core.extraction.segmenter.audio.ConstantLengthAudioSegmenter) PassthroughSegmenter(org.vitrivr.cineast.core.extraction.segmenter.general.PassthroughSegmenter) VideoHistogramSegmenter(org.vitrivr.cineast.core.extraction.segmenter.video.VideoHistogramSegmenter) ImageSegmenter(org.vitrivr.cineast.core.extraction.segmenter.image.ImageSegmenter) ImageSequenceDecoder(org.vitrivr.cineast.core.extraction.decode.image.ImageSequenceDecoder) ModularMeshDecoder(org.vitrivr.cineast.core.extraction.decode.m3d.ModularMeshDecoder) Decoder(org.vitrivr.cineast.core.extraction.decode.general.Decoder) FFMpegVideoDecoder(org.vitrivr.cineast.core.extraction.decode.video.FFMpegVideoDecoder) FFMpegAudioDecoder(org.vitrivr.cineast.core.extraction.decode.audio.FFMpegAudioDecoder) DefaultImageDecoder(org.vitrivr.cineast.core.extraction.decode.image.DefaultImageDecoder) MediaSegmentDescriptor(org.vitrivr.cineast.core.data.entities.MediaSegmentDescriptor) MediaType(org.vitrivr.cineast.core.data.MediaType) MetadataExtractor(org.vitrivr.cineast.core.extraction.metadata.MetadataExtractor) SegmentContainer(org.vitrivr.cineast.core.data.segments.SegmentContainer) MediaObjectMetadataDescriptor(org.vitrivr.cineast.core.data.entities.MediaObjectMetadataDescriptor)

Example 4 with SegmentContainer

use of org.vitrivr.cineast.core.data.segments.SegmentContainer in project cineast by vitrivr.

the class ExtractionPipeline method run.

/**
 * When an object implementing interface <code>Runnable</code> is used to create a thread, starting the thread causes the object's
 * <code>run</code> method to be called in that separately executing
 * thread.
 * <p>
 * The general contract of the method <code>run</code> is that it may take any action whatsoever.
 *
 * @see Thread#run()
 */
@Override
public void run() {
    /* Set running flag to true. */
    synchronized (this) {
        this.running = true;
    }
    /* Process SegmentContainers in Queue: For each Extractor in list dispatch an extraction task. */
    while (this.isRunning() || !this.segmentQueue.isEmpty()) {
        if (!this.isRunning()) {
            LOGGER.debug("Received stop signal, still {} elements left", this.segmentQueue.size());
        }
        try {
            SegmentContainer s = this.segmentQueue.poll(500, TimeUnit.MILLISECONDS);
            if (s != null) {
                LOGGER.debug("Segment {} is being handed to the extraction pipeline.", s.getId());
                for (Extractor f : extractors) {
                    try {
                        this.executorService.execute(new ExtractionTask(f, s, this));
                        LOGGER.debug("Submitted segment {} for feature {}", s.getId(), f.getClass().getSimpleName());
                    } catch (RejectedExecutionException e) {
                        this.segmentQueue.clear();
                        LOGGER.fatal("Failed to submit segment {} for feature {}. Aborting...\n{}", s.getId(), f.getClass().getSimpleName(), LogHelper.getStackTrace(e));
                        break;
                    }
                }
                /* Sort list of extractors by execution time. */
                (this.extractors).sort((o1, o2) -> Long.compare(getAverageExecutionTime(o2.getClass().getSimpleName()), getAverageExecutionTime(o1.getClass().getSimpleName())));
            }
        } catch (InterruptedException e) {
            LOGGER.warn("ShotDispatcher was interrupted: {}", LogHelper.getStackTrace(e));
        }
    }
    this.shutdown();
    synchronized (this) {
        this.running = false;
    }
}
Also used : SegmentContainer(org.vitrivr.cineast.core.data.segments.SegmentContainer) Extractor(org.vitrivr.cineast.core.features.extractor.Extractor) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Aggregations

SegmentContainer (org.vitrivr.cineast.core.data.segments.SegmentContainer)4 List (java.util.List)3 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 QueryConfig (org.vitrivr.cineast.core.config.QueryConfig)2 ReadableQueryConfig (org.vitrivr.cineast.core.config.ReadableQueryConfig)2 CorrespondenceFunction (org.vitrivr.cineast.core.data.CorrespondenceFunction)2 MediaObjectMetadataDescriptor (org.vitrivr.cineast.core.data.entities.MediaObjectMetadataDescriptor)2 ScoreElement (org.vitrivr.cineast.core.data.score.ScoreElement)2 DoubleEvaluator (com.fathzer.soft.javaluator.DoubleEvaluator)1 ImmutableList (com.google.common.collect.ImmutableList)1 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1 Collections (java.util.Collections)1 LinkedHashMap (java.util.LinkedHashMap)1