์ฝ˜ํ…์ธ ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

๋ณด์•ˆ ๊ฒฝ๋ณด ์‹œ์Šคํ…œ ํ”„๋กœ์ ํŠธ ์‚ฌ์šฉ 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

์„œ๋ฒ„ ์ƒ์„ฑ ๋ฐ ์ธ์ฆ

server = smtplib.SMTP('smtp.gmail.com: 587')
server.starttls()
server.login(from_email, password)

์ด๋ฉ”์ผ ๋ณด๋‚ด๊ธฐ ๊ธฐ๋Šฅ

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()

๊ฐ์ฒด ๊ฐ์ง€ ํด๋ž˜์Šค๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ์ถ”๋ก ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

detector = ObjectDetection(capture_index=0)
detector()

๋์ž…๋‹ˆ๋‹ค! ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๊ฐœ์ฒด๊ฐ€ ๊ฐ์ง€๋˜๋ฉด ์ด๋ฉ”์ผ๋กœ ํ•œ ๋ฒˆ์˜ ์•Œ๋ฆผ์„ ๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์•Œ๋ฆผ์€ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ „์†ก๋˜์ง€ ์•Š๊ณ  ์ฆ‰์‹œ ์ „์†ก๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ”„๋กœ์ ํŠธ ์š”๊ตฌ ์‚ฌํ•ญ์— ๋งž๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž์œ ๋กญ๊ฒŒ ์‚ฌ์šฉ์ž ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋ฉ”์ผ ์ˆ˜์‹  ์ƒ˜ํ”Œ

์ด๋ฉ”์ผ ์ˆ˜์‹  ์ƒ˜ํ”Œ



์ƒ์„ฑ 2023-12-02, ์—…๋ฐ์ดํŠธ 2024-05-03
์ž‘์„ฑ์ž: glenn-jocher (3), RizwanMunawar (1)

๋Œ“๊ธ€