Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
from fastapi import FastAPI, HTTPException
|
|
|
2 |
from pydantic import BaseModel
|
3 |
import cv2
|
4 |
import uuid
|
@@ -181,9 +182,14 @@ def check_nsfw_final(img_url):
|
|
181 |
|
182 |
return result
|
183 |
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
# Frame extraction
|
185 |
def extract_frames(video_path, num_frames, temp_dir):
|
186 |
-
print(video_path)
|
187 |
vidcap = cv2.VideoCapture(video_path)
|
188 |
total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
|
189 |
frames = []
|
@@ -204,29 +210,28 @@ def extract_frames(video_path, num_frames, temp_dir):
|
|
204 |
|
205 |
# Video processing
|
206 |
def process_video(video_path, num_frames):
|
207 |
-
|
208 |
-
os.makedirs(temp_dir, exist_ok=True)
|
209 |
|
210 |
-
frames
|
|
|
211 |
|
212 |
nsfw_count = 0
|
213 |
total_frames = len(frames)
|
214 |
frame_results = []
|
215 |
|
216 |
for frame_path in frames:
|
217 |
-
|
218 |
-
|
|
|
|
|
219 |
nsfw_detected = check_nsfw_final(img_url)
|
220 |
frame_results.append({
|
221 |
-
"frame_path":
|
222 |
"nsfw_detected": nsfw_detected
|
223 |
})
|
224 |
if nsfw_detected:
|
225 |
nsfw_count += 1
|
226 |
|
227 |
-
# Cleanup
|
228 |
-
shutil.rmtree(temp_dir, ignore_errors=True)
|
229 |
-
|
230 |
result = {
|
231 |
"nsfw_count": nsfw_count,
|
232 |
"total_frames": total_frames,
|
|
|
1 |
from fastapi import FastAPI, HTTPException
|
2 |
+
from fastapi.staticfiles import StaticFiles
|
3 |
from pydantic import BaseModel
|
4 |
import cv2
|
5 |
import uuid
|
|
|
182 |
|
183 |
return result
|
184 |
|
185 |
+
# Directory for serving frame images
|
186 |
+
FRAMES_DIR = "/tmp/frames"
|
187 |
+
|
188 |
+
# Mount static file route
|
189 |
+
app.mount("/frames", StaticFiles(directory=FRAMES_DIR), name="frames")
|
190 |
+
|
191 |
# Frame extraction
|
192 |
def extract_frames(video_path, num_frames, temp_dir):
|
|
|
193 |
vidcap = cv2.VideoCapture(video_path)
|
194 |
total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
|
195 |
frames = []
|
|
|
210 |
|
211 |
# Video processing
|
212 |
def process_video(video_path, num_frames):
|
213 |
+
os.makedirs(FRAMES_DIR, exist_ok=True) # Ensure frames directory exists
|
|
|
214 |
|
215 |
+
# Extract frames to the static files directory
|
216 |
+
frames = extract_frames(video_path, num_frames, FRAMES_DIR)
|
217 |
|
218 |
nsfw_count = 0
|
219 |
total_frames = len(frames)
|
220 |
frame_results = []
|
221 |
|
222 |
for frame_path in frames:
|
223 |
+
# Construct HTTP URL for the frame
|
224 |
+
frame_filename = os.path.basename(frame_path)
|
225 |
+
img_url = f"https://sdafd-nsfw-video-detect-api.hf.space/frames/{frame_filename}"
|
226 |
+
|
227 |
nsfw_detected = check_nsfw_final(img_url)
|
228 |
frame_results.append({
|
229 |
+
"frame_path": img_url, # Use HTTP URL
|
230 |
"nsfw_detected": nsfw_detected
|
231 |
})
|
232 |
if nsfw_detected:
|
233 |
nsfw_count += 1
|
234 |
|
|
|
|
|
|
|
235 |
result = {
|
236 |
"nsfw_count": nsfw_count,
|
237 |
"total_frames": total_frames,
|