Nymbo commited on
Commit
8571798
·
verified ·
1 Parent(s): c1121e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -68
app.py CHANGED
@@ -21,57 +21,56 @@ def dl(inp):
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
- #os.system(f'yt-dlp "{inp}" --trim-filenames 160 -o "{inp_out}.mp4" -S res,mp4 --recode mp4')
30
  out = f"{uid}/{inp_out}.mp4"
31
- #out_ap = os.path.abspath(out_f)
32
- #out = f'https://nymbo-reverse-image.hf.space/file={out_ap}'
33
- print (out)
34
  except Exception as e:
35
- print (e)
36
- #out = f'{e}'
37
  return out,gr.HTML(""),"",""
38
 
39
  def process_vid(file,cur_frame,every_n):
40
  new_video_in = str(file)
 
41
  capture = cv2.VideoCapture(new_video_in)
 
 
 
 
42
  frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
 
43
  rev_img_searcher = ReverseImageSearcher()
44
  html_out=""
45
  count = int(every_n)
46
- if cur_frame == "" or cur_frame==None:
47
  start_frame = 0
48
- elif cur_frame != "" and cur_frame!=None:
49
  start_frame = int(cur_frame)
50
  try:
51
  for i in range(start_frame, frame_count-1):
52
  if count == int(every_n):
53
  count = 1
54
- print(i)
55
  capture.set(cv2.CAP_PROP_POS_FRAMES, i)
56
- ret, frame_f = capture.read(i)
 
 
 
57
  cv2.imwrite(f"{uid}-vid_tmp{i}.png", frame_f)
58
  out = os.path.abspath(f"{uid}-vid_tmp{i}.png")
 
59
  out_url = f'https://nymbo-reverse-image.hf.space/file={out}'
60
- print(out)
61
  res = rev_img_searcher.search(out_url)
62
- #print (res)
63
- out_cnt =0
64
  if len(res) > 0:
65
- #count = 0
66
  for search_item in res:
67
- print (f'counting {count}')
68
- out_cnt+=1
69
- out_dict={
70
- 'Title': f'{search_item.page_title}',
71
- 'Site': f'{search_item.page_url}',
72
- 'Img': f'{search_item.image_url}',
73
- }
74
- print (dir(search_item))
75
  html_out = f"""{html_out}
76
  <div>
77
  Title: {search_item.page_title}<br>
@@ -80,17 +79,18 @@ def process_vid(file,cur_frame,every_n):
80
  <img class='my_im' src='{search_item.image_url}'><br>
81
  </div>"""
82
  return (gr.HTML(f'<h1>Total Found: {out_cnt}</h1><br>{html_out}'), f"Found frame: {i}", i+int(every_n))
83
- else:
84
- pass
85
- count +=1
86
- print (i+1)
87
- #return (None,f"Searching Frame: {i}", "")
88
  except Exception as e:
89
- return (gr.HTML(f'{e}'),"","")
90
- return (gr.HTML('No frame matches found.'),"","")
 
 
91
 
92
  def process_im(file,url):
 
93
  if not url.startswith("https://nymbo"):
 
94
  return url
95
  else:
96
  read_file = Image.open(file)
@@ -98,28 +98,28 @@ 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
  return (out_url)
102
 
103
  def rev_im(image):
104
- #image_url = 'https://i.pinimg.com/originals/c4/50/35/c450352ac6ea8645ead206721673e8fb.png'
105
- out_list = []
106
- out_im = []
107
- html_out = """"""
108
- image=cv2.imread(image)
 
109
  cv2.imwrite(f"{uid}-im_tmp.png", image)
110
  out = os.path.abspath(f"{uid}-im_tmp.png")
 
111
  out_url = f'https://nymbo-reverse-image.hf.space/file={out}'
112
  rev_img_searcher = ReverseImageSearcher()
113
  res = rev_img_searcher.search(out_url)
 
114
  count = 0
 
115
  for search_item in res:
