Package: olympict
OlympictBased onolympipe, this project will make image processing pipelines easy to use using the basic multiprocessing module.
This module uses type checking to ensure your data process validity from the start.Basic image processing pipelineLoading images from a folder and resize them to a new folderfromolympictimportImagePipelinep0=ImagePipeline.load_folder("./examples")# path containing the imagesp1=p0.resize((150,250))# new width, new heightp2=p1.save_to_folder("./resized_examples")# path to save the imagesp2.wait_for_completion()# the code blocks here until all images are processedprint("Finished resizing")Loading images from a folder and overwrite them with a new sizefromolympictimportImagePipelinep0=ImagePipeline.load_folder("./examples")# path containing the imagesp1=p0.resize((150,250))p2=p1.save()# overwrite the imagesp2.wait_for_completion()Loading images from a folder and resize them keeping the aspect ratio using a padding colorfromolympictimportImagePipelineblue=(255,0,0)# Colors are BGR to match opencv formatp0=ImagePipeline.load_folder("./examples")p1=p0.resize((150,250),pad_color=blue)p2=p1.save()# overwrite the imagesp2.wait_for_completion()Load image to make a specific operationfromolympictimportImagePipeline,Imgdefoperation(image:Img)->Img:# set the green channel as a mean of blue and red channelsimg[:,:,1]=(img[:,:,0]+img[:,:,2])/2returnimgp0=ImagePipeline.load_folder("./examples")p1=p0.task_img(operation)p2=p1.save()# overwrite the imagesp2.wait_for_completion()Check ongoing operationfromolympictimportImagePipeline,Imgdefoperation(image:Img)->Img:# set the green channel as a mean of blue and red channelsimg[:,:,1]=(img[:,:,0]+img[:,:,2])/2returnimgp0=ImagePipeline.load_folder("./examples").debug_window("Raw image")p1=p0.task_img(operation).debug_window("Processed image")p2=p1.save()# overwrite the imagesp2.wait_for_completion()Load a video and process each individual framefromolympictimportVideoPipelinep0=VideoPipeline.load_folder("./examples")# will load .mp4 and .mkv filesp1=p0.to_sequence()# split each video frame into a basic imagep2=p1.resize((100,3),(255,255,255))# resize each image with white paddingp3=p2.save_to_folder("./sequence")# save images individuallyp3.wait_for_completion()img_paths=glob("./sequence/*.png")# count imagesprint("Number of images:",len(img_paths))Complex example with preview windowsimportosfromrandomimportrandintimportreimporttimefromolympictimportImagePipelinefromolympict.files.o_imageimportOlympImagedefimg_simple_order(path:str)->int:number_pattern=r"\d+"res=re.findall(number_pattern,os.path.basename(path))returnint(res[0])if__name__=="__main__":defwait(x:OlympImage):time.sleep(0.1)print(x.path)returnxdefgenerator():foriinrange(96):img=np.zeros((256,256,3),np.uint8)img[i,:,:]=(255,255,255)o=OlympImage()o.path=f'/tmp/{i}.png'o.img=imgyieldoreturnp=(ImagePipeline(generator()).task(wait).debug_window("start it").task_img(lambdax:x[::-1,:,:]).debug_window("flip it").keep_each_frame_in(1,3).debug_window("stuttered").draw_bboxes(lambdax:[((randint(0,50),randint(0,50),randint(100,200),randint(100,200),"change",0.5,),(randint(0,255),25,245),)]).debug_window("bboxed"))p.wait_for_completion()/!\ To use Huggingface Models, you must install this package with [hf] extraspoetryaddolympict[hf]or
pipinstallolympict[hf]Use with Huggingface image classification modelsfromolympictimportImagePipelinefromolympict.files.o_imageimportOlympImagedefprint_metas(x:OlympImage):print(x.metadata)returnxif__name__=="__main__":# very important, without this processes will get stuckfromtorch.multiprocessingimportset_start_methodset_start_method("spawn")(ImagePipeline.load_folder("./classif").classify("google/mobilenet_v2_1.0_224").task(print_metas)).wait_for_completion()This project is still an early version, feedback is very helpful.