Skip to content

Commit c115536

Browse files
authoredApr 2, 2020
Update analytics.py
1 parent 18a8134 commit c115536

File tree

1 file changed

+8
-89
lines changed

1 file changed

+8
-89
lines changed
 

‎analytics.py

+8-89
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@
55
import pickle
66
import os
77
import shutil
8-
from zipfile import ZipFile
9-
10-
def zipdir(path, ziph): #creates zip file
11-
# ziph is zipfile handle
12-
for root, dirs, files in os.walk(path):
13-
for file in files:
14-
ziph.write(os.path.join(root, file))
158

169
def plot(x, y, name, ylabel="residualMetric"): # plot metric on graph
1710
assert len(x) == len(y)
@@ -21,7 +14,7 @@ def plot(x, y, name, ylabel="residualMetric"): # plot metric on graph
2114
ax.plot(x, y, label=f'$x = Frame, $y = {ylabel}')
2215
plt.title(name)
2316
ax.legend()
24-
fig.savefig(f'PLOT2/{name}_plot.png')
17+
fig.savefig(f'PLOT/{name}_plot.png')
2518

2619
def graphSession(filepath, pframeSet=300, countMax = 1000, skip=0, process=["residualMetric"], meanMetricSize=10): # create graph of mean compounded residualMetric
2720
"""
@@ -30,23 +23,17 @@ def graphSession(filepath, pframeSet=300, countMax = 1000, skip=0, process=["res
3023
pframeSet: number of predicted frames every i-frame
3124
countMax: number of data points on the graphs
3225
skip: how many frames to skip per frame
33-
process: 3 Parts, process 1 = residualMetric, process 2 = sizeMetric, process 3 = meanMetric
26+
process: 3 Parts, process 1 = residualMetric, process 2 = meanMetric
3427
Process 1: create graph of residualMetric per I-Frame interval
35-
36-
Process 2: Graph the data size of zip file containing the I-Frame and Residual Frame to
37-
analyze data savings across a video.
38-
39-
Process 3: create graph of mean compounded residualMetric for x Frames specified by meanMetricSize
28+
Process 2: create graph of mean compounded residualMetric for x Frames specified by meanMetricSize
4029
"""
41-
assert "residualMetric" in process or "sizeMetric" in process or "meanMetric" in process
30+
assert "residualMetric" in process or "meanMetric" in process
4231

4332
video_name, video, frame_width, frame_height, videofps, videoframecount = read_video(filepath)
44-
print(f"FPS: {videofps}")
4533
running = True
4634
count = 0
4735

4836
residualTally = []
49-
sizeTally = []
5037
meanTally = []
5138
meanBuffer = []
5239

@@ -63,32 +50,12 @@ def graphSession(filepath, pframeSet=300, countMax = 1000, skip=0, process=["res
6350
if "residualMetric" in process:
6451
residualTally.append(residualMetric)
6552

66-
if "sizeMetric" in process:
67-
68-
if os.path.exists('sizeBuffer'):
69-
shutil.rmtree("sizeBuffer")
70-
os.mkdir('sizeBuffer')
71-
72-
cv2.imwrite(os.path.join("sizeBuffer", 'iframe.png'), iframe)
73-
cv2.imwrite(os.path.join("sizeBuffer", 'residual.png'), residualFrame)
74-
75-
if os.path.exists('sizeBuffer.zip'):
76-
os.remove("sizeBuffer.zip")
77-
78-
zipObj = ZipFile('sizeBuffer.zip', 'w') # create a ZipFile object
79-
zipObj.write(os.path.join("sizeBuffer", 'iframe.png')) # Add multiple files to the zip
80-
zipObj.write(os.path.join("sizeBuffer", 'residual.png'))
81-
zipObj.close() # close the Zip File
82-
83-
sizeTally.append(os.path.getsize('sizeBuffer.zip'))
84-
8553
if "meanMetric" in process:
8654
meanBuffer.append(residualMetric)
8755
if len(meanBuffer) == meanMetricSize:
8856
meanTally.append(sum(meanBuffer)/meanMetricSize)
8957
meanBuffer.pop(0)
9058

91-
9259
running, frame = read_frame(video)
9360
count+=1
9461

@@ -99,70 +66,22 @@ def graphSession(filepath, pframeSet=300, countMax = 1000, skip=0, process=["res
9966
if "residualMetric" in process:
10067
plot(metricRange, residualTally, f"residual_{video_name}_{pframeSet}predF{skip}skipF", ylabel="residualMetric")
10168

102-
if "sizeMetric" in process:
103-
plot(metricRange, sizeTally, f"size_{video_name}_{pframeSet}predF{skip}skipF", ylabel=" zipped [residualFrame, iFrame] Size")
104-
10569
if "meanMetric" in process:
10670
meanMetricRange = np.arange(len(meanTally))
10771
plot(meanMetricRange, meanTally, f"mean_{video_name}_{pframeSet}predF{skip}skipF{meanMetricSize}meanSize", ylabel=f"{meanMetricSize} Frame Mean Compounded residualMetric")
10872

109-
return residualTally, sizeTally, meanTally
110-
111-
### TESTS ####
112-
113-
def test1(): #analyze 4 examples
114-
imagePath = "testImages"
115-
outPath = "OUTPUT"
116-
117-
setsToTest = [("personFrame1.png", "personFrame2.png", "personFrameOutput"),
118-
("personFrameLong1.png", "personFrameLong2.png", "personFrameLongOutput"),
119-
("carFrame1.png", "carFrame2.png", "carFrameOutput"),
120-
("carFrameLong1.png", "carFrameLong2.png", "carFrameLongOutput")
121-
]
122-
123-
for pairs in setsToTest:
124-
anchorPath = f"{imagePath}/{pairs[0]}"
125-
targetPath = f"{imagePath}/{pairs[1]}"
126-
127-
print("Pair", pairs)
128-
timer = time.time()
129-
130-
main(anchorPath, targetPath, f"{outPath}/{pairs[2]}", saveOutput=True)
131-
totTime = time.time() - timer
132-
print(f"Done, executed in {totTime:.2f} s\n")
133-
134-
def test2(filepath, pframeSet=300, countMax = 1000, skip=0, process=["residualMetric"]): # create graph of residualMetric per I-Frame interval
135-
return graphSession(filepath, pframeSet, countMax, skip, process)
136-
137-
def test3(filepath, pframeSet=300, countMax = 1000, skip=0, process=["pickleMetric"]): # create graph of residualMetric + I-frame size
138-
return graphSession(filepath, pframeSet, countMax, skip, process)
139-
140-
def test4(filepath, pframeSet=300, countMax = 1000, skip=0, process=["meanMetric"]): # create graph of residualMetric + I-frame size
141-
return graphSession(filepath, pframeSet, countMax, skip, process)
142-
143-
### TESTS ####
73+
return residualTally, meanTally
14474

14575
if __name__ == "__main__":
14676
timer = time.time()
14777

14878
"""
149-
videopathA = "/Users/gbanuru/PycharmProjects/BlockMatching/VIDEOS/UAV123_person1_resized360.mp4"
150-
videopathB = "/Users/gbanuru/PycharmProjects/BlockMatching/VIDEOS/UAV123_car2_resized360.mp4"
151-
videopathC = "/Users/gbanuru/PycharmProjects/BlockMatching/VIDEOS/CityWalk_resized360.mp4"
152-
videopathD = "/Users/gbanuru/PycharmProjects/BlockMatching/VIDEOS/HouseTour_resized360.mp4"
153-
79+
videopathA = "VIDEOS/UAV123_person1_resized360.mp4"
15480
PROCESS = ["meanMetric"]
155-
15681
graphSession(videopathA, pframeSet=5, process=PROCESS, meanMetricSize=5)
157-
graphSession(videopathB, pframeSet=5, process=PROCESS, meanMetricSize=5)
158-
graphSession(videopathC, pframeSet=5, process=PROCESS, meanMetricSize=5)
159-
graphSession(videopathD, pframeSet=5, process=PROCESS, meanMetricSize=5)
160-
16182
graphSession(videopathA, pframeSet=10, process=PROCESS, meanMetricSize=10)
162-
graphSession(videopathB, pframeSet=10, process=PROCESS, meanMetricSize=10)
163-
graphSession(videopathC, pframeSet=10, process=PROCESS, meanMetricSize=10)
164-
graphSession(videopathD, pframeSet=10, process=PROCESS, meanMetricSize=10)
83+
16584
"""
16685

16786
totTime = time.time() - timer
168-
print(f"Done, executed in {totTime:.2f} s")
87+
print(f"Done, executed in {totTime:.2f} s")

0 commit comments

Comments
 (0)
Please sign in to comment.