Spaces:
Sleeping
Sleeping
import gradio as gr | |
import requests | |
import yt_dlp | |
import cv2 | |
from google_img_source_search import ReverseImageSearcher | |
from PIL import Image | |
import os | |
import uuid | |
uid=uuid.uuid4() | |
size_js=""" | |
function imgSize(){ | |
var myImg = document.getElementsByClassName("my_im"); | |
var realWidth = myImg.naturalWidth; | |
var realHeight = myImg.naturalHeight; | |
alert("Original width=" + realWidth + ", " + "Original height=" + realHeight); | |
}""" | |
def goog(inp): | |
out = requests.get(https://serpapi.com/search.html?engine=google&q=data%3Aimage%2Fjpeg%3Bbase64%2C%2F9j%2F4AAQSkZJRgABAQAAAQABAAD%2F2wCEAAoHCBUUEhgVFBUZGRgYFR0ZFRwYGhweHBoaHxwZGhwYHBocIC4lJR4rIxwaJzsrLDExNTU1ISQ7QDs0QjA0NTEBDAwMEA8QHBISHj0rJSs0NDY9NDQ9ND80NDQ0NDQ2NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NP%2FAABEIALcBEwMBIgACEQEDEQH%2FxAAcAAEAAgMBAQEAAAAAAAAAAAAABQYDBAcBAgj%2FxAA5EAACAQIFAgQEBAUEAgMAAAABAgADEQQFEiExQVEGImFxEzKBkUKhsfAUI1LB0Qdi4fGSohYzgv%2FEABgBAQEBAQEAAAAAAAAAAAAAAAABAgME%2F8QAIREBAQACAgMAAgMAAAAAAAAAAAECERIhAzFBE1EicYH%2F2gAMAwEAAhEDEQA%2FAOzREQEREBERAREQEREBERAREQPImtjcYtJdTfQTBg81SodPyt0B6%2Bxk3N6XV1tIxESoREQEREBERAREQEREBERAREQEREBERAREQEREBERAREQEREBERAREQKt4qraalO%2FFifsRvNTEkadXt9PUR48Ol6DXtfUv6H%2FM1KTmpTNnBsLkW3%2Fz%2BU52b27Y3UlWfIsx%2BKulj5l6%2FwBQ7%2B8l5zjKswKOlrlgx4vuD6c2l%2FwuKWoupTcXse4I5BHQy43fVTy4cbuemzERNuRERAREQEREBERAREQEREBER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OaZdi1dbpYEGx9%2BDcfT9ZcvDVZijhiSqkaCexFyPpO3hz741y8uHXJOxPJ7PS4EREBERAREQEREBERATwiexAwlbT5tM8%2BGSXYxxaIlQnxUphgQRcHkT7i0CtYjwVg2fWKehjyUZlvfuL2mng%2FwDT%2FD0mZkqVPNzqKm3t5RLjaAJNRdqbmXggVUKrWKk8EoDbvtffaaWRf6enDMWXEXJG%2Fkt1vzqMv9otJxhyqjYzwErVVqrUCOOvww1z3IY8zLnngs4mmEeqLrwxS%2F5XtaXS09tJxxXlVDyXwRUoXDVlKkWCqmkD2t3m6fCbXJLht7qDe3Ft%2FX9%2Bst1p7JfHjVmVikYnwzXZbApyL3JsRYg%2FrtPG8MVT8yodt7sTvckEbbW2sJd7RaT8eK865k3hTHI7NTFJtRBYkkEgXG%2B3I%2FufSYsRl2PUnVhg55BV1twO%2FwBfvOpWi0v48U%2FJk5dhsDiyRqwzrtvcqf0b2m3QwWKvY0ntfr27frOjWnmmYvhxa%2FLk5hlPhPFCs9RwEDuzWDA2v0tx0E6Xl2HCUwo2tMmmfdOaxwxxu4zc7ZpliIE2y9iIgIiICIiAiIgIiICIiAnhiIHwyzHESj2IiVCIiAiIkCexEDyLRED0LGmIhTTGmIgNM8tEQAEyKIiB9T2IkCIiAiIgf%2F%2FZ&location=Austin%2C+Texas%2C+United+States&google_domain=google.com&gl=us&hl=en) | |
print (out) | |
def dl(inp): | |
out = None | |
out_file=[] | |
try: | |
inp_out=inp.replace("https://","") | |
inp_out=inp_out.replace("/","_").replace(".","_").replace("=","_").replace("?","_") | |
os.system(f'yt-dlp "{inp}" --trim-filenames 160 -o "{inp_out}.mp4" -S res,mp4 --recode mp4') | |
out = f"{inp_out}.mp4" | |
#out_ap = os.path.abspath(out_f) | |
#out = f'https://omnibus-reverse-image.hf.space/file={out_ap}' | |
print (out) | |
except Exception as e: | |
print (e) | |
#out = f'{e}' | |
return out | |
def process_vid(file): | |
new_video_in = str(file) | |
capture = cv2.VideoCapture(new_video_in) | |
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT)) | |
rev_img_searcher = ReverseImageSearcher() | |
html_out="" | |
try: | |
for i in range(frame_count-1): | |
print(i) | |
capture.set(cv2.CAP_PROP_POS_FRAMES, i) | |
ret, frame_f = capture.read(i) | |
cv2.imwrite(f"{uid}-vid_tmp{i}.png", frame_f) | |
out = os.path.abspath(f"{uid}-vid_tmp{i}.png") | |
out_url = f'https://omnibus-reverse-image.hf.space/file={out}' | |
print(out) | |
res = rev_img_searcher.search(out_url) | |
#print (res) | |
if len(res) > 0: | |
count = 0 | |
for search_item in res: | |
print (f'counting {count}') | |
count+=1 | |
out_dict={ | |
'Title': f'{search_item.page_title}', | |
'Site': f'{search_item.page_url}', | |
'Img': f'{search_item.image_url}', | |
} | |
print (dir(search_item)) | |
html_out = f"""{html_out} | |
<div> | |
Title: {search_item.page_title}<br> | |
Site: <a href='{search_item.page_url}' target='_blank' rel='noopener noreferrer'>{search_item.page_url}</a><br> | |
Img: <a href='{search_item.image_url}' target='_blank' rel='noopener noreferrer'>{search_item.image_url}</a><br> | |
<img class='my_im' src='{search_item.image_url}'><br> | |
</div>""" | |
return (gr.HTML(f'<h1>Total Found: {count}</h1><br>{html_out}')) | |
else: | |
pass | |
except Exception as e: | |
return (gr.HTML(f'{e}')) | |
return (gr.HTML('No frame matches found.')) | |
def process_im(file): | |
read_file = Image.open(file) | |
read_file.save(f"{uid}-tmp.png") | |
action_input = f"{uid}-tmp.png" | |
out = os.path.abspath(action_input) | |
out_url = f'https://omnibus-reverse-image.hf.space/file={out}' | |
return (out_url) | |
def rev_im(image_url): | |
#image_url = 'https://i.pinimg.com/originals/c4/50/35/c450352ac6ea8645ead206721673e8fb.png' | |
out_list = [] | |
out_im = [] | |
html_out = """""" | |
rev_img_searcher = ReverseImageSearcher() | |
res = rev_img_searcher.search(image_url) | |
count = 0 | |
for search_item in res: | |
count+=1 | |
out_dict={ | |
'Title': f'{search_item.page_title}', | |
'Site': f'{search_item.page_url}', | |
'Img': f'{search_item.image_url}', | |
} | |
print (dir(search_item)) | |
html_out = f"""{html_out} | |
<div> | |
Title: {search_item.page_title}<br> | |
Site: <a href='{search_item.page_url}' target='_blank' rel='noopener noreferrer'>{search_item.page_url}</a><br> | |
Img: <a href='{search_item.image_url}' target='_blank' rel='noopener noreferrer'>{search_item.image_url}</a><br> | |
<img class='my_im' src='{search_item.image_url}'><br> | |
</div>""" | |
return (gr.HTML(f'<h1>Total Found: {count}</h1><br>{html_out}')) | |
def shuf(tog): | |
if tog == "URL": | |
return gr.update(visible=True),gr.update(visible=False),gr.update(visible=False) | |
if tog == "Image": | |
return gr.update(visible=False),gr.update(visible=True),gr.update(visible=False) | |
if tog == "Video": | |
return gr.update(visible=False),gr.update(visible=False),gr.update(visible=True) | |
with gr.Blocks() as app: | |
with gr.Row(): | |
gr.Column() | |
with gr.Column(): | |
source_tog=gr.Radio(choices=["URL","Image","Video"],value="URL") | |
with gr.Box(visible=True) as url_box: | |
inp_url=gr.Textbox(label="Image URL") | |
go_btn_url=gr.Button() | |
with gr.Box(visible=False) as im_box: | |
inp_im=gr.Image(label="Search Image",type='filepath') | |
go_btn_im=gr.Button() | |
with gr.Box(visible=False) as vid_box: | |
vid_url=gr.Textbox(label="Video URL") | |
vid_url_btn=gr.Button("Load URL") | |
inp_vid=gr.Video(label="Search Video") | |
go_btn_vid=gr.Button() | |
gr.Column() | |
#paste_clip = gr.Button("Paste from Clipboard") | |
with gr.Row(): | |
html_out = gr.HTML("""""") | |
vid_url_btn.click(dl,vid_url,inp_vid) | |
source_tog.change(shuf,[source_tog],[url_box,im_box,vid_box]).then(goog,None,None) | |
inp_im.change(process_im,inp_im,[inp_url]) | |
go_btn_vid.click(process_vid,inp_vid,[html_out]) | |
go_btn_im.click(rev_im,inp_url,[html_out]) | |
go_btn_url.click(rev_im,inp_url,[html_out]) | |
app.launch() |