Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,9 @@ from google_img_source_search import ReverseImageSearcher
|
|
6 |
from PIL import Image
|
7 |
import os
|
8 |
import uuid
|
9 |
-
|
|
|
|
|
10 |
size_js = """
|
11 |
function imgSize(){
|
12 |
var myImg = document.getElementsByClassName("my_im");
|
@@ -19,8 +21,12 @@ 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:
|
@@ -33,32 +39,56 @@ def dl(inp):
|
|
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
|
44 |
-
else:
|
45 |
-
start_frame = int(cur_frame)
|
46 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
53 |
-
|
54 |
continue
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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>
|
@@ -67,56 +97,77 @@ def process_vid(file, cur_frame, every_n):
|
|
67 |
Img: <a href='{search_item.image_url}' target='_blank' rel='noopener noreferrer'>{search_item.image_url}</a><br>
|
68 |
<img class='my_im' src='{search_item.image_url}'><br>
|
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 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
def rev_im(image):
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
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>
|
108 |
-
Site: <a href='{search_item.page_url}' target='_blank' rel='noopener noreferrer'>{search_item.page_url}</a><br>
|
109 |
-
Img: <a href='{search_item.image_url}' target='_blank' rel='noopener noreferrer'>{search_item.image_url}</a><br>
|
110 |
-
<img class='my_im' src='{search_item.image_url}'><br>
|
111 |
-
</div>"""
|
112 |
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,18 +183,22 @@ with gr.Blocks() as app:
|
|
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])
|
|
|
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");
|
|
|
21 |
out = None
|
22 |
out_file = []
|
23 |
try:
|
24 |
+
# Create the directory if it doesn't exist
|
25 |
+
os.makedirs(str(uid), exist_ok=True)
|
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 "{uid}/{inp_out}.mp4" -S res,mp4 --recode mp4')
|
32 |
else:
|
|
|
39 |
return out, gr.HTML(""), "", ""
|
40 |
|
41 |
def process_vid(file, cur_frame, every_n):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
try:
|
43 |
+
# Ensure the file exists and is readable
|
44 |
+
if not os.path.exists(file):
|
45 |
+
return gr.HTML("Error: Video file not found."), "", ""
|
46 |
+
|
47 |
+
new_video_in = str(file)
|
48 |
+
capture = cv2.VideoCapture(new_video_in)
|
49 |
+
|
50 |
+
if not capture.isOpened():
|
51 |
+
return gr.HTML("Error: Could not open video file."), "", ""
|
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 |
+
# Create directory for temporary files if it doesn't exist
|
64 |
+
os.makedirs(str(uid), exist_ok=True)
|
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 |
+
temp_img_path = f"{uid}/vid_tmp{i}.png"
|
77 |
+
cv2.imwrite(temp_img_path, frame_f)
|
78 |
+
|
79 |
+
if not os.path.exists(temp_img_path):
|
80 |
+
continue
|
81 |
+
|
82 |
+
out = os.path.abspath(temp_img_path)
|
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 |
out_cnt = 0
|
88 |
+
|
89 |
if len(res) > 0:
|
90 |
for search_item in res:
|
91 |
+
print(f'counting {count}')
|
92 |
out_cnt += 1
|
93 |
html_out = f"""{html_out}
|
94 |
<div>
|
|
|
97 |
Img: <a href='{search_item.image_url}' target='_blank' rel='noopener noreferrer'>{search_item.image_url}</a><br>
|
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 (gr.HTML(f'Error: {str(e)}'), "", "")
|
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 |
else:
|
117 |
+
try:
|
118 |
+
read_file = Image.open(file)
|
119 |
+
os.makedirs(str(uid), exist_ok=True)
|
120 |
+
output_path = f"{uid}/tmp.png"
|
121 |
+
read_file.save(output_path)
|
122 |
+
out = os.path.abspath(output_path)
|
123 |
+
out_url = f'https://nymbo-reverse-image.hf.space/file={out}'
|
124 |
+
return out_url
|
125 |
+
except Exception as e:
|
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 |
+
return gr.HTML("Error: Could not read image file.")
|
137 |
+
|
138 |
+
cv2.imwrite(output_path, image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
|
140 |
+
if not os.path.exists(output_path):
|
141 |
+
return gr.HTML("Error: Failed to save processed image.")
|
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 = f"""{html_out}
|
154 |
+
<div>
|
155 |
+
Title: {search_item.page_title}<br>
|
156 |
+
Site: <a href='{search_item.page_url}' target='_blank' rel='noopener noreferrer'>{search_item.page_url}</a><br>
|
157 |
+
Img: <a href='{search_item.image_url}' target='_blank' rel='noopener noreferrer'>{search_item.image_url}</a><br>
|
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 |
+
return gr.HTML(f"Error: {str(e)}")
|
165 |
|
166 |
with gr.Blocks() as app:
|
167 |
with gr.Row():
|
168 |
gr.Column()
|
169 |
with gr.Column():
|
170 |
+
source_tog = gr.Radio(choices=["Image", "Video"], value="Image")
|
171 |
with gr.Box(visible=True) as im_box:
|
172 |
inp_url = gr.Textbox(label="Image URL")
|
173 |
load_im_btn = gr.Button("Load Image")
|
|
|
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 |
if tog == "Image":
|
195 |
return gr.update(visible=True), gr.update(visible=False)
|
196 |
if tog == "Video":
|
197 |
return gr.update(visible=False), gr.update(visible=True)
|
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])
|