Spaces:
Sleeping
Sleeping
Irpan
commited on
Commit
·
c74983a
1
Parent(s):
aa618ec
add code
Browse files
camera_movement_estimator/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
camera_movement_estimator/__init__.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
from .camera_movement_estimator import CameraMovementEstimator
|
camera_movement_estimator/camera_movement_estimator.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pickle
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
import os
|
5 |
+
import sys
|
6 |
+
sys.path.append('../')
|
7 |
+
from utils import measure_distance,measure_xy_distance
|
8 |
+
|
9 |
+
class CameraMovementEstimator():
|
10 |
+
def __init__(self,frame):
|
11 |
+
self.minimum_distance = 5
|
12 |
+
|
13 |
+
self.lk_params = dict(
|
14 |
+
winSize = (15,15),
|
15 |
+
maxLevel = 2,
|
16 |
+
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,10,0.03)
|
17 |
+
)
|
18 |
+
|
19 |
+
first_frame_grayscale = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
|
20 |
+
mask_features = np.zeros_like(first_frame_grayscale)
|
21 |
+
mask_features[:,0:20] = 1
|
22 |
+
mask_features[:,900:1050] = 1
|
23 |
+
|
24 |
+
self.features = dict(
|
25 |
+
maxCorners = 100,
|
26 |
+
qualityLevel = 0.3,
|
27 |
+
minDistance =3,
|
28 |
+
blockSize = 7,
|
29 |
+
mask = mask_features
|
30 |
+
)
|
31 |
+
|
32 |
+
def add_adjust_positions_to_tracks(self,tracks, camera_movement_per_frame):
|
33 |
+
for object, object_tracks in tracks.items():
|
34 |
+
for frame_num, track in enumerate(object_tracks):
|
35 |
+
for track_id, track_info in track.items():
|
36 |
+
position = track_info['position']
|
37 |
+
camera_movement = camera_movement_per_frame[frame_num]
|
38 |
+
position_adjusted = (position[0]-camera_movement[0],position[1]-camera_movement[1])
|
39 |
+
tracks[object][frame_num][track_id]['position_adjusted'] = position_adjusted
|
40 |
+
|
41 |
+
|
42 |
+
|
43 |
+
def get_camera_movement(self,frames,read_from_stub=False, stub_path=None):
|
44 |
+
# Read the stub
|
45 |
+
if read_from_stub and stub_path is not None and os.path.exists(stub_path):
|
46 |
+
with open(stub_path,'rb') as f:
|
47 |
+
return pickle.load(f)
|
48 |
+
|
49 |
+
camera_movement = [[0,0]]*len(frames)
|
50 |
+
|
51 |
+
old_gray = cv2.cvtColor(frames[0],cv2.COLOR_BGR2GRAY)
|
52 |
+
old_features = cv2.goodFeaturesToTrack(old_gray,**self.features)
|
53 |
+
|
54 |
+
for frame_num in range(1,len(frames)):
|
55 |
+
frame_gray = cv2.cvtColor(frames[frame_num],cv2.COLOR_BGR2GRAY)
|
56 |
+
new_features, _,_ = cv2.calcOpticalFlowPyrLK(old_gray,frame_gray,old_features,None,**self.lk_params)
|
57 |
+
|
58 |
+
max_distance = 0
|
59 |
+
camera_movement_x, camera_movement_y = 0,0
|
60 |
+
|
61 |
+
for i, (new,old) in enumerate(zip(new_features,old_features)):
|
62 |
+
new_features_point = new.ravel()
|
63 |
+
old_features_point = old.ravel()
|
64 |
+
|
65 |
+
distance = measure_distance(new_features_point,old_features_point)
|
66 |
+
if distance>max_distance:
|
67 |
+
max_distance = distance
|
68 |
+
camera_movement_x,camera_movement_y = measure_xy_distance(old_features_point, new_features_point )
|
69 |
+
|
70 |
+
if max_distance > self.minimum_distance:
|
71 |
+
camera_movement[frame_num] = [camera_movement_x,camera_movement_y]
|
72 |
+
old_features = cv2.goodFeaturesToTrack(frame_gray,**self.features)
|
73 |
+
|
74 |
+
old_gray = frame_gray.copy()
|
75 |
+
|
76 |
+
if stub_path is not None:
|
77 |
+
with open(stub_path,'wb') as f:
|
78 |
+
pickle.dump(camera_movement,f)
|
79 |
+
|
80 |
+
return camera_movement
|
81 |
+
|
82 |
+
def draw_camera_movement(self,frames, camera_movement_per_frame):
|
83 |
+
output_frames=[]
|
84 |
+
|
85 |
+
for frame_num, frame in enumerate(frames):
|
86 |
+
frame= frame.copy()
|
87 |
+
|
88 |
+
overlay = frame.copy()
|
89 |
+
cv2.rectangle(overlay,(0,0),(500,100),(255,255,255),-1)
|
90 |
+
alpha =0.6
|
91 |
+
cv2.addWeighted(overlay,alpha,frame,1-alpha,0,frame)
|
92 |
+
|
93 |
+
x_movement, y_movement = camera_movement_per_frame[frame_num]
|
94 |
+
frame = cv2.putText(frame,f"Camera Movement X: {x_movement:.2f}",(10,30), cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,0),3)
|
95 |
+
frame = cv2.putText(frame,f"Camera Movement Y: {y_movement:.2f}",(10,60), cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,0),3)
|
96 |
+
|
97 |
+
output_frames.append(frame)
|
98 |
+
|
99 |
+
return output_frames
|