-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageClassCut.py
112 lines (95 loc) · 3.13 KB
/
imageClassCut.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import json
import time
import cv2
import os
import multiprocessing
from queue import Empty
path = 'D:\\internship2023\\annotations\\instances_train2017.json'
imagesPath = "D:\\internship2023\\train2017\\"
savePath = "D:\\internship2023\\imageClassTrain"
def cutImageAndSaveToFile(imageId : int, box : list[int], category_id : int):
if box[2] < 32 or box[3] < 32:
return
fullId = str(imageId).zfill(12)
filePath = savePath +"\\" +str(category_id) + "\\"+ fullId + str(category_id) + "b"
for i in box:
filePath = filePath + str(int(i)) + "_"
filePath = filePath + ".jpg"
loadPath = imagesPath + fullId + ".jpg"
img = cv2.imread(loadPath)
box = list(map(lambda x : int(x), box))
cutImg = img[box[1] : box[1] + box[3], box[0] : box[0] + box[2]]
cutImg = cv2.resize(cutImg, (128, 128))
cv2.imwrite(filePath, cutImg)
return
DONE = False
def worker_funciton(queue : multiprocessing.Queue, index):
print(f"Worker {index} start!")
while True:
try:
data = queue.get(timeout=1)
if type(data) is not tuple:
break
cutImageAndSaveToFile(data[0], data[1], data[2])
except Empty:
continue
print(f"Worker {index} done!")
return
def factory_function(queue, num_workers, index):
print(f"Factory {index} start!")
anns = None
with open(path) as f:
anns = json.load(f)
annsMap = map(lambda x: x, anns["annotations"])
for i, ann in enumerate(annsMap):
if i % 100 == 0:
print(i)
queue.put((ann["image_id"] , ann['bbox'], ann["category_id"]))
DONE = True
for i in range(num_workers):
queue.put("DONE")
print(f"Factory {index} done!")
return
def multi(num_proc : int = 4):
queue = multiprocessing.Queue()
factory = multiprocessing.Process(target=factory_function, args=(queue, num_proc, 0))
factory.start()
workers = []
for i in range(num_proc):
worker = multiprocessing.Process(target=worker_funciton, args=(queue, i))
worker.start()
workers.append(worker)
factory.join()
for w in workers:
w.join()
print("Done")
def annToParams(ann):
cutImageAndSaveToFile(imageId=ann["image_id"] ,category_id=ann["category_id"], box=ann['bbox'])
def createFolders():
if not os.path.exists(savePath):
os.mkdir(savePath)
for i in range(90):
p = savePath + "\\" + str(i+1)
if not os.path.exists(p):
os.mkdir(p)
def boxes():
anns = None
with open(path) as f:
anns = json.load(f)
annsMap = map(lambda x: x, anns["annotations"])
for ann in annsMap:
annToParams(ann)
if __name__ == "__main__":
createFolders()
multi(8)
# boxes()
# numProcesses = 4
# pool = multiprocessing.Pool(processes=numProcesses)
# results = pool.imap(annToParams, annsMap)
# pool.close()
# pool.join()
# for ann in annsInter:
# cutImage(imageId=ann["image_id"] ,category_id=ann["category_id"], box=ann['bbox'])
# print(ann['bbox'], " : ", ann['category_id'])
# for i in range(90):
# os.mkdir(savePath + str(i+1))