Myteam commited on
Commit
309d76f
·
verified ·
1 Parent(s): 681154d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -9
app.py CHANGED
@@ -3,17 +3,21 @@ import torch
3
  from transformers import AutoImageProcessor, VideoMAEForVideoClassification
4
  import cv2
5
  import numpy as np
 
6
 
7
  # Load model and processor
8
  model = VideoMAEForVideoClassification.from_pretrained("model")
9
  processor = AutoImageProcessor.from_pretrained("model")
10
  model.eval()
11
 
12
- # Function to extract and preprocess 16 evenly spaced frames from video
13
  def preprocess_video(video_path, frame_count=16):
14
  cap = cv2.VideoCapture(video_path)
15
  frames = []
16
  total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
 
 
 
17
  for i in np.linspace(0, total - 1, frame_count, dtype=int):
18
  cap.set(cv2.CAP_PROP_POS_FRAMES, i)
19
  ret, frame = cap.read()
@@ -26,20 +30,22 @@ def preprocess_video(video_path, frame_count=16):
26
 
27
  # Inference function
28
  def predict(video_path):
 
 
29
  frames = preprocess_video(video_path)
30
- if not frames:
31
- return "❌ Error reading video."
32
  inputs = processor(frames, return_tensors="pt")
33
  with torch.no_grad():
34
  logits = model(**inputs).logits
35
  predicted = torch.argmax(logits, dim=1).item()
36
- return "⚠️ Abnormal" if predicted == 1 else "✅ Normal"
37
 
38
- # Gradio interface
39
  gr.Interface(
40
  fn=predict,
41
- inputs=gr.Video(label="Upload a video or use webcam"),
42
- outputs="text",
43
- title="Abnormal Behavior Detection"
 
44
  ).launch()
45
-
 
3
  from transformers import AutoImageProcessor, VideoMAEForVideoClassification
4
  import cv2
5
  import numpy as np
6
+ import os
7
 
8
  # Load model and processor
9
  model = VideoMAEForVideoClassification.from_pretrained("model")
10
  processor = AutoImageProcessor.from_pretrained("model")
11
  model.eval()
12
 
13
+ # Function to extract and preprocess 16 frames
14
  def preprocess_video(video_path, frame_count=16):
15
  cap = cv2.VideoCapture(video_path)
16
  frames = []
17
  total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
18
+ if total < frame_count:
19
+ cap.release()
20
+ return None
21
  for i in np.linspace(0, total - 1, frame_count, dtype=int):
22
  cap.set(cv2.CAP_PROP_POS_FRAMES, i)
23
  ret, frame = cap.read()
 
30
 
31
  # Inference function
32
  def predict(video_path):
33
+ if video_path is None or not os.path.exists(video_path):
34
+ return "❌ No video uploaded or camera input failed."
35
  frames = preprocess_video(video_path)
36
+ if frames is None:
37
+ return "❌ Couldn't read video or not enough frames."
38
  inputs = processor(frames, return_tensors="pt")
39
  with torch.no_grad():
40
  logits = model(**inputs).logits
41
  predicted = torch.argmax(logits, dim=1).item()
42
+ return "⚠️ Abnormal Behavior Detected" if predicted == 1 else "✅ Normal Behavior"
43
 
44
+ # Interface
45
  gr.Interface(
46
  fn=predict,
47
+ inputs=gr.Video(label="Upload .mp4 video or record with webcam", format="mp4"),
48
+ outputs=gr.Text(label="Prediction"),
49
+ title="Abnormal Behavior Detection",
50
+ description="Upload a video in .mp4 format or use your webcam to analyze behavior"
51
  ).launch()