62 lines
1.7 KiB
Python
62 lines
1.7 KiB
Python
import datetime
|
|
from ultralytics import YOLO
|
|
import cv2 as cv
|
|
|
|
|
|
#define some constants
|
|
CONFIDENCE_THRESHOLD = 0.8
|
|
GREEN = (0, 255,0)
|
|
|
|
#initialize the video capture object
|
|
video_cap = cv.VideoCapture(0)
|
|
|
|
#load the pretrained YOLOv8n model
|
|
model = YOLO("yolov8n.pt")
|
|
|
|
while True:
|
|
#start time to compute the fps
|
|
start = datetime.datetime.now()
|
|
ret, frame = video_cap.read()
|
|
|
|
#if there are no more frames to process, break out of the loop
|
|
if not ret:
|
|
break
|
|
|
|
#run the YOLO model on the frame
|
|
detections = model(frame)[0]
|
|
|
|
#loop over the detections
|
|
for data in detections.boxes.data.tolist():
|
|
#extract the confidence (i.e., probability) associated with the detection
|
|
confidence = data[4]
|
|
|
|
#filter out weak detections by ensuring the
|
|
#confidence is grater than the minimum confidence
|
|
if float(confidence) < CONFIDENCE_THRESHOLD:
|
|
continue
|
|
|
|
#if the confidence is greater than the minimum confidence,
|
|
#draw the bounding box on the frame
|
|
xmin, ymin, xmax, ymax = int(data[0]), int(data[1]), int(data[2]), int(data[3])
|
|
cv.rectangle(frame, (xmin, ymin), (xmax, ymax), GREEN, 2)
|
|
|
|
#end of time to compute the fps
|
|
end = datetime.datetime.now()
|
|
#show the time it took to process 1 frame
|
|
total = (end - start).total_seconds()
|
|
print(f"Time to process 1 frame: {total * 1000:.0f} milliseconds")
|
|
|
|
#calculate the frame per second and draw it on the frame
|
|
fps = f"FPS: {1 / total:.2f}"
|
|
cv.putText(frame, fps, (50,50), cv.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 8)
|
|
|
|
#show the frame to our screen
|
|
cv.imshow("Frame", frame)
|
|
if cv.waitKey(1) == 'e':
|
|
break
|
|
|
|
video_cap.release()
|
|
cv.destroyAllWindows()
|
|
|
|
|