import os
import cv2
import json
import shutil
import numpy as np
import pybboxes as pbx
from pybboxes import BoundingBox
def bnd_box_to_yolo_line(box, img_size):
(x_min, y_min) = (box[0], box[1])
(w, h) = (box[2], box[3])
x_max = x_min + w
y_max = y_min + h
x_center = float((x_min + x_max)) / 2 / img_size[1]
y_center = float((y_min + y_max)) / 2 / img_size[0]
w = float((x_max - x_min)) / img_size[1]
h = float((y_max - y_min)) / img_size[0]
return (x_center, y_center, w, h)
def convert(size,box):
dw = 1./size[0]
dh = 1./size[1]
x = (box[0]+box[1])/2.0
y = (box[2]+box[3])/2.0
w = box[1] - box[0]
h = box[3] - box[2]
x = x*dw
w = w*dw
y = y*dh
h = h*dh
return (x,y,w,h)
dir_path = ".\\images"
file_format = ['jpg','JPG']
for (root, directories, files) in os.walk(dir_path):
for file in files:
# try:
# img = cv2.imread(root+"\\"+file)
# if img.shape[0] !=640:
# img = cv2.resize(img,(640,640),interpolation = cv2.INTER_CUBIC)
# cv2.imwrite(root+"\\"+file,img)
# except:
# pass
if file_format[0] in file or file_format[1] in file:
file_path = root+"\\"+file
if file.find(file_format[0])>0 or file.find(file_format[1]):
with open("labels"+"\\"+file.replace('jpg','json').replace('JPG','json'), "r",encoding="UTF-8") as st_json:
st_python = json.load(st_json)
origin_w = int(st_python['images'][0]['width'])
origin_h = int(st_python['images'][0]['height'])
annotations = st_python['annotations']
f = open(".\\images"+"\\"+file.replace('jpg','txt').replace('JPG','txt'), 'w')
img = cv2.imread(file_path)
for ann in annotations:
(xmin,ymin,xmax,ymax) = ann['bbox']
print(xmin,ymin,xmax,ymax)
x_scale = (640/origin_w)
y_scale = (640/origin_h)
print(x_scale)
(xmin, ymin, xmax, ymax) = (xmin*x_scale,ymin*y_scale,xmax*x_scale,ymax*y_scale)
img = cv2.resize(img,(origin_w,origin_h),interpolation = cv2.INTER_CUBIC)
cv2.imwrite(root+"\\"+file,img)
my_coco_box = (xmin,ymin,xmax,ymax)
bb = bnd_box_to_yolo_line(my_coco_box,(640,640))
# coco_bbox = BoundingBox.from_voc(*my_coco_box, image_size=(640,640),strict=False) # <[98 345 322 117] (322x117) | Image: (640x480)>
# no longer raises exception
# bb = coco_bbox.to_yolo()
# bb = convert((origin_w,origin_h),(xmin,ymin,xmax,ymax))
# bb = bb.values
f.write(str(0) + ' ' + str(bb[0]) + " " + str(bb[1]) + " " + str(bb[2]) + " " + str(bb[3]) + "\n")
# cv2.imshow('rec',img)
f.close()
print(file_path)
# # b = ()
# with open("labels"+"\\09_20201013_189291.json", "r",encoding="UTF-8") as st_json:
# st_python = json.load(st_json)
# images = st_python['images']
#
# annotations = st_python['annotations']
# f = open(".\\images\\09_20201013_189291.txt", 'w')
# x = int(images[0]['width'])
# y = int(images[0]['height'])
# for ann in annotations:
# xmin,ymin,xmax,ymax = annotations[0]['bbox']
# x_scale = 640/x
# y_scale = 640/y
#
#
# x = int(np.round(xmin*x_scale))
# y = int(np.round(ymin*y_scale))
# xmax= int(np.round(xmax*(x_scale)))
# ymax= int(np.round(ymax*y_scale))
# f.write(str(0) + ' ' + str(x) + " " + str(y) + " " + str(xmax) + " " + str(ymax) + "\n")
# f.close()