analysis on all possible files in a provided directory, writes to JSON

This commit is contained in:
Mikayla Dobson
2023-01-04 22:09:25 -06:00
parent 8ce90346d9
commit 9510e36e45
4 changed files with 64 additions and 5 deletions

31
main.py
View File

@@ -3,23 +3,46 @@
# most of this application adapted from the following walkthrough:
# https://towardsdatascience.com/how-to-use-a-pre-trained-model-vgg-for-image-classification-8dd7c4a4a517
import sys, os
import sys, os, json, time
from predict import predict
from keras.applications.vgg16 import VGG16
print("\n\n\n")
print("Imports successful! Running startup processes...")
# generate current time for use in identifying outfiles
cur_time = str(int(time.time()))
# create the target directory if it doesn't exist
if (not os.path.exists("./predictions")):
print("Did not find predictions directory, creating...")
os.makedirs("./predictions")
# declare model to be used for each prediction
model = VGG16(weights='imagenet')
# receive directory path as CLI argument and get a list of all files in path
path = sys.argv[1]
if (path[-1] != "/"):
path += "/"
files = os.listdir(path)
# store all results in one list
all_results = []
print("Running image analysis. This may take some time")
# for each file in directory, append its prediction result to main list
for file in files:
result = predict(model, file)
all_results.append({ path: file, result: result })
result = predict(model, path + file)
if result is not None:
all_results.append({ "path": file, "prediction": result })
print(all_results)
print("Analysis complete! Writing JSON to ./predictions/predictions" + cur_time + ".json")
# convert object to JSON and write to JSON file
with open("./predictions/predictions" + cur_time + ".json", "w") as outfile:
json.dump(all_results, outfile)
print("Process complete!")