-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdetector.py
144 lines (116 loc) · 5.15 KB
/
detector.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import ssl
# run this on proxy free net and replace your mail with the unique token generated
# when you allow 2fa on your mail via google app password for third party apps
import cv2
from ultralytics import YOLO
import math
import cvzone
import time
import torch
import smtplib
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login('your gmail', 'ur token')
# Initialize time variables
pr_time = 0
curr_time = 0
previous_time=int(time.time())
centre_points_current_frame=[]
centre_points_previous_frame=[]
# Open the video file
vid_path = r"C:\Users\manth\Downloads\Easy and cute ways to identify your luggage.mp4"
cap = cv2.VideoCapture(vid_path)
# Set the width and height for video frames
des_width = 1280
des_height = 720
# Set the resolution of the video capture
cap.set(cv2.CAP_PROP_FRAME_WIDTH, des_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, des_height)
# Load YOLO model
model = YOLO("yolov8n.pt")
print(torch.cuda.is_available())
device = 'cuda' if torch.cuda.is_available() else 'cpu'
print(f'Using device: {device}')
# model.to("cuda:0")
# # if cv2.cuda.getCudaEnabledDeviceCount() > 0:
# # Check for available GPUs
# model.to("cuda:0") # Move the model to the GPU
# COCO classes
coco_classes = [
"person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat",
"traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",
"dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack",
"umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball",
"kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket",
"bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
"sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair",
"couch", "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote",
"keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book",
"clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush"
]
# def detect_abandoned(p1,p2):
# diff=int(math.hypot(p1[0]-p2[0],p1[1]-p2[1]))
# print("difference",diff)
# if diff<30:
# print("NON ATTENDED!!!!!!!!")
# else:
# print("attended")
while True:
# Read a frame from the video
ret, img = cap.read()
# if the frame is read
if not ret:
break
# Flip the image horizontally
img = cv2.flip(img, 1)
# Perform object detection using YOLO
results = model.predict(img, stream=True)
# Process detection results
for result in results:
boxes = result.boxes
for box in boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0])
w, h = x2 - x1, y2 - y1
cx, cy = x2 - (w / 2), y2 - (h / 2)
confidence = math.ceil((box.conf[0] * 100)) / 100
class_index = int(box.cls[0])
# Check if the detected object is a suitcase
if coco_classes[class_index] == "suitcase":
# Highlight the bounding box
print("suitcase detected!!!!")
current_time=int(time.time())
elapsed_time=current_time-previous_time
# Display class name
cv2.putText(img,f'{coco_classes[class_index]}',(x2,y2),cv2.FONT_HERSHEY_PLAIN,0.5,(255,0,0),2,2)
print("obj_centre_points:",(int(cx),int(cy)))
centre_points_current_frame.append((cx,cy))
cv2.circle(img, (int(cx), int(cy)), 2, (0, 0, 255), 3)
cv2.putText(img, format(elapsed_time), (x1, y1), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 2, 2)
for pt1 in centre_points_current_frame:
for pt2 in centre_points_previous_frame:
diff=(math.hypot(pt1[0]-pt2[0],pt1[1]-pt2[1]))
# print("DIFFERENCE",diff)
if diff<10 :
print("UNATTENDED>>>>>>>>>")
cv2.rectangle(img, (x1, y1), (x1 + w, y1 + h), (0, 0, 255), 2)
server.sendmail('sender id', 'receiver id', 'Bag is Unattended !!! PPT banvte ek slide ahe mail vali !!!')
else:
print("attended")
cv2.rectangle(img, (x1, y1), (x1 + w, y1 + h), (0, 255, 0), 2)
# Calculate frames per second (FPS)
curr_time = time.time()
fps = 1 / (curr_time - pr_time)
pr_time = curr_time
# Display FPS on the image
cv2.putText(img, f"{str(int(fps))}fps", (10, 70), cv2.FONT_HERSHEY_PLAIN, 2, (225, 0, 0), 3)
# Show the final image
resized=cv2.resize(img,(680,420))
cv2.imshow("Object Detection", resized)
centre_points_previous_frame=centre_points_current_frame.copy()
# Break the loop if the 'Esc' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the video capture
# close all windows
cap.release()
cv2.destroyAllWindows()