Added configs
This commit is contained in:
@@ -1,15 +1,16 @@
|
||||
import os
|
||||
import json
|
||||
|
||||
from config import Config
|
||||
from meta_data_extractor import MetaExtractor, ImageMeta
|
||||
from meta_data_visualizer import visualize_metadata
|
||||
import cv2
|
||||
|
||||
|
||||
|
||||
|
||||
class DataPreprocessor:
|
||||
def __init__(self, image_paths):
|
||||
def __init__(self, image_paths, max_images_per_dataset):
|
||||
self.image_paths = image_paths
|
||||
self.max_images_per_dataset = max_images_per_dataset
|
||||
|
||||
#This method filters the images by specific criteria and extracts and saves the metadata for each filtered image
|
||||
def _is_accepted(self, meta: ImageMeta) -> bool:
|
||||
@@ -18,32 +19,38 @@ class DataPreprocessor:
|
||||
faces_ok = all(p.face_bbox for p in meta.persons)
|
||||
return has_person and has_object and faces_ok
|
||||
|
||||
def preprocess_images(self, output_directory, debug=False):
|
||||
def preprocess_images(self, output_directory):
|
||||
extractor = MetaExtractor()
|
||||
if not os.path.exists(output_directory):
|
||||
os.mkdir(output_directory)
|
||||
|
||||
img_counter = 0
|
||||
for img_file_path in self.image_paths:
|
||||
image_meta = extractor.extract(img_file_path)
|
||||
if debug:
|
||||
print("Debug Routine")
|
||||
img = cv2.imread(img_file_path)
|
||||
cv2.imshow("Before", img)
|
||||
cv2.waitKey()
|
||||
img_vis = visualize_metadata(img, image_meta)
|
||||
cv2.imshow("After", img_vis)
|
||||
cv2.waitKey()
|
||||
print("image_meta")
|
||||
print(image_meta.to_dict())
|
||||
exit()
|
||||
if Config.debug_show_preprocessing:
|
||||
self._debug_show(image_meta, img_file_path)
|
||||
|
||||
is_accepted = self._is_accepted(image_meta)
|
||||
if is_accepted:
|
||||
base_name = os.path.basename(img_file_path).split(".")[0]
|
||||
meta_data_file_path = os.path.join(output_directory, os.path.basename(img_file_path).split(".")[0] + ".json")
|
||||
output_image_file_path = os.path.join(output_directory, os.path.basename(img_file_path))
|
||||
with open(meta_data_file_path, "w") as f:
|
||||
json.dump(image_meta.to_dict(), f, indent=2)
|
||||
img = cv2.imread(img_file_path)
|
||||
cv2.imwrite(output_image_file_path, img)
|
||||
img_counter += 1
|
||||
if img_counter >= self.max_images_per_dataset:
|
||||
return
|
||||
|
||||
print(base_name)
|
||||
exit()
|
||||
|
||||
with open(os.path.join(output_directory, f"{base_name}.json"), "w") as f:
|
||||
json.dump(meta, f, indent=2)
|
||||
def _debug_show(self, image_meta, img_file_path):
|
||||
print("Debug Routine")
|
||||
img = cv2.imread(img_file_path)
|
||||
cv2.imshow("Before", img)
|
||||
cv2.waitKey()
|
||||
img_vis = visualize_metadata(img, image_meta)
|
||||
cv2.imshow("After", img_vis)
|
||||
cv2.waitKey()
|
||||
print("image_meta")
|
||||
print(image_meta.to_dict())
|
||||
exit()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user