Edge AI - 3. Edge AI 프로그래밍 실습 (3-1. 이미지 인식 AI (Image Recognition AI))

2025. 3. 10. 18:19AI/AI

📌 3-1. 이미지 인식 AI (Image Recognition AI)

Edge AI에서 이미지 인식(Computer Vision)은 가장 많이 활용되는 기술 중 하나입니다.
이를 위해 OpenCV + TensorFlow Lite, Jetson Nano + YOLOv8, Google Coral TPU 기반 실시간 객체 탐지 등의 방법을 사용할 수 있습니다.


🌟 1. OpenCV + TensorFlow Lite로 Edge AI 이미지 분류

TensorFlow Lite(TFLite)는 경량화된 딥러닝 모델을 엣지 디바이스에서 실행할 수 있도록 최적화된 프레임워크입니다.
OpenCV를 함께 활용하면 카메라에서 촬영한 이미지를 실시간으로 분석할 수 있습니다.

(1) TensorFlow Lite 환경 구축

📌 설치 방법

pip install opencv-python numpy tflite-runtime

(2) TFLite 모델을 활용한 이미지 분류

  • 사전 학습된 MobileNet 모델을 활용하여 이미지를 분류할 수 있습니다.

📌 TensorFlow Lite 이미지 분류 코드

import cv2
import numpy as np
import tensorflow.lite as tflite

# 모델 로드
interpreter = tflite.Interpreter(model_path="mobilenet_v1.tflite")
interpreter.allocate_tensors()

# 카메라 입력
cap = cv2.VideoCapture(0)

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

    # 이미지 전처리
    img = cv2.resize(frame, (224, 224))
    img = np.expand_dims(img, axis=0).astype(np.float32)

    # 모델 실행
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()
    interpreter.set_tensor(input_details[0]['index'], img)
    interpreter.invoke()
    
    output = interpreter.get_tensor(output_details[0]['index'])
    print("Prediction:", output)

    cv2.imshow("Image Classification", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

📌 활용 사례

  • 스마트폰 & IoT 기기에서 경량 이미지 분류
  • 실시간 스마트 CCTV 얼굴 인식
  • 스마트 팩토리에서 제품 불량 검사

🌟 2. Jetson Nano에서 YOLOv8 경량 모델 실행

Jetson Nano는 NVIDIA의 GPU 기반 Edge AI 개발 보드로, YOLO(You Only Look Once)와 같은 경량 객체 탐지 모델을 빠르게 실행할 수 있습니다.

(1) Jetson Nano 환경 구축

📌 CUDA 및 TensorRT 설치

sudo apt install nvidia-cuda-toolkit
sudo apt install nvidia-tensorrt

📌 YOLOv8 모델 다운로드 및 설치

pip install ultralytics

📌 YOLOv8 모델 실행 코드 (Jetson Nano 최적화 버전)

from ultralytics import YOLO
import cv2

# YOLOv8 모델 로드
model = YOLO("yolov8n.pt")  # 'n'은 Nano 버전 (경량 모델)

# 웹캠 또는 비디오 스트림
cap = cv2.VideoCapture(0)

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

    # YOLO 객체 탐지 수행
    results = model(frame)
    
    # 결과 출력
    for r in results:
        for box in r.boxes:
            x1, y1, x2, y2 = map(int, box.xyxy[0])
            cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)

    cv2.imshow("YOLOv8 Object Detection", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

📌 활용 사례

  • 자율주행 차량의 보행자 감지
  • 스마트시티 CCTV 객체 탐지
  • 공장 자동화 시스템에서 제품 분류

🌟 3. Google Coral TPU로 실시간 객체 탐지

Google Coral Edge TPU는 TensorFlow Lite 모델을 최적화하여 초저전력으로 빠르게 실행할 수 있도록 지원하는 AI 가속기입니다.

(1) Edge TPU 환경 구축

📌 Edge TPU 패키지 설치

echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list
sudo apt update
sudo apt install libedgetpu1-std

(2) Edge TPU 모델 실행

Google Coral TPU는 TensorFlow Lite 모델을 최적화하여 빠르게 실행할 수 있습니다.

📌 Edge TPU 객체 탐지 코드

import cv2
import numpy as np
import tflite_runtime.interpreter as tflite

# Edge TPU 모델 로드
interpreter = tflite.Interpreter(model_path="ssd_mobilenet_v2_edgetpu.tflite")
interpreter.allocate_tensors()

cap = cv2.VideoCapture(0)

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

    # 이미지 전처리
    img = cv2.resize(frame, (300, 300))
    img = np.expand_dims(img, axis=0).astype(np.float32)

    # 모델 실행
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()
    interpreter.set_tensor(input_details[0]['index'], img)
    interpreter.invoke()

    # 결과 출력
    output = interpreter.get_tensor(output_details[0]['index'])
    print("Detected Objects:", output)

    cv2.imshow("Edge TPU Object Detection", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

📌 활용 사례

  • 실시간 감시 시스템 (스마트 CCTV, 무인 점포)
  • 공장 자동화 시스템에서 품질 검사
  • 의료 영상 분석 (AI 기반 X-ray 판독)

📌 요약 정리

방법  주요 기술 활용 사례
OpenCV + TensorFlow Lite 이미지 분류, 실시간 영상 분석 스마트 CCTV, IoT 디바이스
Jetson Nano + YOLOv8 실시간 객체 탐지, GPU 가속 자율주행, 스마트팩토리
Google Coral TPU 저전력 AI 가속, Edge TPU 최적화 보안 감시, 의료 AI