본문 바로가기
-------------코딩-------------/Python 기초 코딩

show bbox(coco)

by 탶선 2023. 8. 2.
반응형
import os
import cv2
import json

base_path = ""
json_path = base_path + '/' + "_annotations.coco.json"
with open(json_path, "r") as json_file:
    st_python = json.load(json_file)
    
red_color = (0,0,255)
tmp = 0

for i in range(len(st_python['images'])):

    file_name = st_python['images'][i]['file_name']
    img_id = st_python['images'][i]['id']

    img = cv2.imread(base_path+'/'+file_name)

    while True:
        bbox = st_python['annotations'][tmp]['bbox']
        cv2.rectangle(img, (bbox[0],bbox[1]), (bbox[0]+int(bbox[2]), bbox[1]+int(bbox[3])), red_color,2)
        if st_python['annotations'][tmp+1]['image_id'] != img_id:
            break            
        tmp += 1
    try:
        cv2.imshow(file_name,img)
    except:
        print('can\'t show image')
        continue
    k = cv2.waitKey()
    if k == 0x1B: #ESC키
        cv2.destroyAllWindows()
        break
    elif k == ord('d'):
        os.remove(base_path+'/'+file_name)
    elif k == ord('t'):
        os.copy('')

        
    cv2.destroyAllWindows()
    tmp += 1
반응형

댓글