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

Added functionality to dataset generator; now works with multiple videos, and...

Added functionality to dataset generator; now works with multiple videos, and extracts a given percentage per video.
parent b0203a19
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,10 @@ import numpy as np ...@@ -5,7 +5,10 @@ import numpy as np
import sys import sys
from pathlib import Path from pathlib import Path
setFPS = 2 # Skips every 1/n frames, according to this ratio.
# Maybe do random frame n*frameCount instead?
percentExtractedPerVideo = 0.3
firstNvideos = 6
resizeWidth = 480 resizeWidth = 480
resizeHeight = 270 resizeHeight = 270
...@@ -13,11 +16,14 @@ groundsPath = "src/python/groundTruth/" ...@@ -13,11 +16,14 @@ groundsPath = "src/python/groundTruth/"
### Generates a dataset of labelled images from one video (soon a whole folder) by comparing to ground truth. ### Generates a dataset of labelled images from one video (soon a whole folder) by comparing to ground truth.
### Takes about 5 minutes for a 30 minute input. ### Takes about 5 minutes for a 30 minute input.
def generateDatasetFromFrames(inputVideoPath, desiredFPS): def generateDatasetFromFrames(inputVideosPath, extractionRatio):
decoder = cv2.VideoCapture(inputVideoPath) for i in range(firstNvideos):
inputVideo = Path(inputVideoPath).name curVideo = os.listdir(inputVideosPath)[i]
decoder = cv2.VideoCapture(inputVideosPath + curVideo)
sourceNoExt = os.path.splitext(inputVideo)[0] desiredFPS = int(cv2.CAP_PROP_FPS * extractionRatio)
sourceNoExt = os.path.splitext(curVideo)[0]
groundTruthName = "groundTruth_" + sourceNoExt + ".csv" groundTruthName = "groundTruth_" + sourceNoExt + ".csv"
# Looks for ground truth csv. # Looks for ground truth csv.
...@@ -29,8 +35,8 @@ def generateDatasetFromFrames(inputVideoPath, desiredFPS): ...@@ -29,8 +35,8 @@ def generateDatasetFromFrames(inputVideoPath, desiredFPS):
break break
if groundFound == False: if groundFound == False:
print("No ground truth file found to label from.") print("No ground truth file found to label from, for file {file}.\n".format(file = curVideo))
return False continue
step = int(decoder.get(cv2.CAP_PROP_FPS) / desiredFPS) step = int(decoder.get(cv2.CAP_PROP_FPS) / desiredFPS)
try: try:
...@@ -60,15 +66,16 @@ def generateDatasetFromFrames(inputVideoPath, desiredFPS): ...@@ -60,15 +66,16 @@ def generateDatasetFromFrames(inputVideoPath, desiredFPS):
groundTruthCSV.seek(0) groundTruthCSV.seek(0)
if found == False: if found == False:
cv2.imwrite("dataset/noFish/" + "frame{number}{source}.jpg".format(number=frameNo, source=sourceNoExt), frame) cv2.imwrite("dataset/noFish/" + "frame{number}{source}.jpg".format(number=frameNo, source=sourceNoExt), frame)
print("Frames saved as labelled images in dataset folder.") print("Frames saved as labelled images in dataset folder.\n")
return True except:
print("Something went wrong in reading file {file}".format(file = curVideo))
finally: finally:
decoder.release() decoder.release()
groundTruthCSV.close() groundTruthCSV.close()
def main(video,fps): def main(video,fps):
print(generateDatasetFromFrames(video,fps)) generateDatasetFromFrames(video,fps)
if __name__ == "__main__": if __name__ == "__main__":
main("sourceVideos/Myggbukta-[2021-05-21_10-47-06]-408.mp4", setFPS) main("sourceVideos/", percentExtractedPerVideo)
\ No newline at end of file \ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment