jaspermurphy1989 commited on
Commit
cb3ac6d
·
verified ·
1 Parent(s): bcdda3c

Update app.py

Browse files

Updated code and corrected video file correct

Files changed (1) hide show
  1. app.py +32 -8
app.py CHANGED
@@ -1,17 +1,41 @@
1
  import streamlit as st
2
- from PIL import Image
 
3
  import os
 
4
 
5
  st.set_page_config(page_title="AI Thumbnail Generator", layout="centered")
6
 
7
  st.title("🎯 AI-Based Thumbnail Generator")
8
- st.write("Upload a video frame (image) to generate a thumbnail.")
 
 
 
 
 
 
 
 
 
 
9
 
10
- uploaded_file = st.file_uploader("Upload Image Frame", type=["jpg", "png", "jpeg"])
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- if uploaded_file:
13
- st.image(uploaded_file, caption="Uploaded Frame", use_column_width=True)
 
 
14
 
15
- # Mock prediction (replace with your AI model later)
16
- st.success(" This is a good thumbnail candidate!")
17
- st.download_button("Download Thumbnail", uploaded_file, file_name="thumbnail.jpg")
 
1
  import streamlit as st
2
+ import cv2
3
+ import tempfile
4
  import os
5
+ from PIL import Image
6
 
7
  st.set_page_config(page_title="AI Thumbnail Generator", layout="centered")
8
 
9
  st.title("🎯 AI-Based Thumbnail Generator")
10
+ st.write("Upload a video to extract thumbnail-worthy frames using AI.")
11
+
12
+ uploaded_video = st.file_uploader("Upload Video File", type=["mp4", "mov", "avi", "mkv", "wmv"])
13
+
14
+ if uploaded_video is not None:
15
+ st.video(uploaded_video)
16
+ st.success("✅ Video uploaded successfully!")
17
+
18
+ # Save the uploaded video to a temp file
19
+ tfile = tempfile.NamedTemporaryFile(delete=False)
20
+ tfile.write(uploaded_video.read())
21
 
22
+ # Extract frames using OpenCV
23
+ vidcap = cv2.VideoCapture(tfile.name)
24
+ success, image = vidcap.read()
25
+ count = 0
26
+ thumbnail_frame = None
27
+ while success:
28
+ if count % 60 == 0: # extract every 2 seconds assuming 30 FPS
29
+ frame_path = f"frame_{count}.jpg"
30
+ cv2.imwrite(frame_path, image)
31
+ thumbnail_frame = frame_path # keep last extracted frame as mock thumbnail
32
+ success, image = vidcap.read()
33
+ count += 1
34
 
35
+ if thumbnail_frame:
36
+ st.subheader("Generated Thumbnail Frame")
37
+ image = Image.open(thumbnail_frame)
38
+ st.image(image, caption="Best Frame", use_column_width=True)
39
 
40
+ with open(thumbnail_frame, "rb") as file:
41
+ st.download_button("📥 Download Thumbnail", file, file_name="thumbnail.jpg")