Can we count detected objects in Tensorflow 2 object detection API class wise?
as i am new to this so having hard time in manipulating the output of object detection model according to my use case described below
Say you have two classes tomato and potato in a super market shelf stock, I would like to count each object class wise
for example; potato_count:5 tomato_count:3.
for reference the following output i have is like this, just to give an idea:-
{'raw_detection_scores': array([[3.8237274e-03, 3.1729043e-03, 5.1983595e-03, ..., 1.0126382e-02,4.1468740e-03, 3.5721064e-03],
[3.7932396e-03, 1.9723773e-03, 2.3661852e-03, ..., 3.4036636e-03,
9.3266368e-04, 4.3996871e-03],
[3.2063425e-03, 2.9956400e-03, 3.9784312e-03, ..., 5.5939257e-03,
2.3936033e-03, 2.7040839e-03],
...,
[4.1239262e-03, 3.9246678e-04, 4.5044391e-05, ..., 1.2922287e-04,
2.8958917e-04, 2.2355914e-03],
[1.4656782e-03, 4.8859119e-03, 1.4899671e-03, ..., 2.8479993e-03,
2.8250813e-03, 2.1298528e-03],
[1.8135607e-03, 2.2478402e-03, 1.1820495e-03, ..., 9.5197558e-04,
1.3802052e-03, 2.2761822e-03]], dtype=float32), 'detection_anchor_indices': array([44692., 44710., 44728., 44818., 39652., 44674., 39670., 40036.,
40018., 44800., 39634., 39371., 44830., 38090., 44728., 44731.,
44728., 44710., 10078., 39796., 27838., 37604., 16933., 24833.,
39778., 44659., 45058., 38084., 44791., 44710., 44692., 30244.,
5284., 38090., 37604., 38204., 33593., 38192., 37982., 39796.,
44692., 6635., 33118., 24389., 37604., 44910., 33112., 39601.,
16133., 3845., 39918., 48370., 19204., 44740., 39778., 16792.,
6629., 25763., 38150., 48187., 15839., 38180., 23524., 44914.,
40036., 1438., 25763., 38078., 35992., 38012., 39888., 38084.,
44578., 40018., 51075., 38204., 15833., 37976., 40258., 37604.,
48751., 39906., 31684., 16453., 38054., 5140., 42568., 36484.,
38012., 38202., 37946., 14024., 2404., 40002., 5764., 39870.,
48823., 26878., 38198., 39430.], dtype=float32), 'detection_multiclass_scores': array([[1.6726255e-03, 3.2217724e-07, 2.8865278e-02, ..., 2.6032329e-04,
2.0583175e-05, 4.5886636e-04],
[1.0796189e-03, 5.8811463e-07, 3.6984652e-02, ..., 6.6033006e-04,
3.2279208e-05, 4.3705106e-04],
[6.1860681e-04, 2.4921978e-06, 6.0835034e-02, ..., 2.0631850e-03,
5.2474130e-05, 3.7664175e-04],
...,
[2.0163953e-03, 1.0121465e-03, 1.6601086e-03, ..., 5.1327944e-03,
1.6998947e-03, 9.6607208e-04],
[1.2855232e-03, 5.4006279e-03, 1.0573506e-02, ..., 1.3051391e-02,
1.0753423e-02, 1.3659596e-03],
[8.1962347e-04, 9.5169604e-01, 1.5044212e-03, ..., 5.1358938e-03,
8.4767938e-03, 3.2877922e-04]], dtype=float32),
'detection_classes': array([4, 4, 4, 4, 1, 4, 1, 1, 1, 4, 1, 1, 4, 6, 6, 4, 2, 6, 7, 7, 4, 6,
1, 6, 7, 4, 7, 9, 4, 2, 6, 7, 1, 3, 9, 6, 1, 6, 6, 3, 2, 5, 1, 2,
3, 6, 1, 1, 8, 1, 1, 1, 7, 4, 3, 1, 9, 6, 6, 8, 9, 6, 8, 2, 8, 7,
2, 6, 1, 6, 9, 5, 2, 9, 9, 3, 1, 9, 8, 5, 9, 9, 3, 5, 6, 1, 6, 6,
9, 6, 6, 1, 2, 1, 2, 9, 9, 4, 4, 7]), 'detection_boxes': array([[5.92004418e-01, 1.69490814e-01, 7.45701075e-01, 2.46565759e-01],
[5.89631081e-01, 2.46157080e-01, 7.39599228e-01, 3.18454713e-01],
[5.87109149e-01, 3.14503819e-01, 7.36972034e-01, 3.85336846e-01],
[5.87837219e-01, 7.05797434e-01, 7.28340387e-01, 7.74214983e-01],
[6.35630414e-02, 1.69735521e-01, 2.04962432e-01, 2.47269154e-01],
[5.92036664e-01, 9.53008384e-02, 7.46890843e-01, 1.73706189e-01],
[6.85142130e-02, 2.45773658e-01, 2.10277155e-01, 3.21099281e-01],
[9.23785418e-02, 7.77337551e-01, 2.25108251e-01, 8.45668435e-01],
[9.24619362e-02, 7.06092656e-01, 2.26126671e-01, 7.77547657e-01],
[5.85118353e-01, 6.37673438e-01, 7.26098835e-01, 7.05848277e-01],
[5.94619289e-02, 9.38070714e-02, 2.03622460e-01, 1.72308475e-01],
[3.44502553e-02, 7.54546002e-03, 2.08990484e-01, 9.39401984e-02],
[5.87913811e-01, 7.74250209e-01, 7.28712976e-01, 8.34733903e-01],
[9.84132528e-01, 3.18221241e-01, 9.96858120e-01, 3.95583898e-01],
[5.87109149e-01, 3.14503819e-01, 7.36972034e-01, 3.85336846e-01],
[5.89539468e-01, 3.49524260e-01, 7.35065162e-01, 4.21008408e-01],
[5.87109149e-01, 3.14503819e-01, 7.36972034e-01, 3.85336846e-01],
[5.89631081e-01, 2.46157080e-01, 7.39599228e-01, 3.18454713e-01],
[1.87163889e-01, 9.88169909e-01, 3.71130943e-01, 9.98176932e-01],
[9.36717317e-02, 7.77330160e-01, 2.24804163e-01, 8.45728278e-01],
[6.63008153e-01, 9.89469707e-01, 8.10642183e-01, 1.00000000e+00],
[9.70665693e-01, 3.16653520e-01, 9.95440483e-01, 3.85887355e-01],
[3.70503038e-01, 2.54840344e-01, 4.76123840e-01, 3.14984292e-01],
[5.87433934e-01, 7.05650687e-01, 7.27492571e-01, 7.73511648e-01],
[9.28397924e-02, 7.06507027e-01, 2.26004675e-01, 7.77664006e-01],
[5.82323313e-01, 1.54982358e-02, 7.39678025e-01, 1.03125945e-01],
[5.87077260e-01, 7.05565095e-01, 7.27602482e-01, 7.74259925e-01],
[9.83516991e-01, 3.11883837e-01, 9.97174442e-01, 3.89778942e-01],
[5.88727355e-01, 6.20116591e-01, 7.30183959e-01, 6.90428734e-01],
[5.89631081e-01, 2.46157080e-01, 7.39599228e-01, 3.18454713e-01],
[5.92004418e-01, 1.69490814e-01, 7.45701075e-01, 2.46565759e-01],
[7.42125034e-01, 0.00000000e+00, 8.81110668e-01, 8.84985179e-03],
[5.44907227e-02, 0.00000000e+00, 2.06223458e-01, 1.28744319e-02],
[9.84132528e-01, 3.18221241e-01, 9.96858120e-01, 3.95583898e-01],
[9.70665693e-01, 3.16653520e-01, 9.95440483e-01, 3.85887355e-01],
[9.84484971e-01, 5.55115521e-01, 9.97788608e-01, 6.32665694e-01],
[7.99783111e-01, 9.75158930e-01, 9.83929038e-01, 9.97444630e-01],
[9.85278428e-01, 5.28306305e-01, 9.97080624e-01, 6.08543932e-01],
[9.88123000e-01, 9.33963358e-02, 9.99226749e-01, 1.72955215e-01],
[9.36717317e-02, 7.77330160e-01, 2.24804163e-01, 8.45728278e-01],
[5.92004418e-01, 1.69490814e-01, 7.45701075e-01, 2.46565759e-01],
[9.08265784e-02, 7.78585851e-01, 2.25109786e-01, 8.43916476e-01],
[7.94785440e-01, 9.86553550e-01, 9.70936120e-01, 9.98435020e-01],
[5.84929466e-01, 7.75964439e-01, 7.24675894e-01, 8.34971726e-01],
[9.70665693e-01, 3.16653520e-01, 9.95440483e-01, 3.85887355e-01], 3.21474910e-01]],
dtype=float32), 'raw_detection_boxes': array([[-0.0132168 , -0.00798112, 0.03437265, 0.02366759],
[-0.01795438, -0.01333077, 0.04313567, 0.03091241],
[-0.00845873, -0.01297706, 0.02555573, 0.02979016],
[-0.01206583, -0.01901898, 0.03632494, 0.04061931],
[-0.01634497, -0.00570066, 0.04027664, 0.01987169],
[-0.02299639, -0.01094626, 0.05078602, 0.02601441],
[-0.01034649, -0.00047059, 0.03106559, 0.04336115],
[-0.01548673, -0.00679935, 0.03944379, 0.05214766],
[-0.00469762, -0.00637354, 0.02257038, 0.05068764],
[-0.00889431, -0.01532986, 0.03383063, 0.06445184],
[-0.01338234, 0.00258018, 0.03299785, 0.03899822],
[-0.02030504, -0.00274394, 0.04193052, 0.04610612],
[-0.0114202 , 0.00825354, 0.0315875 , 0.05609718],
[-0.01720474, 0.00155611, 0.03969076, 0.06473814],
[-0.0055348 , 0.00137738, 0.02347516, 0.06321988],
[-0.0093858 , -0.00954537, 0.03353771, 0.0789085 ],
[-0.01528691, 0.0120711 , 0.03230394, 0.05128276],
[-0.02242971, 0.00611713, 0.04139108, 0.0590462 ],
[-0.01265933, 0.01957938, 0.03226281, 0.06821183],
[-0.0190082 , 0.01264081, 0.04051029, 0.07676097],
[-0.00625486, 0.01262659, 0.02384217, 0.07535952],
[-0.01057751, 0.00036938, 0.03408406, 0.09211845],
[-0.01712188, 0.02387175, 0.03272626, 0.0631646 ],
[-0.02457684, 0.01729448, 0.04191976, 0.07130254],
[-0.01416131, 0.03209703, 0.03322188, 0.08013913],
[-0.02092581, 0.02524993, 0.04159252, 0.08845924],
[-0.00731821, 0.02507119, 0.02447667, 0.08743346],
[-0.01213621, 0.01294496, 0.03459452, 0.10395688],
[-0.01857999, 0.0361888 , 0.03388733, 0.07542843],
[-0.02637036, 0.02969162, 0.04293538, 0.08341235],
[-0.01507254, 0.04520991, 0.03351783, 0.09184141],
[-0.02222046, 0.03861695, 0.04212021, 0.10008947],
[-0.00780608, 0.03797973, 0.02448018, 0.09932629],
[-0.01303079, 0.02687315, 0.03459996, 0.1151351 ],
[-0.0191509 , 0.04890272, 0.03473954, 0.08777986],
[-0.02749499, 0.04277577, 0.04370061, 0.09534387],
[-0.01489433, 0.05867497, 0.03314201, 0.10344677],
[-0.02239214, 0.05207732, 0.04205906, 0.11197228],
[-0.00734611, 0.05139816, 0.02392033, 0.11116292],
[-0.01289164, 0.0412713 , 0.03449183, 0.12679553],
[-0.01872004, 0.06203329, 0.03483813, 0.09988385],
[-0.02761277, 0.05606709, 0.04412681, 0.10715124], 0.2496243 ]],
dtype=float32), 'detection_scores': array([0.9957284 , 0.9954956 , 0.9948391 , 0.9935589 , 0.9928843 ,
0.9922596 , 0.99091065, 0.9904872 , 0.9904753 , 0.9836049 ,
0.97076845, 0.76198786, 0.11483946, 0.08861226, 0.06485316,
0.06403089, 0.06083503, 0.05606595, 0.05304798, 0.05192479,
0.05068725, 0.0497607 , 0.04650801, 0.04170695, 0.04141748,
0.0396772 , 0.03875464, 0.03834933, 0.03700855, 0.03698465,
0.03656569, 0.03464538, 0.03429574, 0.03408125, 0.033981 ,
0.03356522, 0.03337869, 0.03140217, 0.03058183, 0.02957818,
0.02886528, 0.02712101, 0.02674139, 0.02655837, 0.02634463,
0.02611795, 0.02595255, 0.02580112, 0.0251711 , 0.02473494,
0.02423027, 0.02406707, 0.02352765, 0.02347961, 0.02342641,
0.02327773, 0.02312759, 0.0229713 , 0.02272761, 0.02240831,
0.02240023, 0.02203956, 0.02200234, 0.02167007, 0.02112213,
0.0210447 , 0.02079707, 0.02007249, 0.01999336, 0.01993376,
0.01986268, 0.0196887 , 0.01967749, 0.01877454, 0.01874545,
0.01856974, 0.01855248, 0.01853141, 0.01839408, 0.01838818,
0.01830906, 0.01829055, 0.01759666, 0.01758116, 0.01747909,
0.01745978, 0.01728415, 0.01719788, 0.0171611 , 0.01715598,
0.01704106, 0.01684934, 0.01672551, 0.01663077, 0.01645952,
0.01627839, 0.01607156, 0.01592609, 0.01579505, 0.01570672],
dtype=float32), 'num_detections': 100}
Please guyys help me out in this
thanks in advance
Check out his link: https://www.tensorflow.org/api_docs/python/tf/data/Dataset
Here, you can find how to iterate over Dataset using ".as_numpy_iterator()", but also how to use different methods to manipulate the input dataset.
Hope this will be useful.
def lookup_cell(self, column, row):
return(self.puzzle[row-1][column-1])
def lookup_column(self, column):
output = []
for i in range(9):
output.append(self.lookup_cell(column, i+1))
return output
def check_puzzle(self):
valid = True
#check all the rows
for i in range(1,10):
row = self.lookup_row(i)
while 0 in row: row.remove(0)
for i in range(1,10):
if row.count(i) > 1:
valid = False
#check all the columns
for i in range(1,10):
print(i)
print(easy.lookup_column(i))
puzzle = '''0,9,0,7,5,1,0,2,3 /n
2,1,8,6,0,3,7,5,4 /n
0,0,0,4,0,2,0,0,0 /n
1,0,0,0,0,0,0,9,2 /n
0,0,0,5,0,0,3,8,0 /n
3,0,0,8,2,0,5,0,6 /n
0,0,0,0,7,0,0,4,8 /n
0,4,9,0,0,0,0,7,0 /n
0,2,0,0,0,5,6,3,1 '''
easy = Sudoku(puzzle)
Here the code works and prints the columns of the puzzle properly:
for i in range(1,10):
print(easy.lookup_column(i))
when this runs I get an error which I'll add below:
easy.check_puzzle()
1 [9, 2, 4, 1, 5, 3, 7, 4, 2] 2 [7, 1, 2, 9, 3, 8, 4, 9, 5] 3
Traceback (most recent call last):
File "/Users/ellis/Desktop/Sudoku.py", line 121, in <module>
easy.check_puzzle()
File "/Users/ellis/Desktop/Sudoku.py", line 81, in check_puzzle
print(easy.lookup_column(i))
File "/Users/ellis/Desktop/Sudoku.py", line 65, in lookup_column
output.append(self.lookup_cell(column, i+1))
File "/Users/ellis/Desktop/Sudoku.py", line 19, in lookup_cell
return(self.puzzle[row-1][column-1])
IndexError: list index out of range
Your code works outside of the class method because
for i in range(1,10):
print(easy.lookup_column(i))
is not calling
def lookup_cell(self, column, row):
return(self.puzzle[row-1][column-1]
The traceback you posted shows that the code above is causing the issue, where your accessing values in the object that's outside its bounds
I'm not exactly sure what you are trying to accomplish, but why not use a list for puzzle instead of a string?
Try this:
class Sudoku(object):
def __init__(self,puzzle):
self.puzzle = puzzle
print("Inside class; column lookup")
for i in range(1,10):
print('Column {0} ='.format(i),self.lookup_column(i))
self.check_puzzle()
def lookup_cell(self, column, row):
return self.puzzle[row-1][column-1]
def lookup_row(self,row):
output = []
for i in range(9):
output.append(self.lookup_cell(i+1, row))
return output
def lookup_column(self, column):
output = []
for i in range(9):
output.append(self.lookup_cell(column, i+1))
return output
def check_puzzle(self):
valid = True
#check all the rows
for i in range(1,10):
row = self.lookup_row(i)
while 0 in row: row.remove(0)
for i in range(1,10):
if row.count(i) > 1:
valid = False
puzzle = [
[0,9,0,7,5,1,0,2,3],
[2,1,8,6,0,3,7,5,4],
[0,0,0,4,0,2,0,0,0],
[1,0,0,0,0,0,0,9,2],
[0,0,0,5,0,0,3,8,0],
[3,0,0,8,2,0,5,0,6],
[0,0,0,0,7,0,0,4,8],
[0,4,9,0,0,0,0,7,0],
[0,2,0,0,0,5,6,3,1]]
easy = Sudoku(puzzle)
print("Outside class; column lookup")
for i in range(1,10):
print('Column {0} ='.format(i),easy.lookup_column(i))
easy.check_puzzle()