Spanicin commited on
Commit
2899a36
1 Parent(s): 8ad7dd5

Update src/utils/paste_pic.py

Browse files
Files changed (1) hide show
  1. src/utils/paste_pic.py +67 -66
src/utils/paste_pic.py CHANGED
@@ -1,66 +1,67 @@
1
- import cv2, os
2
- import numpy as np
3
- from tqdm import tqdm
4
- import uuid
5
-
6
- from src.utils.videoio import save_video_with_watermark
7
-
8
- def paste_pic(video_path, pic_path, crop_info, new_audio_path, full_video_path):
9
-
10
- if not os.path.isfile(pic_path):
11
- raise ValueError('pic_path must be a valid path to video/image file')
12
- elif pic_path.split('.')[-1] in ['jpg', 'png', 'jpeg']:
13
- # loader for first frame
14
- full_img = cv2.imread(pic_path)
15
- else:
16
- # loader for videos
17
- video_stream = cv2.VideoCapture(pic_path)
18
- fps = video_stream.get(cv2.CAP_PROP_FPS)
19
- full_frames = []
20
- while 1:
21
- still_reading, frame = video_stream.read()
22
- if not still_reading:
23
- video_stream.release()
24
- break
25
- break
26
- full_img = frame
27
- frame_h = full_img.shape[0]
28
- frame_w = full_img.shape[1]
29
-
30
- video_stream = cv2.VideoCapture(video_path)
31
- fps = video_stream.get(cv2.CAP_PROP_FPS)
32
- crop_frames = []
33
- while 1:
34
- still_reading, frame = video_stream.read()
35
- if not still_reading:
36
- video_stream.release()
37
- break
38
- crop_frames.append(frame)
39
-
40
- if len(crop_info) != 3:
41
- print("you didn't crop the image")
42
- return
43
- else:
44
- r_w, r_h = crop_info[0]
45
- clx, cly, crx, cry = crop_info[1]
46
- lx, ly, rx, ry = crop_info[2]
47
- lx, ly, rx, ry = int(lx), int(ly), int(rx), int(ry)
48
- # oy1, oy2, ox1, ox2 = cly+ly, cly+ry, clx+lx, clx+rx
49
- # oy1, oy2, ox1, ox2 = cly+ly, cly+ry, clx+lx, clx+rx
50
- oy1, oy2, ox1, ox2 = cly, cry, clx, crx
51
-
52
-
53
- tmp_path = str(uuid.uuid4())+'.mp4'
54
- out_tmp = cv2.VideoWriter(tmp_path, cv2.VideoWriter_fourcc(*'MP4V'), fps, (frame_w, frame_h))
55
- for crop_frame in tqdm(crop_frames, 'seamlessClone:'):
56
- p = cv2.resize(crop_frame.astype(np.uint8), (crx-clx, cry - cly))
57
-
58
- mask = 255*np.ones(p.shape, p.dtype)
59
- location = ((ox1+ox2) // 2, (oy1+oy2) // 2)
60
- gen_img = cv2.seamlessClone(p, full_img, mask, location, cv2.NORMAL_CLONE)
61
- out_tmp.write(gen_img)
62
-
63
- out_tmp.release()
64
-
65
- save_video_with_watermark(tmp_path, new_audio_path, full_video_path, watermark=False)
66
- os.remove(tmp_path)
 
 
1
+ import cv2, os
2
+ import numpy as np
3
+ from tqdm import tqdm
4
+ import uuid
5
+
6
+ from src.utils.videoio import save_video_with_watermark
7
+
8
+ def paste_pic(video_path, pic_path, crop_info, new_audio_path, full_video_path):
9
+
10
+ if not os.path.isfile(pic_path):
11
+ raise ValueError('pic_path must be a valid path to video/image file')
12
+ elif pic_path.split('.')[-1] in ['jpg', 'png', 'jpeg']:
13
+ # loader for first frame
14
+ full_img = cv2.imread(pic_path)
15
+ else:
16
+ # loader for videos
17
+ video_stream = cv2.VideoCapture(pic_path)
18
+ fps = video_stream.get(cv2.CAP_PROP_FPS)
19
+ full_frames = []
20
+ while 1:
21
+ still_reading, frame = video_stream.read()
22
+ if not still_reading:
23
+ video_stream.release()
24
+ break
25
+ break
26
+ full_img = frame
27
+ frame_h = full_img.shape[0]
28
+ frame_w = full_img.shape[1]
29
+
30
+ video_stream = cv2.VideoCapture(video_path)
31
+ fps = video_stream.get(cv2.CAP_PROP_FPS)
32
+ crop_frames = []
33
+ while 1:
34
+ still_reading, frame = video_stream.read()
35
+ if not still_reading:
36
+ video_stream.release()
37
+ break
38
+ crop_frames.append(frame)
39
+
40
+ if len(crop_info) != 3:
41
+ print("you didn't crop the image")
42
+ return
43
+ else:
44
+ r_w, r_h = crop_info[0]
45
+ clx, cly, crx, cry = crop_info[1]
46
+ lx, ly, rx, ry = crop_info[2]
47
+ lx, ly, rx, ry = int(lx), int(ly), int(rx), int(ry)
48
+ # oy1, oy2, ox1, ox2 = cly+ly, cly+ry, clx+lx, clx+rx
49
+ # oy1, oy2, ox1, ox2 = cly+ly, cly+ry, clx+lx, clx+rx
50
+ oy1, oy2, ox1, ox2 = cly, cry, clx, crx
51
+
52
+
53
+ tmp_path = str(uuid.uuid4())+'.mp4'
54
+ out_tmp = cv2.VideoWriter(tmp_path, cv2.VideoWriter_fourcc(*'MP4V'), fps, (frame_w, frame_h))
55
+ for crop_frame in tqdm(crop_frames, 'seamlessClone:'):
56
+ p = cv2.resize(crop_frame.astype(np.uint8), (crx-clx, cry - cly))
57
+
58
+ mask = 255*np.ones(p.shape, p.dtype)
59
+ location = ((ox1+ox2) // 2, (oy1+oy2) // 2)
60
+ gen_img = cv2.seamlessClone(p, full_img, mask, location, cv2.NORMAL_CLONE)
61
+ out_tmp.write(gen_img)
62
+
63
+ out_tmp.release()
64
+
65
+ base64_video,temp_file_path = save_video_with_watermark(tmp_path, new_audio_path, full_video_path, watermark=False)
66
+ return base64_video,temp_file_path
67
+ os.remove(tmp_path)