Created
June 10, 2018 08:55
-
-
Save OterLabb/4add94f9932632e855a794b1c97bed1f to your computer and use it in GitHub Desktop.
Parse result.txt from darknet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from PIL import Image | |
from itertools import groupby, zip_longest, dropwhile | |
with open('result.txt') as fin: | |
stripped = (line.strip() for line in fin) | |
start_at = dropwhile(lambda L: not L.startswith('Enter'), stripped) | |
grouped = (list(g) for k, g in groupby(start_at, lambda L: L.startswith('Enter'))) | |
for name, rest in zip_longest(*iter([grouped] * 2), fillvalue=[]): | |
if rest: | |
for imagepath in name: | |
imagePathString = str(imagepath) | |
imagePathFinal = imagePathString.split(' ')[3] | |
imagePathFinal2 = imagePathFinal.replace("jpg:","jpg") | |
imagePathFinal3 = imagePathFinal2.split('Renamed')[0] | |
imagePathCropped = imagePathFinal3 + 'Cropped' | |
if not os.path.exists(imagePathCropped): | |
os.makedirs(imagePathCropped) | |
imageName = str(imagePathFinal2.split('Renamed')[1]) # Split at the last folder name | |
#break | |
#imageName = imagePathFinal2.split('/')[1] | |
numberImage = 0 | |
for birdscore in rest: | |
### Parse percent into integer | |
score = birdscore.split()[1] | |
scorewithout = score.replace("%","") | |
scoreint = int(scorewithout) | |
if birdscore.split()[0] == 'bird:' and scoreint >= 60: | |
try: | |
x1 = int(birdscore.split()[3]) | |
y1 = int(birdscore.split()[5]) | |
rightx = int(birdscore.split()[7]) | |
bottomytemp = birdscore.split()[9] | |
bottomy = int(bottomytemp.replace(")","")) | |
x2 = x1 + rightx | |
y2 = y1 + bottomy | |
numberImage += 1 | |
imageNameWithoutSlash = imageName.replace("/","") | |
imageNameWithoutJpg = imageNameWithoutSlash.replace(".jpg","") | |
img = Image.open(imagePathFinal2) | |
cropped_img = img.crop((x1,y1,x2,y2)) | |
print("Cropping ", imageNameWithoutJpg, "at x1:",x1,"y1:",y1,"x2:",x2,"y2:",y2) | |
saveTo = imagePathCropped + imageNameWithoutJpg + "_" + str(numberImage) + ".jpg" | |
cropped_img.save(saveTo) | |
cropped_img.close() | |
except IndexError: | |
x1 = 'null' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment