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) { ...@@ -125,9 +125,10 @@ int main(int argc, char** argv) {
runEnd_s = runStart_s + timePerSimFrame_s*runCounter; runEnd_s = runStart_s + timePerSimFrame_s*runCounter;
i += runCounter; i += runCounter;
if (runCounter>0) { 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 end = chrono::steady_clock::now();
auto elapsed = chrono::duration_cast<chrono::seconds>(end - start).count(); auto elapsed = chrono::duration_cast<chrono::seconds>(end - start).count();
......
...@@ -3,31 +3,23 @@ ...@@ -3,31 +3,23 @@
# Observed: [ (3, 4), (5, 9), (6, 11) ] # Observed: [ (3, 4), (5, 9), (6, 11) ]
# True: [ (2, 4), (5, 10), (10, 12) ] # True: [ (2, 4), (5, 10), (10, 12) ]
def calculateSuccess(observedIntervalsList, trueIntervalsList): def calculateSuccess(observedIntervalsList, trueIntervalsList):
# List of success rates / similarity indices per observed interval. cumulativeIntersection = 0
indices = [] cumulativeObs = 0
cumulativeTrue = 0
for obsInt in observedIntervalsList: 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. # 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)): 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. # 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. # 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]): if not (obsInt[0] > trueIntervalsList[i][1] or obsInt[1] < trueIntervalsList[i][0]):
overlappingTrueInts += 1 cumulativeIntersection += min(obsInt[1], trueIntervalsList[i][1]) - max(obsInt[0], trueIntervalsList[i][0])
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])
obsIntIndex += intersection / union # Both lists of intervals are assumed to be SORTED.
if overlappingTrueInts > 0: for trueInt in trueIntervalsList:
obsIntIndex *= overlappingTrueInts cumulativeTrue += trueInt[1]-trueInt[0]
indices.append(obsIntIndex)
# Pads with zeroes if there is an excess of true intervals that do not overlap with any observed intervals. union = cumulativeObs + cumulativeTrue
while len(indices) < len(trueIntervalsList): index = cumulativeIntersection / union
indices.append(0) print("Correctness: {:.4f}".format(index) + ", or {:.1f}%".format(index*100))
print(indices) \ No newline at end of file
print(sum(indices)/len(indices))
\ No newline at end of file
...@@ -27,7 +27,7 @@ def main(inputVideo): ...@@ -27,7 +27,7 @@ def main(inputVideo):
print("No input folder found. Creating it now. Place input video files in that folder.") print("No input folder found. Creating it now. Place input video files in that folder.")
return return
print("Processing file {file}".format(file=inputVideo)) 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) prefix, ext = os.path.splitext(inputVideo)
print("\nVideo processed and outputted to {csv}.".format(csv=prefix+".csv")) print("\nVideo processed and outputted to {csv}.".format(csv=prefix+".csv"))
......
...@@ -5,6 +5,7 @@ import logging ...@@ -5,6 +5,7 @@ import logging
import sys import sys
import ffmpeg import ffmpeg
import subprocess import subprocess
import Jaccard
# Logger for logging purposes (seeing which file is currently being processed) # Logger for logging purposes (seeing which file is currently being processed)
logFormat = "%(levelname)s %(asctime)s - %(message)s" logFormat = "%(levelname)s %(asctime)s - %(message)s"
...@@ -26,23 +27,23 @@ def trim(inputVideo, csvTimestamps): ...@@ -26,23 +27,23 @@ def trim(inputVideo, csvTimestamps):
# Removes duplicate intervals (start==end) # Removes duplicate intervals (start==end)
dupeClean = [] dupeClean = []
for timestampNo in range(len(csvTimestamps)-1): for timestampNo in range(len(csvTimestamps)):
if csvTimestamps[timestampNo] != csvTimestamps[timestampNo+1]: if csvTimestamps[timestampNo][0] != csvTimestamps[timestampNo][1]:
dupeClean.append(csvTimestamps[timestampNo]) dupeClean.append(csvTimestamps[timestampNo])
print(dupeClean) 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, # 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. # 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 # ffmpeg
.concat(*trims) # .concat(*trims)
.filter('setpts', expr='PTS-STARTPTS') # .filter('setpts', expr='PTS-STARTPTS')
.output(outputPath) # .output(outputPath)
.run(quiet=True) # .run(quiet=True)
) #)
def doesGroundTruthExist(groundTruthInput): def doesGroundTruthExist(groundTruthInput):
for file in os.listdir(): for file in os.listdir():
...@@ -64,18 +65,23 @@ def readForCSV(inputVideoName): ...@@ -64,18 +65,23 @@ def readForCSV(inputVideoName):
videoFound = True videoFound = True
with open(file) as csvFile: with open(file) as csvFile:
reader = csv.reader(csvFile, delimiter=",") reader = csv.reader(csvFile, delimiter=",")
firstRow = next(reader)
# Calculates Jaccard index if ground truth file exists.
if doesGroundTruthExist(groundTruthName): if doesGroundTruthExist(groundTruthName):
# Log instead of print # Log instead of print
print("Ground truth file found for video {video}, calculating success rate:".format(video=inputVideoName)) print("Ground truth file found for video {video}, calculating success rate:".format(video=inputVideoName))
with open(groundTruthName) as truthFile: with open(groundTruthName) as truthFile:
truthReader = csv.reader(truthFile, delimiter=",") truthReader = csv.reader(truthFile, delimiter=",")
# Calculates Jaccard index if ground truth file exists. # Cast whole csv input as a list of integers.
Jaccard.calculateSuccess(firstRow, next(truthReader)) obsList = [[int(i) for i in interval] for interval in list(reader)]
# Reads first row only. trueList = [[int(i) for i in interval] for interval in list(truthReader)]
trim(inputVideoName, firstRow) Jaccard.calculateSuccess( obsList, trueList )
#trim(inputVideoName, list(reader))
print("Done, trimmed file in output folder.")
break break
if not videoFound: if not videoFound:
# Log instead of print. # Log instead of print.
print("Input video file found, but no .csv file generated or found.") 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