๋ณด์ ๊ฒฝ๋ณด ์์คํ ํ๋ก์ ํธ ์ฌ์ฉ Ultralytics YOLOv8
Ultralytics YOLOv8 ์ ํ์ฉํ ๋ณด์ ๊ฒฝ๋ณด ์์คํ ํ๋ก์ ํธ๋ ๊ณ ๊ธ ์ปดํจํฐ ๋น์ ๊ธฐ๋ฅ์ ํตํฉํ์ฌ ๋ณด์ ์กฐ์น๋ฅผ ๊ฐํํฉ๋๋ค. YOLOv8 Ultralytics ์์ ๊ฐ๋ฐํ ์ด ์์คํ ์ ์ค์๊ฐ ๋ฌผ์ฒด ๊ฐ์ง ๊ธฐ๋ฅ์ ์ ๊ณตํ์ฌ ์ ์ฌ์ ์ธ ๋ณด์ ์ํ์ ์ฆ์ ์๋ณํ๊ณ ๋์ํ ์ ์์ต๋๋ค. ์ด ํ๋ก์ ํธ๋ ๋ช ๊ฐ์ง ์ฅ์ ์ ์ ๊ณตํฉ๋๋ค:
- ์ค์๊ฐ ํ์ง: YOLOv8 ์ ํจ์จ์ฑ์ ํตํด ๋ณด์ ๊ฒฝ๋ณด ์์คํ ์ ๋ณด์ ์ฌ๊ณ ๋ฅผ ์ค์๊ฐ์ผ๋ก ํ์งํ๊ณ ๋์ํ์ฌ ๋์ ์๊ฐ์ ์ต์ํํ ์ ์์ต๋๋ค.
- ์ ํ๋: YOLOv8 ๋ ๋ฌผ์ฒด ๊ฐ์ง ์ ํ๋๊ฐ ๋ฐ์ด๋ ์คํ์ ์ค์ด๊ณ ๋ณด์ ๊ฒฝ๋ณด ์์คํ ์ ์ ๋ขฐ์ฑ์ ํฅ์์ํค๋ ๊ฒ์ผ๋ก ์ ๋ช ํฉ๋๋ค.
- ํตํฉ ๊ธฐ๋ฅ: ์ด ํ๋ก์ ํธ๋ ๊ธฐ์กด ๋ณด์ ์ธํ๋ผ์ ์ํํ๊ฒ ํตํฉ๋์ด ์ ๊ทธ๋ ์ด๋๋ ์ง๋ฅํ ๊ฐ์ ๊ณ์ธต์ ์ ๊ณตํ ์ ์์ต๋๋ค.
Watch: Ultralytics YOLOv8 ๋ฌผ์ฒด ๊ฐ์ง๋ฅผ ํตํ ๋ณด์ ๊ฒฝ๋ณด ์์คํ
ํ๋ก์ ํธ
์ฝ๋
๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ฐ์ ธ์ค๊ธฐ
import torch
import numpy as np
import cv2
from time import time
from ultralytics import YOLO
from ultralytics.utils.plotting import Annotator, colors
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
๋ฉ์์ง ๋งค๊ฐ๋ณ์ ์ค์
์ฐธ๊ณ
์ฑ ๋น๋ฐ๋ฒํธ ์์ฑ ํ์
- ์ฑ ๋น๋ฐ๋ฒํธ ์์ฑ๊ธฐ๋ก ์ด๋ํ์ฌ '๋ณด์ ํ๋ก์ ํธ'์ ๊ฐ์ ์ฑ ์ด๋ฆ์ ์ง์ ํ๊ณ 16์๋ฆฌ ๋น๋ฐ๋ฒํธ๋ฅผ ์ป์ต๋๋ค. ์ด ๋น๋ฐ๋ฒํธ๋ฅผ ๋ณต์ฌํ์ฌ ์๋ด์ ๋ฐ๋ผ ์ง์ ๋ ๋น๋ฐ๋ฒํธ ์ ๋ ฅ๋์ ๋ถ์ฌ๋ฃ์ต๋๋ค.
password = ""
from_email = "" # must match the email used to generate the password
to_email = "" # receiver email
์๋ฒ ์์ฑ ๋ฐ ์ธ์ฆ
์ด๋ฉ์ผ ๋ณด๋ด๊ธฐ ๊ธฐ๋ฅ
def send_email(to_email, from_email, object_detected=1):
"""Sends an email notification indicating the number of objects detected; defaults to 1 object."""
message = MIMEMultipart()
message['From'] = from_email
message['To'] = to_email
message['Subject'] = "Security Alert"
# Add in the message body
message_body = f'ALERT - {object_detected} objects has been detected!!'
message.attach(MIMEText(message_body, 'plain'))
server.sendmail(from_email, to_email, message.as_string())
๊ฐ์ฒด ๊ฐ์ง ๋ฐ ์๋ฆผ ๋ฐ์ ์
class ObjectDetection:
def __init__(self, capture_index):
"""Initializes an ObjectDetection instance with a given camera index."""
self.capture_index = capture_index
self.email_sent = False
# model information
self.model = YOLO("yolov8n.pt")
# visual information
self.annotator = None
self.start_time = 0
self.end_time = 0
# device information
self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
def predict(self, im0):
"""Run prediction using a YOLO model for the input image `im0`."""
results = self.model(im0)
return results
def display_fps(self, im0):
"""Displays the FPS on an image `im0` by calculating and overlaying as white text on a black rectangle."""
self.end_time = time()
fps = 1 / np.round(self.end_time - self.start_time, 2)
text = f'FPS: {int(fps)}'
text_size = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 1.0, 2)[0]
gap = 10
cv2.rectangle(im0, (20 - gap, 70 - text_size[1] - gap), (20 + text_size[0] + gap, 70 + gap), (255, 255, 255), -1)
cv2.putText(im0, text, (20, 70), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 0), 2)
def plot_bboxes(self, results, im0):
"""Plots bounding boxes on an image given detection results; returns annotated image and class IDs."""
class_ids = []
self.annotator = Annotator(im0, 3, results[0].names)
boxes = results[0].boxes.xyxy.cpu()
clss = results[0].boxes.cls.cpu().tolist()
names = results[0].names
for box, cls in zip(boxes, clss):
class_ids.append(cls)
self.annotator.box_label(box, label=names[int(cls)], color=colors(int(cls), True))
return im0, class_ids
def __call__(self):
"""Executes object detection on video frames from a specified camera index, plotting bounding boxes and returning modified frames."""
cap = cv2.VideoCapture(self.capture_index)
assert cap.isOpened()
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
frame_count = 0
while True:
self.start_time = time()
ret, im0 = cap.read()
assert ret
results = self.predict(im0)
im0, class_ids = self.plot_bboxes(results, im0)
if len(class_ids) > 0: # Only send email If not sent before
if not self.email_sent:
send_email(to_email, from_email, len(class_ids))
self.email_sent = True
else:
self.email_sent = False
self.display_fps(im0)
cv2.imshow('YOLOv8 Detection', im0)
frame_count += 1
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
server.quit()
๊ฐ์ฒด ๊ฐ์ง ํด๋์ค๋ฅผ ํธ์ถํ๊ณ ์ถ๋ก ์ ์คํํฉ๋๋ค.
๋์ ๋๋ค! ์ฝ๋๋ฅผ ์คํํ๋ฉด ๊ฐ์ฒด๊ฐ ๊ฐ์ง๋๋ฉด ์ด๋ฉ์ผ๋ก ํ ๋ฒ์ ์๋ฆผ์ ๋ฐ๊ฒ ๋ฉ๋๋ค. ์๋ฆผ์ ๋ฐ๋ณต์ ์ผ๋ก ์ ์ก๋์ง ์๊ณ ์ฆ์ ์ ์ก๋ฉ๋๋ค. ํ์ง๋ง ํ๋ก์ ํธ ์๊ตฌ ์ฌํญ์ ๋ง๊ฒ ์ฝ๋๋ฅผ ์์ ๋กญ๊ฒ ์ฌ์ฉ์ ์ง์ ํ ์ ์์ต๋๋ค.