EEE
EEE
EEE
Anıl Akpınar
220201013
Introduction:
This documentation provides an in-depth explanation of the object detection workflow using the YOLO
(You Only Look Once) algorithm in Python, followed by the integration of detection logs into
MATLAB for visualization. The goal is to detect objects in video frames, save detection data, and
overlay the results onto the video for analysis.
Why YOLO?
YOLO is chosen for object detection due to its real-time performance and accuracy. It divides the
image into a grid and predicts bounding boxes and class probabilities for each grid cell. This single-
stage approach makes YOLO significantly faster than two-stage methods like Faster R-CNN.
import cv2
import numpy as np
import datetime
video_path = '/home/pervane/Videos/Screencasts/Screencast.mp4'
yolo_net = cv2.dnn.readNetFromDarknet('/home/pervane/Projects/YOLO/ipkamera/yolov4-tiny-
custom.cfg', '/home/pervane/Projects/YOLO/ipkamera/yolov4-tiny-custom_final.weights')
layer_names = yolo_net.getLayerNames()
output_layers = [layer_names[i - 1] for i in yolo_net.getUnconnectedOutLayers()]
cap = cv2.VideoCapture(video_path)
log_file.write("Detected Objects:\n")
while True:
log_file.close()
cap.release()
cv2.destroyAllWindows()
YOLO outputs bounding boxes, confidence scores, and class IDs. Only detections with confidence
above a threshold (e.g., 0.5) are considered valid.
Log File Generation:
Detected objects' information, including timestamps, class IDs, and bounding box coordinates, is
written to a CSV log file. This log serves as a record for later visualization.
The CSV log file created by the Python script is imported into MATLAB. This log contains all the
necessary information to overlay detections onto the video.
figure;
hold on;
axis equal;
title('YOLO Detections');
xlabel('X');
ylabel('Y');
for i = 1:length(positions_x)
rectangle('Position', [positions_x(i), positions_y(i), widths(i), heights(i)], ...
'EdgeColor', 'r', 'LineWidth', 2);
text(positions_x(i), positions_y(i) - 10, ...
sprintf('Class: %d, Conf: %.2f', class_ids(i), confidences(i)), ...
'Color', 'blue', 'FontSize', 8);
end
hold off;
Visualization Issues:
An issue was encountered where bounding boxes did not align perfectly with video frames. This could
be due to timing mismatches or incorrect frame-rate assumptions.
Solution Considerations:
Ensure that the log timestamps are synchronized with the video frames.
Verify that the bounding box coordinates match the video resolution.
Conclusion:
This workflow demonstrates a complete pipeline from object detection using YOLO in Python to
visualizing the results in MATLAB. Although the detection process was successful, further refinement
is needed in MATLAB to achieve perfect frame synchronization and accurate bounding box overlays.
Future improvements may include using Python for visualization to maintain consistency across the
process.