Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,35 @@
|
|
| 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 |
-
|
|
|
|
| 11 |
|
|
|
|
| 12 |
size_js = """
|
| 13 |
-
function imgSize(){
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
}
|
| 19 |
"""
|
| 20 |
|
| 21 |
def dl(inp):
|
| 22 |
out = None
|
| 23 |
try:
|
| 24 |
-
inp_out = inp.replace("https://", "")
|
| 25 |
-
inp_out = inp_out.replace("/", "_").replace(".", "_").replace("=", "_").replace("?", "_")
|
| 26 |
-
output_path = f'{uid}/{inp_out}.mp4'
|
| 27 |
-
|
| 28 |
-
ydl_opts = {
|
| 29 |
-
'outtmpl': output_path,
|
| 30 |
-
'format': 'mp4',
|
| 31 |
-
'recodevideo': 'mp4',
|
| 32 |
-
'trim_file_name': 160,
|
| 33 |
-
'postprocessors': [{
|
| 34 |
-
'key': 'FFmpegVideoConvertor',
|
| 35 |
-
'preferedformat': 'mp4',
|
| 36 |
-
}],
|
| 37 |
-
}
|
| 38 |
-
|
| 39 |
if "twitter" in inp:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
out = output_path
|
| 46 |
print(out)
|
| 47 |
except Exception as e:
|
| 48 |
print(e)
|
|
@@ -53,36 +40,33 @@ def process_vid(file, cur_frame, every_n):
|
|
| 53 |
if not os.path.isfile(new_video_in):
|
| 54 |
print(f"Video file {new_video_in} does not exist.")
|
| 55 |
return (gr.HTML('Video file not found.'), "", "")
|
| 56 |
-
|
| 57 |
capture = cv2.VideoCapture(new_video_in)
|
| 58 |
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 59 |
rev_img_searcher = ReverseImageSearcher()
|
| 60 |
html_out = ""
|
| 61 |
count = int(every_n)
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
else:
|
| 65 |
-
start_frame = int(cur_frame)
|
| 66 |
-
|
| 67 |
try:
|
| 68 |
for i in range(start_frame, frame_count - 1):
|
| 69 |
if count == int(every_n):
|
| 70 |
count = 1
|
| 71 |
-
print(
|
| 72 |
capture.set(cv2.CAP_PROP_POS_FRAMES, i)
|
| 73 |
ret, frame_f = capture.read()
|
| 74 |
if not ret:
|
| 75 |
print(f"Failed to read frame {i}")
|
| 76 |
continue
|
| 77 |
-
|
| 78 |
-
cv2.imwrite(
|
| 79 |
-
out = os.path.abspath(
|
| 80 |
out_url = f'https://nymbo-reverse-image.hf.space/file={out}'
|
| 81 |
-
print(
|
| 82 |
-
|
| 83 |
res = rev_img_searcher.search(out_url)
|
| 84 |
out_cnt = 0
|
| 85 |
-
if
|
| 86 |
for search_item in res:
|
| 87 |
out_cnt += 1
|
| 88 |
html_out += f"""
|
|
@@ -92,12 +76,13 @@ def process_vid(file, cur_frame, every_n):
|
|
| 92 |
Img: <a href='{search_item.image_url}' target='_blank' rel='noopener noreferrer'>{search_item.image_url}</a><br>
|
| 93 |
<img class='my_im' src='{search_item.image_url}'><br>
|
| 94 |
</div>"""
|
| 95 |
-
return
|
| 96 |
-
|
| 97 |
-
|
| 98 |
except Exception as e:
|
| 99 |
-
return
|
| 100 |
-
|
|
|
|
| 101 |
|
| 102 |
def process_im(file, url):
|
| 103 |
if not url.startswith("https://nymbo"):
|
|
@@ -113,6 +98,9 @@ def process_im(file, url):
|
|
| 113 |
def rev_im(image):
|
| 114 |
html_out = ""
|
| 115 |
image = cv2.imread(image)
|
|
|
|
|
|
|
|
|
|
| 116 |
cv2.imwrite(f"{uid}-im_tmp.png", image)
|
| 117 |
out = os.path.abspath(f"{uid}-im_tmp.png")
|
| 118 |
out_url = f'https://nymbo-reverse-image.hf.space/file={out}'
|
|
@@ -128,6 +116,7 @@ def rev_im(image):
|
|
| 128 |
Img: <a href='{search_item.image_url}' target='_blank' rel='noopener noreferrer'>{search_item.image_url}</a><br>
|
| 129 |
<img class='my_im' src='{search_item.image_url}'><br>
|
| 130 |
</div>"""
|
|
|
|
| 131 |
return gr.HTML(f'<h1>Total Found: {count}</h1><br>{html_out}')
|
| 132 |
|
| 133 |
with gr.Blocks() as app:
|
|
@@ -139,7 +128,7 @@ with gr.Blocks() as app:
|
|
| 139 |
inp_url = gr.Textbox(label="Image URL")
|
| 140 |
load_im_btn = gr.Button("Load Image")
|
| 141 |
inp_im = gr.Image(label="Search Image", type='filepath')
|
| 142 |
-
go_btn_im = gr.Button(
|
| 143 |
with gr.Box(visible=False) as vid_box:
|
| 144 |
vid_url = gr.Textbox(label="Video URL")
|
| 145 |
vid_url_btn = gr.Button("Load URL")
|
|
@@ -150,6 +139,7 @@ with gr.Blocks() as app:
|
|
| 150 |
with gr.Row():
|
| 151 |
go_btn_vid = gr.Button("Start")
|
| 152 |
next_btn = gr.Button("Next")
|
|
|
|
| 153 |
gr.Column()
|
| 154 |
with gr.Row():
|
| 155 |
html_out = gr.HTML("")
|
|
@@ -157,14 +147,11 @@ with gr.Blocks() as app:
|
|
| 157 |
hid_box = gr.Textbox()
|
| 158 |
|
| 159 |
def shuf(tog):
|
| 160 |
-
if tog == "Image"
|
| 161 |
-
|
| 162 |
-
if tog == "Video":
|
| 163 |
-
return gr.update(visible=False), gr.update(visible=True)
|
| 164 |
-
|
| 165 |
def load_image(url):
|
| 166 |
return url
|
| 167 |
-
|
| 168 |
im_load = load_im_btn.click(load_image, inp_url, inp_im)
|
| 169 |
next_btn.click(process_vid, [inp_vid, hid_box, every_n], [html_out, stat_box, hid_box])
|
| 170 |
vid_load = vid_url_btn.click(dl, vid_url, [inp_vid, html_out, stat_box, hid_box])
|
|
|
|
| 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 |
+
# Unique identifier for files
|
| 11 |
+
uid = uuid.uuid4()
|
| 12 |
|
| 13 |
+
# JavaScript for displaying image dimensions
|
| 14 |
size_js = """
|
| 15 |
+
function imgSize(){
|
| 16 |
+
var myImg = document.getElementsByClassName("my_im");
|
| 17 |
+
var realWidth = myImg.naturalWidth;
|
| 18 |
+
var realHeight = myImg.naturalHeight;
|
| 19 |
+
alert("Original width=" + realWidth + ", " + "Original height=" + realHeight);
|
| 20 |
+
}
|
| 21 |
"""
|
| 22 |
|
| 23 |
def dl(inp):
|
| 24 |
out = None
|
| 25 |
try:
|
| 26 |
+
inp_out = inp.replace("https://", "").replace("/", "_").replace(".", "_").replace("=", "_").replace("?", "_")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
if "twitter" in inp:
|
| 28 |
+
os.system(f'yt-dlp "{inp}" --extractor-arg "twitter:api=syndication" --trim-filenames 160 -o "{uid}/{inp_out}.mp4" -S res,mp4 --recode mp4')
|
| 29 |
+
else:
|
| 30 |
+
os.system(f'yt-dlp "{inp}" --trim-filenames 160 -o "{uid}/{inp_out}.mp4" -S res,mp4 --recode mp4')
|
| 31 |
+
|
| 32 |
+
out = f"{uid}/{inp_out}.mp4"
|
|
|
|
| 33 |
print(out)
|
| 34 |
except Exception as e:
|
| 35 |
print(e)
|
|
|
|
| 40 |
if not os.path.isfile(new_video_in):
|
| 41 |
print(f"Video file {new_video_in} does not exist.")
|
| 42 |
return (gr.HTML('Video file not found.'), "", "")
|
| 43 |
+
|
| 44 |
capture = cv2.VideoCapture(new_video_in)
|
| 45 |
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 46 |
rev_img_searcher = ReverseImageSearcher()
|
| 47 |
html_out = ""
|
| 48 |
count = int(every_n)
|
| 49 |
+
start_frame = int(cur_frame) if cur_frame else 0
|
| 50 |
+
|
|
|
|
|
|
|
|
|
|
| 51 |
try:
|
| 52 |
for i in range(start_frame, frame_count - 1):
|
| 53 |
if count == int(every_n):
|
| 54 |
count = 1
|
| 55 |
+
print(i)
|
| 56 |
capture.set(cv2.CAP_PROP_POS_FRAMES, i)
|
| 57 |
ret, frame_f = capture.read()
|
| 58 |
if not ret:
|
| 59 |
print(f"Failed to read frame {i}")
|
| 60 |
continue
|
| 61 |
+
|
| 62 |
+
cv2.imwrite(f"{uid}-vid_tmp{i}.png", frame_f)
|
| 63 |
+
out = os.path.abspath(f"{uid}-vid_tmp{i}.png")
|
| 64 |
out_url = f'https://nymbo-reverse-image.hf.space/file={out}'
|
| 65 |
+
print(out)
|
| 66 |
+
|
| 67 |
res = rev_img_searcher.search(out_url)
|
| 68 |
out_cnt = 0
|
| 69 |
+
if res:
|
| 70 |
for search_item in res:
|
| 71 |
out_cnt += 1
|
| 72 |
html_out += f"""
|
|
|
|
| 76 |
Img: <a href='{search_item.image_url}' target='_blank' rel='noopener noreferrer'>{search_item.image_url}</a><br>
|
| 77 |
<img class='my_im' src='{search_item.image_url}'><br>
|
| 78 |
</div>"""
|
| 79 |
+
return gr.HTML(f'<h1>Total Found: {out_cnt}</h1><br>{html_out}'), f"Found frame: {i}", i + int(every_n)
|
| 80 |
+
count += 1
|
| 81 |
+
print(i + 1)
|
| 82 |
except Exception as e:
|
| 83 |
+
return gr.HTML(f'{e}'), "", ""
|
| 84 |
+
|
| 85 |
+
return gr.HTML('No frame matches found.'), "", ""
|
| 86 |
|
| 87 |
def process_im(file, url):
|
| 88 |
if not url.startswith("https://nymbo"):
|
|
|
|
| 98 |
def rev_im(image):
|
| 99 |
html_out = ""
|
| 100 |
image = cv2.imread(image)
|
| 101 |
+
if image is None:
|
| 102 |
+
return gr.HTML("Failed to load image.")
|
| 103 |
+
|
| 104 |
cv2.imwrite(f"{uid}-im_tmp.png", image)
|
| 105 |
out = os.path.abspath(f"{uid}-im_tmp.png")
|
| 106 |
out_url = f'https://nymbo-reverse-image.hf.space/file={out}'
|
|
|
|
| 116 |
Img: <a href='{search_item.image_url}' target='_blank' rel='noopener noreferrer'>{search_item.image_url}</a><br>
|
| 117 |
<img class='my_im' src='{search_item.image_url}'><br>
|
| 118 |
</div>"""
|
| 119 |
+
|
| 120 |
return gr.HTML(f'<h1>Total Found: {count}</h1><br>{html_out}')
|
| 121 |
|
| 122 |
with gr.Blocks() as app:
|
|
|
|
| 128 |
inp_url = gr.Textbox(label="Image URL")
|
| 129 |
load_im_btn = gr.Button("Load Image")
|
| 130 |
inp_im = gr.Image(label="Search Image", type='filepath')
|
| 131 |
+
go_btn_im = gr.Button()
|
| 132 |
with gr.Box(visible=False) as vid_box:
|
| 133 |
vid_url = gr.Textbox(label="Video URL")
|
| 134 |
vid_url_btn = gr.Button("Load URL")
|
|
|
|
| 139 |
with gr.Row():
|
| 140 |
go_btn_vid = gr.Button("Start")
|
| 141 |
next_btn = gr.Button("Next")
|
| 142 |
+
|
| 143 |
gr.Column()
|
| 144 |
with gr.Row():
|
| 145 |
html_out = gr.HTML("")
|
|
|
|
| 147 |
hid_box = gr.Textbox()
|
| 148 |
|
| 149 |
def shuf(tog):
|
| 150 |
+
return (gr.update(visible=True), gr.update(visible=False)) if tog == "Image" else (gr.update(visible=False), gr.update(visible=True))
|
| 151 |
+
|
|
|
|
|
|
|
|
|
|
| 152 |
def load_image(url):
|
| 153 |
return url
|
| 154 |
+
|
| 155 |
im_load = load_im_btn.click(load_image, inp_url, inp_im)
|
| 156 |
next_btn.click(process_vid, [inp_vid, hid_box, every_n], [html_out, stat_box, hid_box])
|
| 157 |
vid_load = vid_url_btn.click(dl, vid_url, [inp_vid, html_out, stat_box, hid_box])
|