Skip to content

Commit

Permalink
Support for unequal image sizes, same detection check error
Browse files Browse the repository at this point in the history
- The deleteSimilarDetections function should now work on image datasets of different sizes.
- Fixed the error of checking the same tag in detection similarity check.
  • Loading branch information
PandapowrTR authored May 7, 2024
1 parent a7745cd commit ee3862f
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions Data/Dominate/DominateLabeledImage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os, shutil, os, gc, time, sys, uuid, copy, cv2, warnings
import concurrent.futures
cv2.setLogLevel(0)

warnings.warn("ignore")
warnings.filterwarnings("ignore", category=UserWarning)
Expand Down Expand Up @@ -209,8 +210,6 @@ def processFile(args):
root,
labels,
tempFolder,
imgWidth,
imgHeight,
maxSimilarity,
deletedFiles,
files,
Expand All @@ -221,14 +220,21 @@ def processFile(args):
and file not in deletedFiles
):
label = labels[".".join(file.split(".")[:-1])]


if not os.path.exists(os.path.join(root, file)):
return
img = cv2.imread(os.path.join(root, file))
if img is None:
return
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = []
for l in label:
future = executor.submit(processLabel, l, img, args)
future = executor.submit(
processLabel,
l,
img,
args,
)
futures.append(future)

concurrent.futures.wait(futures)
Expand All @@ -239,15 +245,13 @@ def processLabel(l, img, args):
root,
labels,
tempFolder,
imgWidth,
imgHeight,
maxSimilarity,
deletedFiles,
files,
) = args
points = l["bbox"]
points = ObjectDetection.convertLabelPoints(
points, labelFormat, "pascal_voc", imgWidth, imgHeight
points, labelFormat, "pascal_voc", l["imageWidth"], l["imageHeight"]
)
xmin, ymin, xmax, ymax = [int(p) for p in points]
img = img[ymin:ymax, xmin:xmax]
Expand All @@ -272,8 +276,6 @@ def processCheckFile(checkFile, cutDetection, args):
root,
labels,
tempFolder,
imgWidth,
imgHeight,
maxSimilarity,
deletedFiles,
files,
Expand All @@ -283,7 +285,7 @@ def processCheckFile(checkFile, cutDetection, args):
and checkFile != file
and checkFile not in deletedFiles
):
checkLabels = labels[".".join(file.split(".")[:-1])]
checkLabels = labels[".".join(checkFile.split(".")[:-1])]
with concurrent.futures.ThreadPoolExecutor() as executor:
cFutures = []
for cl in checkLabels:
Expand All @@ -299,8 +301,6 @@ def processCheckLabel(cl, checkFile, cutDetection, args):
root,
labels,
tempFolder,
imgWidth,
imgHeight,
maxSimilarity,
deletedFiles,
files,
Expand All @@ -310,10 +310,12 @@ def processCheckLabel(cl, checkFile, cutDetection, args):
checkImgPoints,
labelFormat,
"pascal_voc",
imgWidth,
imgHeight,
cl["imageWidth"],
cl["imageHeight"],
)
cxmin, cymin, cxmax, cymax = [int(p) for p in checkImgPoints]
if not os.path.exists(os.path.join(root, checkFile)):
return
checkImg = cv2.imread(os.path.join(root, checkFile))
checkImg = checkImg[cymin:cymax, cxmin:cxmax]
checkImg = cv2.resize(checkImg, (100, 100))
Expand All @@ -326,24 +328,27 @@ def processCheckLabel(cl, checkFile, cutDetection, args):
return
os.remove(checkCutDetection)

imgHeight = 0
imgWidth = 0
sizes = {}
for root, _, files in os.walk(dataPath):
for file in files:
if file.lower().endswith((".jpg", ".png", ".jpeg")):
img = Image.open(os.path.join(root, file))
imgHeight = img.height
imgWidth = img.width
break
break
sizes[".".join(file.split(".")[:-1])] = {
"imageHeight": img.height,
"imageWidth": img.width,
}
labels = {}
for root, _, files in os.walk(labelsPath):
for file in files:
if file.lower().endswith((".json", ".xml", ".txt")):
rawFileName = ".".join(file.split(".")[:-1])
labels.update(
{
".".join(file.split(".")[:-1]): ObjectDetection.loadLabel(
os.path.join(root, file), labelFormat, imgWidth, imgHeight
rawFileName: ObjectDetection.loadLabel(
os.path.join(root, file),
labelFormat,
sizes[rawFileName]["imageWidth"],
sizes[rawFileName]["imageHeight"],
)
}
)
Expand All @@ -364,8 +369,6 @@ def processCheckLabel(cl, checkFile, cutDetection, args):
root,
labels,
tempFolder,
imgWidth,
imgHeight,
maxSimilarity,
deletedFiles,
files.copy(),
Expand Down Expand Up @@ -778,7 +781,9 @@ def augData(
try:
BurobotOutput.clearAndMemoryTo()
BurobotOutput.printBurobot()
print(f"🔄 Deleting {maxSimilarity*100}% similar or more detections 🔍🧐")
print(
f"🔄 Deleting {maxSimilarity*100}% similar or more detections 🔍🧐"
)
deleteSimilarDetections(
imgSavePath, labelSavePath, labelSaveFormat, maxSimilarity
)
Expand Down

0 comments on commit ee3862f

Please sign in to comment.