-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect.py
More file actions
57 lines (46 loc) · 1.72 KB
/
Copy pathdetect.py
File metadata and controls
57 lines (46 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from ultralytics import YOLO
import cv2
from twilio.rest import Client
from dotenv import load_dotenv
import os
# Load a model
model = YOLO('yolov8n.pt') # load an official model
model = YOLO('D:/2023/Coding/project/yolo/yolo/dataset/runs/detect/train/weights/best.pt') # load a custom model
source = 'D:/2023/Coding/project/yolo/yolo/dataset/D09_20230825094234.mp4'
# Twilio settings
load_dotenv()
account_sid = os.getenv('ACCOUNT_SID')
auth_token = os.getenv('AUTH_TOKEN')
client = Client(account_sid, auth_token)
cap = cv2.VideoCapture(source)
# Counter for blue hat detection
blue_hat_counter = 0
# Loop through the video frames
while cap.isOpened():
# Read a frame from the video
success, frame = cap.read()
if success:
# Run YOLOv8 inference on the frame
results = model(frame)
for result in results:
if 'blue hat' in result.names:
blue_hat_counter += 1
if blue_hat_counter % 10 == 0: # Change this number to adjust the frequency of messages
message = client.messages.create(
from_= 'whatsapp:+14155238886',
body = 'Topi biru terdeteksi',
to='whatsapp:+62xxxxxx'
)
# Visualize the results on the frame
annotated_frame = results[0].plot()
# Display the annotated frame
cv2.imshow("YOLOv8 Inference", annotated_frame)
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
# Break the loop if the end of the video is reached
break
# Release the video capture object and close the display window
cap.release()
cv2.destroyAllWindows()