meraj12 commited on
Commit
aeedcdd
·
verified ·
1 Parent(s): 2c24abf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import cv2
4
+ import numpy as np
5
+ from PIL import Image
6
+ from rembg import remove
7
+ from realesrgan import RealESRGAN
8
+ from moviepy.editor import ImageSequenceClip
9
+
10
+ # Upload section
11
+ st.title("🖼️ AI Image & Video Effects Studio")
12
+ st.markdown("Upload multiple images, apply cool effects, and export a video!")
13
+
14
+ uploaded_files = st.file_uploader("Upload images", type=["png", "jpg", "jpeg"], accept_multiple_files=True)
15
+
16
+ # Select effect
17
+ effect = st.selectbox("Choose an effect to apply", ["None", "Cartoon", "Edge Detection", "Remove Background", "Face Enhancement"])
18
+
19
+ output_dir = "outputs"
20
+ os.makedirs(output_dir, exist_ok=True)
21
+
22
+ processed_images = []
23
+
24
+ def cartoonize(image):
25
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
26
+ gray = cv2.medianBlur(gray, 5)
27
+ edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
28
+ cv2.THRESH_BINARY, 9, 9)
29
+ color = cv2.bilateralFilter(image, 9, 300, 300)
30
+ cartoon = cv2.bitwise_and(color, color, mask=edges)
31
+ return cartoon
32
+
33
+ def edge_detect(image):
34
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
35
+ edges = cv2.Canny(gray, 100, 200)
36
+ return cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
37
+
38
+ def enhance_face(image_pil):
39
+ from realesrgan import RealESRGAN
40
+ model = RealESRGAN("cpu", scale=2)
41
+ model.load_weights("RealESRGAN_x2.pth")
42
+ return model.predict(image_pil)
43
+
44
+ def save_video(images, filename="output_video.mp4"):
45
+ clip = ImageSequenceClip(images, fps=1)
46
+ clip.write_videofile(filename, codec="libx264")
47
+ return filename
48
+
49
+ if uploaded_files:
50
+ for file in uploaded_files:
51
+ img = Image.open(file).convert("RGB")
52
+ np_img = np.array(img)
53
+ np_img_bgr = cv2.cvtColor(np_img, cv2.COLOR_RGB2BGR)
54
+
55
+ if effect == "Cartoon":
56
+ result = cartoonize(np_img_bgr)
57
+ elif effect == "Edge Detection":
58
+ result = edge_detect(np_img_bgr)
59
+ elif effect == "Remove Background":
60
+ result = remove(np_img)
61
+ elif effect == "Face Enhancement":
62
+ result = enhance_face(img)
63
+ else:
64
+ result = np_img_bgr
65
+
66
+ result_rgb = cv2.cvtColor(result, cv2.COLOR_BGR2RGB) if isinstance(result, np.ndarray) else result
67
+ final_img = Image.fromarray(result_rgb)
68
+
69
+ st.image(final_img, caption="Processed Image", use_column_width=True)
70
+
71
+ img_path = os.path.join(output_dir, f"{file.name}")
72
+ final_img.save(img_path)
73
+ processed_images.append(img_path)
74
+
75
+ # Convert to video
76
+ if st.button("🎞️ Convert to Video Slideshow"):
77
+ video_path = save_video(processed_images)
78
+ st.video(video_path)
79
+
80
+ # Download
81
+ for img_path in processed_images:
82
+ with open(img_path, "rb") as f:
83
+ st.download_button("Download Image", f, file_name=os.path.basename(img_path))
84
+