Skip to content

Instantly share code, notes, and snippets.

@cmastudios
Created April 29, 2015 22:18
Show Gist options
  • Save cmastudios/e9c91e8ac514ccef04b3 to your computer and use it in GitHub Desktop.
Save cmastudios/e9c91e8ac514ccef04b3 to your computer and use it in GitHub Desktop.
Filter images from RR 2015, save them raw to a file
#!/usr/bin/python
#expectations
#sudo apt-get install python-pip
#sudo pip install Pillow
#working directory must contain a folder "Vision Images" that contains folders
#created by the RR 2015 vision system
#to run:
#chmod +x filename.py
#./filename.py
#output files are saved to the 'output/' folder.
#format:
#xrot -0.00
#<raw image data, array of size 640 * 480 * 3>
import csv
import os
import logging
from PIL import Image
logging.basicConfig(level = logging.WARNING)
logger = logging.getLogger('rrimagereader')
gi = 0
def read(dir, csvfile):
global gi
logger.info('Reading %s...' % (dir))
features = {}
solutions = {}
with open(os.path.join('Vision Images', dir, csvfile), 'rb') as csvdb:
reader = csv.reader(csvdb, delimiter = ',', quotechar = '"')
rownum = 0
for row in reader:
colnum = 0
if rownum == 0:
for column in row:
features[colnum] = column
colnum += 1
if not 'frame' in features.values():
logger.warning('No frame element for %s. Skipping' % (dir))
return
else:
frame = 0
for column in row:
if features[colnum] == 'frame':
frame = int(round(float(column)))
elif features[colnum] == 'xrot':
solutions[frame] = float(column)
colnum += 1
rownum += 1
logger.debug(solutions)
for frame, xrot in solutions.iteritems():
color = os.path.join('Vision Images', dir, 'color', 'robot_%d.jpg' % (frame))
output = os.path.join('output', 'dp%d.bmp' % (gi))
output2 = os.path.join('output2', 'dp%d.jpg' % (gi))
if os.path.isfile(color):
im = Image.open(color)
rgb = im.load()
im.save(output2, 'JPEG')
im.save('/dev/shm/intermediate-rr.bmp', 'BMP')
with open('/dev/shm/intermediate-rr.bmp', 'rb') as inf:
inf.seek(54)
bmpdata = inf.read()
with open(output, 'wb') as outf:
outf.write('xrot %.2f\n' % (xrot))
outf.write(bmpdata)
gi += 1
names = ['vision_output.csv', 'vision_output_ 0.csv', 'vision_output_ 1.csv', 'vision_output_0.csv', 'vision_output_1.csv']
if not os.path.isdir('output'):
os.mkdir('output', 0755)
if not os.path.isdir('output2'):
os.mkdir('output2', 0755)
for root, dirs, files in os.walk('Vision Images'):
for dir in dirs:
for name in names:
if os.path.isfile(os.path.join('Vision Images', dir, name)):
try:
read(dir, name)
except csv.Error:
logger.warning('Error when reading %s\'s CSV file. Skipping' % (dir))
break
else:
logger.warning('No vision output found in %s.' % (dir))
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment