-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVideostofile.py
86 lines (72 loc) · 3.09 KB
/
Videostofile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#Create a folder with image files from the video.
#Usage: Videotofile.py videopath imagefolder
#imports:
import time, sys, os
import cv2
def CreateVideoimages(videopath, imagefolder,orb):
print ("capuring video")
cap = cv2.VideoCapture(videopath) #capture the videofile
#cb = CvBridge()
# for finding cv version
#(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
#print major_ver
fps = cap.get(cv2.CAP_PROP_FPS) #get the fps of the videofile
if fps != fps or fps <=1e-2:
print ("Warning: can't get fps")
else:
print ("fps achieved succesfull: fps = "+ str(fps))
#start a Window to show the prossesed images in
cv2.startWindowThread()
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
#create the imagefolder if it doesnt exists
if not os.path.exists(imagefolder):
os.makedirs(imagefolder)
#If nessesary, open the rgb.txt file for Writing
if orb:
print ("Opening textfile")
rgbtxtloc = imagefolder[:-3] + "/rgb.txt"
textfile = open(rgbtxtloc,'w')
print ("Starting creating imgs")
newframeavailable = True #boolean if there are new frames available
frame_id = 0
#loop through all the frames in the videofile
while(newframeavailable): #as long as there are new frames availabe
timestamp = cap.get(cv2.CAP_PROP_POS_MSEC) #get the current timestamp
newframeavailable, frame = cap.read() #get new frame if availabe and update bool
if not newframeavailable: #If there are now new frames
break #stop the while loop
#There are new frames
frame_id += 1 #create a new frame id
imagename = imagefolder+ "/" + str(timestamp/1000)+".png"
written = cv2.imwrite(imagename , frame)
if not written:
print ("Writing frame number " + str(frame_id) + " failed")
if orb: #write new line in rgb.txt
textfile.write(str(timestamp/1000) + " rgb/"+str(timestamp/1000)+".png\n")
cv2.imshow('img',frame) #show the prossesed frame
cv2.waitKey(1)
#All frames have been prossesed
print ("Done creating images, closing up...")
cv2.destroyAllWindows() #close the window
cap.release() #close the capture video
if orb: #close the text file
textfile.close()
if len(sys.argv) == 3: #if the user has provided enough arguments
#extract the arguments
videopath = sys.argv[1]
imagefolder = sys.argv[2]
#run the CreateVideoBag function
CreateVideoimages(videopath, imagefolder, False)
#voila
print ("Done")
elif len(sys.argv) == 4 and sys.argv[3] == "orb-slam2": # It has to be set up for ORB-SLAM2
#extract the arguments
videopath = sys.argv[1]
imagefolder = sys.argv[2] + "/rgb"
#run the CreateVideoBag function
CreateVideoimages(videopath, imagefolder, True)
#voila
print ("Done")
else:
#The user has not priveded the right amount of arguments, point to this
print ("Please supply two arguments as following: Videotofiles.py videopath imagefolder orb-slam2 \n When adding \"orb-slam2\", the nessary text file will also be created.")