Views
No views yet
| Training Loss | Epoch | Step | Validation Loss | Accuracy |
|---|---|---|---|---|
| 0.7291666666666666 | 8.00 | N/A | 0.8405 | 0.7292 |
pip install transformers torch torchvision opencv-python pillow1import torch
2from transformers import AutoModelForVideoClassification, AutoProcessor
3import cv2
4import numpy as np
5
6# Load model and processor
7model = AutoModelForVideoClassification.from_pretrained("Nikeytas/videomae-crime-detector-maxdata-v1")
8processor = AutoProcessor.from_pretrained("Nikeytas/videomae-crime-detector-maxdata-v1")
9
10# Process video
11def classify_video(video_path, num_frames=16):
12 # Extract frames
13 cap = cv2.VideoCapture(video_path)
14 frames = []
15
16 total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
17 indices = np.linspace(0, total_frames - 1, num_frames, dtype=int)
18
19 for idx in indices:
20 cap.set(cv2.CAP_PROP_POS_FRAMES, idx)
21 ret, frame = cap.read()
22 if ret:
23 frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
24 frames.append(frame_rgb)
25
26 cap.release()
27
28 # Process with model
29 inputs = processor(frames, return_tensors="pt")
30
31 with torch.no_grad():
32 outputs = model(**inputs)
33 predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
34 predicted_class = torch.argmax(predictions, dim=-1).item()
35 confidence = predictions[0][predicted_class].item()
36
37 label = "Violent Crime" if predicted_class == 1 else "Non-Violent"
38 return label, confidence
39
40# Example usage
41video_path = "path/to/your/video.mp4"
42prediction, confidence = classify_video(video_path)
43print(f"Prediction: {prediction} (Confidence: {confidence:.3f})")1import os
2from pathlib import Path
3
4def process_video_directory(video_dir, output_file="results.txt"):
5 results = []
6
7 for video_file in Path(video_dir).glob("*.mp4"):
8 try:
9 prediction, confidence = classify_video(str(video_file))
10 results.append({
11 "file": video_file.name,
12 "prediction": prediction,
13 "confidence": confidence
14 })
15 print(f"✅ {video_file.name}: {prediction} ({confidence:.3f})")
16 except Exception as e:
17 print(f"❌ Error processing {video_file.name}: {e}")
18
19 # Save results
20 with open(output_file, "w") as f:
21 for result in results:
22 f.write(f"{result['file']}: {result['prediction']} ({result['confidence']:.3f})\n")
23
24 return results
25
26# Process all videos in a directory
27results = process_video_directory("./videos/")1@misc{Nikeytas_videomae_crime_detector_maxdata_v1,
2 title={VideoMAE Fine-tuned for Crime Detection},
3 author={Research Team},
4 year={2024},
5 publisher={Hugging Face},
6 url={https://huggingface.co/Nikeytas/videomae-crime-detector-maxdata-v1}
7}