Skip to content
This repository was archived by the owner on Aug 10, 2022. It is now read-only.

Commit 76638d7

Browse files
authored
added barebones GUI interface
1 parent 15feafc commit 76638d7

File tree

1 file changed

+50
-14
lines changed

1 file changed

+50
-14
lines changed

opticalFlow/farnebackOpticalFlow.py

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,43 @@
55
Right now, in the result visualization, the intensity of a pixel's motion will change both it's color and magnitude.
66
Brighter pixels have more motion.
77
The output visualization is stored in the same location as the input video with the name <input_vid_filename>_FB_FLOW.mp4
8-
98
The idea is that perhaps the data about how certain pixels/features are moving across the screen could be used to figure out how the player camera / aim was changing.
109
"""
1110

11+
from tkinter import *
12+
from tkinter import filedialog
13+
from tkinter.messagebox import showinfo
1214
import numpy as np
1315
import cv2 as cv
1416

17+
# GUI FILE BROWSER------------------------------------------------------------
18+
19+
window = Tk()
20+
window.geometry('300x150') # sets the size of the GUI window
21+
window.title('Select a Video File') # creates a title for the window
22+
23+
# function allowing you to find/select video in GUI
24+
def get_file_path():
25+
global file_path
26+
# Open and return file path
27+
file_path = filedialog.askopenfilename(title = "Select a Video File", filetypes = (("mp4", "*.mp4"), ("mov files", "*.mov") ,("wmv", "*.wmv"), ("avi", "*.avi")))
28+
showinfo(title='Selected File', message=file_path)
29+
30+
# function allowing you to select the output path in the GUI
31+
def output():
32+
global outpath
33+
outpath = filedialog.asksaveasfilename(filetypes=[("mp4", '*.mp4')])
34+
window.destroy()
35+
36+
# Creating a button to search for the input file and to select the output destinatio and file name
37+
b1 = Button(window, text = 'Open a File', command = get_file_path).pack()
38+
b2 = Button(window, text = 'Save File Name', command = output).pack()
39+
window.mainloop()
40+
1541
# PARAMETERS--------------------------------
1642

1743
# path to input video file
18-
vidpath = r""
44+
vidpath = file_path
1945

2046
# do you want to save the output video?
2147
savevid = True
@@ -44,11 +70,15 @@
4470
# create black result image
4571
hsv_img = np.zeros_like(old_frame)
4672
hsv_img[...,1] = 255
73+
# get features from first frame
74+
print(f"\nRunning farneback Optical Flow on: {vidpath}")
4775

4876
# if saving video
4977
if savevid:
5078
# path to save output video
51-
savepath = vidpath.split('.')[0] + '_FB_FLOW' + '.mp4'
79+
filename = outpath
80+
savepath = filename + '_FB_FLOW' + '.mp4'
81+
print(f"Saving Output video to: {savepath}")
5282

5383
# get shape of video frames
5484
height, width, channels = old_frame.shape
@@ -57,33 +87,36 @@
5787
fourcc = cv.VideoWriter_fourcc(*'mp4v')
5888
videoOut = cv.VideoWriter(savepath, fourcc, fps, (width, height))
5989

90+
6091
# PROCESS VIDEO ---------------------------
6192
while(True):
6293
# get frame and convert to grayscale
6394
_, new_frame = cap.read()
64-
new_frame_gray = cv.cvtColor(new_frame, cv.COLOR_BGR2GRAY)
95+
if _:
96+
new_frame_gray = cv.cvtColor(new_frame, cv.COLOR_BGR2GRAY)
6597

6698
# do Farneback optical flow
67-
flow = cv.calcOpticalFlowFarneback(old_frame_gray, new_frame_gray, None, pyr_scale, levels, winsize, iterations, poly_n, poly_sigma, flags)
99+
flow = cv.calcOpticalFlowFarneback(old_frame_gray, new_frame_gray, None, pyr_scale, levels, winsize, iterations, poly_n, poly_sigma, flags)
68100

69101
# conversion
70-
mag, ang = cv.cartToPolar(flow[...,0], flow[...,1])
102+
mag, ang = cv.cartToPolar(flow[...,0], flow[...,1])
71103

72104
# draw onto the result image - color is determined by direction, brightness is by magnitude of motion
73105
#hsv_img[...,0] = ang*180/np.pi/2
74106
#hsv_img[...,2] = cv.normalize(mag, None, 0, 255, cv.NORM_MINMAX)
75107

76108
# color and brightness by magnitude
77-
hsv_img[...,0] = cv.normalize(mag, None, 0, 255, cv.NORM_MINMAX)
78-
hsv_img[...,1] = cv.normalize(mag, None, 0, 255, cv.NORM_MINMAX)
79-
hsv_img[...,2] = cv.normalize(mag, None, 0, 255, cv.NORM_MINMAX)
109+
hsv_img[...,0] = cv.normalize(mag, None, 0, 255, cv.NORM_MINMAX)
110+
hsv_img[...,1] = cv.normalize(mag, None, 0, 255, cv.NORM_MINMAX)
111+
hsv_img[...,2] = cv.normalize(mag, None, 0, 255, cv.NORM_MINMAX)
80112

81-
bgr_img = cv.cvtColor(hsv_img, cv.COLOR_HSV2BGR)
113+
bgr_img = cv.cvtColor(hsv_img, cv.COLOR_HSV2BGR)
82114

83115
# show the image and break out if ESC pressed
84-
cv.imshow('Farneback Optical Flow', bgr_img)
85-
k = cv.waitKey(30) & 0xff
86-
if k == 27:
116+
cv.imshow('Farneback Optical Flow', bgr_img)
117+
k = cv.waitKey(30) & 0xff
118+
else:
119+
k == 27
87120
break
88121

89122
# write frames to new output video
@@ -95,4 +128,7 @@
95128

96129
# cleanup
97130
videoOut.release()
98-
cv.destroyAllWindows()
131+
cv.destroyAllWindows()
132+
133+
# after video is finished
134+
print('\nComplete!\n')

0 commit comments

Comments
 (0)