116
- count+=1
117
- out_dict={
118
- 'Title': f'{search_item.page_title}',
119
- 'Site': f'{search_item.page_url}',
120
- 'Img': f'{search_item.image_url}',
121
- }
122
- print (dir(search_item))
123
  html_out = f"""{html_out}
124
  <div>
125
  Title: {search_item.page_title}<br>
@@ -131,50 +131,50 @@ def rev_im(image):
131
  return (gr.HTML(f'<h1>Total Found: {count}</h1><br>{html_out}'))
132
 
133
 
134
-
135
-
136
  with gr.Blocks() as app:
137
  with gr.Row():
138
  gr.Column()
139
  with gr.Column():
140
 
141
- source_tog=gr.Radio(choices=["Image","Video"],value="Image")
142
  with gr.Box(visible=True) as im_box:
143
- inp_url=gr.Textbox(label="Image URL")
144
- load_im_btn=gr.Button("Load Image")
145
- inp_im=gr.Image(label="Search Image",type='filepath')
146
- go_btn_im=gr.Button()
147
  with gr.Box(visible=False) as vid_box:
148
- vid_url=gr.Textbox(label="Video URL")
149
- vid_url_btn=gr.Button("Load URL")
150
- inp_vid=gr.Video(label="Search Video")
151
  with gr.Row():
152
- every_n=gr.Number(label = "Every /nth frame", value = 10)
153
- stat_box=gr.Textbox(label="Status")
154
  with gr.Row():
155
- go_btn_vid=gr.Button("Start")
156
- next_btn=gr.Button("Next")
157
 
158
- gr.Column()
159
- #paste_clip = gr.Button("Paste from Clipboard")
160
  with gr.Row():
161
  html_out = gr.HTML("""""")
162
  with gr.Row(visible=False):
163
- hid_box=gr.Textbox()
 
164
  def shuf(tog):
 
165
  if tog == "Image":
166
- return gr.update(visible=True),gr.update(visible=False)
167
  if tog == "Video":
168
- return gr.update(visible=False),gr.update(visible=True)
 
169
  def load_image(url):
 
170
  return url
171
- im_load = load_im_btn.click(load_image,inp_url,inp_im)
172
- next_btn.click(process_vid,[inp_vid,hid_box,every_n],[html_out,stat_box,hid_box])
173
- vid_load = vid_url_btn.click(dl,vid_url,[inp_vid,html_out,stat_box,hid_box])
174
- #inp_im.change(process_im,[inp_im,inp_url],[inp_url])
175
- vid_proc = go_btn_vid.click(process_vid,[inp_vid,hid_box,every_n],[html_out,stat_box,hid_box])
176
- im_proc = go_btn_im.click(rev_im,inp_im,[html_out])
177
- source_tog.change(shuf,[source_tog],[im_box,vid_box],cancels=[vid_proc,im_proc,im_load,vid_load])
178
 
179
- #go_btn_url.click(rev_im,inp_url,[html_out])
180
  app.queue(concurrency_count=20).launch()
 
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(f"Download complete: {out}")
 
 
32
  except Exception as e:
33
+ print(f"Error in downloading video: {e}")
 
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
51
+ else:
52
  start_frame = int(cur_frame)
53
  try:
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"Frame {i} could not be captured.")
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>
76
  Title: {search_item.page_title}<br>
 
79
  <img class='my_im' src='{search_item.image_url}'><br>
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
  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
+ print(f"Reversing image search for: {image}")
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
+ print(f"Found result {count} for image")
 
 
 
 
 
123
  html_out = f"""{html_out}
124
  <div>
125
  Title: {search_item.page_title}<br>
 
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")
143
+ inp_im = gr.Image(label="Search Image", type='filepath')
144
+ go_btn_im = gr.Button()
145
  with gr.Box(visible=False) as vid_box:
146
+ vid_url = gr.Textbox(label="Video URL")
147
+ vid_url_btn = gr.Button("Load URL")
148
+ inp_vid = gr.Video(label="Search Video")
149
  with gr.Row():
150
+ every_n = gr.Number(label="Every /nth frame", value=10)
151
+ stat_box = gr.Textbox(label="Status")
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])
176
+ vid_proc = go_btn_vid.click(process_vid, [inp_vid, hid_box, every_n], [html_out, stat_box, hid_box])
177
+ im_proc = go_btn_im.click(rev_im, inp_im, [html_out])
178
+ source_tog.change(shuf, [source_tog], [im_box, vid_box], cancels=[vid_proc, im_proc, im_load, vid_load])
179
 
 
180
  app.queue(concurrency_count=20).launch()