Skip to content

Commit

Permalink
Merge pull request #1 from vermavinay982/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
vermavinay982 authored Aug 28, 2021
2 parents a4e8bdc + 2035cc8 commit b6e5392
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 67 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ __pycache__/
*.py[cod]
*$py.class

#Media
*.mp4
*.jpg
*.avi

# C extensions
*.so

Expand Down
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ Its upto you what amazing you can do with this tool. Feel free to contribute and
It is a flexible library that provides you total flexibility.

# Axis Stacking
It has 3 axis - axis 0 - horizontal x axis - side by side videos
It has 3 axis
- axis 0 - horizontal x axis - side by side videos
- axis 1 - vertical y axis - top and bottom merge of videos
- axis 2 - that is back to back - video will be generated combining the 2 videos in time axis
- axis 3 - shuffle in the middle - one frame this another that - again first 1,2,1,2,1,2.....
- axis 3 - shuffle in the middle - one frame this another that - again first 1,2,1,2,1,2.....(Future Update)

The video path or the stream path is given as list
and based on the list - the precedence is decided for stacking the video together
Expand All @@ -42,7 +43,7 @@ and based on the list - the precedence is decided for stacking the video togethe
or the shorter video will be used to break the loop irrespective of longer video time
use_time_of = 0,1 whatever - that will be used to generate the video

3. if you want to fetch frames that are merged so you can do other things
3. If you want to fetch frames that are merged so you can do other things
that you can process the frames and use it at your end.


Expand All @@ -54,6 +55,26 @@ videos = ['archery.mp4','cycling.mp4']
stack_video(videos, axis=0)
```

## Advanced Way to Use
```python
path1 = '../../../archery.mp4'
path2 = '../../../cars.mp4'

videos = [path1, path2, path1, path2]
video_size = (300,300) # w/h
limit_video = 0 # video index of video that will decide to close streaming
video_path = 'test.mp4'

output_video = stack_video(
videos,
axis=0,
size=video_size,
limit_video=limit_video,
write_path=video_path,
writer_fps=None,
display=True)
```


- License: Proprietary
- Author: [Vinay Kumar Verma](mailto:vermavinay982@gmail.com)
Expand Down
41 changes: 0 additions & 41 deletions documentation

This file was deleted.

File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
with open('readme.md','r') as f:
long_desc = f.read()

with open('short_desc.txt','r') as f:
with open('documents/short_desc.txt','r') as f:
short_desc = f.read()

with open('requirements.txt','r') as f:
Expand All @@ -13,7 +13,7 @@

setup(
name='vigenpy',
version='0.1.3',
version='0.2.0',
description=short_desc,
license='Proprietary',
long_description=long_desc,
Expand Down
99 changes: 78 additions & 21 deletions vigenpy/video/stack_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,57 @@
__email__ = "vermavinay982@gmail.com"
__module_name__ = "[Stack Video]"

import numpy as np
import cv2
import os
import cv2
import time
import numpy as np
from tqdm import tqdm

def write_video(frame_list, video_name, resized_frame, writer_fps):
def write_video(frame_list, video_name, output_shape, writer_fps):
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
height, width = resized_frame.shape[:2]
height, width = output_shape

out = cv2.VideoWriter(video_name, fourcc, int(writer_fps), (width, height))
for encoded_frame in tqdm(window_frame_list):
for encoded_frame in tqdm(frame_list):
frame_encoded = np.frombuffer(encoded_frame, np.uint8)
frame = cv2.imdecode(frame_encoded, cv2.IMREAD_COLOR)
out.write(frame)
out.release()
return video_name

def stack_video(videos:list=[], axis:int=0)->str:
frame_list = list()
def stack_video(
videos:list=[],
axis:int=0,
size=(300,400),
limit_video=None,
write_path=None,
writer_fps=None,
display=False
)->str:
"""
stack_video
Write or display the video after stacking it
"""
if limit_video is None and write_path is not None:
print("Cant write an infinite video, Keep a limit video")
return None

frame_list = list() # storing video frames to write
for video in videos:
if not os.path.exists(video):
print(f'Video {video} Not There')
return None
else:
print(f'Video {video} Found')

caps = [cv2.VideoCapture(cam) for cam in videos]
size = (300,400)
fps = min([cap.get(cv2.CAP_PROP_FPS) for cap in caps])
print("Min FPS of Video is :",fps)
past_frame=[]
start = True
video_over = False

while True:
frames = []
for i,cap in enumerate(caps):
Expand All @@ -42,33 +66,66 @@ def stack_video(videos:list=[], axis:int=0)->str:
caps[i] = cv2.VideoCapture(videos[i])
print('Video Ended, RESTARTING',videos[i])
frame = past_frame[i]
# continue
if i==limit_video:
print(f'{videos[i]} Video Ended, Stopping Now',)
video_over = True
# break # stop for both cams later will buffer

if start:
# only initialize at start
start = False
past_frame = [frame, frame]

# in case of frame drop
past_frame = [frame for i in videos]

past_frame[i]=frame
frame = cv2.resize(frame, size)
frames.append(frame)


if video_over: break

if axis==0:
resized_frame = np.hstack(frames)

if axis==1:
resized_frame = np.vstack(frames)

cv2.imshow('test',resized_frame)
if ord('q')==cv2.waitKey(1):
cv2.destroyAllWindows()
break

# _, encoded_frame = cv2.imencode('.jpg', resized_frame)
# frame_list.append(encoded_frame)
output_shape = resized_frame.shape[:2]
_, encoded_frame = cv2.imencode('.jpg', resized_frame)
frame_list.append(encoded_frame)

if display:
cv2.imshow('test',resized_frame)
wait_key = cv2.waitKey(1)

if ord('q')== wait_key or ord('Q')== wait_key:
cv2.destroyAllWindows()
break

if writer_fps is None:
writer_fps = fps

if write_path:
video_name = write_video(frame_list, write_path, output_shape, writer_fps=writer_fps)
print("Video written sucessfully at :",video_name)

return video_name

if __name__ == '__main__':

path1 = '../../../archery.mp4'
path2 = '../../../cars.mp4'
videos = [path1, path2]
stack_video(videos, axis=0)

videos = [path1, path2, path1, path2]
video_size = (300,300) # w/h
limit_video = 0 # video index, that will decide to close streaming
video_path = 'test.mp4'

output_video = stack_video(
videos,
axis=0,
size=video_size,
limit_video=limit_video,
write_path=video_path,
writer_fps=None,
display=True)
print(output_video)

0 comments on commit b6e5392

Please sign in to comment.