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 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}
"""
return (gr.HTML(f'Total Found: {count}
{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}
"""
return (gr.HTML(f'Total Found: {count}
{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])
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()