asad231 commited on
Commit
494b03f
·
verified ·
1 Parent(s): bf75a57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -23
app.py CHANGED
@@ -255,10 +255,10 @@ def detect_deepfake_video(video_path):
255
  if not ret:
256
  break
257
 
258
- if frame_count % 10 == 0: # ہر 10ویں فریم کا تجزیہ کریں
259
  frame_path = "temp_frame.jpg"
260
  cv2.imwrite(frame_path, frame)
261
- result = detect_deepfake_image(frame_path) # Deepfake detection function
262
  frame_scores.append(result["score"])
263
  os.remove(frame_path)
264
 
@@ -267,7 +267,7 @@ def detect_deepfake_video(video_path):
267
  cap.release()
268
 
269
  if not frame_scores:
270
- return {"label": "UNKNOWN", "score": 0.0} # اگر کوئی فریم پراسیس نہ ہو سکے
271
 
272
  avg_score = np.mean(frame_scores)
273
  confidence = round(float(avg_score), 2)
@@ -291,19 +291,23 @@ def download_video(url):
291
  return None
292
 
293
  # Download YouTube video
294
- def get_youtube_embed_code(youtube_url):
295
- """Generate an embedded iframe for YouTube videos"""
296
- return f"""
297
- <iframe width="560" height="315" src="https://www.youtube.com/embed/{youtube_url.split('=')[-1]}"
298
- frameborder="0" allowfullscreen></iframe>
299
- """
 
 
 
 
 
 
 
300
 
301
  # Select Video Source
302
  video_path = None
303
  video_display_url = None
304
- video_title = None
305
- video_thumbnail = None
306
- youtube_embed = None
307
 
308
  # Process Uploaded Video
309
  if uploaded_video is not None:
@@ -312,25 +316,25 @@ if uploaded_video is not None:
312
  f.write(uploaded_video.read())
313
  video_path = temp_file.name # Set video path for detection
314
  video_display_url = temp_file.name
315
- video_title = "Uploaded Video"
316
 
317
- # Process Video from URL (Check if YouTube or Direct Link)
318
  elif video_url:
319
  if "youtube.com" in video_url or "youtu.be" in video_url:
320
- youtube_embed = get_youtube_embed_code(video_url) # Get embedded iframe
321
- video_title = "YouTube Video"
 
 
 
 
 
322
  else:
323
  video_path = download_video(video_url) # Download direct MP4 video
324
  video_display_url = video_path
325
- video_title = "Online MP4 Video"
326
 
327
  # ✅ Display Video Properly
328
- if youtube_embed:
329
- st.markdown(f"### 🎬 {video_title}")
330
- st.markdown(youtube_embed, unsafe_allow_html=True) # Show YouTube Video
331
- elif video_display_url:
332
- st.markdown(f"### 🎬 {video_title}")
333
- st.video(video_display_url) # Show Uploaded or MP4 URL Video
334
 
335
  # ✅ "Analyze Video" بٹن ہمیشہ شو ہوگا
336
  analyze_button = st.button("Analyze Video")
 
255
  if not ret:
256
  break
257
 
258
+ if frame_count % 10 == 0:
259
  frame_path = "temp_frame.jpg"
260
  cv2.imwrite(frame_path, frame)
261
+ result = detect_deepfake_image(frame_path)
262
  frame_scores.append(result["score"])
263
  os.remove(frame_path)
264
 
 
267
  cap.release()
268
 
269
  if not frame_scores:
270
+ return {"label": "UNKNOWN", "score": 0.0}
271
 
272
  avg_score = np.mean(frame_scores)
273
  confidence = round(float(avg_score), 2)
 
291
  return None
292
 
293
  # Download YouTube video
294
+ def download_youtube_video(youtube_url):
295
+ try:
296
+ yt = YouTube(youtube_url)
297
+ video_stream = yt.streams.filter(file_extension="mp4", res="360p").first()
298
+ if not video_stream:
299
+ video_stream = yt.streams.get_lowest_resolution()
300
+
301
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
302
+ video_stream.download(filename=temp_file.name)
303
+
304
+ return temp_file.name # Return local file path
305
+ except Exception as e:
306
+ return None
307
 
308
  # Select Video Source
309
  video_path = None
310
  video_display_url = None
 
 
 
311
 
312
  # Process Uploaded Video
313
  if uploaded_video is not None:
 
316
  f.write(uploaded_video.read())
317
  video_path = temp_file.name # Set video path for detection
318
  video_display_url = temp_file.name
 
319
 
320
+ # Process Video from URL
321
  elif video_url:
322
  if "youtube.com" in video_url or "youtu.be" in video_url:
323
+ st.write("⏳ Downloading YouTube Video...")
324
+ video_path = download_youtube_video(video_url) # Download YouTube video
325
+ if video_path:
326
+ st.success("✅ YouTube video downloaded successfully!")
327
+ video_display_url = video_path
328
+ else:
329
+ st.error("❌ Failed to download YouTube video. Try another link.")
330
  else:
331
  video_path = download_video(video_url) # Download direct MP4 video
332
  video_display_url = video_path
 
333
 
334
  # ✅ Display Video Properly
335
+ if video_display_url:
336
+ st.markdown("### 🎬 Video Preview")
337
+ st.video(video_display_url) # Play Video
 
 
 
338
 
339
  # ✅ "Analyze Video" بٹن ہمیشہ شو ہوگا
340
  analyze_button = st.button("Analyze Video")