Skip to content
Snippets Groups Projects
Commit 351cbafa authored by Leonardo de Lima Gaspar's avatar Leonardo de Lima Gaspar
Browse files

Fully functional Jaccard index/similary score complete. Will now look for...

Fully functional Jaccard index/similary score complete. Will now look for ground truth file at end of computation, with name groundTruth[videoFile].csv, and calculate score. Note that the trimming functionality is commented out for faster testing of model.
parent 7505ff41
Branches
No related tags found
No related merge requests found
......@@ -125,9 +125,10 @@ int main(int argc, char** argv) {
runEnd_s = runStart_s + timePerSimFrame_s*runCounter;
i += runCounter;
if (runCounter>0) {
csv << (int)runStart_s << "," << (int)runEnd_s << ",";
csv << floor(runStart_s) << "," << ceil(runEnd_s) << "\n";
}
}
csv.close();
auto end = chrono::steady_clock::now();
auto elapsed = chrono::duration_cast<chrono::seconds>(end - start).count();
......
......@@ -3,31 +3,23 @@
# Observed: [ (3, 4), (5, 9), (6, 11) ]
# True: [ (2, 4), (5, 10), (10, 12) ]
def calculateSuccess(observedIntervalsList, trueIntervalsList):
# List of success rates / similarity indices per observed interval.
indices = []
cumulativeIntersection = 0
cumulativeObs = 0
cumulativeTrue = 0
for obsInt in observedIntervalsList:
cumulativeObs += obsInt[1]-obsInt[0]
# Calculates the similarity index by considering the index of each observed interval with respect to how many true intervals are overlapping it.
# This means they are initially overweighted (producing indices that are too low), which is fixed by multiplying by the count of overlaps.
obsIntIndex = 0
overlappingTrueInts = 0
for i in range(len(trueIntervalsList)):
# Checks if true interval overlaps with observed. No bias in direction, meaning observed interval too low has same error as too high.
# Can be changed by adding more if statements based on the condition, so that having observed intervals too high is punished less than too low.
if not (obsInt[0] > trueIntervalsList[i][1] or obsInt[1] < trueIntervalsList[i][0]):
overlappingTrueInts += 1
intersection = min(obsInt[1], trueIntervalsList[i][1]) - max(obsInt[0], trueIntervalsList[i][0])
union = max(obsInt[1], trueIntervalsList[i][1]) - min(obsInt[0], trueIntervalsList[i][0])
cumulativeIntersection += min(obsInt[1], trueIntervalsList[i][1]) - max(obsInt[0], trueIntervalsList[i][0])
obsIntIndex += intersection / union
if overlappingTrueInts > 0:
obsIntIndex *= overlappingTrueInts
indices.append(obsIntIndex)
# Both lists of intervals are assumed to be SORTED.
for trueInt in trueIntervalsList:
cumulativeTrue += trueInt[1]-trueInt[0]
# Pads with zeroes if there is an excess of true intervals that do not overlap with any observed intervals.
while len(indices) < len(trueIntervalsList):
indices.append(0)
print(indices)
print(sum(indices)/len(indices))
\ No newline at end of file
union = cumulativeObs + cumulativeTrue
index = cumulativeIntersection / union
print("Correctness: {:.4f}".format(index) + ", or {:.1f}%".format(index*100))
\ No newline at end of file
......@@ -27,7 +27,7 @@ def main(inputVideo):
print("No input folder found. Creating it now. Place input video files in that folder.")
return
print("Processing file {file}".format(file=inputVideo))
os.system("opencvtest.exe {input}".format(input="input/{file}".format(file=inputVideo)))
#os.system("opencvtest.exe {input}".format(input="input/{file}".format(file=inputVideo)))
prefix, ext = os.path.splitext(inputVideo)
print("\nVideo processed and outputted to {csv}.".format(csv=prefix+".csv"))
......
......@@ -5,6 +5,7 @@ import logging
import sys
import ffmpeg
import subprocess
import Jaccard
# Logger for logging purposes (seeing which file is currently being processed)
logFormat = "%(levelname)s %(asctime)s - %(message)s"
......@@ -26,23 +27,23 @@ def trim(inputVideo, csvTimestamps):
# Removes duplicate intervals (start==end)
dupeClean = []
for timestampNo in range(len(csvTimestamps)-1):
if csvTimestamps[timestampNo] != csvTimestamps[timestampNo+1]:
for timestampNo in range(len(csvTimestamps)):
if csvTimestamps[timestampNo][0] != csvTimestamps[timestampNo][1]:
dupeClean.append(csvTimestamps[timestampNo])
print(dupeClean)
for i in range(len(dupeClean)-1):
for i in range(len(dupeClean)):
# Trims the source video for each interval. 'setpts' filter is used so timestamps are handled correctly; without it,
# still frames will appear in the final video, as it trims based on closest keyframes and fills the gap with stills.
trims.append(ffmpeg.input(inputPath).trim(start=dupeClean[i], end=dupeClean[i+1]).filter('setpts', expr='PTS-STARTPTS'))
trims.append(ffmpeg.input(inputPath).trim(start=dupeClean[i][0], end=dupeClean[i][1]).filter('setpts', expr='PTS-STARTPTS'))
(
ffmpeg
.concat(*trims)
.filter('setpts', expr='PTS-STARTPTS')
.output(outputPath)
.run(quiet=True)
)
#(
# ffmpeg
# .concat(*trims)
# .filter('setpts', expr='PTS-STARTPTS')
# .output(outputPath)
# .run(quiet=True)
#)
def doesGroundTruthExist(groundTruthInput):
for file in os.listdir():
......@@ -64,18 +65,23 @@ def readForCSV(inputVideoName):
videoFound = True
with open(file) as csvFile:
reader = csv.reader(csvFile, delimiter=",")
firstRow = next(reader)
# Calculates Jaccard index if ground truth file exists.
if doesGroundTruthExist(groundTruthName):
# Log instead of print
print("Ground truth file found for video {video}, calculating success rate:".format(video=inputVideoName))
with open(groundTruthName) as truthFile:
truthReader = csv.reader(truthFile, delimiter=",")
# Calculates Jaccard index if ground truth file exists.
Jaccard.calculateSuccess(firstRow, next(truthReader))
# Reads first row only.
trim(inputVideoName, firstRow)
# Cast whole csv input as a list of integers.
obsList = [[int(i) for i in interval] for interval in list(reader)]
trueList = [[int(i) for i in interval] for interval in list(truthReader)]
Jaccard.calculateSuccess( obsList, trueList )
#trim(inputVideoName, list(reader))
print("Done, trimmed file in output folder.")
break
if not videoFound:
# Log instead of print.
print("Input video file found, but no .csv file generated or found.")
......
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment