Skip to content

Commit d47cca3

Browse files
author
Vaibhav
committed
Better structure
1 parent 89bea2f commit d47cca3

File tree

12 files changed

+36
-16
lines changed

12 files changed

+36
-16
lines changed

Readme.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# VideoStabilization
1+
# VideoStabilization
22

33
## Description
44

@@ -20,13 +20,16 @@ Finally a cropped region of the video is outputted where the cropped region box
2020

2121
## How To
2222

23-
* Run the webapp:
24-
```console
25-
bar@foo:~/VideoStabilization$ python3 preproc.py <video_file>
26-
bar@foo:~/VideoStabilization$ python3 stabilize.py
27-
bar@foo:~/VideoStabilization$ python3 generate.py <video_file>
23+
* Run:
24+
25+
```bash
26+
bar@foo:~/VideoStabilization$ python3 src/preproc.py <video_file_path>
27+
bar@foo:~/VideoStabilization$ python3 src/stabilize.py <Displacement_threshold_pixels>
28+
bar@foo:~/VideoStabilization$ python3 src/generate.py <video_file_path> <Displacement_threshold_pixels>
2829
```
2930

31+
Note: This will create some temporary pickle files, please ignore them.
32+
3033
## Built With
3134

3235
* [Python3](https://www.python.org/download/releases/3.0/)

sampleVideos/vid1.mp4

4.13 MB
Binary file not shown.
File renamed without changes.
File renamed without changes.

shakey.mp4

-18.6 MB
Binary file not shown.

smoothTrajectory.pkl

18.7 KB
Binary file not shown.
File renamed without changes.

preproc.py renamed to src/preproc.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55

66
sift = cv2.xfeatures2d.SIFT_create(200)
7+
78
def getAffMat(I1, I2):
89
I1 = cv2.cvtColor(I1, cv2.COLOR_BGR2GRAY)
910
I2 = cv2.cvtColor(I2, cv2.COLOR_BGR2GRAY)
@@ -15,12 +16,12 @@ def getAffMat(I1, I2):
1516
# Finding good matches using ratio testing
1617
bf = cv2.BFMatcher()
1718
matches = bf.knnMatch(desc1, desc2, k=2)
18-
print('Before filtering->', len(matches))
19+
1920
good = []
2021
for m,n in matches:
2122
if m.distance < 0.7*n.distance:
2223
good.append(m)
23-
print('After filtering->', len(good))
24+
2425
pts_src = []
2526
pts_dst = []
2627
for i in range(len(good)):
@@ -35,27 +36,37 @@ def getAffMat(I1, I2):
3536

3637
v = cv2.VideoCapture(sys.argv[1])
3738
n_frames = int(v.get(cv2.CAP_PROP_FRAME_COUNT))
39+
3840
# Generating the Xdata and Ydata
3941
transforms = [[], [], [], []]
4042
count = 0
4143
while v.isOpened():
44+
4245
ret, frame = v.read()
46+
4347
if ret == True:
48+
4449
if count > 0:
4550
transMat = getAffMat(prev, frame)
46-
transforms[0].append(transMat[0][2])
47-
transforms[1].append(transMat[1][2])
48-
transforms[2].append(np.arctan2(transMat[1][0], transMat[0][0]))
49-
transforms[3].append(np.sqrt(transMat[1][0]**2 + transMat[0][0]**2))
50-
51+
try:
52+
transforms[0].append(transMat[0][2])
53+
transforms[1].append(transMat[1][2])
54+
transforms[2].append(np.arctan2(transMat[1][0], transMat[0][0]))
55+
transforms[3].append(np.sqrt(transMat[1][0]**2 + transMat[0][0]**2))
56+
except:
57+
transforms[0].append(0)
58+
transforms[1].append(0)
59+
transforms[2].append(0)
60+
transforms[3].append(1)
61+
5162
count += 1
5263
prev = frame
53-
print((count/n_frames)*100, '%')
64+
print( str((count/n_frames)*100) + "% completed")
5465
else:
5566
break
5667

5768
v.release()
5869

5970
# Storing the data
6071
with open('transforms.pkl', 'wb') as f:
61-
pickle.dump(transforms, f)
72+
pickle.dump(transforms, f)

stabilize.py renamed to src/stabilize.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,27 @@
5151
obj += lbd3*(cp.abs(fx[i+3]-3*fx[i+2] + 3*fx[i+1]-fx[i]) + cp.abs(fy[i+3]-3*fy[i+2]+3*fy[i+1]-fy[i]) + cp.abs(fth[i+3]-3*fth[i+2]+3*fth[i+1]-fth[i]) + cp.abs(fs[i+3]-3*fs[i+2]+3*fs[i+1]-fs[i]))
5252

5353
prob = cp.Problem(cp.Minimize(obj), constraints)
54-
prob.solve()
54+
print("Started Solving the optimization using ECOS")
55+
prob.solve(solver=cp.ECOS)
56+
print("Optimization solved")
5557

5658
# Results
5759
plt.subplot(2, 2, 1)
5860
plt.plot(trajectory[0])
5961
plt.plot(fx.value)
62+
plt.title("Horizontal trajectory")
6063
plt.subplot(2, 2, 2)
6164
plt.plot(trajectory[1])
6265
plt.plot(fy.value)
66+
plt.title("Verical trajectory")
6367
plt.subplot(2, 2, 3)
6468
plt.plot(trajectory[2])
6569
plt.plot(fth.value)
70+
plt.title("Angle of rotn. trajectory")
6671
plt.subplot(2, 2, 4)
6772
plt.plot(trajectory[3])
6873
plt.plot(fs.value)
74+
plt.title("Scale trajectory")
6975
plt.show()
7076

7177
smoothTrajectory = np.array([fx.value, fy.value, fth.value, fs.value])

trajectory.pkl

18.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)