* 이 글은 YOLOv7 기준으로 작성하였음
이전에 YOLO 모자이크에 관련한 글을 작성했었는데, 더 간단하게 정리함
GitHub - WongKinYiu/yolov7: Implementation of paper - YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time
Implementation of paper - YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors - GitHub - WongKinYiu/yolov7: Implementation of paper - YOLOv7: Trainable bag-of...
github.com
일단 Github에서 받은 YOLO로 들어가 보면 utils 폴더가 있다.
이 폴더에 있는 plots.py 파일에서 함수 하나만 추가해 주면 된다.
추가할 함수 위치는 상관없지만 나중에 찾기 편하도록 plot_one_box 함수 아래에 선언할 것이다.
이 plot_one_box 함수가 바로 객체 검출을 할 때 인식된 객체에 네모 박스를 생성하는 함수이다.
따라서 아래에 모자이크 함수를 추가한다.
# utils/plots.py
def mosaic(xywh, img, label=None, strength=27):
"""
Plots mosaic box on image img
:param xywh: 인식 객체 좌표
:param img: 현재 프레임 이미지
:param label: 인식 객체 종류
:param strength: 모자이크 수치
"""
if label:
x, y, w, h = int(xywh[0]), int(xywh[1]), int(xywh[2]) - int(xywh[0]), int(xywh[3]) - int(xywh[1])
roi = img[y:y + h, x:x + w]
# while True:
try:
roi = cv2.resize(roi, (w // strength, h // strength))
except cv2.error:
strength -= 1
mosaic = cv2.resize(roi, (w, h), interpolation=cv2.INTER_AREA)
img[y:y + h, x:x + w] = mosaic
그리고 메인 실행 파일인 detect.py 에서 아마 plot_one_box 함수 하나만 참조되어 있을 것인데, 모자이크 함수를 추가해 주어야 한다.
# detect.py [line 14]
from utils.plots import plot_one_box, mosaic
또한 네모 박스말고 모자이크를 하기 위해 실행 구문을 교체해준다.
# detect.py [line 131]
if save_img or view_img: # Add bbox to image
label = f'{names[int(cls)]} {conf:.2f}'
# plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=1)
mosaic(xyxy, im0, label=label, strength=27)
모자이크 수치, strength를 조정하여 본인이 원하는 수치를 찾아야 한다.
이 수치는 이미지, 영상의 해상도에 따라 영향을 받아 달라질 수 있다.
얼굴 인식 모델을 사용하여 모자이크를 진행한 결과.
![]() |
![]() |
원본 이미지 | 결과 이미지 |
'Development > YOLO' 카테고리의 다른 글
YOLO cfg 파일에 대한 정보 (0) | 2023.06.19 |
---|---|
Yolov7 프로젝트 실행 파일 만들기 (0) | 2023.01.09 |
YOLO v7 인식된 물체를 모자이크 하기 (0) | 2022.12.07 |
윈도우 환경 YOLO IndexError: invalid index to scalar variable. (0) | 2022.11.17 |