Spaces:
Build error
Build error
error handling, check for file, temp dir
Browse files
app.py
CHANGED
|
@@ -1,181 +1,173 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
import yt_dlp
|
| 4 |
import cv2
|
| 5 |
from google_img_source_search import ReverseImageSearcher
|
| 6 |
from PIL import Image
|
| 7 |
-
import os
|
| 8 |
import uuid
|
| 9 |
-
uid=uuid.uuid4()
|
| 10 |
-
size_js="""
|
| 11 |
-
function imgSize(){
|
| 12 |
-
var myImg = document.getElementsByClassName("my_im");
|
| 13 |
-
var realWidth = myImg.naturalWidth;
|
| 14 |
-
var realHeight = myImg.naturalHeight;
|
| 15 |
-
alert("Original width=" + realWidth + ", " + "Original height=" + realHeight);
|
| 16 |
-
}"""
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def dl(inp):
|
| 19 |
out = None
|
| 20 |
-
|
| 21 |
try:
|
| 22 |
-
inp_out=inp.replace("https://","")
|
| 23 |
-
inp_out=inp_out.replace("/","_").replace(".","_").replace("=","_").replace("?","_")
|
| 24 |
if "twitter" in inp:
|
| 25 |
-
os.system(f'yt-dlp "{inp}" --extractor-arg "twitter:api=syndication" --trim-filenames 160 -o "{uid}/{inp_out}.mp4" -S res,mp4 --recode mp4')
|
| 26 |
else:
|
| 27 |
-
os.system(f'yt-dlp "{inp}" --trim-filenames 160 -o "{uid}/{inp_out}.mp4" -S res,mp4 --recode mp4')
|
| 28 |
-
|
| 29 |
-
#os.system(f'yt-dlp "{inp}" --trim-filenames 160 -o "{inp_out}.mp4" -S res,mp4 --recode mp4')
|
| 30 |
out = f"{uid}/{inp_out}.mp4"
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
-
print
|
| 36 |
-
|
| 37 |
-
return out,gr.HTML(""),"",""
|
| 38 |
|
| 39 |
-
|
|
|
|
| 40 |
new_video_in = str(file)
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 43 |
rev_img_searcher = ReverseImageSearcher()
|
| 44 |
-
html_out=""
|
| 45 |
count = int(every_n)
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
start_frame = int(cur_frame)
|
| 50 |
try:
|
| 51 |
-
for i in range(start_frame, frame_count-1):
|
| 52 |
if count == int(every_n):
|
| 53 |
count = 1
|
| 54 |
-
print(i)
|
| 55 |
capture.set(cv2.CAP_PROP_POS_FRAMES, i)
|
| 56 |
-
ret, frame_f = capture.read(
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
res = rev_img_searcher.search(out_url)
|
| 62 |
-
|
| 63 |
-
out_cnt =0
|
| 64 |
if len(res) > 0:
|
| 65 |
-
|
| 66 |
for search_item in res:
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
out_dict={
|
| 70 |
-
'Title': f'{search_item.page_title}',
|
| 71 |
-
'Site': f'{search_item.page_url}',
|
| 72 |
-
'Img': f'{search_item.image_url}',
|
| 73 |
-
}
|
| 74 |
-
print (dir(search_item))
|
| 75 |
-
html_out = f"""{html_out}
|
| 76 |
<div>
|
| 77 |
Title: {search_item.page_title}<br>
|
| 78 |
-
Site: <a href='{search_item.page_url}' target='_blank'
|
| 79 |
-
Img: <a href='{search_item.image_url}' target='_blank'
|
| 80 |
<img class='my_im' src='{search_item.image_url}'><br>
|
| 81 |
-
</div>
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
count +=1
|
| 86 |
-
print (i+1)
|
| 87 |
-
#return (None,f"Searching Frame: {i}", "")
|
| 88 |
except Exception as e:
|
| 89 |
-
|
| 90 |
-
|
|
|
|
| 91 |
|
| 92 |
-
|
|
|
|
| 93 |
if not url.startswith("https://nymbo"):
|
| 94 |
return url
|
| 95 |
-
|
| 96 |
read_file = Image.open(file)
|
| 97 |
read_file.save(f"{uid}-tmp.png")
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
| 102 |
|
|
|
|
| 103 |
def rev_im(image):
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
html_out = """"""
|
| 108 |
-
image=cv2.imread(image)
|
| 109 |
-
cv2.imwrite(f"{uid}-im_tmp.png", image)
|
| 110 |
-
out = os.path.abspath(f"{uid}-im_tmp.png")
|
| 111 |
-
out_url = f'https://nymbo-reverse-image.hf.space/file={out}'
|
| 112 |
-
rev_img_searcher = ReverseImageSearcher()
|
| 113 |
-
res = rev_img_searcher.search(out_url)
|
| 114 |
-
count = 0
|
| 115 |
-
for search_item in res:
|
| 116 |
-
count+=1
|
| 117 |
-
out_dict={
|
| 118 |
-
'Title': f'{search_item.page_title}',
|
| 119 |
-
'Site': f'{search_item.page_url}',
|
| 120 |
-
'Img': f'{search_item.image_url}',
|
| 121 |
-
}
|
| 122 |
-
print (dir(search_item))
|
| 123 |
-
html_out = f"""{html_out}
|
| 124 |
-
<div>
|
| 125 |
-
Title: {search_item.page_title}<br>
|
| 126 |
-
Site: <a href='{search_item.page_url}' target='_blank' rel='noopener noreferrer'>{search_item.page_url}</a><br>
|
| 127 |
-
Img: <a href='{search_item.image_url}' target='_blank' rel='noopener noreferrer'>{search_item.image_url}</a><br>
|
| 128 |
-
<img class='my_im' src='{search_item.image_url}'><br>
|
| 129 |
-
</div>"""
|
| 130 |
-
|
| 131 |
-
return (gr.HTML(f'<h1>Total Found: {count}</h1><br>{html_out}'))
|
| 132 |
-
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
|
|
|
| 137 |
with gr.Blocks() as app:
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
go_btn_vid=gr.Button("Start")
|
| 157 |
-
next_btn=gr.Button("Next")
|
| 158 |
-
|
| 159 |
-
gr.Column()
|
| 160 |
-
#paste_clip = gr.Button("Paste from Clipboard")
|
| 161 |
-
with gr.Row():
|
| 162 |
-
html_out = gr.HTML("""""")
|
| 163 |
-
with gr.Row(visible=False):
|
| 164 |
-
hid_box=gr.Textbox()
|
| 165 |
-
def shuf(tog):
|
| 166 |
-
if tog == "Image":
|
| 167 |
-
return gr.update(visible=True),gr.update(visible=False)
|
| 168 |
-
if tog == "Video":
|
| 169 |
-
return gr.update(visible=False),gr.update(visible=True)
|
| 170 |
-
def load_image(url):
|
| 171 |
-
return url
|
| 172 |
-
im_load = load_im_btn.click(load_image,inp_url,inp_im)
|
| 173 |
-
next_btn.click(process_vid,[inp_vid,hid_box,every_n],[html_out,stat_box,hid_box])
|
| 174 |
-
vid_load = vid_url_btn.click(dl,vid_url,[inp_vid,html_out,stat_box,hid_box])
|
| 175 |
-
#inp_im.change(process_im,[inp_im,inp_url],[inp_url])
|
| 176 |
-
vid_proc = go_btn_vid.click(process_vid,[inp_vid,hid_box,every_n],[html_out,stat_box,hid_box])
|
| 177 |
-
im_proc = go_btn_im.click(rev_im,inp_im,[html_out])
|
| 178 |
-
source_tog.change(shuf,[source_tog],[im_box,vid_box],cancels=[vid_proc,im_proc,im_load,vid_load])
|
| 179 |
-
|
| 180 |
-
#go_btn_url.click(rev_im,inp_url,[html_out])
|
| 181 |
app.queue(concurrency_count=20).launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
import yt_dlp
|
| 4 |
import cv2
|
| 5 |
from google_img_source_search import ReverseImageSearcher
|
| 6 |
from PIL import Image
|
| 7 |
+
import os
|
| 8 |
import uuid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Generate a unique identifier for temporary file storage
|
| 11 |
+
uid = str(uuid.uuid4())
|
| 12 |
+
|
| 13 |
+
# Ensure the temporary directory exists
|
| 14 |
+
if not os.path.exists(uid):
|
| 15 |
+
os.makedirs(uid)
|
| 16 |
+
|
| 17 |
+
size_js = """
|
| 18 |
+
function imgSize(){
|
| 19 |
+
var myImg = document.getElementsByClassName("my_im");
|
| 20 |
+
var realWidth = myImg.naturalWidth;
|
| 21 |
+
var realHeight = myImg.naturalHeight;
|
| 22 |
+
alert("Original width=" + realWidth + ", " + "Original height=" + realHeight);
|
| 23 |
+
}
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
# Function to download video using yt-dlp
|
| 27 |
def dl(inp):
|
| 28 |
out = None
|
| 29 |
+
inp_out = inp.replace("https://", "").replace("/", "_").replace(".", "_").replace("=", "_").replace("?", "_")
|
| 30 |
try:
|
|
|
|
|
|
|
| 31 |
if "twitter" in inp:
|
| 32 |
+
os.system(f'yt-dlp "{inp}" --extractor-arg "twitter:api=syndication" --trim-filenames 160 -o "{uid}/{inp_out}.mp4" -S res,mp4 --recode mp4')
|
| 33 |
else:
|
| 34 |
+
os.system(f'yt-dlp "{inp}" --trim-filenames 160 -o "{uid}/{inp_out}.mp4" -S res,mp4 --recode mp4')
|
| 35 |
+
|
|
|
|
| 36 |
out = f"{uid}/{inp_out}.mp4"
|
| 37 |
+
if not os.path.exists(out):
|
| 38 |
+
print("Error: Video download failed. File not found.")
|
| 39 |
+
return None, gr.HTML("<h1>Error: Video download failed.</h1>"), "", ""
|
| 40 |
+
|
| 41 |
+
print(f"Downloaded video file: {out}")
|
| 42 |
except Exception as e:
|
| 43 |
+
print(f"Exception during video download: {e}")
|
| 44 |
+
return None, gr.HTML(f"<h1>Error: {e}</h1>"), "", ""
|
| 45 |
+
return out, gr.HTML(""), "", ""
|
| 46 |
|
| 47 |
+
# Function to process video and perform reverse image search
|
| 48 |
+
def process_vid(file, cur_frame, every_n):
|
| 49 |
new_video_in = str(file)
|
| 50 |
+
if not os.path.exists(new_video_in):
|
| 51 |
+
print("Error: Video file does not exist.")
|
| 52 |
+
return gr.HTML("<h1>Error: Video file not found.</h1>"), "", ""
|
| 53 |
+
|
| 54 |
+
capture = cv2.VideoCapture(new_video_in)
|
| 55 |
+
if not capture.isOpened():
|
| 56 |
+
print("Error: Video file could not be opened.")
|
| 57 |
+
return gr.HTML("<h1>Error: Failed to open video file.</h1>"), "", ""
|
| 58 |
+
|
| 59 |
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 60 |
rev_img_searcher = ReverseImageSearcher()
|
| 61 |
+
html_out = ""
|
| 62 |
count = int(every_n)
|
| 63 |
+
|
| 64 |
+
start_frame = int(cur_frame) if cur_frame else 0
|
| 65 |
+
|
|
|
|
| 66 |
try:
|
| 67 |
+
for i in range(start_frame, frame_count - 1):
|
| 68 |
if count == int(every_n):
|
| 69 |
count = 1
|
|
|
|
| 70 |
capture.set(cv2.CAP_PROP_POS_FRAMES, i)
|
| 71 |
+
ret, frame_f = capture.read()
|
| 72 |
+
|
| 73 |
+
if not ret:
|
| 74 |
+
print(f"Error: Failed to read frame at index {i}.")
|
| 75 |
+
continue
|
| 76 |
+
|
| 77 |
+
frame_path = f"{uid}-vid_tmp{i}.png"
|
| 78 |
+
cv2.imwrite(frame_path, frame_f)
|
| 79 |
+
|
| 80 |
+
if not os.path.exists(frame_path):
|
| 81 |
+
print(f"Error: Failed to save frame at index {i}.")
|
| 82 |
+
continue
|
| 83 |
+
|
| 84 |
+
out_url = f'https://nymbo-reverse-image.hf.space/file={os.path.abspath(frame_path)}'
|
| 85 |
res = rev_img_searcher.search(out_url)
|
| 86 |
+
|
|
|
|
| 87 |
if len(res) > 0:
|
| 88 |
+
out_cnt = 0
|
| 89 |
for search_item in res:
|
| 90 |
+
out_cnt += 1
|
| 91 |
+
html_out += f"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
<div>
|
| 93 |
Title: {search_item.page_title}<br>
|
| 94 |
+
Site: <a href='{search_item.page_url}' target='_blank'>{search_item.page_url}</a><br>
|
| 95 |
+
Img: <a href='{search_item.image_url}' target='_blank'>{search_item.image_url}</a><br>
|
| 96 |
<img class='my_im' src='{search_item.image_url}'><br>
|
| 97 |
+
</div>
|
| 98 |
+
"""
|
| 99 |
+
return gr.HTML(f'<h1>Total Found: {out_cnt}</h1><br>{html_out}'), f"Found frame: {i}", i + int(every_n)
|
| 100 |
+
count += 1
|
|
|
|
|
|
|
|
|
|
| 101 |
except Exception as e:
|
| 102 |
+
print(f"Exception during video processing: {e}")
|
| 103 |
+
return gr.HTML(f"<h1>Error: {e}</h1>"), "", ""
|
| 104 |
+
return gr.HTML('No frame matches found.'), "", ""
|
| 105 |
|
| 106 |
+
# Function to process image file
|
| 107 |
+
def process_im(file, url):
|
| 108 |
if not url.startswith("https://nymbo"):
|
| 109 |
return url
|
| 110 |
+
try:
|
| 111 |
read_file = Image.open(file)
|
| 112 |
read_file.save(f"{uid}-tmp.png")
|
| 113 |
+
out_url = f'https://nymbo-reverse-image.hf.space/file={os.path.abspath(f"{uid}-tmp.png")}'
|
| 114 |
+
return out_url
|
| 115 |
+
except Exception as e:
|
| 116 |
+
print(f"Exception during image processing: {e}")
|
| 117 |
+
return gr.HTML(f"<h1>Error: {e}</h1>")
|
| 118 |
|
| 119 |
+
# Function to perform reverse image search
|
| 120 |
def rev_im(image):
|
| 121 |
+
if not os.path.exists(image):
|
| 122 |
+
print("Error: Image file does not exist.")
|
| 123 |
+
return gr.HTML("<h1>Error: Image file not found.</h1>")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
try:
|
| 126 |
+
image = cv2.imread(image)
|
| 127 |
+
if image is None:
|
| 128 |
+
print("Error: Failed to read image.")
|
| 129 |
+
return gr.HTML("<h1>Error: Could not read image.</h1>")
|
| 130 |
|
| 131 |
+
cv2.imwrite(f"{uid}-im_tmp.png", image)
|
| 132 |
+
out_url = f'https://nymbo-reverse-image.hf.space/file={os.path.abspath(f"{uid}-im_tmp.png")}'
|
| 133 |
+
rev_img_searcher = ReverseImageSearcher()
|
| 134 |
+
res = rev_img_searcher.search(out_url)
|
| 135 |
|
| 136 |
+
html_out = ""
|
| 137 |
+
count = 0
|
| 138 |
+
for search_item in res:
|
| 139 |
+
count += 1
|
| 140 |
+
html_out += f"""
|
| 141 |
+
<div>
|
| 142 |
+
Title: {search_item.page_title}<br>
|
| 143 |
+
Site: <a href='{search_item.page_url}' target='_blank'>{search_item.page_url}</a><br>
|
| 144 |
+
Img: <a href='{search_item.image_url}' target='_blank'>{search_item.image_url}</a><br>
|
| 145 |
+
<img class='my_im' src='{search_item.image_url}'><br>
|
| 146 |
+
</div>
|
| 147 |
+
"""
|
| 148 |
+
return gr.HTML(f'<h1>Total Found: {count}</h1><br>{html_out}')
|
| 149 |
+
except Exception as e:
|
| 150 |
+
print(f"Exception during reverse image search: {e}")
|
| 151 |
+
return gr.HTML(f"<h1>Error: {e}</h1>")
|
| 152 |
|
| 153 |
+
# Gradio app layout
|
| 154 |
with gr.Blocks() as app:
|
| 155 |
+
source_tog = gr.Radio(choices=["Image", "Video"], value="Image")
|
| 156 |
+
inp_url = gr.Textbox(label="Image URL")
|
| 157 |
+
load_im_btn = gr.Button("Load Image")
|
| 158 |
+
inp_im = gr.Image(label="Search Image", type='filepath')
|
| 159 |
+
go_btn_im = gr.Button("Search Image")
|
| 160 |
+
vid_url = gr.Textbox(label="Video URL")
|
| 161 |
+
vid_url_btn = gr.Button("Load Video")
|
| 162 |
+
inp_vid = gr.Video(label="Search Video")
|
| 163 |
+
every_n = gr.Number(label="Every nth Frame", value=10)
|
| 164 |
+
go_btn_vid = gr.Button("Start Video Search")
|
| 165 |
+
html_out = gr.HTML("")
|
| 166 |
+
|
| 167 |
+
source_tog.change(lambda x: (gr.update(visible=x == "Image"), gr.update(visible=x == "Video")), [source_tog], [inp_im, inp_vid])
|
| 168 |
+
load_im_btn.click(lambda url: url, inp_url, inp_im)
|
| 169 |
+
go_btn_im.click(rev_im, inp_im, html_out)
|
| 170 |
+
vid_url_btn.click(dl, vid_url, [inp_vid, html_out])
|
| 171 |
+
go_btn_vid.click(process_vid, [inp_vid, "", every_n], [html_out])
|
| 172 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
app.queue(concurrency_count=20).launch()
|