Skip to content

Instantly share code, notes, and snippets.

@gksriharsha
Created November 7, 2022 23:36
Show Gist options
  • Save gksriharsha/c989af9563ef0df46964129e1643b89b to your computer and use it in GitHub Desktop.
Save gksriharsha/c989af9563ef0df46964129e1643b89b to your computer and use it in GitHub Desktop.
The fill percentage is calculated for each stand. There are some methods developed to calculate the audience fill percentage in different ways. The deeplearning method performs the count of the people and the others estimate the fill %. This method is executed for each stand.
def fill_per_stand(self,stand:AudienceStand,solution='PixelCounting'):
img = self.stadium
blue_list = []
red_list = []
green_list = []
grey_count = 0
pix_count = 0
if solution == 'PixelCounting':
for x in range(min(stand.top_left[0],stand.bottom_left[0]),max(stand.top_right[0],stand.bottom_right[0])):
for y in range(min(stand.top_left[1],stand.bottom_left[1]),max(stand.top_right[1],stand.bottom_right[1])):
if stand.within_stand(np.array([x,y])):
blue = img[y, x, 0]
green = img[y, x, 1]
red = img[y, x, 2]
blue_list.append(blue)
red_list.append(red)
green_list.append(green)
if (red<=green<=blue and max(red,max(blue,green))- min(red,min(blue,green)) <= 30) or max(red,max(blue,green)) > 240:
grey_count += 1
pix_count += 1
fill = (pix_count- grey_count) * 100 / pix_count
stand.update_fill(fill)
return fill
elif solution == 'PixelBinning': # Works best for day time.
avg_pool_2d = tf.keras.layers.AveragePooling2D(pool_size=stand.seat_size, strides=stand.seat_size, padding='valid')
output = np.squeeze(avg_pool_2d(stand.stand_img[None,:,:]*1.0))
for x in range(output.shape[0]):
for y in range(output.shape[1]):
blue = img[y, x, 0]
green = img[y, x, 1]
red = img[y, x, 2]
blue_list.append(blue)
red_list.append(red)
green_list.append(green)
if (red<=green<=blue and max(red,max(blue,green))- min(red,min(blue,green)) <= 30) or max(red,max(blue,green)) > 240:
grey_count += 1
pix_count += 1
fill = (pix_count- grey_count) * 100 / pix_count
stand.update_fill(fill)
return fill
elif solution == 'OutlierDetection':
clf = pickle.load(open('LOF_night.pkl', 'rb'))
X_test = self.stadium
X_test = X_test.reshape(-1,3)
y_pred_outliers = clf.predict(X_test)
fill = sum(y_pred_outliers == -1)*100/len(y_pred_outliers)
stand.update_fill(fill)
return fill
elif solution == 'DeepLearning':
im = stand.stand_img
json_file = open('Model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights("model_A_weights.h5")
def mode_val(x):
values, counts = np.unique(x, return_counts=True)
m = counts.argmax()
return values[m], counts[m]
im = np.expand_dims(im,axis = 0)
ans = loaded_model.predict(im)
multiplier = 0
threshold = mode_val(ans)[0] + multiplier * np.std(ans[ma.masked_where(ans > mode_val(ans)[0],ans).mask])
count = sum(ans[ma.masked_where(ans >= threshold,ans).mask])
stand.update_fill(count)
print(np.mean(ans),np.std(ans),threshold)
#plt.imshow(ans.reshape(ans.shape[1],ans.shape[2]) , cmap = c.jet )
#plt.show()
return count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment