seawolf2357 commited on
Commit
69279bb
ยท
verified ยท
1 Parent(s): d2a03ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -38
app.py CHANGED
@@ -1,47 +1,44 @@
1
- import requests
2
  import gradio as gr
3
- import os
4
  import imageio
5
  import numpy as np
6
  from PIL import Image
7
- import requests
8
- from io import BytesIO
9
- import tempfile
10
 
11
- # Pexels API ํ‚ค๋ฅผ ์—ฌ๊ธฐ์— ์ž…๋ ฅํ•˜์„ธ์š”
12
- API_KEY = os.getenv('API_KEY')
13
- API_URL = 'https://api.pexels.com/v1/search'
 
 
 
14
 
15
- def search_images(keyword):
16
- headers = {'Authorization': API_KEY}
17
- params = {'query': keyword, 'per_page': 5} # per_page ๊ฐ’์„ 5๋กœ ์„ค์ •
18
- response = requests.get(API_URL, headers=headers, params=params)
19
-
20
- if response.status_code == 200 and response.json()['photos']:
21
- photos = response.json()['photos']
22
- images = []
23
- for photo in photos:
24
- image_url = photo['src']['original']
25
- response = requests.get(image_url)
26
- img = Image.open(BytesIO(response.content))
27
- images.append(img)
28
-
29
- # ์ด๋ฏธ์ง€๋ฅผ ์˜์ƒ์œผ๋กœ ํ•ฉ์น˜๊ธฐ
30
- with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmpfile:
31
- with imageio.get_writer(tmpfile.name, fps=0.5) as video: # 2์ดˆ ๊ฐ„๊ฒฉ
32
- for img in images:
33
- video.append_data(np.array(img))
34
- return tmpfile.name # ์ž„์‹œ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ ๋ฐ˜ํ™˜
35
- else:
36
- return "๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."
37
 
38
- interface = gr.Interface(
39
- fn=search_images,
40
- inputs=gr.Textbox(lines=2, placeholder="๊ฒ€์ƒ‰ํ•  ์ด๋ฏธ์ง€ ํ‚ค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”"),
41
- outputs=gr.Video(label="๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ"),
42
- title="Pexels ์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰๊ธฐ",
43
- description="Pexels์—์„œ ํ‚ค์›Œ๋“œ์— ๋งž๋Š” ์ด๋ฏธ์ง€๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ณ , ํ•ด๋‹น ์ด๋ฏธ์ง€๋ฅผ ์˜์ƒ์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค."
 
 
 
 
 
 
 
 
 
44
  )
45
 
46
- if __name__ == "__main__":
47
- interface.launch()
 
 
1
  import gradio as gr
 
2
  import imageio
3
  import numpy as np
4
  from PIL import Image
 
 
 
5
 
6
+ def resize_image(image, target_width, target_height):
7
+ """
8
+ ์ด๋ฏธ์ง€๋ฅผ ์ง€์ •๋œ ํฌ๊ธฐ๋กœ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค.
9
+ """
10
+ image_pil = Image.fromarray(image).resize((target_width, target_height), Image.ANTIALIAS)
11
+ return np.array(image_pil)
12
 
13
+ def create_video(images):
14
+ """
15
+ ์ฃผ์–ด์ง„ ์ด๋ฏธ์ง€ ๋ฆฌ์ŠคํŠธ๋กœ๋ถ€ํ„ฐ ๋น„๋””์˜ค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
16
+ ๋ชจ๋“  ์ด๋ฏธ์ง€๋Š” ๋น„๋””์˜ค์— ์ถ”๊ฐ€๋˜๊ธฐ ์ „์— ๋™์ผํ•œ ํฌ๊ธฐ๋กœ ์กฐ์ •๋ฉ๋‹ˆ๋‹ค.
17
+ """
18
+ target_width = 1920
19
+ target_height = 1080
20
+ with imageio.get_writer('output_video.mp4', fps=2) as video:
21
+ for img in images:
22
+ img_resized = resize_image(img, target_width, target_height)
23
+ video.append_data(img_resized)
24
+ return 'output_video.mp4'
 
 
 
 
 
 
 
 
 
 
25
 
26
+ def process_images(image_files):
27
+ """
28
+ ์—…๋กœ๋“œ๋œ ์ด๋ฏธ์ง€ ํŒŒ์ผ๋“ค์„ ์ฒ˜๋ฆฌํ•˜์—ฌ ๋น„๋””์˜ค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
29
+ """
30
+ images = [imageio.imread(image_file) for image_file in image_files]
31
+ video_file = create_video(images)
32
+ return video_file
33
+
34
+ # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
35
+ iface = gr.Interface(
36
+ fn=process_images,
37
+ inputs=gr.inputs.Image(type="file", label="Upload Images", multiple=True),
38
+ outputs="file",
39
+ title="Image to Video Converter",
40
+ description="Upload multiple images to create a video."
41
  )
42
 
43
+ # Gradio ์•ฑ ์‹คํ–‰
44
+ iface.launch()