Skip to content

Instantly share code, notes, and snippets.

@twobob
Created May 1, 2017 04:25
Show Gist options
  • Save twobob/dad0a110b0c2b2eb4895d8e6e5e76760 to your computer and use it in GitHub Desktop.
Save twobob/dad0a110b0c2b2eb4895d8e6e5e76760 to your computer and use it in GitHub Desktop.
Create a JSON file from a list of txt files parsing the values
# -*- coding: utf-8 -*-
'''
where .JPG.txt AI tag files are like
Tags for IMG_3150.JPG (Tag - Probability)
child - 0.9972
fun - 0.9727
little - 0.9688
people - 0.9647
cute - 0.9637
et cetera
'''
import os, sys
import glob
import simplejson as json
from os.path import expanduser
directory = expanduser('~\\pictures\\clarifai\\')
JSONdirectory = expanduser('~\\pictures\\clarifai\\JSON\\')
thumbsdirectory = expanduser('~\\pictures\\clarifai\\thumbs\\')
lines_list = []
data={}
finaldata=[]
emptyHolder = {}
txts=glob.glob1(directory, "*.txt")
with open(JSONdirectory+"data.JSON", 'w') as outfile:
for txt in txts:
lines_list = open(directory+txt).read().splitlines()[2:]
data['Question']=emptyHolder
data['img']={'filename':txt.replace('.JPG.txt', '.JPG')}
data['thumb']={'filename':txt.replace('.JPG.txt', '.THUMB.JPEG')}
data['Question']['content']={}
for i in lines_list:
data['Question']['content'][i.split(' - ')[0]]=i.split(' - ')[1]
finaldata.append(data)
json.dump({'Questions':finaldata}, outfile, sort_keys = True, indent = 4, ensure_ascii = False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment