Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,8 +6,8 @@ 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;
|
|
@@ -17,34 +17,27 @@ size_js="""
|
|
| 17 |
|
| 18 |
def dl(inp):
|
| 19 |
out = None
|
| 20 |
-
out_file=[]
|
| 21 |
try:
|
| 22 |
-
inp_out=inp.replace("https://","")
|
| 23 |
-
inp_out=inp_out.replace("/","_").replace(".","_").replace("=","_").replace("?","_")
|
| 24 |
-
print(f"Downloading video from URL: {inp}")
|
| 25 |
if "twitter" in inp:
|
| 26 |
os.system(f'yt-dlp "{inp}" --extractor-arg "twitter:api=syndication" --trim-filenames 160 -o "{uid}/{inp_out}.mp4" -S res,mp4 --recode mp4')
|
| 27 |
else:
|
| 28 |
os.system(f'yt-dlp "{inp}" --trim-filenames 160 -o "{uid}/{inp_out}.mp4" -S res,mp4 --recode mp4')
|
| 29 |
|
| 30 |
out = f"{uid}/{inp_out}.mp4"
|
| 31 |
-
print(
|
| 32 |
except Exception as e:
|
| 33 |
-
print(
|
| 34 |
-
return out,gr.HTML(""),"",""
|
| 35 |
|
| 36 |
-
def process_vid(file,cur_frame,every_n):
|
| 37 |
new_video_in = str(file)
|
| 38 |
-
print(f"Processing video file: {new_video_in}")
|
| 39 |
capture = cv2.VideoCapture(new_video_in)
|
| 40 |
-
if not capture.isOpened():
|
| 41 |
-
print(f"Error: Could not open video file {new_video_in}")
|
| 42 |
-
return (gr.HTML(f'Error: Could not open video file {new_video_in}'), "", "")
|
| 43 |
-
|
| 44 |
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 45 |
-
print(f"Total frames in video: {frame_count}")
|
| 46 |
rev_img_searcher = ReverseImageSearcher()
|
| 47 |
-
html_out=""
|
| 48 |
count = int(every_n)
|
| 49 |
if cur_frame == "" or cur_frame is None:
|
| 50 |
start_frame = 0
|
|
@@ -54,22 +47,18 @@ def process_vid(file,cur_frame,every_n):
|
|
| 54 |
for i in range(start_frame, frame_count-1):
|
| 55 |
if count == int(every_n):
|
| 56 |
count = 1
|
| 57 |
-
print(f"Processing frame: {i}")
|
| 58 |
capture.set(cv2.CAP_PROP_POS_FRAMES, i)
|
| 59 |
ret, frame_f = capture.read()
|
| 60 |
if not ret:
|
| 61 |
-
print(f"
|
| 62 |
continue
|
| 63 |
cv2.imwrite(f"{uid}-vid_tmp{i}.png", frame_f)
|
| 64 |
out = os.path.abspath(f"{uid}-vid_tmp{i}.png")
|
| 65 |
-
print(f"Saved frame {i} as image: {out}")
|
| 66 |
out_url = f'https://nymbo-reverse-image.hf.space/file={out}'
|
| 67 |
res = rev_img_searcher.search(out_url)
|
| 68 |
-
print(f"Reverse image search results for frame {i}: {res}")
|
| 69 |
out_cnt = 0
|
| 70 |
if len(res) > 0:
|
| 71 |
for search_item in res:
|
| 72 |
-
print(f'Found result {out_cnt + 1} for frame {i}')
|
| 73 |
out_cnt += 1
|
| 74 |
html_out = f"""{html_out}
|
| 75 |
<div>
|
|
@@ -80,17 +69,12 @@ def process_vid(file,cur_frame,every_n):
|
|
| 80 |
</div>"""
|
| 81 |
return (gr.HTML(f'<h1>Total Found: {out_cnt}</h1><br>{html_out}'), f"Found frame: {i}", i+int(every_n))
|
| 82 |
count += 1
|
| 83 |
-
print(f"Skipping frame {i + 1}")
|
| 84 |
except Exception as e:
|
| 85 |
-
print(f"Error during video processing: {e}")
|
| 86 |
return (gr.HTML(f'{e}'), "", "")
|
| 87 |
-
print("No matching frames found in video.")
|
| 88 |
return (gr.HTML('No frame matches found.'), "", "")
|
| 89 |
|
| 90 |
-
def process_im(file,url):
|
| 91 |
-
print(f"Processing image with URL: {url}")
|
| 92 |
if not url.startswith("https://nymbo"):
|
| 93 |
-
print(f"URL does not match required pattern: {url}")
|
| 94 |
return url
|
| 95 |
else:
|
| 96 |
read_file = Image.open(file)
|
|
@@ -98,28 +82,26 @@ def process_im(file,url):
|
|
| 98 |
action_input = f"{uid}-tmp.png"
|
| 99 |
out = os.path.abspath(action_input)
|
| 100 |
out_url = f'https://nymbo-reverse-image.hf.space/file={out}'
|
| 101 |
-
print(f"Processed image saved as: {out}")
|
| 102 |
return (out_url)
|
| 103 |
|
| 104 |
def rev_im(image):
|
| 105 |
-
|
|
|
|
|
|
|
| 106 |
image = cv2.imread(image)
|
| 107 |
-
if image is None:
|
| 108 |
-
print(f"Error: Could not read image {image}")
|
| 109 |
-
return gr.HTML(f'Error: Could not read image {image}')
|
| 110 |
-
|
| 111 |
cv2.imwrite(f"{uid}-im_tmp.png", image)
|
| 112 |
out = os.path.abspath(f"{uid}-im_tmp.png")
|
| 113 |
-
print(f"Image saved for reverse search: {out}")
|
| 114 |
out_url = f'https://nymbo-reverse-image.hf.space/file={out}'
|
| 115 |
rev_img_searcher = ReverseImageSearcher()
|
| 116 |
res = rev_img_searcher.search(out_url)
|
| 117 |
-
print(f"Reverse image search results: {res}")
|
| 118 |
count = 0
|
| 119 |
-
html_out = """"""
|
| 120 |
for search_item in res:
|
| 121 |
count += 1
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
html_out = f"""{html_out}
|
| 124 |
<div>
|
| 125 |
Title: {search_item.page_title}<br>
|
|
@@ -130,13 +112,11 @@ def rev_im(image):
|
|
| 130 |
|
| 131 |
return (gr.HTML(f'<h1>Total Found: {count}</h1><br>{html_out}'))
|
| 132 |
|
| 133 |
-
|
| 134 |
with gr.Blocks() as app:
|
| 135 |
with gr.Row():
|
| 136 |
gr.Column()
|
| 137 |
with gr.Column():
|
| 138 |
-
|
| 139 |
-
source_tog = gr.Radio(choices=["Image", "Video"], value="Image")
|
| 140 |
with gr.Box(visible=True) as im_box:
|
| 141 |
inp_url = gr.Textbox(label="Image URL")
|
| 142 |
load_im_btn = gr.Button("Load Image")
|
|
@@ -152,24 +132,18 @@ with gr.Blocks() as app:
|
|
| 152 |
with gr.Row():
|
| 153 |
go_btn_vid = gr.Button("Start")
|
| 154 |
next_btn = gr.Button("Next")
|
| 155 |
-
|
| 156 |
-
gr.Column()
|
| 157 |
with gr.Row():
|
| 158 |
html_out = gr.HTML("""""")
|
| 159 |
with gr.Row(visible=False):
|
| 160 |
hid_box = gr.Textbox()
|
| 161 |
-
|
| 162 |
def shuf(tog):
|
| 163 |
-
print(f"Toggling source type to: {tog}")
|
| 164 |
if tog == "Image":
|
| 165 |
return gr.update(visible=True), gr.update(visible=False)
|
| 166 |
if tog == "Video":
|
| 167 |
return gr.update(visible=False), gr.update(visible=True)
|
| 168 |
-
|
| 169 |
def load_image(url):
|
| 170 |
-
print(f"Loading image from URL: {url}")
|
| 171 |
return url
|
| 172 |
-
|
| 173 |
im_load = load_im_btn.click(load_image, inp_url, inp_im)
|
| 174 |
next_btn.click(process_vid, [inp_vid, hid_box, every_n], [html_out, stat_box, hid_box])
|
| 175 |
vid_load = vid_url_btn.click(dl, vid_url, [inp_vid, html_out, stat_box, hid_box])
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
import os
|
| 8 |
import uuid
|
| 9 |
+
uid = str(uuid.uuid4())
|
| 10 |
+
size_js = """
|
| 11 |
function imgSize(){
|
| 12 |
var myImg = document.getElementsByClassName("my_im");
|
| 13 |
var realWidth = myImg.naturalWidth;
|
|
|
|
| 17 |
|
| 18 |
def dl(inp):
|
| 19 |
out = None
|
| 20 |
+
out_file = []
|
| 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 |
out = f"{uid}/{inp_out}.mp4"
|
| 30 |
+
print(out)
|
| 31 |
except Exception as e:
|
| 32 |
+
print(e)
|
| 33 |
+
return out, gr.HTML(""), "", ""
|
| 34 |
|
| 35 |
+
def process_vid(file, cur_frame, every_n):
|
| 36 |
new_video_in = str(file)
|
|
|
|
| 37 |
capture = cv2.VideoCapture(new_video_in)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
|
|
|
|
| 39 |
rev_img_searcher = ReverseImageSearcher()
|
| 40 |
+
html_out = ""
|
| 41 |
count = int(every_n)
|
| 42 |
if cur_frame == "" or cur_frame is None:
|
| 43 |
start_frame = 0
|
|
|
|
| 47 |
for i in range(start_frame, frame_count-1):
|
| 48 |
if count == int(every_n):
|
| 49 |
count = 1
|
|
|
|
| 50 |
capture.set(cv2.CAP_PROP_POS_FRAMES, i)
|
| 51 |
ret, frame_f = capture.read()
|
| 52 |
if not ret:
|
| 53 |
+
print(f"Failed to capture frame at {i}")
|
| 54 |
continue
|
| 55 |
cv2.imwrite(f"{uid}-vid_tmp{i}.png", frame_f)
|
| 56 |
out = os.path.abspath(f"{uid}-vid_tmp{i}.png")
|
|
|
|
| 57 |
out_url = f'https://nymbo-reverse-image.hf.space/file={out}'
|
| 58 |
res = rev_img_searcher.search(out_url)
|
|
|
|
| 59 |
out_cnt = 0
|
| 60 |
if len(res) > 0:
|
| 61 |
for search_item in res:
|
|
|
|
| 62 |
out_cnt += 1
|
| 63 |
html_out = f"""{html_out}
|
| 64 |
<div>
|
|
|
|
| 69 |
</div>"""
|
| 70 |
return (gr.HTML(f'<h1>Total Found: {out_cnt}</h1><br>{html_out}'), f"Found frame: {i}", i+int(every_n))
|
| 71 |
count += 1
|
|
|
|
| 72 |
except Exception as e:
|
|
|
|
| 73 |
return (gr.HTML(f'{e}'), "", "")
|
|
|
|
| 74 |
return (gr.HTML('No frame matches found.'), "", "")
|
| 75 |
|
| 76 |
+
def process_im(file, url):
|
|
|
|
| 77 |
if not url.startswith("https://nymbo"):
|
|
|
|
| 78 |
return url
|
| 79 |
else:
|
| 80 |
read_file = Image.open(file)
|
|
|
|
| 82 |
action_input = f"{uid}-tmp.png"
|
| 83 |
out = os.path.abspath(action_input)
|
| 84 |
out_url = f'https://nymbo-reverse-image.hf.space/file={out}'
|
|
|
|
| 85 |
return (out_url)
|
| 86 |
|
| 87 |
def rev_im(image):
|
| 88 |
+
out_list = []
|
| 89 |
+
out_im = []
|
| 90 |
+
html_out = """"""
|
| 91 |
image = cv2.imread(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
cv2.imwrite(f"{uid}-im_tmp.png", image)
|
| 93 |
out = os.path.abspath(f"{uid}-im_tmp.png")
|
|
|
|
| 94 |
out_url = f'https://nymbo-reverse-image.hf.space/file={out}'
|
| 95 |
rev_img_searcher = ReverseImageSearcher()
|
| 96 |
res = rev_img_searcher.search(out_url)
|
|
|
|
| 97 |
count = 0
|
|
|
|
| 98 |
for search_item in res:
|
| 99 |
count += 1
|
| 100 |
+
out_dict = {
|
| 101 |
+
'Title': f'{search_item.page_title}',
|
| 102 |
+
'Site': f'{search_item.page_url}',
|
| 103 |
+
'Img': f'{search_item.image_url}',
|
| 104 |
+
}
|
| 105 |
html_out = f"""{html_out}
|
| 106 |
<div>
|
| 107 |
Title: {search_item.page_title}<br>
|
|
|
|
| 112 |
|
| 113 |
return (gr.HTML(f'<h1>Total Found: {count}</h1><br>{html_out}'))
|
| 114 |
|
|
|
|
| 115 |
with gr.Blocks() as app:
|
| 116 |
with gr.Row():
|
| 117 |
gr.Column()
|
| 118 |
with gr.Column():
|
| 119 |
+
source_tog = gr.Radio(choices=["Image","Video"], value="Image")
|
|
|
|
| 120 |
with gr.Box(visible=True) as im_box:
|
| 121 |
inp_url = gr.Textbox(label="Image URL")
|
| 122 |
load_im_btn = gr.Button("Load Image")
|
|
|
|
| 132 |
with gr.Row():
|
| 133 |
go_btn_vid = gr.Button("Start")
|
| 134 |
next_btn = gr.Button("Next")
|
| 135 |
+
gr.Column()
|
|
|
|
| 136 |
with gr.Row():
|
| 137 |
html_out = gr.HTML("""""")
|
| 138 |
with gr.Row(visible=False):
|
| 139 |
hid_box = gr.Textbox()
|
|
|
|
| 140 |
def shuf(tog):
|
|
|
|
| 141 |
if tog == "Image":
|
| 142 |
return gr.update(visible=True), gr.update(visible=False)
|
| 143 |
if tog == "Video":
|
| 144 |
return gr.update(visible=False), gr.update(visible=True)
|
|
|
|
| 145 |
def load_image(url):
|
|
|
|
| 146 |
return url
|
|
|
|
| 147 |
im_load = load_im_btn.click(load_image, inp_url, inp_im)
|
| 148 |
next_btn.click(process_vid, [inp_vid, hid_box, every_n], [html_out, stat_box, hid_box])
|
| 149 |
vid_load = vid_url_btn.click(dl, vid_url, [inp_vid, html_out, stat_box, hid_box])
|