()
| 19 | |
| 20 | |
| 21 | def main(): |
| 22 | # ---===--- Get the filename --- # |
| 23 | filename = sg.popup_get_file('Filename to play') |
| 24 | if filename is None: |
| 25 | return |
| 26 | vidFile = cv.VideoCapture(filename) |
| 27 | # ---===--- Get some Stats --- # |
| 28 | num_frames = vidFile.get(cv.CAP_PROP_FRAME_COUNT) |
| 29 | fps = vidFile.get(cv.CAP_PROP_FPS) |
| 30 | |
| 31 | sg.theme('Black') |
| 32 | |
| 33 | # ---===--- define the window layout --- # |
| 34 | layout = [[sg.Text('OpenCV Demo', size=(15, 1), font='Helvetica 20')], |
| 35 | [sg.Image(key='-IMAGE-')], |
| 36 | [sg.Slider(range=(0, num_frames), size=(60, 10), orientation='h', key='-SLIDER-')], |
| 37 | [sg.Push(), sg.Button('Exit', font='Helvetica 14')]] |
| 38 | |
| 39 | # create the window and show it without the plot |
| 40 | window = sg.Window('Demo Application - OpenCV Integration', layout, no_titlebar=False, location=(0, 0)) |
| 41 | |
| 42 | # locate the elements we'll be updating. Does the search only 1 time |
| 43 | image_elem = window['-IMAGE-'] |
| 44 | slider_elem = window['-SLIDER-'] |
| 45 | timeout = 1000//fps # time in ms to use for window reads |
| 46 | |
| 47 | # ---===--- LOOP through video file by frame --- # |
| 48 | cur_frame = 0 |
| 49 | while vidFile.isOpened(): |
| 50 | event, values = window.read(timeout=timeout) |
| 51 | if event in ('Exit', None): |
| 52 | break |
| 53 | ret, frame = vidFile.read() |
| 54 | if not ret: # if out of data stop looping |
| 55 | break |
| 56 | # if someone moved the slider manually, the jump to that frame |
| 57 | if int(values['-SLIDER-']) != cur_frame-1: |
| 58 | cur_frame = int(values['-SLIDER-']) |
| 59 | vidFile.set(cv.CAP_PROP_POS_FRAMES, cur_frame) |
| 60 | slider_elem.update(cur_frame) |
| 61 | cur_frame += 1 |
| 62 | |
| 63 | imgbytes = cv.imencode('.ppm', frame)[1].tobytes() # can also use png. ppm found to be more efficient |
| 64 | image_elem.update(data=imgbytes) |
| 65 | |
| 66 | main() |
| 67 |
no test coverage detected