Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,13 @@
|
|
| 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 |
uid = uuid.uuid4()
|
| 11 |
-
|
| 12 |
size_js = """
|
| 13 |
function imgSize(){
|
| 14 |
var myImg = document.getElementsByClassName("my_im");
|
|
@@ -17,193 +16,158 @@ size_js = """
|
|
| 17 |
alert("Original width=" + realWidth + ", " + "Original height=" + realHeight);
|
| 18 |
}"""
|
| 19 |
|
|
|
|
| 20 |
def dl(inp):
|
| 21 |
out = None
|
| 22 |
-
out_file = []
|
| 23 |
try:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
inp_out = inp.replace("https://", "")
|
| 28 |
-
inp_out = inp_out.replace("/", "_").replace(".", "_").replace("=", "_").replace("?", "_")
|
| 29 |
-
|
| 30 |
if "twitter" in inp:
|
| 31 |
-
os.system(f'yt-dlp "{inp}" --extractor-arg "twitter:api=syndication" --trim-filenames 160 -o "{
|
| 32 |
else:
|
| 33 |
-
os.system(f'yt-dlp "{inp}" --trim-filenames 160 -o "{
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
-
print(e)
|
| 39 |
return out, gr.HTML(""), "", ""
|
| 40 |
|
|
|
|
| 41 |
def process_vid(file, cur_frame, every_n):
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 54 |
-
rev_img_searcher = ReverseImageSearcher()
|
| 55 |
-
html_out = ""
|
| 56 |
-
count = int(every_n)
|
| 57 |
-
|
| 58 |
-
if cur_frame == "" or cur_frame is None:
|
| 59 |
-
start_frame = 0
|
| 60 |
-
else:
|
| 61 |
-
start_frame = int(cur_frame)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
for i in range(start_frame, frame_count-1):
|
| 67 |
if count == int(every_n):
|
| 68 |
count = 1
|
| 69 |
-
print(i)
|
| 70 |
capture.set(cv2.CAP_PROP_POS_FRAMES, i)
|
| 71 |
ret, frame_f = capture.read()
|
| 72 |
-
|
|
|
|
| 73 |
if not ret or frame_f is None:
|
|
|
|
| 74 |
continue
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
if not os.path.exists(temp_img_path):
|
| 80 |
continue
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
| 83 |
out_url = f'https://nymbo-reverse-image.hf.space/file={out}'
|
| 84 |
-
print(out)
|
| 85 |
-
|
|
|
|
| 86 |
res = rev_img_searcher.search(out_url)
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
if len(res) > 0:
|
| 90 |
for search_item in res:
|
| 91 |
-
print(f'counting {count}')
|
| 92 |
out_cnt += 1
|
| 93 |
-
html_out
|
| 94 |
<div>
|
| 95 |
Title: {search_item.page_title}<br>
|
| 96 |
-
Site: <a href='{search_item.page_url}' target='_blank'
|
| 97 |
-
Img: <a href='{search_item.image_url}' target='_blank'
|
| 98 |
<img class='my_im' src='{search_item.image_url}'><br>
|
| 99 |
</div>"""
|
| 100 |
-
|
| 101 |
-
capture.release()
|
| 102 |
-
return (gr.HTML(f'<h1>Total Found: {out_cnt}</h1><br>{html_out}'), f"Found frame: {i}", i+int(every_n))
|
| 103 |
count += 1
|
| 104 |
-
print(i+1)
|
| 105 |
-
|
| 106 |
-
capture.release()
|
| 107 |
-
|
| 108 |
except Exception as e:
|
| 109 |
-
return
|
| 110 |
-
|
| 111 |
-
return (gr.HTML('No frame matches found.'), "", "")
|
| 112 |
|
|
|
|
| 113 |
def process_im(file, url):
|
| 114 |
if not url.startswith("https://nymbo"):
|
| 115 |
return url
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
print(f"Error in process_im: {str(e)}")
|
| 127 |
-
return url
|
| 128 |
|
|
|
|
| 129 |
def rev_im(image):
|
| 130 |
try:
|
| 131 |
-
os.makedirs(str(uid), exist_ok=True)
|
| 132 |
-
output_path = f"{uid}/im_tmp.png"
|
| 133 |
-
|
| 134 |
image = cv2.imread(image)
|
| 135 |
-
if image is None:
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
out = os.path.abspath(output_path)
|
| 144 |
-
out_url = f'https://nymbo-reverse-image.hf.space/file={out}'
|
| 145 |
-
|
| 146 |
rev_img_searcher = ReverseImageSearcher()
|
| 147 |
res = rev_img_searcher.search(out_url)
|
| 148 |
-
count = 0
|
| 149 |
html_out = ""
|
| 150 |
-
|
| 151 |
for search_item in res:
|
| 152 |
count += 1
|
| 153 |
-
html_out
|
| 154 |
<div>
|
| 155 |
Title: {search_item.page_title}<br>
|
| 156 |
-
Site: <a href='{search_item.page_url}' target='_blank'
|
| 157 |
-
Img: <a href='{search_item.image_url}' target='_blank'
|
| 158 |
<img class='my_im' src='{search_item.image_url}'><br>
|
| 159 |
</div>"""
|
| 160 |
-
|
| 161 |
return gr.HTML(f'<h1>Total Found: {count}</h1><br>{html_out}')
|
| 162 |
-
|
| 163 |
except Exception as e:
|
| 164 |
-
|
|
|
|
| 165 |
|
|
|
|
| 166 |
with gr.Blocks() as app:
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
with gr.Row():
|
| 184 |
-
go_btn_vid = gr.Button("Start")
|
| 185 |
-
next_btn = gr.Button("Next")
|
| 186 |
-
gr.Column()
|
| 187 |
-
|
| 188 |
-
with gr.Row():
|
| 189 |
-
html_out = gr.HTML("")
|
| 190 |
-
with gr.Row(visible=False):
|
| 191 |
-
hid_box = gr.Textbox()
|
| 192 |
-
|
| 193 |
def shuf(tog):
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
def load_image(url):
|
| 200 |
-
return url
|
| 201 |
-
|
| 202 |
-
im_load = load_im_btn.click(load_image, inp_url, inp_im)
|
| 203 |
-
next_btn.click(process_vid, [inp_vid, hid_box, every_n], [html_out, stat_box, hid_box])
|
| 204 |
-
vid_load = vid_url_btn.click(dl, vid_url, [inp_vid, html_out, stat_box, hid_box])
|
| 205 |
-
vid_proc = go_btn_vid.click(process_vid, [inp_vid, hid_box, every_n], [html_out, stat_box, hid_box])
|
| 206 |
im_proc = go_btn_im.click(rev_im, inp_im, [html_out])
|
| 207 |
-
source_tog.change(shuf, [source_tog], [im_box, vid_box]
|
| 208 |
|
| 209 |
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 |
uid = uuid.uuid4()
|
|
|
|
| 11 |
size_js = """
|
| 12 |
function imgSize(){
|
| 13 |
var myImg = document.getElementsByClassName("my_im");
|
|
|
|
| 16 |
alert("Original width=" + realWidth + ", " + "Original height=" + realHeight);
|
| 17 |
}"""
|
| 18 |
|
| 19 |
+
# Function to download video from URL
|
| 20 |
def dl(inp):
|
| 21 |
out = None
|
|
|
|
| 22 |
try:
|
| 23 |
+
inp_out = inp.replace("https://", "").replace("/", "_").replace(".", "_").replace("=", "_").replace("?", "_")
|
| 24 |
+
video_path = f"{uid}/{inp_out}.mp4"
|
| 25 |
+
|
|
|
|
|
|
|
|
|
|
| 26 |
if "twitter" in inp:
|
| 27 |
+
os.system(f'yt-dlp "{inp}" --extractor-arg "twitter:api=syndication" --trim-filenames 160 -o "{video_path}" -S res,mp4 --recode mp4')
|
| 28 |
else:
|
| 29 |
+
os.system(f'yt-dlp "{inp}" --trim-filenames 160 -o "{video_path}" -S res,mp4 --recode mp4')
|
| 30 |
+
|
| 31 |
+
# Verify video download
|
| 32 |
+
if not os.path.exists(video_path):
|
| 33 |
+
print(f"Downloaded video {video_path} does not exist.")
|
| 34 |
+
return None, gr.HTML("Download failed."), "", ""
|
| 35 |
+
if os.path.getsize(video_path) == 0:
|
| 36 |
+
print(f"Downloaded video {video_path} is empty.")
|
| 37 |
+
return None, gr.HTML("Downloaded video is empty."), "", ""
|
| 38 |
+
|
| 39 |
+
out = video_path
|
| 40 |
+
print(f"Video downloaded successfully: {out}")
|
| 41 |
except Exception as e:
|
| 42 |
+
print(f"Error downloading video: {e}")
|
| 43 |
return out, gr.HTML(""), "", ""
|
| 44 |
|
| 45 |
+
# Function to process video frames
|
| 46 |
def process_vid(file, cur_frame, every_n):
|
| 47 |
+
if not file:
|
| 48 |
+
return gr.HTML("No video file provided."), "", ""
|
| 49 |
+
|
| 50 |
+
new_video_in = str(file)
|
| 51 |
+
capture = cv2.VideoCapture(new_video_in)
|
| 52 |
+
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 53 |
+
rev_img_searcher = ReverseImageSearcher()
|
| 54 |
+
html_out = ""
|
| 55 |
+
count = int(every_n)
|
| 56 |
+
start_frame = int(cur_frame) if cur_frame else 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
try:
|
| 59 |
+
for i in range(start_frame, frame_count - 1):
|
|
|
|
|
|
|
| 60 |
if count == int(every_n):
|
| 61 |
count = 1
|
|
|
|
| 62 |
capture.set(cv2.CAP_PROP_POS_FRAMES, i)
|
| 63 |
ret, frame_f = capture.read()
|
| 64 |
+
|
| 65 |
+
# Validate frame read
|
| 66 |
if not ret or frame_f is None:
|
| 67 |
+
print(f"Failed to read frame at index {i}.")
|
| 68 |
continue
|
| 69 |
+
|
| 70 |
+
# Check if frame is empty
|
| 71 |
+
if frame_f.size == 0:
|
| 72 |
+
print(f"Frame at index {i} is empty.")
|
|
|
|
| 73 |
continue
|
| 74 |
+
|
| 75 |
+
frame_path = f"{uid}-vid_tmp{i}.png"
|
| 76 |
+
cv2.imwrite(frame_path, frame_f)
|
| 77 |
+
out = os.path.abspath(frame_path)
|
| 78 |
out_url = f'https://nymbo-reverse-image.hf.space/file={out}'
|
| 79 |
+
print(f"Frame saved: {out}")
|
| 80 |
+
|
| 81 |
+
# Perform reverse image search
|
| 82 |
res = rev_img_searcher.search(out_url)
|
| 83 |
+
if res:
|
| 84 |
+
out_cnt = 0
|
|
|
|
| 85 |
for search_item in res:
|
|
|
|
| 86 |
out_cnt += 1
|
| 87 |
+
html_out += f"""
|
| 88 |
<div>
|
| 89 |
Title: {search_item.page_title}<br>
|
| 90 |
+
Site: <a href='{search_item.page_url}' target='_blank'>{search_item.page_url}</a><br>
|
| 91 |
+
Img: <a href='{search_item.image_url}' target='_blank'>{search_item.image_url}</a><br>
|
| 92 |
<img class='my_im' src='{search_item.image_url}'><br>
|
| 93 |
</div>"""
|
| 94 |
+
return gr.HTML(f'<h1>Total Found: {out_cnt}</h1><br>{html_out}'), f"Found frame: {i}", i + int(every_n)
|
|
|
|
|
|
|
| 95 |
count += 1
|
| 96 |
+
print(f"Processed frame: {i + 1}")
|
|
|
|
|
|
|
|
|
|
| 97 |
except Exception as e:
|
| 98 |
+
return gr.HTML(f'Error during video processing: {e}'), "", ""
|
| 99 |
+
return gr.HTML('No frame matches found.'), "", ""
|
|
|
|
| 100 |
|
| 101 |
+
# Function to process image input
|
| 102 |
def process_im(file, url):
|
| 103 |
if not url.startswith("https://nymbo"):
|
| 104 |
return url
|
| 105 |
+
try:
|
| 106 |
+
read_file = Image.open(file)
|
| 107 |
+
read_file.save(f"{uid}-tmp.png")
|
| 108 |
+
action_input = f"{uid}-tmp.png"
|
| 109 |
+
out = os.path.abspath(action_input)
|
| 110 |
+
out_url = f'https://nymbo-reverse-image.hf.space/file={out}'
|
| 111 |
+
return out_url
|
| 112 |
+
except Exception as e:
|
| 113 |
+
print(f"Error processing image: {e}")
|
| 114 |
+
return None
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
# Function to perform reverse image search
|
| 117 |
def rev_im(image):
|
| 118 |
try:
|
|
|
|
|
|
|
|
|
|
| 119 |
image = cv2.imread(image)
|
| 120 |
+
if image is None or image.size == 0:
|
| 121 |
+
print("Error: Image is empty or invalid.")
|
| 122 |
+
return gr.HTML("Error: Invalid image.")
|
| 123 |
+
|
| 124 |
+
cv2.imwrite(f"{uid}-im_tmp.png", image)
|
| 125 |
+
out = os.path.abspath(f"{uid}-im_tmp.png")
|
| 126 |
+
out_url = f'https://nymbo-reverse-image.hf.space/file={out}'
|
| 127 |
+
|
|
|
|
|
|
|
|
|
|
| 128 |
rev_img_searcher = ReverseImageSearcher()
|
| 129 |
res = rev_img_searcher.search(out_url)
|
|
|
|
| 130 |
html_out = ""
|
| 131 |
+
count = 0
|
| 132 |
for search_item in res:
|
| 133 |
count += 1
|
| 134 |
+
html_out += f"""
|
| 135 |
<div>
|
| 136 |
Title: {search_item.page_title}<br>
|
| 137 |
+
Site: <a href='{search_item.page_url}' target='_blank'>{search_item.page_url}</a><br>
|
| 138 |
+
Img: <a href='{search_item.image_url}' target='_blank'>{search_item.image_url}</a><br>
|
| 139 |
<img class='my_im' src='{search_item.image_url}'><br>
|
| 140 |
</div>"""
|
|
|
|
| 141 |
return gr.HTML(f'<h1>Total Found: {count}</h1><br>{html_out}')
|
|
|
|
| 142 |
except Exception as e:
|
| 143 |
+
print(f"Error during reverse image search: {e}")
|
| 144 |
+
return gr.HTML(f"Error: {e}")
|
| 145 |
|
| 146 |
+
# Gradio app interface
|
| 147 |
with gr.Blocks() as app:
|
| 148 |
+
source_tog = gr.Radio(choices=["Image", "Video"], value="Image")
|
| 149 |
+
im_box = gr.Box(visible=True)
|
| 150 |
+
inp_url = gr.Textbox(label="Image URL")
|
| 151 |
+
load_im_btn = gr.Button("Load Image")
|
| 152 |
+
inp_im = gr.Image(label="Search Image", type='filepath')
|
| 153 |
+
go_btn_im = gr.Button()
|
| 154 |
+
vid_box = gr.Box(visible=False)
|
| 155 |
+
vid_url = gr.Textbox(label="Video URL")
|
| 156 |
+
vid_url_btn = gr.Button("Load URL")
|
| 157 |
+
inp_vid = gr.Video(label="Search Video")
|
| 158 |
+
every_n = gr.Number(label="Every /nth frame", value=10)
|
| 159 |
+
stat_box = gr.Textbox(label="Status")
|
| 160 |
+
go_btn_vid = gr.Button("Start")
|
| 161 |
+
next_btn = gr.Button("Next")
|
| 162 |
+
html_out = gr.HTML("")
|
| 163 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
def shuf(tog):
|
| 165 |
+
return (gr.update(visible=(tog == "Image")), gr.update(visible=(tog == "Video")))
|
| 166 |
+
|
| 167 |
+
im_load = load_im_btn.click(lambda url: url, inp_url, inp_im)
|
| 168 |
+
vid_load = vid_url_btn.click(dl, vid_url, [inp_vid, html_out, stat_box])
|
| 169 |
+
vid_proc = go_btn_vid.click(process_vid, [inp_vid, "", every_n], [html_out, stat_box])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
im_proc = go_btn_im.click(rev_im, inp_im, [html_out])
|
| 171 |
+
source_tog.change(shuf, [source_tog], [im_box, vid_box])
|
| 172 |
|
| 173 |
app.queue(concurrency_count=20).launch()
|