호타리 2025. 5. 22. 16:38

2025.05.22

 

source yolov5/yolov5-env/bin/activate

 

1. 라즈베리파이의 Pictures 디렉토리에 있는 이미지 파일을 선택하여 YOLO 모델 탐지를 수행하고 그 결과 이미지를 화면에 출력하시오. 출력 화면에는 TrackBar 를 설치하여 Slide를 조정함에 따라 Confidence 값이 미달하는 객체는 탐지결과에서 제외되도록 구성하시오.

2. 동일한 기능을 WebCam 화면에도 적용하여 동영상 객체 인식을 구현하시오.

import cv2
import torch
import numpy as np
import os
from glob import glob

# Load YOLOv5s model (pretrained)
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
model.eval()

def detect_and_show_static(img_path):
    img = cv2.imread(img_path)
    if img is None:
        print(f"Failed to load {img_path}")
        return

    window = 'Image Detection'
    cv2.namedWindow(window)
    cv2.createTrackbar('Confidence %', window, 50, 100, lambda x: None)

    while True:
        thresh = cv2.getTrackbarPos('Confidence %', window) / 100.0
        model.conf = thresh
        results = model(img)

        display = img.copy()
        for *box, conf, cls in results.xyxy[0].cpu().numpy():
            x1, y1, x2, y2 = map(int, box)
            label = f"{model.names[int(cls)]}: {conf:.2f}"
            cv2.rectangle(display, (x1, y1), (x2, y2), (0, 255, 0), 2)
            cv2.putText(display, label, (x1, y1 - 5),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)

        cv2.imshow(window, display)
        key = cv2.waitKey(30) & 0xFF
        if key == 27:  # ESC
            break
        elif key == ord('n'):
            break

    cv2.destroyWindow(window)

img_files = glob(os.path.expanduser('~/Pictures/*.[jp][pn]g'))
for img_path in img_files:
    print("Showing:", img_path)
    detect_and_show_static(img_path)

def webcam_detection():
    window = 'WebCam Detection'
    cv2.namedWindow(window)
    cv2.createTrackbar('Confidence %', window, 50, 100, lambda x: None)

    cap = cv2.VideoCapture(0)
    if not cap.isOpened():
        print("Webcam open failed")
        return

    while True:
        ret, frame = cap.read()
        if not ret:
            break

        thresh = cv2.getTrackbarPos('Confidence %', window) / 100.0
        model.conf = thresh
        results = model(frame)

        for *box, conf, cls in results.xyxy[0].cpu().numpy():
            x1, y1, x2, y2 = map(int, box)
            label = f"{model.names[int(cls)]}: {conf:.2f}"
            cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 2)
            cv2.putText(frame, label, (x1, y1 - 5),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1)

        cv2.imshow(window, frame)
        if cv2.waitKey(1) & 0xFF == 27:
            break

    cap.release()
    cv2.destroyAllWindows()

webcam_detection()

 

3. 'Person' 이 감지될 경우 영상을 저장하고 계속 감시. (기타 객체 무시)

import cv2
import torch
import numpy as np
import os
from glob import glob
from datetime import datetime

model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
model.eval()

SAVE_DIR = "person_captures"
os.makedirs(SAVE_DIR, exist_ok=True)

def detect_and_show_static(img_path):
    img = cv2.imread(img_path)
    if img is None:
        print(f"Failed to load {img_path}")
        return

    window = 'Image Detection'
    cv2.namedWindow(window)
    cv2.createTrackbar('Confidence %', window, 50, 100, lambda x: None)

    while True:
        thresh = cv2.getTrackbarPos('Confidence %', window) / 100.0
        model.conf = thresh
        results = model(img)

        display = img.copy()
        for *box, conf, cls in results.xyxy[0].cpu().numpy():
            x1, y1, x2, y2 = map(int, box)
            label = f"{model.names[int(cls)]}: {conf:.2f}"
            color = (0, 255, 0) if int(cls) == 0 else (255, 0, 0)
            cv2.rectangle(display, (x1, y1), (x2, y2), color, 2)
            cv2.putText(display, label, (x1, y1 - 5),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1)

        cv2.imshow(window, display)
        key = cv2.waitKey(30) & 0xFF
        if key in (27, ord('n')):
            break

    cv2.destroyWindow(window)

img_files = glob(os.path.expanduser('~/Pictures/*.[jp][pn]g'))
for img_path in img_files:
    print("Showing:", img_path)
    detect_and_show_static(img_path)

def webcam_detection():
    window = 'WebCam Detection'
    cv2.namedWindow(window)
    cv2.createTrackbar('Confidence %', window, 50, 100, lambda x: None)

    cap = cv2.VideoCapture(0)
    if not cap.isOpened():
        print("Webcam open failed")
        return

    while True:
        ret, frame = cap.read()
        if not ret:
            break

        thresh = cv2.getTrackbarPos('Confidence %', window) / 100.0
        model.conf = thresh

        results = model(frame)
        detections = results.xyxy[0].cpu().numpy()

        person_detected = False
        for x1, y1, x2, y2, conf, cls in detections:
            cls = int(cls)
            label = f"{model.names[cls]}: {conf:.2f}"
            color = (255, 0, 0)
            cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), color, 2)
            cv2.putText(frame, label, (int(x1), int(y1) - 5),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1)

            if cls == 0:
                person_detected = True

        if person_detected:
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
            save_path = os.path.join(SAVE_DIR, f"person_{timestamp}.jpg")
            cv2.imwrite(save_path, frame)
            print(f"??? Person detected! Saved to {save_path}")

        cv2.imshow(window, frame)
        if cv2.waitKey(1) & 0xFF == 27:
            break

    cap.release()
    cv2.destroyAllWindows()

webcam_detection()