-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewgetDescriptors.py
80 lines (69 loc) · 3.13 KB
/
newgetDescriptors.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
71
72
73
74
75
76
77
78
79
80
import os
import cv2
import glob
import pickle
import numpy as np
import time
def pickle_it(data, path):
"""
Сохранить данные data в файл path
:param data: данные, класс, массив объектов
:param path: путь до итогового файла
:return:
"""
with open(path, 'wb') as f:
pickle.dump(data, f, protocol=pickle.HIGHEST_PROTOCOL)
def unpickle_it(path):
"""
Достать данные из pickle файла
:param path: путь до файла с данными
:return:
"""
with open(path, 'rb') as f:
return pickle.load(f)
def savemyfile(data, datapath):
f = open(datapath, "wb")
pickle.dump(data, f)
f.close()
def main(detector, detectorName, amountOfDescriptors):
print("Currently in descriptors function")
p = "Descriptors/New/" + detectorName + "/" + str(amountOfDescriptors) + "/"
if not os.path.exists(p) or (os.path.exists(p) and len(os.listdir(p))):
if not os.path.exists(p):
os.makedirs(p)
print("Processing new descriptors")
allPlayersAllImages = []
playerspath = "EducationalPhotosIPhoneCutNamed2018-02-25/*/"
for playerFolder in glob.glob(playerspath):
print("in folder ", playerFolder)
playerName = playerFolder.split('\\')[1]
everyPlayerImagesDescriptors = []
for image in glob.glob(playerFolder + '*.jpg'):
img = cv2.imread(image)
kp, imageDescriptors = detector.detectAndCompute(img, None)
imageDescriptors = imageDescriptors[:min(amountOfDescriptors, len(imageDescriptors))]
if amountOfDescriptors > len(imageDescriptors):
print("Too big amountOfDescriptors value error ", amountOfDescriptors, " > ", len(imageDescriptors))
imageDescriptorsFile = p + "/" + playerName + "/" + image + ".txt"
savemyfile(imageDescriptors, imageDescriptorsFile)
everyPlayerImagesDescriptors.append(imageDescriptors)
everyPlayerImagesDescriptorsFile = p + "/" + playerName + "/TotalCurrent.txt"
savemyfile(everyPlayerImagesDescriptors, everyPlayerImagesDescriptorsFile)
allPlayersAllImages.append(everyPlayerImagesDescriptors)
savemyfile(allPlayersAllImages, p + "/Total.txt")
else:
mainFile = p + "/Total.txt"
print("Importing collected descriptors from: ", mainFile)
f = open(mainFile, "rb")
allPlayersAllImages = pickle.load(f)
print(len(allPlayersAllImages), " (must be 17 like amount of images)")
f.close()
return allPlayersAllImages
def reshaper(detector, detectorName, amountOfDescriptors):
p = "Descriptors/New/" + detectorName + "/" + str(amountOfDescriptors) + "/"
mainFile = p + "/Total.txt"
print("Importing collected descriptors from: ", mainFile)
f = open(mainFile, "rb")
allPlayersAllImages = pickle.load(f)
print(len(allPlayersAllImages), " (must be 17 like amount of images)")
f.close()