Spaces:
Runtime error
Runtime error
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: 30px; | |
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) |