Skip to content

Instantly share code, notes, and snippets.

@orasik
Created September 20, 2017 13:01
Show Gist options
  • Save orasik/5228745b4f17ccd52e311ffade892d6e to your computer and use it in GitHub Desktop.
Save orasik/5228745b4f17ccd52e311ffade892d6e to your computer and use it in GitHub Desktop.
plot YOLO2 dims on an image
# I wrote this code to draw a box on image using yolo2 format
# Yolo2 format: <object-class> <x> <y> <width> <height>
# reference: https://pjreddie.com/darknet/yolo/
# I use this code to make sure I've converted x1,y1 x2,y2 format to yolo2 format correctly by plotting the box on images.
import skvideo.io
import matplotlib.pyplot as plt
import sys
import os
import cv2
from PIL import Image
import json
filename = 'flat/video1C1Rpz3r3WwKl0Y4-1049'
label_path = 'labels/training/'
image_path = 'images/training/'
my_label = open(label_path + filename + '.txt', "r")
img = Image.open(image_path + filename + '.png')
w, h = img.size
img2 = cv2.imread(image_path + filename + '.png')
dims = my_label.read().split(' ')
print(dims)
x = int(float(dims[1]) * w)
y = int(float(dims[2]) * h)
mw = int(float(dims[3]) * w)
mh = int(float(dims[4]) * h)
print('x = {}'.format(x))
print('y = {}'.format(y))
print('w = {}'.format(mw))
print('h = {}'.format(mh))
draw_x = int(x - (mw/2))
draw_y = int(y - (mh/2))
draw_x2 = int(x + (mw/2))
draw_y2 = int(y + (mh/2))
print('x = {}'.format(draw_x))
print('y = {}'.format(draw_y))
print('w = {}'.format(draw_x2))
print('h = {}'.format(draw_y2))
cv2.rectangle(img2,(draw_x,draw_y),(draw_x2 , draw_y2),(255,0,250),7)
plt.imshow(img2)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment