inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

[개정판] 딥러닝 컴퓨터 비전 완벽 가이드

Fast RCNN- NMS 적용 코드

366

젓인

작성한 질문수 158

0

안녕하세요. Fast RCNN Opencv Detection에서 NMS 적용을 후처리로 해보고 싶어서 코드를 짜봤는데, 결과가 NMS 안한 것은 17개, 한 것은 14개로 나왔는데 다음과 같이 코드를 짰는데 맞을까요? 감사합니다

import time

def get_detected_img(cv_net, img_array, score_threshold, nms_threshold, use_copied_array=True, is_print=True):
    
    rows = img_array.shape[0]
    cols = img_array.shape[1]
    
    draw_img = None
    if use_copied_array:
        draw_img = img_array.copy()
    else:
        draw_img = img_array
    
    cv_net.setInput(cv2.dnn.blobFromImage(img_array, swapRB=True, crop=False))
    
    start = time.time()
    cv_out = cv_net.forward()
    
    green_color=(0, 255, 0)
    red_color=(0, 0, 255)
    
    class_ids = []
    confidences = []
    boxes = []
    
    # detected 된 object들을 iteration 하면서 정보 추출
    for detection in cv_out[0,0,:,:]:
        score = float(detection[2])
        class_id = int(detection[1])
        
        # detected된 object들의 score가 함수 인자로 들어온 score_threshold 이상만 추출
        if score > score_threshold:
            # detected된 object들은 scale된 기준으로 예측되었으므로 다시 원본 이미지 비율로 계산
            left = detection[3] * cols
            top = detection[4] * rows
            right = detection[5] * cols
            bottom = detection[6] * rows
            
            width = right - left
            height = bottom - top
            
            confidences.append(float(score))
            class_ids.append(class_id)
            boxes.append([left, top, width, height])
    
    idxs = cv2.dnn.NMSBoxes(boxes, confidences, score_threshold, nms_threshold)
    
    if len(idxs) > 0:
        for i in idxs.flatten():
            box = boxes[i]
            left = box[0]
            top = box[1]
            width = box[2]
            height = box[3]
            
            # labels_to_names 딕셔너리로 class_id값을 클래스명으로 변경. opencv에서는 class_id + 1로 매핑해야함.
            caption = "{}: {:.4f}".format(labels_to_names_0[class_ids[i]], confidences[i])
            #cv2.rectangle()은 인자로 들어온 draw_img에 사각형을 그림. 위치 인자는 반드시 정수형.
            cv2.rectangle(draw_img, (int(left), int(top)), (int(left+width), int(top+height)), color=green_color, thickness=2)
            cv2.putText(draw_img, caption, (int(left), int(top - 5)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, red_color, 1)
            print(caption)
         
    if is_print:
        print('Detection 수행시간:',round(time.time() - start, 2),"초")

    return draw_img

딥러닝 컴퓨터-비전 python 머신러닝 배워볼래요? tensorflow keras

답변 1

0

권 철민

안녕하십니까,

별 이슈가 없어 보입니다.

감사합니다.

강의 환경설정 질문

0

38

2

Custom Dataset에서의 polygon 정보 관련

0

82

3

cvat.ai 보안 수준이 궁금합니다

0

78

2

캐클 nucleus 챌린지 runpod 실습 코드 에러 질문드립니다.

0

94

3

추론 결과의 Precision(또는 mAP) 평가 방법

0

82

2

mmdetection mask rcnn inferenct 실습 시 runpod 템플릿 관해서 질문드립니다.

0

58

2

runpod에서 google drive 연결 시 오류 발생

0

106

2

로드맵 선택

0

65

1

mmcv

0

56

2

Anchor box의 Positive 처리 위치

0

60

2

해당 강의 runpod 적용 후 에러 제보드립니다

0

83

2

run pod credit 관련 제보

0

95

2

mmdetection 2.x과 3.x 호환 관련 표기

0

76

2

mm_faster_rcnn_train_kitti.ipynb 실행 오류

0

94

3

질문 드립니다.

0

75

3

mm_faster_rcnn_train_coco_bccd 실행 오류 질문드립니다.

0

76

1

강사님께 수정을 제안드리고 싶은 것이 있습니다.

0

92

1

google automl efficientdet 다운로드 및 설치 오류

0

72

1

이상 탐지에 사용할 비전 기술 조언 부탁드립니다.

0

101

2

OpenCV 관련 질문드립니다.

0

69

2

mmcv 설치관련해서 문의드려요

0

326

3

강의 구성 관련해서 질문이 있습니다

1

134

2

모델 변환 성능 질문드립니다.

0

122

1

NMS 로직 문의 드려요

0

112

2