sdafd commited on
Commit
d36f560
·
verified ·
1 Parent(s): e8c37eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -11
app.py CHANGED
@@ -8,6 +8,7 @@ import random
8
  import string
9
  import json
10
  import ast
 
11
 
12
 
13
  def check_nsfw(img_url):
@@ -175,34 +176,32 @@ def extract_frames(video_path, num_frames):
175
  frames = []
176
 
177
  for i in range(num_frames):
178
- frame_number = int(i * total_frames / num_frames) # Sample evenly
179
  vidcap.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
180
  success, image = vidcap.read()
181
  if success:
182
- # Save frame to temporary file and append it to list
183
- temp_img = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
184
- cv2.imwrite(temp_img.name, image)
185
- frames.append(temp_img.name)
186
  else:
187
  break
188
 
189
  vidcap.release()
190
  return frames
191
 
192
- # Gradio app logic
193
  def process_video(video, num_frames):
194
-
195
- # Extract frames from the video
196
  frames = extract_frames(video, num_frames)
197
 
198
  nsfw_count = 0
199
  total_frames = len(frames)
200
 
201
- # Check each frame for NSFW content
202
  for frame_path in frames:
203
  if check_nsfw_final(f"https://sdafd-video-nsfw-detect.hf.space/file={frame_path}"):
204
  nsfw_count += 1
205
- os.remove(frame_path) # Clean up temp frame files
 
 
 
206
 
207
  return f"{nsfw_count} out of {total_frames} frames were NSFW."
208
 
@@ -224,4 +223,4 @@ with gr.Blocks() as app:
224
  outputs=output_text
225
  )
226
 
227
- app.launch()
 
8
  import string
9
  import json
10
  import ast
11
+ import uuid
12
 
13
 
14
  def check_nsfw(img_url):
 
176
  frames = []
177
 
178
  for i in range(num_frames):
179
+ frame_number = int(i * total_frames / num_frames)
180
  vidcap.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
181
  success, image = vidcap.read()
182
  if success:
183
+ random_filename = f"{uuid.uuid4().hex}.jpg"
184
+ cv2.imwrite(random_filename, image)
185
+ frames.append(random_filename)
 
186
  else:
187
  break
188
 
189
  vidcap.release()
190
  return frames
191
 
 
192
  def process_video(video, num_frames):
 
 
193
  frames = extract_frames(video, num_frames)
194
 
195
  nsfw_count = 0
196
  total_frames = len(frames)
197
 
 
198
  for frame_path in frames:
199
  if check_nsfw_final(f"https://sdafd-video-nsfw-detect.hf.space/file={frame_path}"):
200
  nsfw_count += 1
201
+
202
+ for frame_path in frames:
203
+ if os.path.exists(frame_path):
204
+ os.remove(frame_path)
205
 
206
  return f"{nsfw_count} out of {total_frames} frames were NSFW."
207
 
 
223
  outputs=output_text
224
  )
225
 
226
+ app.launch()