การพัฒนาโปรแกรมตรวจจับวัตถุในภาพด้วย Model yoloV8
การพัฒนาโปรแกรมตรวจจับขนาดของวัตถุในภาพ (Count Object) ซึ่งมีขั้นตอนดังนี้ คลิกเพื่อโหลดภาพ
yolov8_object_detection_on_custom_dataset.ipynb - Colab
train: ../dataset003/images/training
val: ../dataset003/labels/validation
#number of classes
nc: 3
#class names
names: ['1','2','3']
👉การทดสอบโปรแกรมตรวจจับวัตถุในภาพด้วย Model yoloV8 โดยรับภาพจากกล้อง cctv HIKVISION ด้วยการสื่อสารแบบ RTSP
import cv2
from ultralytics import YOLO
import threading
import queue
# RTSP URL of the camera
rtsp_url = "rtsp://admin:Vision321@192.168.1.64:554/Streaming/Channels/101"
# Queue to hold frames
frame_queue = queue.Queue(maxsize=1) # Limit queue size to 1 to always get the latest frame
# Function to read frames from the RTSP stream
def capture_frames():
cap = cv2.VideoCapture(rtsp_url)
cap.set(cv2.CAP_PROP_BUFFERSIZE, 1) # Set buffer size to 1
while True:
ret, frame = cap.read()
if not ret:
print("Error: Failed to read frame.")
break
# Add the frame to the queue, discarding old frames if the queue is full
if frame_queue.full():
frame_queue.get() # Discard the oldest frame
frame_queue.put(frame)
cap.release()
# Start the frame capture thread
threading.Thread(target=capture_frames, daemon=True).start()
# Load YOLO model
yolo = YOLO('yolov8s.pt')
# Function to get colors for classes
def getColours(cls_num):
base_colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
color_index = cls_num % len(base_colors)
increments = [(1, -2, 1), (-2, 1, -1), (1, -1, 2)]
color = [base_colors[color_index][i] + increments[color_index][i] *
(cls_num // len(base_colors)) % 256 for i in range(3)]
return tuple(color)
# Main loop to process frames
while True:
if not frame_queue.empty():
frame = frame_queue.get()
# Resize the frame
# frame = cv2.resize(frame, (500, 300))
# Use YOLO for object detection and tracking
results = yolo.track(frame, stream=True)
# Loop through the results from YOLO
for result in results:
classes_names = result.names
for box in result.boxes:
if box.conf[0] > 0.4:
x1, y1, x2, y2 = map(int, box.xyxy[0])
cls = int(box.cls[0])
class_name = classes_names[cls]
colour = getColours(cls)
# Draw bounding box and label
cv2.rectangle(frame, (x1, y1), (x2, y2), colour, 2)
cv2.putText(frame, f'{class_name} {box.conf[0]:.2f}', (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 1, colour, 2)
# Display the frame
cv2.imshow('frame', frame)
# Exit on 'q' key press
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
เมื่อรันโปรแกรมแล้วผลลัพท์ที่ได้
import cv2
from ultralytics import YOLO
# โหลดโมเดล YOLO
yolo = YOLO('yolov8s.pt')
# โหลดวิดีโอ
videoCap = cv2.VideoCapture(0) # 0 หมายถึงใช้กล้อง default ของเครื่อง
# ฟังก์ชันสำหรับเลือกสีของคลาส
def getColours(cls_num):
base_colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
color_index = cls_num % len(base_colors)
increments = [(1, -2, 1), (-2, 1, -1), (1, -1, 2)]
color = [base_colors[color_index][i] + increments[color_index][i] *
(cls_num // len(base_colors)) % 256 for i in range(3)]
return tuple(color)
while True:
ret, frame = videoCap.read()
if not ret:
continue
# ใช้ YOLO ในการตรวจจับและติดตามวัตถุในเฟรมปัจจุบัน
results = yolo.track(frame, stream=True)
# วนลูปผลลัพธ์ที่ได้จาก YOLO
for result in results:
# ดึงชื่อคลาส
classes_names = result.names
# วนลูปทุกรายการที่ตรวจพบ
for box in result.boxes:
# ตรวจสอบค่า confidence
if box.conf[0] > 0.4:
# ดึงพิกัดของกล่อง
[x1, y1, x2, y2] = box.xyxy[0]
# แปลงเป็น integer
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
# ดึงคลาส
cls = int(box.cls[0])
# ดึงชื่อคลาส
class_name = classes_names[cls]
# ดึงสีที่เซ็ตไว้สำหรับคลาสนั้น
colour = getColours(cls)
# วาดสี่เหลี่ยมล้อมตัววัตถุ
cv2.rectangle(frame, (x1, y1), (x2, y2), colour, 2)
# แสดงชื่อคลาสและค่า confidence บนภาพ
cv2.putText(frame, f'{classes_names[int(box.cls[0])]} {box.conf[0]:.2f}', (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 1, colour, 2)
# แสดงภาพที่ได้หลังจากประมวลผล
cv2.imshow('frame', frame)
# หยุดการทำงานเมื่อกด 'q'
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# คืนทรัพยากรและปิดหน้าต่างทั้งหมด
videoCap.release()
cv2.destroyAllWindows()