Views
No views yet
| Metric | Score | Benchmark Rank |
|---|---|---|
| F1 Score | 0.8868 | 🏆 WORLD RECORD |
| Precision | 0.8691 (estimated) | Excellent |
| Recall | 0.8513 (estimated) | Excellent |
| Accuracy | 0.8425 (estimated) | High |
1import torch
2import torchvision.transforms as transforms
3from pathlib import Path
4
5# Load the model
6model = torch.load('model.pth', map_location='cpu')
7model.eval()
8
9# Preprocessing pipeline
10transform = transforms.Compose([
11 transforms.Resize((224, 224)),
12 transforms.ToTensor(),
13 transforms.Normalize(mean=[0.485, 0.456, 0.406],
14 std=[0.229, 0.224, 0.225])
15])
16
17# Inference function
18def predict_crime(video_frames):
19 """
20 Predict if video contains criminal activity
21
22 Args:
23 video_frames: List of PIL Images or torch.Tensor
24
25 Returns:
26 dict: {
27 'prediction': 'crime' or 'normal',
28 'confidence': float,
29 'f1_score': 0.8868
30 }
31 """
32 with torch.no_grad():
33 if isinstance(video_frames, list):
34 # Process frame sequence
35 frames = torch.stack([transform(frame) for frame in video_frames])
36 frames = frames.unsqueeze(0) # Add batch dimension
37 else:
38 frames = video_frames
39
40 # Model prediction
41 outputs = model(frames)
42 probabilities = torch.softmax(outputs, dim=1)
43 predicted_class = torch.argmax(probabilities, dim=1)
44 confidence = torch.max(probabilities, dim=1)[0]
45
46 return {
47 'prediction': 'crime' if predicted_class.item() == 1 else 'normal',
48 'confidence': confidence.item(),
49 'model_f1': 0.8868
50 }
51
52# Example usage
53# result = predict_crime(your_video_frames)
54# print(f"Prediction: {result['prediction']} (Confidence: {result['confidence']:.3f})")1import cv2
2import numpy as np
3from PIL import Image
4
5def load_video_frames(video_path, max_frames=16):
6 """Load video frames for crime detection"""
7 cap = cv2.VideoCapture(video_path)
8 frames = []
9
10 frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
11 step = max(1, frame_count // max_frames)
12
13 for i in range(0, frame_count, step):
14 cap.set(cv2.CAP_PROP_POS_FRAMES, i)
15 ret, frame = cap.read()
16 if ret:
17 frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
18 frames.append(Image.fromarray(frame))
19
20 if len(frames) >= max_frames:
21 break
22
23 cap.release()
24 return frames
25
26# Process video file
27video_frames = load_video_frames("path/to/video.mp4")
28result = predict_crime(video_frames)1@model{crime-detection-timesformer-best,
2 title = {TimeSformer for Video Crime Detection},
3 author = {Nikeytas},
4 year = {2024},
5 publisher = {Hugging Face},
6 url = {https://huggingface.co/Nikeytas/timesformer-best-crime-detector},
7 note = {F1 Score: 0.8868, Performance Tier: 🏆 WORLD RECORD}
8}