Tzktz commited on
Commit
c1ae314
1 Parent(s): 6d491ec

Upload 5 files

Browse files
Files changed (6) hide show
  1. .gitattributes +2 -0
  2. Blur.py +195 -0
  3. app.py +20 -0
  4. input.jpg +3 -0
  5. output.png +3 -0
  6. requirements.txt +6 -0
.gitattributes CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ input.jpg filter=lfs diff=lfs merge=lfs -text
37
+ output.png filter=lfs diff=lfs merge=lfs -text
Blur.py ADDED
@@ -0,0 +1,195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ from PIL import Image
3
+ import numpy as np
4
+ from rembg import remove
5
+ import os
6
+ import shutil
7
+ import glob
8
+ import moviepy.editor as mp
9
+ from moviepy.editor import *
10
+
11
+
12
+ def cv_to_pil(img):
13
+ return Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGRA2RGBA))
14
+
15
+
16
+ def pil_to_cv(img):
17
+ return cv2.cvtColor(np.array(img), cv2.COLOR_RGBA2BGRA)
18
+
19
+
20
+ def video_to_images(video_path, images_path):
21
+ # Open video
22
+ cam = cv2.VideoCapture(video_path)
23
+
24
+ # Get FPS
25
+ fps = cam.get(cv2.CAP_PROP_FPS)
26
+
27
+ # Extract audio
28
+ clip = mp.VideoFileClip(video_path)
29
+ clip.audio.write_audiofile("./audio.mp3")
30
+
31
+ # Create folder for images
32
+ if not os.path.exists(images_path):
33
+ os.makedirs(images_path)
34
+ else:
35
+ shutil.rmtree(images_path)
36
+ os.makedirs(images_path)
37
+
38
+ # Go through frames of video
39
+ frameno = 0
40
+ while (True):
41
+ ret, frame = cam.read()
42
+ if ret:
43
+ # if video is still left continue creating images
44
+ name = images_path + str(frameno).zfill(5) + '.png'
45
+
46
+ print('new frame captured... ', frameno)
47
+
48
+ # Save frame
49
+ cv2.imwrite(name, frame, [int(cv2.IMWRITE_PNG_COMPRESSION), 0])
50
+ frameno += 1
51
+ else:
52
+ break
53
+
54
+ # Close video
55
+ cam.release()
56
+ cv2.destroyAllWindows()
57
+
58
+ return fps
59
+
60
+
61
+ def images_to_video(images_path, video_export_path, fps):
62
+ # Get a list of PNG images on the "test_images" folder
63
+ images = glob.glob(images_path + "*.png")
64
+
65
+ # Sort images by name
66
+ images = sorted(images)
67
+
68
+ # Read the first image to get the frame size
69
+ frame = cv2.imread(images[0])
70
+ height, width, layers = frame.shape
71
+
72
+ temp_video_path = './temp-video.mp4'
73
+
74
+ # Codec
75
+ # fourcc = cv2.VideoWriter_fourcc(*"mp4v")
76
+ fourcc = cv2.VideoWriter_fourcc(*'XVID')
77
+ # fourcc = cv2.VideoWriter_fourcc(*'MPEG')
78
+
79
+ # Create final video
80
+ video = cv2.VideoWriter(filename=temp_video_path, fourcc=fourcc, fps=fps, frameSize=(width, height))
81
+
82
+ # Read each image and write it to the video
83
+ for i, image in enumerate(images):
84
+ print("Writing frame to video ", i, '/', len(images))
85
+
86
+ # Read the image using OpenCV
87
+ frame = cv2.imread(image)
88
+
89
+ # Write frame to video
90
+ video.write(frame)
91
+
92
+ # Exit the video writer
93
+ video.release()
94
+
95
+ # Open final video
96
+ videoclip = VideoFileClip(temp_video_path)
97
+
98
+ # Add audio to final video
99
+ audioclip = AudioFileClip("./audio.mp3")
100
+ new_audioclip = CompositeAudioClip([audioclip])
101
+ videoclip.audio = new_audioclip
102
+
103
+ # Save final video
104
+ videoclip.write_videofile(video_export_path, audio_codec='aac', codec='libx264')
105
+
106
+ # Delete temp files
107
+ os.remove(temp_video_path)
108
+ os.remove("./audio.mp3")
109
+
110
+
111
+ def motion_blur(img, distance, amount):
112
+ # Convert to RGBA
113
+ img = img.convert('RGBA')
114
+
115
+ # Convert pil to cv
116
+ cv_img = pil_to_cv(img)
117
+
118
+ # Generating the kernel
119
+ kernel_motion_blur = np.zeros((distance, distance))
120
+ kernel_motion_blur[int((distance - 1) / 2), :] = np.ones(distance)
121
+ kernel_motion_blur = kernel_motion_blur / distance
122
+
123
+ # Applying the kernel to the input image
124
+ output = cv2.filter2D(cv_img, -1, kernel_motion_blur)
125
+
126
+ # Convert cv to pil
127
+ blur_img = cv_to_pil(output).convert('RGBA')
128
+
129
+ # Blend the original image and the blur image
130
+ final_img = Image.blend(img, blur_img, amount)
131
+
132
+ return final_img
133
+
134
+
135
+ def background_motion_blur(background, distance_blur, amount_blur):
136
+ # Remove background
137
+ subject = remove(background)
138
+ amount_subject = 1
139
+
140
+ # Blur the background
141
+ background_blur = motion_blur(background, distance_blur, amount_blur)
142
+
143
+ # Put the subject on top of the blur background
144
+ subject_on_blur_background = background_blur.copy()
145
+ subject_on_blur_background.paste(background, (0, 0), subject)
146
+
147
+ # Blend the subject and the blur background
148
+ result = Image.blend(background_blur, subject_on_blur_background, amount_subject)
149
+
150
+ return result
151
+
152
+
153
+ def video_motion_blur(video_path, export_video_path, distance_blur, amount_blur, amount_subject):
154
+ # Image folder
155
+ images_path = './images/'
156
+
157
+ # Convert video to images and save FPS
158
+ fps = video_to_images(video_path, images_path)
159
+
160
+ # Create list of images
161
+ image_path_list = glob.glob(images_path + "*.png")
162
+
163
+ # Sort images by name
164
+ image_path_list = sorted(image_path_list)
165
+
166
+ # Create folder for blur images
167
+ blur_images_path = './blur_images/'
168
+ if not os.path.exists(blur_images_path):
169
+ os.makedirs(blur_images_path)
170
+ else:
171
+ shutil.rmtree(blur_images_path)
172
+ os.makedirs(blur_images_path)
173
+
174
+ # Go through image folder
175
+ count = 0
176
+ for filename in image_path_list:
177
+ # Open image an PIL image
178
+ img = Image.open(filename)
179
+
180
+ # Motion blur image
181
+ blur_img = background_motion_blur(img, distance_blur, amount_blur, amount_subject)
182
+
183
+ # Save blurred image
184
+ blur_img.save(blur_images_path + str(count).zfill(5) + '.png')
185
+
186
+ print('motion blur', str(count), '/', len(image_path_list))
187
+
188
+ count += 1
189
+
190
+ # Convert blurred images to final video
191
+ images_to_video(blur_images_path, export_video_path, fps)
192
+
193
+ # Delete temp folders
194
+ shutil.rmtree(images_path)
195
+ shutil.rmtree(blur_images_path)
app.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from Blur import background_motion_blur
3
+
4
+
5
+ def blur(img, distance_blur, amount_blur):
6
+ return background_motion_blur(img, distance_blur, amount_blur)
7
+
8
+
9
+ iface = gr.Interface(fn=blur,
10
+ title="Background Motion Blur",
11
+ examples=[["input.jpg", 200, 1]],
12
+
13
+ inputs=[gr.Image(type='pil', label='Image'),
14
+ gr.Slider(label='Blur Distance', minimum=0, maximum=500, value=100),
15
+ gr.Slider(label='Blur Amount', minimum=0.0, maximum=1.0, value=0.75)],
16
+
17
+ outputs=gr.Image(label='Output'))
18
+
19
+
20
+ iface.launch()
input.jpg ADDED

Git LFS Details

  • SHA256: 8c63042800550650f690099900e66c8e6b3adf924fdb20c0a536cbe345dbc077
  • Pointer size: 132 Bytes
  • Size of remote file: 2.14 MB
output.png ADDED

Git LFS Details

  • SHA256: 112e70a19405c307c41eb39c25a256b93a0949251d3cd61095df585067008046
  • Pointer size: 132 Bytes
  • Size of remote file: 7.8 MB
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ opencv-python
2
+ pillow
3
+ numpy
4
+ rembg
5
+ moviepy
6
+ ffmpeg