|
| 1 | +import cv2 |
| 2 | +from ultralytics import YOLO |
| 3 | +from ultralytics.utils.plotting import Annotator, colors |
| 4 | +import pyresearch |
| 5 | + |
| 6 | +# Load the YOLO model |
| 7 | +model = YOLO("last.pt") |
| 8 | +names = model.model.names |
| 9 | + |
| 10 | +# Set up video capture and get dimensions |
| 11 | +cap = cv2.VideoCapture("demo1.mp4") |
| 12 | +w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS)) |
| 13 | + |
| 14 | +# Define video output parameters for MP4 format |
| 15 | +out = cv2.VideoWriter("visioneye-pinpoint.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h)) |
| 16 | + |
| 17 | +center_point = (-10, h) |
| 18 | + |
| 19 | +while True: |
| 20 | + ret, im0 = cap.read() |
| 21 | + if not ret: |
| 22 | + print("Video frame is empty or video processing has been successfully completed.") |
| 23 | + break |
| 24 | + |
| 25 | + # Run YOLO prediction |
| 26 | + results = model.predict(im0) |
| 27 | + boxes = results[0].boxes.xyxy.cpu() |
| 28 | + clss = results[0].boxes.cls.cpu().tolist() |
| 29 | + |
| 30 | + # Annotate the frame |
| 31 | + annotator = Annotator(im0, line_width=2) |
| 32 | + for box, cls in zip(boxes, clss): |
| 33 | + annotator.box_label(box, label=names[int(cls)], color=colors(int(cls))) |
| 34 | + annotator.visioneye(box, center_point) |
| 35 | + |
| 36 | + # Write the annotated frame to output |
| 37 | + out.write(im0) |
| 38 | + |
| 39 | + # Resize the frame to 1080 pixels in width for display in the imshow window |
| 40 | + display_width = 1080 |
| 41 | + display_height = int((display_width / w) * h) # Calculate the corresponding height to maintain aspect ratio |
| 42 | + im_display = cv2.resize(im0, (display_width, display_height)) |
| 43 | + |
| 44 | + # Show the resized frame |
| 45 | + cv2.imshow("visioneye-pinpoint", im_display) |
| 46 | + |
| 47 | + if cv2.waitKey(1) & 0xFF == ord("q"): |
| 48 | + break |
| 49 | + |
| 50 | +# Release resources |
| 51 | +out.release() |
| 52 | +cap.release() |
| 53 | +cv2.destroyAllWindows() |
0 commit comments