Spaces:
Runtime error
Runtime error
| import requests | |
| import time | |
| from rembg import remove | |
| from PIL import Image | |
| import streamlit as st | |
| from bs4 import BeautifulSoup | |
| def google_api(word): | |
| # Start timer | |
| start_time = time.time() | |
| query = word | |
| num = 5 | |
| url = f'https://www.googleapis.com/customsearch/v1?q={query}&num={num}&searchType=image&key={api_key}&cx={cx}' | |
| response = requests.get(url) | |
| data = response.json() | |
| links = [item['link'] for item in data['items']] | |
| for i, link in enumerate(links): | |
| # Send a request to the image URL | |
| response = requests.get(link) | |
| # Save the image to a file | |
| image_path = f'image_{i}.png' | |
| with open(image_path, 'wb') as f: | |
| f.write(response.content) | |
| st.subheader(f"Image {i+1}:") | |
| try: | |
| # Remove the background from the image | |
| output_path = f'output_{i}.png' | |
| with Image.open(image_path) as input: | |
| # Open the downloaded image | |
| # Resize the image to 1000x1000 | |
| fixed_height = 1000 | |
| height_percent = (fixed_height / float(input.size[1])) | |
| width_size = int((float(input.size[0]) * float(height_percent))) | |
| img = input.resize((width_size, fixed_height),Image.NEAREST) | |
| # Save the resized image to the same file | |
| img.save(image_path) | |
| output = remove(img) | |
| output.save(output_path) | |
| image_list=[] | |
| image_list.append((Image.open(image_path), Image.open(output_path))) | |
| cols = st.columns(len(image_list[0])) | |
| for images in image_list: | |
| for col, image in zip(cols, images): | |
| col.image(image, use_column_width=True) | |
| except: | |
| st.error("An error occurred while processing the image.") | |
| continue | |
| # End timer and calculate elapsed time | |
| elapsed_time = time.time() - start_time | |
| st.success(f"Image processing completed in {elapsed_time:.2f} seconds.") | |
| def web_scrape(word): | |
| # Start timer | |
| start_time = time.time() | |
| word = word.replace(" ","+") | |
| URL = "https://www.google.com/search?q="+word+"&sxsrf=ALeKk03xBalIZi7BAzyIRw8R4_KrIEYONg:1620885765119&source=lnms&tbm=isch&sa=X&ved=2ahUKEwjv44CC_sXwAhUZyjgGHSgdAQ8Q_AUoAXoECAEQAw&cshid=1620885828054361" | |
| page = requests.get(URL) | |
| soup = BeautifulSoup(page.content, 'html.parser') | |
| image_tags = soup.find_all('img') | |
| links = [] | |
| i = 0 | |
| for image_tag in image_tags: | |
| if i == 0: | |
| i = i + 1 | |
| continue | |
| if i >= 6 : | |
| break | |
| else: | |
| links.append(image_tag['src']) | |
| i = i + 1 | |
| # Create a list to hold the original and output images | |
| for i, link in enumerate(links): | |
| # Send a request to the image URL | |
| response = requests.get(link) | |
| # Save the image to a file | |
| image_path = f'image_{i}.png' | |
| with open(image_path, 'wb') as f: | |
| f.write(response.content) | |
| st.subheader(f"Image {i+1}:") | |
| try: | |
| # Remove the background from the image | |
| output_path = f'output_{i}.png' | |
| with Image.open(image_path) as input: | |
| # Open the downloaded image | |
| # Resize the image | |
| # fixed_height = 100 | |
| # height_percent = (fixed_height / float(input.size[1])) | |
| # print(height_percent) | |
| # width_size = int((float(input.size[0]) * float(height_percent))) | |
| # print(width_size) | |
| # img = input.resize((width_size, fixed_height),Image.NEAREST) | |
| # # Save the resized image to the same file | |
| # img.save(image_path) | |
| output = remove(input) | |
| output.save(output_path) | |
| image_list = [] | |
| image_list.append((Image.open(image_path), Image.open(output_path))) | |
| cols = st.columns(len(image_list[0])) | |
| for images in image_list: | |
| for col, image in zip(cols, images): | |
| col.image(image, use_column_width=True) | |
| except: | |
| st.error("An error occurred while processing the image.") | |
| continue | |
| # End timer and calculate elapsed time | |
| elapsed_time = time.time() - start_time | |
| st.success(f"Image processing completed in {elapsed_time:.2f} seconds.") | |
| st.title("Image searcher") | |
| st.subheader("(with Background Remover)") | |
| api_key = 'AIzaSyAx4fTPrUVHPzN-BlDRBLkpd_BeIribnus' | |
| cx = '6daf46cdb49a3770b' | |
| word = st.text_input("Enter search term for images:") | |
| options = ["google-api", "web-scrapping"] | |
| selected_options = st.selectbox("Select options", options) | |
| if st.button("Submit"): | |
| if selected_options=="google-api": | |
| google_api(word) | |
| elif selected_options=="web-scrapping": | |
| web_scrape(word) | |
| else: | |
| st.error("CHOOSE OPTION!!!") | |