|
import cv2 |
|
import numpy as np |
|
from moviepy.editor import VideoFileClip |
|
from huggingface_hub import hf_hub_download |
|
|
|
|
|
def grayscale(frame): |
|
""" |
|
๊ทธ๋ ์ด์ค์ผ์ผ ๋ณํ ํจ์. |
|
์
๋ ฅ๋ BGR ์ด๋ฏธ์ง๋ฅผ ๊ทธ๋ ์ด์ค์ผ์ผ๋ก ๋ณํํ ํ ๋ค์ BGR๋ก ๋ณํํ์ฌ ์ฐจ์์ ์ ์งํฉ๋๋ค. |
|
""" |
|
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) |
|
return cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR) |
|
|
|
|
|
def negative(frame): |
|
""" |
|
๋ค๊ฑฐํฐ๋ธ ๋ณํ ํจ์. |
|
์
๋ ฅ๋ BGR ์ด๋ฏธ์ง๋ฅผ ๋ค๊ฑฐํฐ๋ธ ์ด๋ฏธ์ง๋ก ๋ณํํฉ๋๋ค (์์ ๋ฐ์ ). |
|
""" |
|
return cv2.bitwise_not(frame) |
|
|
|
|
|
def adjust_brightness(frame, value): |
|
""" |
|
๋ฐ๊ธฐ ์กฐ์ ํจ์. |
|
์
๋ ฅ๋ BGR ์ด๋ฏธ์ง์์ HSV๋ก ๋ณํ ํ, ๋ฐ๊ธฐ ์ฑ๋(v)์ value ๊ฐ์ ๋ํด ๋ฐ๊ธฐ๋ฅผ ์กฐ์ ํฉ๋๋ค. |
|
value ๊ฐ์ ์์๋ฉด ์ด๋ก๊ฒ, ์์๋ฉด ๋ฐ๊ฒ ์กฐ์ ํฉ๋๋ค. |
|
""" |
|
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) |
|
h, s, v = cv2.split(hsv) |
|
|
|
|
|
v = np.clip(v.astype(np.int16) + value, 0, 255).astype(np.uint8) |
|
|
|
|
|
final_hsv = cv2.merge((h, s, v)) |
|
return cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR) |
|
|
|
|
|
def adjust_contrast(frame, alpha): |
|
""" |
|
๋๋น ์กฐ์ ํจ์. |
|
์
๋ ฅ๋ BGR ์ด๋ฏธ์ง์์ alpha ๊ฐ์ ๊ณฑํ์ฌ ๋๋น๋ฅผ ์กฐ์ ํฉ๋๋ค. |
|
alpha ๊ฐ์ด ํด์๋ก ์ด๋ฏธ์ง์ ๋๋น๊ฐ ๊ฐํด์ง๋๋ค (1.0~3.0 ์ ๋๊ฐ ์ ์ ). |
|
""" |
|
adjusted = cv2.convertScaleAbs(frame, alpha=alpha, beta=0) |
|
|
|
|
|
return np.clip(adjusted, 0, 255) |
|
|
|
|
|
def blur_image(frame, kernel_size=5): |
|
""" |
|
๊ฐ์ฐ์์ ๋ธ๋ฌ๋ง ํจ์. |
|
์
๋ ฅ๋ BGR ์ด๋ฏธ์ง๋ฅผ ์ปค๋ ํฌ๊ธฐ๋งํผ ํ๋ฆฌ๊ฒ ๋ง๋ญ๋๋ค. |
|
kernel_size ๊ฐ์ ๋ฐ๋์ ํ์์ฌ์ผ ํ๋ฉฐ, ์ปค๋ ํฌ๊ธฐ๊ฐ ํด์๋ก ๋ ํ๋ ค์ง๋๋ค. |
|
""" |
|
blurred = cv2.GaussianBlur(frame, (kernel_size, kernel_size), 0) |
|
return blurred |
|
|
|
|
|
def edge_detection(frame): |
|
""" |
|
์ฃ์ง ๊ฒ์ถ ํจ์. |
|
์
๋ ฅ๋ BGR ์ด๋ฏธ์ง์์ ๊ทธ๋ ์ด์ค์ผ์ผ๋ก ๋ณํํ ํ Sobel ์ฐ์ฐ์ ํตํด ์ฃ์ง๋ฅผ ๊ฒ์ถํฉ๋๋ค. |
|
์ดํ, 2์ฐจ์์ผ๋ก ๋ณํ๋ ์ฃ์ง ์ด๋ฏธ์ง๋ฅผ ๋ค์ BGR 3์ฐจ์์ผ๋ก ๋ณํํ์ฌ ๋ฐํํฉ๋๋ค. |
|
""" |
|
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) |
|
sobelx = cv2.Sobel(gray_frame, cv2.CV_64F, 1, 0, ksize=5) |
|
sobely = cv2.Sobel(gray_frame, cv2.CV_64F, 0, 1, ksize=5) |
|
edges = cv2.magnitude(sobelx, sobely) |
|
edges = np.uint8(edges) |
|
return cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR) |
|
|
|
|
|
""" |
|
ํํฐ ์ ์ฉ ํจ์. |
|
์
๋ ฅ๋ ์ด๋ฏธ์ง์ ์ ํํ ํํฐ (๊ทธ๋ ์ด์ค์ผ์ผ, ๋ค๊ฑฐํฐ๋ธ, ๋ฐ๊ธฐ ์กฐ์ , ๋๋น ์กฐ์ , ๋ธ๋ฌ๋ง, ์ฃ์ง ๊ฒ์ถ)๋ฅผ ์ ์ฉํฉ๋๋ค. |
|
|
|
์ฌ์ฉ ๊ฐ๋ฅํ ํํฐ: |
|
- grayscale: ๊ทธ๋ ์ด์ค์ผ์ผ ๋ณํ |
|
- negative: ๋ค๊ฑฐํฐ๋ธ ๋ณํ |
|
- brightness: ๋ฐ๊ธฐ ์กฐ์ (๊ธฐ๋ณธ๊ฐ: 50) |
|
- contrast: ๋๋น ์กฐ์ (๊ธฐ๋ณธ๊ฐ: alpha=1.5, 1.0~3.0 ๋ฒ์ ์ฌ์ฉ ๊ถ์ฅ) |
|
- blur: ๋ธ๋ฌ๋ง (๊ธฐ๋ณธ ์ปค๋ ํฌ๊ธฐ: 5, ํ์๋ง ์ฌ์ฉ ๊ฐ๋ฅ) |
|
- edge: ์ฃ์ง ๊ฒ์ถ |
|
""" |
|
def apply_filter(frame, filter_name, **kwargs): |
|
if filter_name == 'grayscale': |
|
return grayscale(frame) |
|
elif filter_name == 'negative': |
|
return negative(frame) |
|
elif filter_name == 'brightness': |
|
return adjust_brightness(frame, kwargs.get('value', 50)) |
|
elif filter_name == 'contrast': |
|
return adjust_contrast(frame, kwargs.get('alpha', 1.5)) |
|
elif filter_name == 'blur': |
|
return blur_image(frame, kwargs.get('kernel_size', 5)) |
|
elif filter_name == 'edge': |
|
return edge_detection(frame) |
|
else: |
|
return frame |
|
|
|
|
|
def process_and_play_with_audio(input_video, output_video, filter_name, **kwargs): |
|
""" |
|
์ ํํ ํํฐ๋ฅผ ์ ์ฉํ์ฌ ์๋ณธ๊ณผ ๋ณํ๋ ์์์ ๋๋ํ ๋ฐฐ์นํ๊ณ , ์๋ฆฌ์ ํจ๊ป ์ฌ์ ๋ฐ ์ ์ฅํฉ๋๋ค. |
|
|
|
ํ๋ฆฌ๋ทฐ์์๋ ์๋ณธ์ 50% ํฌ๊ธฐ๋ก ๋ณด์ฌ์ฃผ๋ฉฐ, ์ต์ข
์ ์ฅ๋ ํ์ผ์ ์๋ณธ ํฌ๊ธฐ ๊ทธ๋๋ก ์ ์ฅ๋ฉ๋๋ค. |
|
""" |
|
|
|
clip = VideoFileClip(input_video) |
|
|
|
|
|
def make_frame(img): |
|
|
|
filtered_img = apply_filter(img, filter_name, **kwargs) |
|
|
|
|
|
combined_frame = np.hstack((img, filtered_img)) |
|
|
|
return combined_frame |
|
|
|
|
|
new_clip = clip.fl_image(make_frame) |
|
|
|
|
|
preview_clip = new_clip.resize(0.5) |
|
|
|
|
|
new_clip.write_videofile(output_video, codec='libx264', audio=True) |
|
|
|
|
|
preview_clip.preview() |
|
|
|
|
|
repo_id = 'Nagase-Kotono/CheckAppVersion' |
|
file_name = 'DECO_27_ใใใใผใฉใณใใ_feat_ๅ้ณใใฏ.mp4' |
|
|
|
|
|
input_video = hf_hub_download(repo_id=repo_id, filename=file_name) |
|
|
|
|
|
output_video = 'output_video_with_audio.mp4' |
|
|
|
|
|
process_and_play_with_audio(input_video, output_video, filter_name='edge') |
|
|
|
|
|
|
|
|