File size: 581 Bytes
845aa73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from ultralytics import YOLO
from PIL import Image
import numpy as np
import cv2
import time

# Load the YOLOv8 model
model = YOLO("yolov8n.pt")  # You can use yolov8s.pt for better accuracy

# Detect theft from an image
def detect_theft(pil_image):
    frame = np.array(pil_image)
    results = model(frame)
    
    # Save thief photo with timestamp
    timestamp = time.strftime("%Y%m%d-%H%M%S")
    thief_path = f"thief.jpg"
    results[0].save(filename=thief_path)
    
    # Annotated image
    annotated = results[0].plot()
    return Image.fromarray(annotated), thief_path