File size: 7,807 Bytes
3ec4928 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
import os
import sys
from os import path as osp
from io import BytesIO
from mbench.ytvos_ref import build as build_ytvos_ref
import argparse
import opts
import sys
from pathlib import Path
import os
from os import path as osp
import skimage
from io import BytesIO
import numpy as np
import pandas as pd
import regex as re
import json
import cv2
from PIL import Image, ImageDraw
import torch
from torchvision.transforms import functional as F
from skimage import measure # (pip install scikit-image)
from shapely.geometry import Polygon, MultiPolygon # (pip install Shapely)
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.collections import PatchCollection
from matplotlib.patches import Rectangle
import textwrap
import ipywidgets as widgets
from IPython.display import display, clear_output
from openai import OpenAI
import base64
def number_objects_and_encode(idx, color_mask=False):
encoded_frames = {}
contoured_frames = {} # New dictionary for original images
vid_cat_cnts = {}
vid_meta = metas[idx]
vid_data = train_dataset[idx]
vid_id = vid_meta['video']
frame_indx = vid_meta['sample_indx']
cat_names = set(vid_meta['obj_id_cat'].values())
imgs = vid_data[0]
for cat in cat_names:
cat_frames = []
contour_frames = []
frame_cat_cnts = {}
for i in range(imgs.size(0)):
frame_name = frame_indx[i]
frame = np.copy(imgs[i].permute(1, 2, 0).numpy())
frame_for_contour = np.copy(imgs[i].permute(1, 2, 0).numpy())
frame_data = vid_data[2][frame_name]
obj_ids = list(frame_data.keys())
cat_cnt = 0
for j in range(len(obj_ids)):
obj_id = obj_ids[j]
obj_data = frame_data[obj_id]
obj_bbox = obj_data['bbox']
obj_valid = obj_data['valid']
obj_mask = obj_data['mask'].numpy().astype(np.uint8)
obj_cat = obj_data['category_name']
if obj_cat == cat and obj_valid:
cat_cnt += 1
if color_mask == False:
contours, _ = cv2.findContours(obj_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(frame, contours, -1, colors[j], 3)
for i, contour in enumerate(contours):
# ์ค๊ณฝ์ ์ค์ฌ ๊ณ์ฐ
moments = cv2.moments(contour)
if moments["m00"] != 0: # ์ค์ฌ ๊ณ์ฐ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ
cx = int(moments["m10"] / moments["m00"])
cy = int(moments["m01"] / moments["m00"])
else:
cx, cy = contour[0][0] # ์ค์ฌ ๊ณ์ฐ ๋ถ๊ฐ์ ๋์ฒด ์ขํ ์ฌ์ฉ
# ํ
์คํธ ๋ฐฐ๊ฒฝ (๊ฒ์์ ๋ฐฐ๊ฒฝ ๋ง๋ค๊ธฐ)
font = cv2.FONT_HERSHEY_SIMPLEX
text = obj_id
text_size = cv2.getTextSize(text, font, 1, 2)[0]
text_w, text_h = text_size
# ํ
์คํธ ๋ฐฐ๊ฒฝ ๊ทธ๋ฆฌ๊ธฐ (๊ฒ์์ ๋ฐฐ๊ฒฝ)
cv2.rectangle(frame, (cx - text_w // 2 - 5, cy - text_h // 2 - 5),
(cx + text_w // 2 + 5, cy + text_h // 2 + 5), (0, 0, 0), -1)
# ํ
์คํธ ๊ทธ๋ฆฌ๊ธฐ (ํฐ์ ํ
์คํธ)
cv2.putText(frame, text, (cx - text_w // 2, cy + text_h // 2),
font, 1, (255, 255, 255), 2)
else:
alpha = 0.08
colored_obj_mask = np.zeros_like(frame)
colored_obj_mask[obj_mask == 1] = colors[j]
frame[obj_mask == 1] = (
(1 - alpha) * frame[obj_mask == 1]
+ alpha * colored_obj_mask[obj_mask == 1]
)
contours, _ = cv2.findContours(obj_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(frame, contours, -1, colors[j], 2)
cv2.drawContours(frame_for_contour, contours, -1, colors[j], 2)
if len(contours) > 0:
largest_contour = max(contours, key=cv2.contourArea)
M = cv2.moments(largest_contour)
if M["m00"] != 0:
center_x = int(M["m10"] / M["m00"])
center_y = int(M["m01"] / M["m00"])
else:
center_x, center_y = 0, 0
font = cv2.FONT_HERSHEY_SIMPLEX
text = obj_id
font_scale = 0.9
text_size = cv2.getTextSize(text, font, font_scale, 2)[0]
text_x = center_x - text_size[0] // 1 # ํ
์คํธ์ ๊ฐ๋ก ์ค์ฌ
text_y = center_y
# text_y = center_y + text_size[1] // 2 # ํ
์คํธ์ ์ธ๋ก ์ค์ฌ
# ํ
์คํธ ๋ฐฐ๊ฒฝ ์ฌ๊ฐํ ์ขํ ๊ณ์ฐ
rect_start = (text_x - 5, text_y - text_size[1] - 5) # ๋ฐฐ๊ฒฝ ์ฌ๊ฐํ ์ข์๋จ
# rect_end = (text_x + text_size[0] + 5, text_y + 5)
rect_end = (text_x + text_size[0] + 5, text_y)
cv2.rectangle(frame, rect_start, rect_end, (0, 0, 0), -1)
cv2.putText(frame, text, (text_x, text_y), font, 1, (255, 255, 255), 2)
# plt.figure(figsize=(12, 8))
# plt.imshow(frame)
# plt.title(f"frame {frame_name}")
# plt.tight_layout()
# plt.axis('off')
# plt.show()
buffer = BytesIO()
frame = Image.fromarray(frame)
frame.save(buffer, format='jpeg')
buffer.seek(0)
cat_frames.append(base64.b64encode(buffer.read()).decode("utf-8"))
frame_cat_cnts[frame_name] = cat_cnt
buffer.seek(0) # Reuse buffer instead of creating a new one
buffer.truncate()
frame_for_contour = Image.fromarray(frame_for_contour)
frame_for_contour.save(buffer, format='jpeg')
buffer.seek(0)
contour_frames.append(base64.b64encode(buffer.read()).decode("utf-8"))
encoded_frames[cat] = cat_frames
contoured_frames[cat] = contour_frames
vid_cat_cnts[cat] = frame_cat_cnts
return encoded_frames, vid_cat_cnts, contoured_frames
if __name__ == '__main__':
parser = argparse.ArgumentParser('ReferFormer training and evaluation script', parents=[opts.get_args_parser()])
args = parser.parse_args()
#==================๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ===================
# ์ ์ฒด ๋ฐ์ดํฐ์
train_dataset = build_ytvos_ref(image_set = 'train', args = args)
# ์ ์ฒด ๋ฐ์ดํฐ์
๋ฉํ๋ฐ์ดํฐ
metas = train_dataset.metas
# ์์ ํ๋ณด 8๊ฐ (RGB ํ์)
colors = [
(255, 0, 0), # Red
(0, 255, 0), # Green
(0, 0, 255), # Blue
(255, 255, 0), # Yellow
(255, 0, 255), # Magenta
(0, 255, 255), # Cyan
(128, 0, 128), # Purple
(255, 165, 0) # Orange
]
|