asad231 commited on
Commit
49b55eb
·
verified ·
1 Parent(s): 129f79a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -8
app.py CHANGED
@@ -316,9 +316,71 @@ if uploaded_image is not None:
316
 
317
  st.error(f"⚠️ Result: This image is a Deepfake. (Confidence: {result['score']:.2f})")
318
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
319
  # ---- Deepfake Video Detection Section ----
320
  st.subheader("🎥 Deepfake Video Detection")
 
321
  uploaded_video = st.file_uploader("Upload a Video", type=["mp4", "avi", "mov"])
 
 
 
 
 
 
 
 
 
 
 
322
 
323
  def detect_deepfake_video(video_path):
324
  cap = cv2.VideoCapture(video_path)
@@ -350,15 +412,25 @@ def detect_deepfake_video(video_path):
350
 
351
  return {"label": final_label, "score": confidence}
352
 
353
- if uploaded_video is not None:
354
- st.video(uploaded_video)
355
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
356
- with open(temp_file.name, "wb") as f:
357
- f.write(uploaded_video.read())
358
-
359
- if st.button("Analyze Video"):
 
 
 
 
 
 
 
 
 
 
360
  st.write("🔍 Processing... Please wait.")
361
- result = detect_deepfake_video(temp_file.name)
362
 
363
  if result["label"] == "FAKE":
364
  st.error(f"⚠️ Deepfake Detected! This video appears to be FAKE. (Confidence: {result['score']:.2f})")
 
316
 
317
  st.error(f"⚠️ Result: This image is a Deepfake. (Confidence: {result['score']:.2f})")
318
 
319
+ # # ---- Deepfake Video Detection Section ----
320
+ # st.subheader("🎥 Deepfake Video Detection")
321
+ # uploaded_video = st.file_uploader("Upload a Video", type=["mp4", "avi", "mov"])
322
+
323
+ # def detect_deepfake_video(video_path):
324
+ # cap = cv2.VideoCapture(video_path)
325
+ # frame_scores = []
326
+ # frame_count = 0
327
+
328
+ # while cap.isOpened():
329
+ # ret, frame = cap.read()
330
+ # if not ret:
331
+ # break
332
+
333
+ # if frame_count % 10 == 0: # ہر 10ویں فریم کا تجزیہ کریں
334
+ # frame_path = "temp_frame.jpg"
335
+ # cv2.imwrite(frame_path, frame)
336
+ # result = detect_deepfake_image(frame_path)
337
+ # frame_scores.append(result["score"])
338
+ # os.remove(frame_path)
339
+
340
+ # frame_count += 1
341
+
342
+ # cap.release()
343
+
344
+ # if not frame_scores:
345
+ # return {"label": "UNKNOWN", "score": 0.0} # اگر کوئی فریم پراسیس نہ ہو سکے
346
+
347
+ # avg_score = np.mean(frame_scores)
348
+ # confidence = round(float(avg_score), 2)
349
+ # final_label = "FAKE" if avg_score > 0.5 else "REAL"
350
+
351
+ # return {"label": final_label, "score": confidence}
352
+
353
+ # if uploaded_video is not None:
354
+ # st.video(uploaded_video)
355
+ # temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
356
+ # with open(temp_file.name, "wb") as f:
357
+ # f.write(uploaded_video.read())
358
+
359
+ # if st.button("Analyze Video"):
360
+ # st.write("🔍 Processing... Please wait.")
361
+ # result = detect_deepfake_video(temp_file.name)
362
+
363
+ # if result["label"] == "FAKE":
364
+ # st.error(f"⚠️ Deepfake Detected! This video appears to be FAKE. (Confidence: {result['score']:.2f})")
365
+ # elif result["label"] == "REAL":
366
+ # st.success(f"✅ This video appears to be REAL. (Confidence: {1 - result['score']:.2f})")
367
+ # else:
368
+ # st.warning("⚠️ Unable to analyze the video. Please try a different file.")
369
  # ---- Deepfake Video Detection Section ----
370
  st.subheader("🎥 Deepfake Video Detection")
371
+
372
  uploaded_video = st.file_uploader("Upload a Video", type=["mp4", "avi", "mov"])
373
+ video_url = st.text_input("Or enter a video URL")
374
+
375
+ def download_video(url):
376
+ temp_video = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
377
+ response = requests.get(url, stream=True)
378
+ if response.status_code == 200:
379
+ with open(temp_video.name, "wb") as f:
380
+ for chunk in response.iter_content(chunk_size=8192):
381
+ f.write(chunk)
382
+ return temp_video.name
383
+ return None
384
 
385
  def detect_deepfake_video(video_path):
386
  cap = cv2.VideoCapture(video_path)
 
412
 
413
  return {"label": final_label, "score": confidence}
414
 
415
+ if uploaded_video is not None or video_url:
416
+ if uploaded_video is not None:
417
+ st.video(uploaded_video)
418
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
419
+ with open(temp_file.name, "wb") as f:
420
+ f.write(uploaded_video.read())
421
+ video_path = temp_file.name
422
+ elif video_url:
423
+ st.write("📥 Downloading video from URL...")
424
+ video_path = download_video(video_url)
425
+ if video_path:
426
+ st.video(video_path)
427
+ else:
428
+ st.error("⚠️ Failed to download video. Please check the URL and try again.")
429
+ video_path = None
430
+
431
+ if video_path and st.button("Analyze Video"):
432
  st.write("🔍 Processing... Please wait.")
433
+ result = detect_deepfake_video(video_path)
434
 
435
  if result["label"] == "FAKE":
436
  st.error(f"⚠️ Deepfake Detected! This video appears to be FAKE. (Confidence: {result['score']:.2f})")