File size: 7,616 Bytes
aead09a
0bb3a7f
1ce9878
d82da04
7716718
2255cf1
0bb3a7f
e8f283e
0bb3a7f
 
2b65731
 
 
 
 
 
bf17c4e
a8f7368
ac2b487
0bb3a7f
a8f7368
0bb3a7f
 
8571798
2b65731
0bb3a7f
a8f7368
0bb3a7f
 
2b65731
8571798
ac2b487
8571798
0bb3a7f
ac2b487
0bb3a7f
2b65731
8571798
0bb3a7f
8571798
 
 
 
ac2b487
8571798
ac2b487
0bb3a7f
2b65731
8571798
2b65731
8571798
2b65731
599424e
0bb3a7f
d4e29c7
dc9ebd1
8571798
d4e29c7
8571798
 
 
 
2b65731
 
8571798
2b65731
ac2b487
8571798
 
2b65731
ac2b487
8571798
 
2b65731
ac2b487
2b65731
 
 
 
 
0bb3a7f
8571798
 
599424e
8571798
 
 
 
d82da04
0bb3a7f
8571798
319e8a0
8571798
dc9ebd1
2b65731
dc9ebd1
2b65731
 
 
 
8571798
2b65731
c558940
dc9ebd1
8571798
 
 
 
 
 
2b65731
 
8571798
0bb3a7f
2b65731
 
8571798
2b65731
8571798
2b65731
8571798
 
2b65731
 
 
 
 
 
 
0bb3a7f
2b65731
c6fe582
0bb3a7f
ac2b487
f77bbf5
 
 
0bb3a7f
8571798
f77bbf5
8571798
 
 
 
f77bbf5
8571798
 
 
f77bbf5
8571798
 
f77bbf5
8571798
 
0bb3a7f
8571798
f77bbf5
2b65731
f77bbf5
8571798
 
2b65731
8571798
2b65731
8571798
2b65731
8571798
 
2b65731
8571798
2b65731
8571798
 
 
 
 
 
 
2b65731
ac2b487
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import gradio as gr
import requests 
import yt_dlp
import cv2
from google_img_source_search import ReverseImageSearcher
from PIL import Image
import os 
import uuid
uid=uuid.uuid4()
size_js="""
    function imgSize(){
        var myImg = document.getElementsByClassName("my_im");
        var realWidth = myImg.naturalWidth;
        var realHeight = myImg.naturalHeight;
        alert("Original width=" + realWidth + ", " + "Original height=" + realHeight);
    }"""

def dl(inp):
    out = None
    out_file=[]
    try:
        inp_out=inp.replace("https://","")
        inp_out=inp_out.replace("/","_").replace(".","_").replace("=","_").replace("?","_")
        print(f"Downloading video from URL: {inp}")
        if "twitter" in inp:
            os.system(f'yt-dlp "{inp}" --extractor-arg "twitter:api=syndication" --trim-filenames 160 -o "{uid}/{inp_out}.mp4" -S res,mp4 --recode mp4')  
        else:
            os.system(f'yt-dlp "{inp}" --trim-filenames 160 -o "{uid}/{inp_out}.mp4" -S res,mp4 --recode mp4')          
        
        out = f"{uid}/{inp_out}.mp4"
        print(f"Download complete: {out}")
    except Exception as e:
        print(f"Error in downloading video: {e}")
    return out,gr.HTML(""),"",""

def process_vid(file,cur_frame,every_n):
    new_video_in = str(file)
    print(f"Processing video file: {new_video_in}")
    capture = cv2.VideoCapture(new_video_in)    
    if not capture.isOpened():
        print(f"Error: Could not open video file {new_video_in}")
        return (gr.HTML(f'Error: Could not open video file {new_video_in}'), "", "")
    
    frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
    print(f"Total frames in video: {frame_count}")
    rev_img_searcher = ReverseImageSearcher()
    html_out=""
    count = int(every_n)
    if cur_frame == "" or cur_frame is None:
        start_frame = 0
    else:
        start_frame = int(cur_frame)
    try:
        for i in range(start_frame, frame_count-1):
            if count == int(every_n):
                count = 1
                print(f"Processing frame: {i}")
                capture.set(cv2.CAP_PROP_POS_FRAMES, i)
                ret, frame_f = capture.read()
                if not ret:
                    print(f"Frame {i} could not be captured.")
                    continue
                cv2.imwrite(f"{uid}-vid_tmp{i}.png", frame_f)
                out = os.path.abspath(f"{uid}-vid_tmp{i}.png")
                print(f"Saved frame {i} as image: {out}")
                out_url = f'https://nymbo-reverse-image.hf.space/file={out}'
                res = rev_img_searcher.search(out_url)
                print(f"Reverse image search results for frame {i}: {res}")
                out_cnt = 0
                if len(res) > 0:
                    for search_item in res:
                        print(f'Found result {out_cnt + 1} for frame {i}')
                        out_cnt += 1
                        html_out = f"""{html_out}
                        <div>
                        Title: {search_item.page_title}<br>
                        Site: <a href='{search_item.page_url}' target='_blank' rel='noopener noreferrer'>{search_item.page_url}</a><br>
                        Img: <a href='{search_item.image_url}' target='_blank' rel='noopener noreferrer'>{search_item.image_url}</a><br>
                        <img class='my_im' src='{search_item.image_url}'><br>
                        </div>"""
                    return (gr.HTML(f'<h1>Total Found: {out_cnt}</h1><br>{html_out}'), f"Found frame: {i}", i+int(every_n))
            count += 1
            print(f"Skipping frame {i + 1}")
    except Exception as e:
        print(f"Error during video processing: {e}")
        return (gr.HTML(f'{e}'), "", "")
    print("No matching frames found in video.")
    return (gr.HTML('No frame matches found.'), "", "")

