-
Notifications
You must be signed in to change notification settings - Fork 0
/
Organize.py
158 lines (127 loc) · 5.71 KB
/
Organize.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import os
import sys
import extras
import cv2
import cv2.cv as cv
import numpy as np
class Organizer(object):
FOURCC = cv2.cv.CV_FOURCC(*'XVID')
quitList = ['q', 'Q', 'quit', 'Quit']
def __init__(self, File_Saving_Dict):
#Initialize Dictionary
self.File_Saving_Dict = File_Saving_Dict
print " " #Makes things look nicer
def getInfo(self):
date,trialNum = updateInfo(self.File_Saving_Dict)
self.File_Saving_Dict["Date"] = date
self.File_Saving_Dict["Trial_Number"] = trialNum
def getSaveFrameCount(self):
while True:
input = extras.getUserInput("Frames per Video:")
if input in self.quitList:
return False
try:
self.File_Saving_Dict["Frames_In_Saved_Video"] = int(input)
return True
except:
print "Please enter a valid response. Enter 'q' to quit."
return False
def saveFiles(self):
#Find all desired PG videos
dir = os.listdir(self.File_Saving_Dict["File_PG_Initial_Save_Location"])
length = len(self.File_Saving_Dict["PG_Initial_SaveName"])
for file in dir:
if not (file[:length] == self.File_Saving_Dict["PG_Initial_SaveName"]):
dir.remove(file)
numMoviesTotal = len(dir)
movieCounter = 1
file = dir[movieCounter-1]
cap = cv2.VideoCapture(os.path.join(self.File_Saving_Dict["File_PG_Initial_Save_Location"],file))
width = int(cap.get(3))
height = int(cap.get(4))
numFramesInMovie = int(cap.get(cv.CV_CAP_PROP_FRAME_COUNT))
fps = int(cap.get(cv.CV_CAP_PROP_FPS))
frameCounter = 1
vidNum = 1
out = cv2.VideoWriter(os.path.join(self.File_Saving_Dict["File_Complete_Target_Location"],self.File_Saving_Dict["PG_Target_SaveName"]+'%i.avi'%vidNum),self.FOURCC, fps, (width,height))
f, img = cap.read()
while (True):
cv2.waitKey(1)
f, img = cap.read()
out.write(img)
if (frameCounter == numFramesInMovie):
cap.release()
if not (movieCounter==numMoviesTotal):
file = dir[movieCounter-1]
cap = cv2.VideoCapture(os.path.join(self.File_Saving_Dict["File_PG_Initial_Save_Location"],file))
numFramesInMovie += int(cap.get(cv.CV_CAP_PROP_FRAME_COUNT))
movieCounter += 1
else:
break
if (frameCounter % self.File_Saving_Dict["Frames_In_Saved_Video"] == 0):
out.release()
vidNum +=1
out = cv2.VideoWriter(os.path.join(self.File_Saving_Dict["File_Complete_Target_Location"],self.File_Saving_Dict["PG_Target_SaveName"]+'%i.avi'%vidNum),self.FOURCC, fps, (width,height))
frameCounter += 1
def syncFiles(self):
dir = os.listdir(self.File_Saving_Dict["File_Complete_Target_Location"])
length = len(self.File_Saving_Dict["PS3_Target_SaveName"])
for file in dir:
if not (file[:length] == self.File_Saving_Dict["PS3_Target_SaveName"]):
dir.remove(file)
for videoNum in enumerate(dir):
vidNum = videoNum[0] + 1
if not os.path.isfile(os.path.join(self.File_Saving_Dict["File_Complete_Target_Location"],self.File_Saving_Dict["PS3_Target_SaveName"]+'%i.avi'%vidNum)):
break
cap1 = cv2.VideoCapture(os.path.join(self.File_Saving_Dict["File_Complete_Target_Location"],self.File_Saving_Dict["PS3_Target_SaveName"]+'%i.avi'%vidNum))
cap2 = cv2.VideoCapture(os.path.join(self.File_Saving_Dict["File_Complete_Target_Location"],self.File_Saving_Dict["PG_Target_SaveName"]+'%i.avi'%vidNum))
width1 = int(cap1.get(3))
width2 = int(cap2.get(3))
height1 = int(cap1.get(4))
height2 = int(cap2.get(4))
frames_in_video1 = int(cap1.get(cv.CV_CAP_PROP_FRAME_COUNT))
frames_in_video2 = int(cap2.get(cv.CV_CAP_PROP_FRAME_COUNT))
fps1 = int(cap1.get(cv.CV_CAP_PROP_FPS))
fps2 = int(cap2.get(cv.CV_CAP_PROP_FPS))
timeTotal = min(((float(frames_in_video1)/float(fps1)),(float(frames_in_video2)/float(fps2))))
fps = np.max((fps1,fps2))
framesTotal = int(timeTotal*fps)
fps_ratio = (fps1/fps2)
slower_camera_index = 1 #Camera index is 0 or 1 where 0 is cap1 and 1 is cap2
if fps_ratio < 1:
fps_ratio = 1/fps_ratio
slower_camera_index = 0
faster_camera_index = 1
smaller_resolution_index = 0
if (width2*height2 < width1*height1):
smaller_resolution_index = 1
Height = max(height1,height2)
Width = (width1 + width2)
camera1 = {"cap":cap1,"width":width1,"height":height1,"frames":frames_in_video1,"fps":fps1}
camera2 = {"cap":cap2,"width":width2,"height":height2,"frames":frames_in_video2,"fps":fps2}
camera_list = (camera1,camera2)
out = cv2.VideoWriter(os.path.join(self.File_Saving_Dict["File_Complete_Target_Location"],'Synchronized_Vid%i'%vidNum+'.avi'),self.FOURCC, fps, (Width,Height),0)
image = np.ones((Height,Width))
for n in range(framesTotal-2):
cv2.waitKey(1)
if n%fps_ratio < 1:
if (slower_camera_index == smaller_resolution_index):
f1, img_small = camera_list[slower_camera_index]["cap"].read()
img_small = cv2.cvtColor(img_small, cv2.COLOR_BGR2GRAY)
else:
f1, img_large = camera_list[slower_camera_index]["cap"].read()
img_large = cv2.cvtColor(img_large, cv2.COLOR_BGR2GRAY)
if (slower_camera_index == smaller_resolution_index):
f1, img_large = camera_list[(slower_camera_index+1)%2]["cap"].read()
img_large = cv2.cvtColor(img_large, cv2.COLOR_BGR2GRAY)
else:
f1, img_small = camera_list[(slower_camera_index+1)%2]["cap"].read()
img_small = cv2.cvtColor(img_small, cv2.COLOR_BGR2GRAY)
image[:camera_list[(smaller_resolution_index+1)%2]["height"], :camera_list[(smaller_resolution_index+1)%2]["width"]] = img_large
image[:camera_list[smaller_resolution_index]["height"], camera_list[(smaller_resolution_index+1)%2]["width"]:Width] = img_small
image = image.astype(np.uint8)
out.write(image)
cap1.release()
cap2.release()
out.release()
#cv2.destroyAllWindows()