From cf9a1eead7863eec0c31bd4c1d65d5f83ac4c8ee Mon Sep 17 00:00:00 2001 From: becker Date: Wed, 13 Mar 2024 20:47:56 +0100 Subject: [PATCH] Dateien nach "Aufraeumroboter_Drakkar/Dennis_Dirk_Becker/yolov8" hochladen --- .../yolov8/object_tracking.py | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Aufraeumroboter_Drakkar/Dennis_Dirk_Becker/yolov8/object_tracking.py diff --git a/Aufraeumroboter_Drakkar/Dennis_Dirk_Becker/yolov8/object_tracking.py b/Aufraeumroboter_Drakkar/Dennis_Dirk_Becker/yolov8/object_tracking.py new file mode 100644 index 0000000..d7ebc7c --- /dev/null +++ b/Aufraeumroboter_Drakkar/Dennis_Dirk_Becker/yolov8/object_tracking.py @@ -0,0 +1,61 @@ +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() + +