Spaces:
Runtime error
Runtime error
File size: 3,635 Bytes
0dcc24a 282acb4 0dcc24a cedc74a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
import streamlit as st
import cv2
from PIL import Image
import os
import requests
from io import BytesIO
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Your Google API Key from the .env file
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
st.set_page_config(page_title="Insightly")
st.sidebar.image("https://i.ibb.co/bX6GdqG/insightly-wbg.png", use_column_width=True)
st.title("Frame Capturer ๐ธ")
uploaded_video = st.file_uploader("Choose video", type=["mp4", "mov"])
frame_skip = 150 # display every 150 frames
# Add custom CSS to increase space between images
st.markdown(
"""
<style>
.image-container {
margin-bottom: 60px;
}
.sidebar-link {
display: flex;
justify-content: left;
font-size: 28px;
margin-top: 20px;
}
</style>
""",
unsafe_allow_html=True,
)
# Function to perform reverse image search using the Google API
def reverse_image_search(image_bytes):
url = "https://www.googleapis.com/customsearch/v1"
params = {
"key": GOOGLE_API_KEY,
"cx": "015419011015122782581:_3y6h7e8b9q",
"q": "image",
"searchType": "image",
"imgSize": "large",
"num": 5,
}
response = requests.post(url, params=params, files={"file": image_bytes})
if response.ok:
data = response.json()
return data.get("items", [])
else:
return []
if uploaded_video is not None: # run only when the user uploads a video
vid = uploaded_video.name
with open(vid, mode='wb') as f:
f.write(uploaded_video.read()) # save video to disk
st.markdown(f"""
### Files
- {vid}
""",
unsafe_allow_html=True) # display file name
vidcap = cv2.VideoCapture(vid) # load video from disk
cur_frame = 0
success = True
while success:
success, frame = vidcap.read() # get the next frame from the video
if cur_frame % frame_skip == 0: # only analyze every n=300 frames
print('frame: {}'.format(cur_frame))
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # convert BGR to RGB
pil_img = Image.fromarray(frame_rgb) # convert the OpenCV frame (with type()==numpy) into PIL Image
# Add custom class to the image container for applying CSS
st.image(pil_img, channels='RGB', use_column_width=True, caption=f"Frame {cur_frame}")
# Get the bytes of the current image
image_bytes = BytesIO()
pil_img.save(image_bytes, format="JPEG")
if st.button("Reverse Image Search", key=f"search_{cur_frame}"):
results = reverse_image_search(image_bytes.getvalue())
if results:
st.markdown("### Reverse Image Search Results:")
for result in results:
st.image(result["link"], use_column_width=True, caption=result["title"])
st.markdown(
"""
<div class="image-container"></div>
""",
unsafe_allow_html=True,
)
cur_frame += 1
# Add link to the sidebar
st.sidebar.markdown("<p class='sidebar-link'>๐ <a href='https://insightly-csv-bot.hf.space/'> CSV Bot</a></p>", unsafe_allow_html=True)
st.sidebar.markdown("<p class='sidebar-link'>๐ <a href='https://chandrakalagowda-demo2.hf.space/'> PDF Bot </a></p>", unsafe_allow_html=True)
st.sidebar.markdown("<p class='sidebar-link'>๐ผ๏ธ <a href='https://insightly-image-reader.hf.space'> Image Reader</a></p>", unsafe_allow_html=True) |