def process_im(file,url):
    print(f"Processing image with URL: {url}")
    if not url.startswith("https://nymbo"):
        print(f"URL does not match required pattern: {url}")
        return url
    else:
        read_file = Image.open(file)
        read_file.save(f"{uid}-tmp.png")
        action_input = f"{uid}-tmp.png"
        out = os.path.abspath(action_input)
        out_url = f'https://nymbo-reverse-image.hf.space/file={out}'
        print(f"Processed image saved as: {out}")
        return (out_url)

def rev_im(image):
    print(f"Reversing image search for: {image}")
    image = cv2.imread(image)
    if image is None:
        print(f"Error: Could not read image {image}")
        return gr.HTML(f'Error: Could not read image {image}')
    
    cv2.imwrite(f"{uid}-im_tmp.png", image)
    out = os.path.abspath(f"{uid}-im_tmp.png")
    print(f"Image saved for reverse search: {out}")
    out_url = f'https://nymbo-reverse-image.hf.space/file={out}'    
    rev_img_searcher = ReverseImageSearcher()
    res = rev_img_searcher.search(out_url)
    print(f"Reverse image search results: {res}")
    count = 0
    html_out = """"""
    for search_item in res:
        count += 1
        print(f"Found result {count} for image")
        html_out = f"""{html_out}
        <div>
        Title: {search_item.page_title}<br>
        Site: <a href='{search_item.page_url}' target='_blank' rel='noopener noreferrer'>{search_item.page_url}</a><br>
        Img: <a href='{search_item.image_url}' target='_blank' rel='noopener noreferrer'>{search_item.image_url}</a><br>
        <img class='my_im' src='{search_item.image_url}'><br>
        </div>"""
        
    return (gr.HTML(f'<h1>Total Found: {count}</h1><br>{html_out}'))


with gr.Blocks() as app:
    with gr.Row():
        gr.Column()
        with gr.Column():
            
            source_tog = gr.Radio(choices=["Image", "Video"], value="Image")
            with gr.Box(visible=True) as im_box:
                inp_url = gr.Textbox(label="Image URL")
                load_im_btn = gr.Button("Load Image")
                inp_im = gr.Image(label="Search Image", type='filepath')
                go_btn_im = gr.Button()
            with gr.Box(visible=False) as vid_box:
                vid_url = gr.Textbox(label="Video URL")
                vid_url_btn = gr.Button("Load URL")
                inp_vid = gr.Video(label="Search Video")
                with gr.Row():
                    every_n = gr.Number(label="Every /nth frame", value=10)
                stat_box = gr.Textbox(label="Status")
                with gr.Row():
                    go_btn_vid = gr.Button("Start")
                    next_btn = gr.Button("Next")
                
        gr.Column()
    with gr.Row():
        html_out = gr.HTML("""""")
    with gr.Row(visible=False):
        hid_box = gr.Textbox()
    
    def shuf(tog):
        print(f"Toggling source type to: {tog}")
        if tog == "Image":
            return gr.update(visible=True), gr.update(visible=False)
        if tog == "Video":
            return gr.update(visible=False), gr.update(visible=True)    
    
    def load_image(url):
        print(f"Loading image from URL: {url}")
        return url
    
    im_load = load_im_btn.click(load_image, inp_url, inp_im)
    next_btn.click(process_vid, [inp_vid, hid_box, every_n], [html_out, stat_box, hid_box])
    vid_load = vid_url_btn.click(dl, vid_url, [inp_vid, html_out, stat_box, hid_box])
    vid_proc = go_btn_vid.click(process_vid, [inp_vid, hid_box, every_n], [html_out, stat_box, hid_box])
    im_proc = go_btn_im.click(rev_im, inp_im, [html_out])
    source_tog.change(shuf, [source_tog], [im_box, vid_box], cancels=[vid_proc, im_proc, im_load, vid_load])

app.queue(concurrency_count=20).launch()