The data pipeline is designed to process a collection of images, extracting color information from each image using an unsupervised KMeans clustering model. The pipeline then reconstructs the images from the database and stores the extracted color information linked with the corresponding image's barcode in a MongoDB database.
In "Main1()", to begin with, the pipeline establishes a connection with the Mongo database and retrieves all unique barcode IDs from the colors collection. It then lists all PNG files in the "./input_photos/" directory folder and extracts their file names. By subtracting the sets of barcode IDs obtained from the test database and the input photos from the set of barcode IDs obtained from the production database, a new set of barcode IDs is generated. This approach ensures that the pipeline avoids extracting images that already exist, eliminating redundancy.
In "Main2()", a maximum limit for the number of photos to process at once (max_photos = 50) is defined. The function loads images from the directory ("/workdir/get_colors/input_photos/*.png") and stores them in the variables "all_files" and "all_photos". Each photo undergoes an initial segmentation process to reduce its original size by cutting off a quarter of the image from each side. The original and segmented versions of the images are saved. Subsequently, an object detection algorithm (U-2-Net) is executed on both the original and segmented photos. After the object detection phase, masks are applied to the segmented images. A binary mask of the saturation channel is created to identify pixels with high saturation, which is then used to segment out the high-saturation regions of the image while setting all other pixels to white. The function also checks for cases where the mask application results in blank images. In such cases, a secondary processing step is performed, involving image cropping and repeating the segmentation and object detection steps. Finally, the function proceeds to extract color information from the processed images. It calculates the average color channels of each pixel in the image, excluding fully white pixels. A color label is assigned to each image based on a predefined list of color labels and their corresponding RGB values. These labels are then appended to the original file names, and the images are saved to "./output_colors" directory.
In "Main3()", the pipeline connects to a MongoDB database and retrieves all unique barcode IDs present in the database. It then loads the processed image files from the "./output_colors" directory, extracting the barcode and color information from each filename. If a barcode-color pair is not already present in the database, it is inserted as a new document in the MongoDB collection. This function ensures that the database keeps track of each unique image, identified by its barcode, and its corresponding dominant stool color. Moreover, it prevents duplication of entries, allowing only new and unique barcode-color pairs to be added to the database.