In[]:=
img=
;
In[]:=
points={{362.23980712890625`,5.856842041015625`},{259.7484130859375`,3.82769775390625`},{355.66119384765625`,68.0926513671875`},{250.1883544921875`,74.5430908203125`},{278.0540771484375`,89.62887573242188`},{240.35238647460938`,93.74325561523438`},{274.680419921875`,130.12841796875`},{239.29092407226562`,131.72845458984375`},{259.4684753417969`,104.56793212890625`},{58.09281921386719`,98.33905029296875`},{262.0574645996094`,242.64395141601562`},{63.9501953125`,249.2485809326172`}};Length[points];Dimensions[Partition[points,4]];HighlightImage[img,points]minMaxBoxes={MinMax[First[#]],MinMax[Rest[#]]}&/@Transpose[Partition[points,4],{1,3,2}];imgShape=ImageDimensions[img];minMaxBoxes//Dimensions;scaledboxes=minMaxBoxes//Transpose[#,{3,1,2}]&//#/ImageDimensions[img]&//Transpose[#,{2,3,1}]&;label=Apply[Rectangle,#]&/@Transpose[scaledboxes,{1,3,2}];fromRectangle[rect_]:={rect[[1]],rect[[2]]}scaleRectangles[rects_,img_]:=(fromRectangle/@rects)//Transpose[#,{3,2,1}]&//#*ImageDimensions[img]&//Transpose[#,{3,2,1}]&//Apply[Rectangle,#]&/@#&;scaleRectangles[label,img];HighlightImage[img,scaleRectangles[label,img]];classes={"Dragonfruit","Kiwi","Pineapple"}dataset={img(#[[1]]#[[2]]&/@Transpose[{label,classes}])}With[{img=Keys[#],labels=Values[#]},HighlightImage[img,Labeled[#[[1]],#[[2]]]&/@Transpose[{scaleRectangles[Keys[labels],img],Values[labels]}]]]&/@dataset
Out[]=
Out[]=
{Dragonfruit,Kiwi,Pineapple}
Out[]=
{Rectangle[{0.625471,0.012759},{0.9056,0.248477}]Dragonfruit,Rectangle[{0.598227,0.298763},{0.695135,0.439095}]Kiwi,Rectangle[{0.145232,0.327797},{0.655144,0.830829}]Pineapple}
Out[]=
In[]:=
ClearAll[getClassExtractor];getClassExtractor[dataset_]:=(Values/@Values[dataset])//Flatten//DeleteDuplicates//Sort//FeatureExtraction[#,"IndicatorVector",FeatureTypes"Nominal"]&;getClassExtractor[dataset][{"Dragonfruit","Kiwi","Pineapple"}]ClearAll[rectsToXYWH];rectsToXYWH[rectangles_]:=With[{reshaped=(fromRectangle/@rectangles)//Transpose[#,{3,2,1}]&},With[{centers=(Mean/@reshaped)//Transpose,sizes=((Subtract[#[[2]],#[[1]]]&)/@reshaped)//Transpose},Join[centers,sizes,2]]];ClearAll[trainingSample];trainingSample[datum_,classExtractor_]:=With[{img=Keys[datum],labels=Values[datum]},With[{rectangles=Keys[labels],classes=Values[labels]},{img,Join[rectsToXYWH[rectangles],classExtractor[classes],2]}]]myClassExtractor=getClassExtractor[dataset];trainingSample[First[dataset],myClassExtractor]nClasses=myClassExtractor["Dimensions"]//Length
Out[]=
SparseArray
Out[]=
,{{0.765535,0.130618,0.280129,0.235718,1.,0.,0.},{0.646681,0.368929,0.0969079,0.140332,0.,1.,0.},{0.400188,0.579313,0.509912,0.503032,0.,0.,1.}}
Out[]=
3
In[]:=
ClearAll[computeBestAnchors]computeBestAnchors[dataset_,nAnchor_]:=With[{allXYWH=rectsToXYWH[dataset//Values//Keys//Flatten[#,1]&]},(*gettheWHcomponents*)With[{allWH=allXYWH[[All,3;;4]]},With[{clustered=FindClusters[allWH,nAnchor,Method"KMeans"]},With[{means=Mean/@clustered(*Thisishacky,itwouldbenicetodireclyextractfromclusteringprocess.*)},(*Wecouldjustreturnmeans,butFindClustersmaygeneratefeweranchorboxclustersthanwewant.Tohandlethis,wewewillcheckifenoughmeanswerefoundandrepeatthemostcommonmeanuntilwehavethedesirednumberofanchors.*)If[Length[clustered]<nAnchor,With[{clusterLens=Length/@clustered,nMissing=nAnchor-Length[clustered]},Join[means,Table[Extract[means,First@Position[clusterLens,Max@clusterLens,{1},1]],nMissing]]],means]]]]]myAnchors=computeBestAnchors[dataset,5]