-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyolo-ppe-detection.py
More file actions
63 lines (47 loc) · 2.29 KB
/
Copy pathyolo-ppe-detection.py
File metadata and controls
63 lines (47 loc) · 2.29 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
58
59
60
61
62
63
import cv2
from ultralytics import YOLO
# We will be using cvzone to display all the detections
import cvzone
import math
# For Webcam
# cap = cv2.VideoCapture(0)
# # # setting the width and height of the webcam
# cap.set(3, 1280) # width
# cap.set(4, 720) # height
# For Videos
cap = cv2.VideoCapture("../Videos/ppe-1-1.mp4")
model = YOLO('ppe.pt')
# Based on Dataset which we have created using google colab
classNames = ['Excavator', 'Gloves', 'Hardhat', 'Ladder', 'Mask', 'NO-Hardhat', 'NO-Mask', 'NO-Safety Vest', 'Person',
'SUV', 'Safety Cone', 'Safety Vest', 'bus', 'dump truck', 'fire hydrant', 'machinery', 'mini-van',
'sedan', 'semi', 'trailer', 'truck and trailer', 'truck', 'van', 'vehicle', 'wheel loader']
while True:
success, img = cap.read()
results = model(img, stream = True)
for r in results:
boxes = r.boxes
for box in boxes:
# -------------------- Displaying the Bounding Box
x1, y1, x2, y2 = box.xyxy[0] # can also use box.xywh, to get x, y, width and height of the box
# x1, y1, w, h = box.xywh[0]
x1, y1, x2, y2 = int (x1), int (y1), int (x2), int(y2)
# Using opencv, basic rectangles
# cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 255), 3)
w, h = x2-x1, y2-y1
bbox = x1, y1, w, h
conf = math.ceil((box.conf[0] * 100)) / 100
# We will find the id number, use classNames[id] to get the object name
cls = int(box.cls[0])
currentClass = classNames[cls]
if conf > 0.5:
if currentClass == 'Safety Vest' or currentClass == 'Mask' or currentClass == 'Hardhat' or currentClass == 'Gloves':
myColor = (0, 255, 0)
elif currentClass == 'NO-Safety Vest' or currentClass == 'NO-Mask' or currentClass == 'NO-Hardhat':
myColor = (0, 0, 255)
else:
myColor = (255, 0, 0)\
cv2.rectangle(img, (x1, y1), (x2, y2), myColor)
cvzone.putTextRect(img, f"{currentClass} {conf}",(max(0,x1), max(35, y1)),
scale = 1, thickness = 1, colorB = myColor, colorT = (255, 255, 255), colorR = myColor)
cv2.imshow('Webcam', img)
cv2.waitKey(1)