<commit_before>package org.vitrivr.cineast.core.features;

import boofcv.abst.feature.dense.DescribeImageDense;
import boofcv.struct.feature.TupleDesc_F64;
import boofcv.struct.image.GrayU8;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.vitrivr.cineast.core.config.Config;
import org.vitrivr.cineast.core.config.QueryConfig;
import org.vitrivr.cineast.core.data.FloatVectorImpl;
import org.vitrivr.cineast.core.data.segments.SegmentContainer;
import org.vitrivr.cineast.core.data.StringDoublePair;
import org.vitrivr.cineast.core.features.abstracts.AbstractCodebookFeatureModule;
import org.vitrivr.cineast.core.util.images.HOGHelper;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

/**
 * @author rgasser
 * @version 1.0
 * @created 24.01.17
 */
public abstract class HOG extends AbstractCodebookFeatureModule {

    private static final Logger LOGGER = LogManager.getLogger();

    /**
     * @param tableName
     * @param maxDist
     */
    protected HOG(String tableName, float maxDist) {
        super(tableName, maxDist);
    }

    /**
     *
     * @param shot
     */
    @Override
    public void processShot(SegmentContainer shot) {
        long start = System.currentTimeMillis();
        LOGGER.entry();

        BufferedImage image = shot.getMostRepresentativeFrame().getImage().getBufferedImage();
        if (image != null) {
            DescribeImageDense<GrayU8, TupleDesc_F64> hog = HOGHelper.getHOGDescriptors(image);
            if (hog != null && hog.getDescriptions().size() > 0) {
                float[] histogram_f = this.histogram(true, hog.getDescriptions());
                this.persist(shot.getId(), new FloatVectorImpl(histogram_f));
            } else {
                LOGGER.warn("No HOG feature could be extracted for segment {}. This is not necessarily an error!");
            }
        }

        LOGGER.debug("HOG.processShot() (codebook: {}) done in {}ms", this.codebook(), System.currentTimeMillis() - start);
        LOGGER.exit();
    }

    /**
     *
     * @param sc
     * @param qc
     * @return
     */
    @Override
    public List<StringDoublePair> getSimilar(SegmentContainer sc, QueryConfig qc) {
        long start = System.currentTimeMillis();
        LOGGER.entry();

        qc.setDistanceIfEmpty(QueryConfig.Distance.euclidean);

        List<StringDoublePair> results =  new ArrayList<>(Config.sharedConfig().getRetriever().getMaxResultsPerModule());
        BufferedImage image = sc.getMostRepresentativeFrame().getImage().getBufferedImage();
        if (image != null) {
            DescribeImageDense<GrayU8, TupleDesc_F64> hog = HOGHelper.getHOGDescriptors(image);
            if (hog != null && hog.getDescriptions().size() > 0) {
                float[] histogram_f = this.histogram(true, hog.getDescriptions());
                results.addAll(this.getSimilar(histogram_f, qc));
            }
        }

        LOGGER.debug("HOG.getSimilar() (codebook: {}) done in {}ms", this.codebook(), (System.currentTimeMillis() - start));
        return LOGGER.exit(results);
    }
}<commit_msg>removed unnecessary list copy operation in HOG feature modules<commit_after>package org.vitrivr.cineast.core.features;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.vitrivr.cineast.core.config.QueryConfig;
import org.vitrivr.cineast.core.data.FloatVectorImpl;
import org.vitrivr.cineast.core.data.StringDoublePair;
import org.vitrivr.cineast.core.data.segments.SegmentContainer;
import org.vitrivr.cineast.core.features.abstracts.AbstractCodebookFeatureModule;
import org.vitrivr.cineast.core.util.images.HOGHelper;

import boofcv.abst.feature.dense.DescribeImageDense;
import boofcv.struct.feature.TupleDesc_F64;
import boofcv.struct.image.GrayU8;

/**
 * @author rgasser
 * @version 1.0
 * @created 24.01.17
 */
public abstract class HOG extends AbstractCodebookFeatureModule {

    private static final Logger LOGGER = LogManager.getLogger();

    /**
     * @param tableName
     * @param maxDist
     */
    protected HOG(String tableName, float maxDist) {
        super(tableName, maxDist);
    }

    /**
     *
     * @param shot
     */
    @Override
    public void processShot(SegmentContainer shot) {
        long start = System.currentTimeMillis();
        LOGGER.traceEntry();

        BufferedImage image = shot.getMostRepresentativeFrame().getImage().getBufferedImage();
        if (image != null) {
            DescribeImageDense<GrayU8, TupleDesc_F64> hog = HOGHelper.getHOGDescriptors(image);
            if (hog != null && hog.getDescriptions().size() > 0) {
                float[] histogram_f = this.histogram(true, hog.getDescriptions());
                this.persist(shot.getId(), new FloatVectorImpl(histogram_f));
            } else {
                LOGGER.warn("No HOG feature could be extracted for segment {}. This is not necessarily an error!");
            }
        }

        LOGGER.debug("HOG.processShot() (codebook: {}) done in {}ms", this.codebook(), System.currentTimeMillis() - start);
        LOGGER.traceExit();
    }

    /**
     *
     * @param sc
     * @param qc
     * @return
     */
    @Override
    public List<StringDoublePair> getSimilar(SegmentContainer sc, QueryConfig qc) {
        long start = System.currentTimeMillis();
        LOGGER.traceEntry();

        qc.setDistanceIfEmpty(QueryConfig.Distance.euclidean);

        List<StringDoublePair> results = null;
        BufferedImage image = sc.getMostRepresentativeFrame().getImage().getBufferedImage();
        if (image != null) {
            DescribeImageDense<GrayU8, TupleDesc_F64> hog = HOGHelper.getHOGDescriptors(image);
            if (hog != null && hog.getDescriptions().size() > 0) {
                float[] histogram_f = this.histogram(true, hog.getDescriptions());
                results = this.getSimilar(histogram_f, qc);
            }
        }
        
        if (results == null){
          results = new ArrayList<>(0);
        }

        LOGGER.debug("HOG.getSimilar() (codebook: {}) done in {}ms", this.codebook(), (System.currentTimeMillis() - start));
        return LOGGER.traceExit(results);
    }
}
{'fraction_non_alphanumeric': 0.12229077677582662, 'fraction_numerical': 0.006802721088435374, 'mean_word_length': 2.9836168872085698, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5620668', 'n_tokens_mistral': 1989, 'n_tokens_neox': 1906, 'n_words': 352}