Update app.py
Browse files
app.py
CHANGED
|
@@ -1,84 +1,138 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import os
|
| 3 |
-
import pandas as pd
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
try:
|
| 22 |
-
|
| 23 |
except Exception as e:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
date_recent_file = "date_recent.csv"
|
| 30 |
-
date_old_file = "date_old.csv"
|
| 31 |
-
score_high_file = "score_high.csv"
|
| 32 |
-
score_low_file = "score_low.csv"
|
| 33 |
-
date_recent.to_csv(date_recent_file, index=False)
|
| 34 |
-
date_old.to_csv(date_old_file, index=False)
|
| 35 |
-
score_high.to_csv(score_high_file, index=False)
|
| 36 |
-
score_low.to_csv(score_low_file, index=False)
|
| 37 |
-
|
| 38 |
-
return date_recent, date_old, score_high, score_low, date_recent_file, date_old_file, score_high_file, score_low_file
|
| 39 |
-
|
| 40 |
-
# 리뷰λ μ§ λ²νΌ ν΄λ¦ μ μ€νλλ ν¨μ
|
| 41 |
-
def on_date(file):
|
| 42 |
-
if file is None:
|
| 43 |
-
return [None] * 6
|
| 44 |
-
date_recent, date_old, score_high, score_low, date_recent_file, date_old_file, score_high_file, score_low_file = process_file(file)
|
| 45 |
-
# 첫λ²μ§Έ νλ©΄: 리뷰λ μ§ λ΄λ¦Όμ°¨μ, λλ²μ§Έ νλ©΄: 리뷰λ μ§ μ€λ¦μ°¨μ
|
| 46 |
-
return date_recent, date_old, date_recent_file, date_old_file, score_high_file, score_low_file
|
| 47 |
-
|
| 48 |
-
# 리뷰μ μ λ²νΌ ν΄λ¦ μ μ€νλλ ν¨μ
|
| 49 |
-
def on_score(file):
|
| 50 |
-
if file is None:
|
| 51 |
-
return [None] * 6
|
| 52 |
-
date_recent, date_old, score_high, score_low, date_recent_file, date_old_file, score_high_file, score_low_file = process_file(file)
|
| 53 |
-
# 첫λ²μ§Έ νλ©΄: 리뷰μ μ λ΄λ¦Όμ°¨μ, λλ²μ§Έ νλ©΄: 리뷰μ μ μ€λ¦μ°¨μ
|
| 54 |
-
return score_high, score_low, date_recent_file, date_old_file, score_high_file, score_low_file
|
| 55 |
|
| 56 |
# Gradio μΈν°νμ΄μ€ ꡬμ±
|
| 57 |
-
with gr.Blocks(title="
|
| 58 |
-
#
|
| 59 |
-
file_input = gr.File(label="μμ
νμΌ μ
λ‘λ", file_types=[".xlsx", ".xls"])
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
btn_date.click(on_date, inputs=[file_input], outputs=[output1, output2, dl_date_recent, dl_date_old, dl_score_high, dl_score_low])
|
| 82 |
-
btn_score.click(on_score, inputs=[file_input], outputs=[output1, output2, dl_date_recent, dl_date_old, dl_score_high, dl_score_low])
|
| 83 |
-
|
| 84 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
from moviepy.editor import VideoFileClip, vfx
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import tempfile
|
| 6 |
+
import imageio
|
| 7 |
+
import cv2
|
| 8 |
|
| 9 |
+
def get_video_info(video_path):
|
| 10 |
+
"""μ
λ‘λλ μμμ ν΄μλ, FPS, μ΄ νλ μ μ, μ¬μμκ° μ 보λ₯Ό λ°ν"""
|
| 11 |
+
cap = cv2.VideoCapture(video_path)
|
| 12 |
+
if not cap.isOpened():
|
| 13 |
+
return "μμ μ 보λ₯Ό κ°μ Έμ¬ μ μμ΅λλ€."
|
| 14 |
+
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
|
| 15 |
+
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
|
| 16 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 17 |
+
frame_count = cap.get(cv2.CAP_PROP_FRAME_COUNT)
|
| 18 |
+
duration = frame_count / fps if fps > 0 else 0
|
| 19 |
+
cap.release()
|
| 20 |
+
info = f"ν΄μλ: {int(width)}x{int(height)}\nFPS: {fps:.2f}\nμ΄ νλ μ μ: {int(frame_count)}\nμ¬μμκ°: {duration:.2f}μ΄"
|
| 21 |
+
return info
|
| 22 |
|
| 23 |
+
def extract_thumbnail(video_path, time_sec):
|
| 24 |
+
"""μ£Όμ΄μ§ μκ°(μ΄)μμ νλ μμ μΆμΆνμ¬ μ΄λ―Έμ§λ‘ λ°ν"""
|
| 25 |
+
clip = VideoFileClip(video_path)
|
| 26 |
+
frame = clip.get_frame(time_sec)
|
| 27 |
+
image = Image.fromarray(frame)
|
| 28 |
+
clip.reader.close()
|
| 29 |
+
if clip.audio:
|
| 30 |
+
clip.audio.reader.close_proc()
|
| 31 |
+
return image
|
| 32 |
|
| 33 |
+
def generate_gif(video_file, start_time, end_time, resolution_scale, frame_rate, speed, repeat):
|
| 34 |
+
"""μ
λ ₯λ μμμ μ§μ ꡬκ°μ μλΌλ΄κ³ μ΅μ
μ λ§μΆ° GIF νμΌμ μμ±"""
|
| 35 |
+
def parse_time(t):
|
| 36 |
+
parts = t.split(":")
|
| 37 |
+
if len(parts) == 3:
|
| 38 |
+
h, m, s = parts
|
| 39 |
+
return int(h)*3600 + int(m)*60 + float(s)
|
| 40 |
+
elif len(parts) == 2:
|
| 41 |
+
m, s = parts
|
| 42 |
+
return int(m)*60 + float(s)
|
| 43 |
+
else:
|
| 44 |
+
return float(t)
|
| 45 |
+
start_sec = parse_time(start_time)
|
| 46 |
+
end_sec = parse_time(end_time)
|
| 47 |
+
|
| 48 |
+
clip = VideoFileClip(video_file)
|
| 49 |
+
trimmed = clip.subclip(start_sec, end_sec)
|
| 50 |
+
|
| 51 |
+
if speed != 1:
|
| 52 |
+
trimmed = trimmed.fx(vfx.speedx, speed)
|
| 53 |
+
|
| 54 |
+
if resolution_scale != 1:
|
| 55 |
+
new_width = int(trimmed.w * resolution_scale)
|
| 56 |
+
new_height = int(trimmed.h * resolution_scale)
|
| 57 |
+
trimmed = trimmed.resize(newsize=(new_width, new_height))
|
| 58 |
+
|
| 59 |
+
trimmed = trimmed.set_fps(frame_rate)
|
| 60 |
+
|
| 61 |
+
temp_gif = tempfile.NamedTemporaryFile(delete=False, suffix=".gif")
|
| 62 |
try:
|
| 63 |
+
trimmed.write_gif(temp_gif.name, loop=repeat)
|
| 64 |
except Exception as e:
|
| 65 |
+
return None, f"GIF μμ± μ€ν¨: {e}"
|
| 66 |
+
clip.reader.close()
|
| 67 |
+
if clip.audio:
|
| 68 |
+
clip.audio.reader.close_proc()
|
| 69 |
+
return temp_gif.name, "GIF μμ± μλ£!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
# Gradio μΈν°νμ΄μ€ ꡬμ±
|
| 72 |
+
with gr.Blocks(title="GIFμμ±κΈ°") as demo:
|
| 73 |
+
gr.Markdown("# GIFμμ±κΈ°")
|
|
|
|
| 74 |
|
| 75 |
+
with gr.Tab("μμ μ
λ‘λ"):
|
| 76 |
+
with gr.Row():
|
| 77 |
+
video_input = gr.Video(label="μμ μ
λ‘λ", source="upload", type="filepath")
|
| 78 |
+
video_info = gr.Textbox(label="μμ μ 보", interactive=False)
|
| 79 |
+
video_input.change(fn=lambda v: get_video_info(v) if v is not None else "", inputs=video_input, outputs=video_info)
|
| 80 |
|
| 81 |
+
with gr.Tab("μκ° μ ν λ° μΈλ€μΌ"):
|
| 82 |
+
gr.Markdown("### μμ/μ’
λ£ μκ° μ ν (μκ° νμ: HH:MM:SS)")
|
| 83 |
+
with gr.Row():
|
| 84 |
+
start_time_input = gr.Textbox(label="μμ μκ°", value="00:00:00")
|
| 85 |
+
end_time_input = gr.Textbox(label="μ’
λ£ μκ°", value="00:00:05")
|
| 86 |
+
with gr.Row():
|
| 87 |
+
start_thumbnail = gr.Image(label="μμ μκ° μΈλ€μΌ")
|
| 88 |
+
end_thumbnail = gr.Image(label="μ’
λ£ μκ° μΈλ€μΌ")
|
| 89 |
+
generate_thumb_btn = gr.Button("μΈλ€μΌ μμ±")
|
| 90 |
+
|
| 91 |
+
def generate_thumbnails(video_file, start_time, end_time):
|
| 92 |
+
if video_file is None:
|
| 93 |
+
return None, None
|
| 94 |
+
def parse_time(t):
|
| 95 |
+
parts = t.split(":")
|
| 96 |
+
if len(parts) == 3:
|
| 97 |
+
h, m, s = parts
|
| 98 |
+
return int(h)*3600 + int(m)*60 + float(s)
|
| 99 |
+
elif len(parts) == 2:
|
| 100 |
+
m, s = parts
|
| 101 |
+
return int(m)*60 + float(s)
|
| 102 |
+
else:
|
| 103 |
+
return float(t)
|
| 104 |
+
start_sec = parse_time(start_time)
|
| 105 |
+
end_sec = parse_time(end_time)
|
| 106 |
+
try:
|
| 107 |
+
start_img = extract_thumbnail(video_file, start_sec)
|
| 108 |
+
end_img = extract_thumbnail(video_file, end_sec)
|
| 109 |
+
except Exception as e:
|
| 110 |
+
return None, None
|
| 111 |
+
return start_img, end_img
|
| 112 |
+
|
| 113 |
+
generate_thumb_btn.click(generate_thumbnails, inputs=[video_input, start_time_input, end_time_input], outputs=[start_thumbnail, end_thumbnail])
|
| 114 |
|
| 115 |
+
with gr.Tab("GIF μ€μ λ° μμ±"):
|
| 116 |
+
gr.Markdown("### GIF μ€μ ")
|
| 117 |
+
with gr.Row():
|
| 118 |
+
resolution_scale = gr.Slider(minimum=0.1, maximum=1.0, value=1.0, label="ν΄μλ μ‘°μ (λ°°μ¨)")
|
| 119 |
+
frame_rate = gr.Slider(minimum=1, maximum=60, step=1, value=15, label="νλ μ μλ (FPS)")
|
| 120 |
+
with gr.Row():
|
| 121 |
+
speed = gr.Slider(minimum=0.1, maximum=3.0, step=0.1, value=1.0, label="μλ μ‘°μ ")
|
| 122 |
+
repeat = gr.Slider(minimum=0, maximum=10, step=1, value=0, label="λ°λ³΅ νμ (0μ 무ν λ°λ³΅)")
|
| 123 |
+
generate_gif_btn = gr.Button("GIF μμ±")
|
| 124 |
+
gif_preview = gr.Image(label="μμ±λ GIF 미리보기")
|
| 125 |
+
gif_download = gr.File(label="μμ±λ GIF λ€μ΄λ‘λ")
|
| 126 |
+
|
| 127 |
+
def process_gif(video_file, start_time, end_time, resolution_scale, frame_rate, speed, repeat):
|
| 128 |
+
if video_file is None:
|
| 129 |
+
return None, None
|
| 130 |
+
gif_path, msg = generate_gif(video_file, start_time, end_time, resolution_scale, frame_rate, speed, repeat)
|
| 131 |
+
if gif_path is None:
|
| 132 |
+
return None, None
|
| 133 |
+
return gif_path, gif_path
|
| 134 |
+
|
| 135 |
+
generate_gif_btn.click(process_gif, inputs=[video_input, start_time_input, end_time_input, resolution_scale, frame_rate, speed, repeat], outputs=[gif_preview, gif_download])
|
| 136 |
|
| 137 |
+
if __name__ == "__main__":
|
| 138 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|