Package: needlefinder
Find occurrences of a needle image in a haystack image using multiprocessing and template matching.pip install needlefinderTested against Python 3.11 / Windows 10# For different needle sizesfromneedlefinderimportfind_needle_in_haystackINTER_NEAREST=0INTER_LINEAR=1INTER_CUBIC=2INTER_AREA=3INTER_LANCZOS4=4INTER_LINEAR_EXACT=5INTER_NEAREST_EXACT=6INTER_MAX=7WARP_FILL_OUTLIERS=8WARP_INVERSE_MAP=16interpolation=INTER_AREAhaystack=r"C:\savedsch\1349.png"# accepts almost everything - np.arrays / PIL / base64 / bytes ...needle=r"C:\savedsch\1313.png"df=find_needle_in_haystack(haystack,needle,with_image_data=True,percentage_min=80,percentage_max=120,steps=3,thresh=0.85,interpolation=interpolation,pad_input=False,mode="constant",constant_values=0,needlename="arrow",usecache=True,processes=3,chunks=1,print_stdout=False,print_stderr=True,)print(df)Parameters:-haystack(str):Pathtothehaystackimagefile.-needle(str):Pathtotheneedleimagefile.-with_image_data(bool,optional):IfTrue,includeimagedataintheresultDataFrame.DefaultisTrue.-percentage_min(int,optional):Minimumscalepercentageforresizingtheneedleimage.Defaultis50.-percentage_max(int,optional):Maximumscalepercentageforresizingtheneedleimage.Defaultis150.-steps(int,optional):Scalepercentagestepsforresizingtheneedleimage.Defaultis1.-thresh(float,optional):Thresholdformatching.Valuesbelowthisthresholdareconsiderednon-matches.Defaultis0.9.-interpolation(int,optional):Interpolationmethodforresizing.Defaultis4(cv2.INTER_LANCZOS4).-pad_input(bool,optional):IfTrue,padtheinputimage.DefaultisFalse.-mode(str,optional):Paddingmode.Defaultis'constant'.-constant_values(int,optional):Constantvalueforpadding.Defaultis0.-needlename(str,optional):NameoftheneedleusedforidentificationintheresultDataFrame.Defaultis'arrow'.-usecache(bool,optional):IfTrue,usecachingduringmultiprocessing.DefaultisTrue.-processes(int,optional):Numberofprocessestouseformultiprocessing.Defaultis5.-chunks(int,optional):Chunksizeformultiprocessing.Defaultis1.-print_stdout(bool,optional):IfTrue,printstdoutduringmultiprocessing.DefaultisFalse.-print_stderr(bool,optional):IfTrue,printstderrduringmultiprocessing.DefaultisTrue.Returns:-DataFrame:ResultDataFramecontaininginformationaboutmatchedoccurrences.Columnsinclude'aa_start_x','aa_start_y','aa_scale_factor','aa_width','aa_height','aa_match','aa_end_x','aa_end_y','aa_center_x','aa_center_y','aa_area','aa_needlename'.Ifwith_image_dataisTrue,additionalcolumnsinclude'aa_screenshot','aa_r','aa_g','aa_b','aa_old_index'.# for multiple needles and multiple haystacksneedles={"n1":r"C:\screeeni\16.png","n2":r"C:\screeeni\123.png","n3":r"C:\screeeni\130.png",}haystacks=[r"C:\haystacks\161.png",r"C:\haystacks\523.png",r"C:\haystacks\630.png",]INTER_NEAREST=0INTER_LINEAR=1INTER_CUBIC=2INTER_AREA=3INTER_LANCZOS4=4INTER_LINEAR_EXACT=5INTER_NEAREST_EXACT=6INTER_MAX=7WARP_FILL_OUTLIERS=8WARP_INVERSE_MAP=16interpolation=INTER_AREAdfneedles=find_needles_in_multi_haystacks(haystacks=haystacks,needles=needles,with_image_data=True,thresh=0.9,pad_input=False,mode="constant",constant_values=0,usecache=True,processes=5,chunks=1,print_stdout=False,print_stderr=True,)Findoccurrencesofmultipleneedleimagesinmultiplehaystackimagesusingtemplatematching.Parameters:-haystacks(List[str]):Listofhaystackimages.-needles(Dict[str,str]):Dictionarywherekeysareneedlenamesandvaluesaretheneedleimages.-with_image_data(bool,optional):IfTrue,includeimagedataintheresultDataFrame.DefaultisTrue.-thresh(float,optional):Thresholdformatching.Valuesbelowthisthresholdareconsiderednon-matches.Defaultis0.9.-pad_input(bool,optional):IfTrue,padtheinputimage.DefaultisFalse.-mode(str,optional):Paddingmode.Defaultis'constant'.-constant_values(int,optional):Constantvalueforpadding.Defaultis0.-usecache(bool,optional):IfTrue,usecachingduringmultiprocessing.DefaultisTrue.-processes(int,optional):Numberofprocessestouseformultiprocessing.Defaultis5.-chunks(int,optional):Chunksizeformultiprocessing.Defaultis1.-print_stdout(bool,optional):IfTrue,printstdoutduringmultiprocessing.DefaultisFalse.-print_stderr(bool,optional):IfTrue,printstderrduringmultiprocessing.DefaultisTrue.Returns:-DataFrame:ResultDataFramecontaininginformationaboutmatchedoccurrences.Columnsinclude'aa_start_x','aa_start_y','aa_scale_factor','aa_width','aa_height','aa_match','aa_end_x','aa_end_y','aa_center_x','aa_center_y','aa_area','aa_needlename','aa_img_index'.Ifwith_image_dataisTrue,additionalcolumnsinclude'aa_screenshot','aa_r','aa_g','aa_b'.Raises:-Exception:Ifanerroroccursduringmultiprocessingormatching.Notes:-The'aa_old_index'columnisretainedfromindividualmatches.