tonyassi commited on
Commit
b8cff7a
β€’
1 Parent(s): c5be9b5

Upload Blur.py

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