Wah ternyata yolov5 sangat cocok digunakan pada raspberry pi 3 (baca disini dulu), jadi semangat nihh berbagi kegembiraan bersama pembaca. Jadi seperti yang dulu saat saya belajar yolo v3, ujungnya adalah deteksi orang pada cctv yang bisa dibaca disini. Langsung aja kita akan buat script untuk mengetestnya melalui cctv yang ada di kota malang yang lebih ringan daripada di jogja, alamat untuk memilih titiknya di: http://cctv.malangkota.go.id/
Dan script gabungan dari beberapa tulisan sebelumnya saya bagikan seperti berikut :
import sys import time import json import torch import cv2 import os import contextlib from pathlib import Path os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = '/usr/lib/x86_64-linux-gnu/qt5/plugins' def draw_boxes(frame, results): # Get the bounding boxes, labels, and confidence scores bboxes = results.xyxyn[0][:, :-1].numpy() labels = results.xyxyn[0][:, -1].numpy() confidences = results.xyxyn[0][:, -2].numpy() # Define the COCO class label for 'person' PERSON_LABEL = 0 # Draw boxes for persons only for bbox, label, confidence in zip(bboxes, labels, confidences): if label == PERSON_LABEL: x1, y1, x2, y2 = bbox[:4] x1 = int(x1 * frame.shape[1]) y1 = int(y1 * frame.shape[0]) x2 = int(x2 * frame.shape[1]) y2 = int(y2 * frame.shape[0]) cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2) cv2.putText(frame, f'Orang {confidence:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (200, 255, 0), 2) def main(stream_source): start_time = time.time() # Load YOLOv5 model model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True) # Open the video stream cap = cv2.VideoCapture(stream_source) if not cap.isOpened(): print(f"Error: Could not open video stream {stream_source}") return # start_time = time.time() ret, frame = cap.read() if not ret: print("Error: Could not read frame") cap.release() return # Perform inference results = model(frame) # Extract bounding boxes and labels labels = results.xyxyn[0][:, -1].numpy() n_persons = sum(1 for label in labels if label == 0) # label '0' typically corresponds to 'person' in COCO dataset # Calculate processing time processing_time = time.time() - start_time # Output results as JSON output = { "orang": n_persons, "time": round(processing_time, 2) } print(json.dumps(output)) # Save the frame with bounding boxes as JPEG draw_boxes(frame, results) output_image_path = "/home/lampuku/yolov5/output.jpg" #results.render() # This function draws bounding boxes on the frame cv2.imwrite(output_image_path, frame) cap.release() if __name__ == "__main__": if len(sys.argv) != 2: print("Usage: python single_detect.py <stream_source>") else: stream_source = sys.argv[1] main(stream_source)