def test_ecotaxa(tmp_path): archive_fn = tmp_path / "ecotaxa.zip" print(archive_fn) # Create an archive with Pipeline() as p: i = Unpack(range(10)) meta = Call(dict, i=i, foo="Sömé UTF-8 ſtríng…") image = BinaryBlobs() image_name = Format("image_{}.png", i) EcotaxaWriter(archive_fn, (image_name, image), meta) result = [o.to_dict(meta=meta, image=image) for o in p.transform_stream()] # Read the archive with Pipeline() as p: image, meta = EcotaxaReader(archive_fn) roundtrip_result = [ o.to_dict(meta=meta, image=image) for o in p.transform_stream() ] for meta_field in ("i", "foo"): assert [o["meta"][meta_field] for o in result ] == [o["meta"][meta_field] for o in roundtrip_result] assert_equal([o["image"] for o in result], [o["image"] for o in roundtrip_result])
def test_ecotaxa(tmp_path, ext): archive_fn = tmp_path / ("ecotaxa" + ext) print(archive_fn) # Create an archive with Pipeline() as p: i = Unpack(range(10)) meta = Call(dict, i=i, foo="Sömé UTF-8 ſtríng…") image = BinaryBlobs() image_name = Format("image_{}.png", i) EcotaxaWriter( archive_fn, (image_name, image), meta, object_meta={"foo": 0}, acq_meta={"foo": 1}, process_meta={"foo": 2}, sample_meta={"foo": 3}, ) result = [o.to_dict(meta=meta, image=image) for o in p.transform_stream()] # Read the archive with Pipeline() as p: image, meta = EcotaxaReader(archive_fn) roundtrip_result = [ o.to_dict(meta=meta, image=image) for o in p.transform_stream() ] for meta_field in ("i", "foo"): assert [o["meta"][meta_field] for o in result ] == [o["meta"][meta_field] for o in roundtrip_result] for i, prefix in enumerate(("object_", "acq_", "process_", "sample_")): assert [o["meta"][prefix + "foo"] for o in result] == [i for _ in roundtrip_result] assert_equal([o["image"] for o in result], [o["image"] for o in roundtrip_result])
# Calculate features. The calculated features are added to the global_metadata. # Returns a Variable representing a dict for every object in the stream. meta = CalculateZooProcessFeatures( regionprops, prefix="object_", meta=global_metadata ) # Add object_id to the metadata dictionary meta["object_id"] = object_id # Generate object filenames orig_fn = Format("{object_id}.jpg", object_id=object_id) # Write objects to an EcoTaxa archive: # roi image in original color, roi image in grayscale, metadata associated with each object EcotaxaWriter(archive_fn, (orig_fn, roi_orig), meta) # Progress bar for objects TQDM(Format("Object {object_id}", object_id=object_id)) import datetime BEGIN = datetime.datetime.now() # Execute pipeline p.run() END = datetime.datetime.now() print("MORPHOCUT :"+str(END-BEGIN))
image = ImageReader(path) # Do some thresholding mask = image < 128 # Find regions in the image region = FindRegions(mask, image) # Extract just the object roi_image = region.intensity_image # An object is identified by its label roi_label = region.label # Calculate a filename for the ROI image: # "RUNNING_NUMBER-SOURCE_BASENAME-ROI_LABEL" roi_name = Format( "{:d}-{}-{:d}.jpg", running_number, source_basename, roi_label ) meta = CalculateZooProcessFeatures(region, prefix="object_") # End of parallel execution # Store results EcotaxaWriter("archive.zip", (roi_name, roi_image), meta) # After the Pipeline was defined, it can be executed. # A stream is created and transformed by the operations # defined in the Pipeline. p.run()
img = obj.image img_gray = RGB2Gray(img, True) mask = obj.mask regionprops = ImageProperties(mask, img_gray) object_meta = obj.data object_id = Format("{lst_name}_{id}", lst_name=obj.lst_name, _kwargs=object_meta) object_meta["id"] = object_id object_meta = CalculateZooProcessFeatures(regionprops, object_meta) EcotaxaWriter( os.path.join(export_path, "export.zip"), [ (Format("{object_id}.jpg", object_id=object_id), img), (Format("{object_id}_gray.jpg", object_id=object_id), img_gray), (Format("{object_id}_mask.jpg", object_id=object_id), mask), ], object_meta=object_meta, ) TQDM(object_id) p.run()
# Generate an object identifier i = Enumerate() object_id = Format("{name}_{i:d}", name=name, i=i) # Calculate features. The calculated features are added to the global_metadata. # Returns a Variable representing a dict for every object in the stream. meta = CalculateZooProcessFeatures(regionprops, prefix="object_", meta=global_metadata) # If CalculateZooProcessFeatures is not used, we need to copy global_metadata into the stream: # meta = Call(lambda: global_metadata.copy()) # https://github.com/morphocut/morphocut/issues/51 # Add object_id to the metadata dictionary meta["object_id"] = object_id # Generate object filenames orig_fn = Format("{object_id}.jpg", object_id=object_id) gray_fn = Format("{object_id}-gray.jpg", object_id=object_id) # Write objects to an EcoTaxa archive: # roi image in original color, roi image in grayscale, metadata associated with each object EcotaxaWriter(archive_fn, [(orig_fn, roi_orig), (gray_fn, roi_gray)], meta) # Progress bar for objects TQDM(Format("Object {object_id}", object_id=object_id)) # Execute pipeline p.run()
# Construct object ID object_id = Format( "{lst_name}_{id}", lst_name=obj.lst_name, _kwargs=object_meta ) object_meta["id"] = object_id # Calculate object properties (area, eccentricity, equivalent_diameter, mean_intensity, ...). See skimage.measure.regionprops. regionprops = ImageProperties(mask, img_gray) # Append object properties to metadata in a ZooProcess-like format object_meta = CalculateZooProcessFeatures(regionprops, object_meta) # Write each object to an EcoTaxa archive. # Here, three different versions are written. Remove what you do not need. EcotaxaWriter( os.path.join(export_path, "export.zip"), [ # The original RGB image (Format("{object_id}.jpg", object_id=object_id), img), # A graylevel version (Format("{object_id}_gray.jpg", object_id=object_id), img_gray), # The binary mask (Format("{object_id}_mask.jpg", object_id=object_id), mask), ], object_meta=object_meta, ) # Display progress indicator for individual objects TQDM(object_id) p.run()