pragnakalp commited on
Commit
4aa0c77
·
1 Parent(s): 8fe2da1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -115
app.py CHANGED
@@ -23,119 +23,7 @@ import ffmpeg
23
 
24
  block = gr.Blocks()
25
 
26
- def merge_frames():
27
- path = '/content/video_results/restored_imgs'
28
- image_folder = os.fsencode(path)
29
- print(image_folder)
30
- filenames = []
31
-
32
- for file in os.listdir(image_folder):
33
- filename = os.fsdecode(file)
34
- if filename.endswith( ('.jpg', '.png', '.gif') ):
35
- filenames.append(filename)
36
 
37
- filenames.sort() # this iteration technique has no built in order, so sort the frames
38
- images = list(map(lambda filename: imageio.imread("/content/video_results/restored_imgs/"+filename), filenames))
39
-
40
-
41
- imageio.mimsave('/content/video_output.mp4', images, fps=25.0) # modify the frame duration as needed
42
-
43
-
44
- block = gr.Blocks()
45
-
46
-
47
-
48
- def audio_video():
49
-
50
- input_video = ffmpeg.input('/content/video_output.mp4')
51
-
52
- input_audio = ffmpeg.input('/content/audio.wav')
53
-
54
- ffmpeg.concat(input_video, input_audio, v=1, a=1).output('final_output.mp4').run()
55
- return "final_output.mp4"
56
-
57
-
58
- def compute_aspect_preserved_bbox(bbox, increase_area, h, w):
59
- left, top, right, bot = bbox
60
- width = right - left
61
- height = bot - top
62
-
63
- width_increase = max(increase_area, ((1 + 2 * increase_area) * height - width) / (2 * width))
64
- height_increase = max(increase_area, ((1 + 2 * increase_area) * width - height) / (2 * height))
65
-
66
- left_t = int(left - width_increase * width)
67
- top_t = int(top - height_increase * height)
68
- right_t = int(right + width_increase * width)
69
- bot_t = int(bot + height_increase * height)
70
-
71
- left_oob = -min(0, left_t)
72
- right_oob = right - min(right_t, w)
73
- top_oob = -min(0, top_t)
74
- bot_oob = bot - min(bot_t, h)
75
-
76
- if max(left_oob, right_oob, top_oob, bot_oob) > 0:
77
- max_w = max(left_oob, right_oob)
78
- max_h = max(top_oob, bot_oob)
79
- if max_w > max_h:
80
- return left_t + max_w, top_t + max_w, right_t - max_w, bot_t - max_w
81
- else:
82
- return left_t + max_h, top_t + max_h, right_t - max_h, bot_t - max_h
83
-
84
- else:
85
- return (left_t, top_t, right_t, bot_t)
86
-
87
- def crop_src_image(src_img, detector=None):
88
- if detector is None:
89
- detector = dlib.get_frontal_face_detector()
90
- save_img='/content/image_pre.png'
91
- img = cv2.imread(src_img)
92
- faces = detector(img, 0)
93
- h, width, _ = img.shape
94
- if len(faces) > 0:
95
- bbox = [faces[0].left(), faces[0].top(),faces[0].right(), faces[0].bottom()]
96
- l = bbox[3]-bbox[1]
97
- bbox[1]= bbox[1]-l*0.1
98
- bbox[3]= bbox[3]-l*0.1
99
- bbox[1] = max(0,bbox[1])
100
- bbox[3] = min(h,bbox[3])
101
- bbox = compute_aspect_preserved_bbox(tuple(bbox), 0.5, img.shape[0], img.shape[1])
102
- img = img[bbox[1] :bbox[3] , bbox[0]:bbox[2]]
103
- img = cv2.resize(img, (256, 256))
104
- cv2.imwrite(save_img,img)
105
- else:
106
- img = cv2.resize(img,(256,256))
107
- cv2.imwrite(save_img, img)
108
-
109
- return '/content/image_pre.png'
110
-
111
-
112
- def pad_image(image):
113
- w, h = image.size
114
- if w == h:
115
- return image
116
- elif w > h:
117
- new_image = Image.new(image.mode, (w, w), (0, 0, 0))
118
- new_image.paste(image, (0, (w - h) // 2))
119
- return new_image
120
- else:
121
- new_image = Image.new(image.mode, (h, h), (0, 0, 0))
122
- new_image.paste(image, ((h - w) // 2, 0))
123
- return new_image
124
-
125
- def calculate(image_in, audio_in):
126
- waveform, sample_rate = torchaudio.load(audio_in)
127
- torchaudio.save("/content/audio.wav", waveform, sample_rate, encoding="PCM_S", bits_per_sample=16)
128
- image = Image.open(image_in)
129
- image = pad_image(image)
130
- image.save("image.png")
131
-
132
- pocketsphinx_run = subprocess.run(['pocketsphinx', '-phone_align', 'yes', 'single', '/content/audio.wav'], check=True, capture_output=True)
133
- jq_run = subprocess.run(['jq', '[.w[]|{word: (.t | ascii_upcase | sub("<S>"; "sil") | sub("<SIL>"; "sil") | sub("\\\(2\\\)"; "") | sub("\\\(3\\\)"; "") | sub("\\\(4\\\)"; "") | sub("\\\[SPEECH\\\]"; "SIL") | sub("\\\[NOISE\\\]"; "SIL")), phones: [.w[]|{ph: .t | sub("\\\+SPN\\\+"; "SIL") | sub("\\\+NSN\\\+"; "SIL"), bg: (.b*100)|floor, ed: (.b*100+.d*100)|floor}]}]'], input=pocketsphinx_run.stdout, capture_output=True)
134
- with open("test.json", "w") as f:
135
- f.write(jq_run.stdout.decode('utf-8').strip())
136
-
137
- os.system(f"cd /content/one-shot-talking-face && python3 -B test_script.py --img_path /content/image.png --audio_path /content/audio.wav --phoneme_path /content/test.json --save_dir /content/train")
138
- return "/content/train/image_audio.mp4"
139
 
140
  def one_shot_talking(image_in,audio_in):
141
 
@@ -157,7 +45,7 @@ def one_shot_talking(image_in,audio_in):
157
  image = pad_image(image)
158
  image.save("/content/image_pre.png")
159
  return "/content/audio.wav"
160
- exit()
161
  pocketsphinx_run = subprocess.run(['pocketsphinx', '-phone_align', 'yes', 'single', '/content/audio.wav'], check=True, capture_output=True)
162
  jq_run = subprocess.run(['jq', '[.w[]|{word: (.t | ascii_upcase | sub("<S>"; "sil") | sub("<SIL>"; "sil") | sub("\\\(2\\\)"; "") | sub("\\\(3\\\)"; "") | sub("\\\(4\\\)"; "") | sub("\\\[SPEECH\\\]"; "SIL") | sub("\\\[NOISE\\\]"; "SIL")), phones: [.w[]|{ph: .t | sub("\\\+SPN\\\+"; "SIL") | sub("\\\+NSN\\\+"; "SIL"), bg: (.b*100)|floor, ed: (.b*100+.d*100)|floor}]}]'], input=pocketsphinx_run.stdout, capture_output=True)
163
  with open("test.json", "w") as f:
@@ -209,8 +97,9 @@ def one_shot(image,input_text,gender):
209
  sound = AudioSegment.from_file(f.name, format="mp3")
210
  sound.export("/content/audio.wav", format="wav")
211
 
212
- one_shot_talking(image,'/content/audio.wav')
213
-
 
214
  elif gender == 'Male' or gender == 'male':
215
  print(gender)
216
  models, cfg, task = load_model_ensemble_and_task_from_hf_hub(
 
23
 
24
  block = gr.Blocks()
25
 
 
 
 
 
 
 
 
 
 
 
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  def one_shot_talking(image_in,audio_in):
29
 
 
45
  image = pad_image(image)
46
  image.save("/content/image_pre.png")
47
  return "/content/audio.wav"
48
+
49
  pocketsphinx_run = subprocess.run(['pocketsphinx', '-phone_align', 'yes', 'single', '/content/audio.wav'], check=True, capture_output=True)
50
  jq_run = subprocess.run(['jq', '[.w[]|{word: (.t | ascii_upcase | sub("<S>"; "sil") | sub("<SIL>"; "sil") | sub("\\\(2\\\)"; "") | sub("\\\(3\\\)"; "") | sub("\\\(4\\\)"; "") | sub("\\\[SPEECH\\\]"; "SIL") | sub("\\\[NOISE\\\]"; "SIL")), phones: [.w[]|{ph: .t | sub("\\\+SPN\\\+"; "SIL") | sub("\\\+NSN\\\+"; "SIL"), bg: (.b*100)|floor, ed: (.b*100+.d*100)|floor}]}]'], input=pocketsphinx_run.stdout, capture_output=True)
51
  with open("test.json", "w") as f:
 
97
  sound = AudioSegment.from_file(f.name, format="mp3")
98
  sound.export("/content/audio.wav", format="wav")
99
 
100
+ result = one_shot_talking(image,'/content/audio.wav')
101
+ return result
102
+ exit()
103
  elif gender == 'Male' or gender == 'male':
104
  print(gender)
105
  models, cfg, task = load_model_ensemble_and_task_from_hf_hub(