Spaces:
No application file
No application file
Upload data_augmentation.py
#1
by
tosanoob
- opened
- data_augmentation.py +83 -0
data_augmentation.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
def flip(input_path, output_path):
|
| 5 |
+
"""Open video specified by 'input_path', save flipped video to 'output_path'"""
|
| 6 |
+
cap = cv2.VideoCapture(input_path)
|
| 7 |
+
if not cap.isOpened():
|
| 8 |
+
print("Fail to load video")
|
| 9 |
+
return
|
| 10 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 11 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 12 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 13 |
+
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
|
| 14 |
+
while cap.isOpened():
|
| 15 |
+
ret, frame = cap.read()
|
| 16 |
+
if not ret:
|
| 17 |
+
break
|
| 18 |
+
flipped_frame = cv2.flip(frame, 1)
|
| 19 |
+
out.write(flipped_frame)
|
| 20 |
+
cap.release()
|
| 21 |
+
out.release()
|
| 22 |
+
|
| 23 |
+
def modify_brightness(input_path, output_path, value):
|
| 24 |
+
"""Open video specified by 'input_path', modify saturation by 'value', and save to 'output_path'\n
|
| 25 |
+
value > 0 => brighter, else darker"""
|
| 26 |
+
cap = cv2.VideoCapture(input_path)
|
| 27 |
+
if not cap.isOpened():
|
| 28 |
+
print("Fail to load video")
|
| 29 |
+
return
|
| 30 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 31 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 32 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 33 |
+
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
|
| 34 |
+
while cap.isOpened():
|
| 35 |
+
ret, frame = cap.read()
|
| 36 |
+
if not ret:
|
| 37 |
+
break
|
| 38 |
+
frame = cv2.add(frame, value)
|
| 39 |
+
out.write(frame)
|
| 40 |
+
cap.release()
|
| 41 |
+
out.release()
|
| 42 |
+
|
| 43 |
+
def rotate(input_path, output_path, angle):
|
| 44 |
+
"""Open video specified by 'input_path', rotate an 'angle', and save to 'output_path'\n
|
| 45 |
+
angle > 0 => counter clockwise, else clockwise"""
|
| 46 |
+
cap = cv2.VideoCapture(input_path)
|
| 47 |
+
if not cap.isOpened():
|
| 48 |
+
print("Fail to load video")
|
| 49 |
+
return
|
| 50 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 51 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 52 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 53 |
+
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
|
| 54 |
+
while cap.isOpened():
|
| 55 |
+
ret, frame = cap.read()
|
| 56 |
+
if not ret:
|
| 57 |
+
break
|
| 58 |
+
frame = cv2.warpAffine(frame, cv2.getRotationMatrix2D((width / 2, height / 2), angle, 1), (width, height))
|
| 59 |
+
out.write(frame)
|
| 60 |
+
cap.release()
|
| 61 |
+
out.release()
|
| 62 |
+
|
| 63 |
+
def add_gaussian_noise(input_path, output_path, intensity):
|
| 64 |
+
"""Open video specified by 'input_path', add noise with 'intensity', and save to 'output_path'\n
|
| 65 |
+
intensity varies between [0,1), 0 means noise free"""
|
| 66 |
+
cap = cv2.VideoCapture(input_path)
|
| 67 |
+
if not cap.isOpened():
|
| 68 |
+
print("Fail to load video")
|
| 69 |
+
return
|
| 70 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 71 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 72 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 73 |
+
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
|
| 74 |
+
while cap.isOpened():
|
| 75 |
+
ret, frame = cap.read()
|
| 76 |
+
if not ret:
|
| 77 |
+
break
|
| 78 |
+
frame = frame/255
|
| 79 |
+
frame = cv2.add(frame,np.random.randn(width,height,3)*(intensity**0.5)*0.1)
|
| 80 |
+
frame = frame*255
|
| 81 |
+
out.write(frame.astype(np.uint8))
|
| 82 |
+
cap.release()
|
| 83 |
+
out.release()
|