-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
70 lines (60 loc) · 2.29 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from matplotlib import image, pyplot as plt
from pythonAnalyser import analyseImage
from createPlot import createPlot
from createPlot import decision
import cv2
import os
import glob
import cv2
import shutil
import configparser
def getImagesFromDirectory(imgPath):
image_list = []
for filename in glob.glob(imgPath + '/*.jpg'):
image_list.append(filename)
return image_list
def checkFolders():
if not os.path.exists('reLabel'):
os.makedirs('reLabel')
if not os.path.exists('trainImages'):
os.makedirs('trainImages')
if not os.path.exists('trashImages'):
os.makedirs('trashImages')
def getConfigSettings():
Config = configparser.ConfigParser()
Config.read('config.ini')
return Config.get("YOLO", 'labelsPath'), Config.get("YOLO", 'weightsPath'), Config.get("YOLO", 'configPath') ,Config.get("YOLO", 'imgPath')
def saveLabelFile(img, boxes, w, h, path):
labelFileName = path + img.split('/')[-1].split('.')[0] + ".txt"
labelFile = open(labelFileName, 'a')
for detection in boxes:
x = detection[0] / w
y = detection[1] / h
length = detection[2] / w
height = detection[3] / h
labelFile.write("0 {} {} {} {} \n".format(x, y, length, height))
labelFile.close()
def main():
labelsPath, weightsPath,configPath, imgPath = getConfigSettings()
checkFolders()
imgList = getImagesFromDirectory(imgPath)
counter = 1
for img in imgList:
print("You are working on {} of {}".format(counter, len(imgList)))
im = cv2.imread(img)
h, w, c = im.shape
boxes, image = analyseImage(labelsPath, weightsPath, configPath, img)
createPlot(image)
imgArray = img.split('/')
cut = imgArray[:len(imgArray)-2]
if decision['dec'] == 1:
saveLabelFile(img,boxes,w,h, 'trainImages/')
movePath = '/'.join(cut) + '/trainImages/' + imgArray[-1].split('.')[0] + ".jpg"
elif decision['dec'] == 2:
saveLabelFile(img,boxes,w,h, 'reLabel/')
movePath = '/'.join(cut) + '/reLabel/' + imgArray[-1].split('.')[0] + ".jpg"
elif decision['dec'] == 3:
movePath = '/'.join(cut) + '/trashImages/' + imgArray[-1].split('.')[0] + ".jpg"
shutil.move(img,movePath)
counter += 1
main()