Spaces:
Build error
Build error
Daniel Nouri
commited on
Commit
·
752eb09
1
Parent(s):
a501265
Demo of Crowd Counting work by Thanasutives et al
Browse filesUses API and pretrained models from here:
https://github.com/Pongpisit-Thanasutives/Variations-of-SFANet-for-Crowd-Counting
- app.py +90 -0
- crowd.jpg +0 -0
- requirements.txt +8 -0
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
import sys
|
| 4 |
+
|
| 5 |
+
import cv2
|
| 6 |
+
import gdown
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import numpy as np
|
| 9 |
+
import streamlit as st
|
| 10 |
+
import torch
|
| 11 |
+
from torchvision import transforms
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def setup_env(path='Variations-of-SFANet-for-Crowd-Counting'):
|
| 15 |
+
if os.path.exists(path):
|
| 16 |
+
return path
|
| 17 |
+
subprocess.run(
|
| 18 |
+
[
|
| 19 |
+
'git',
|
| 20 |
+
'clone',
|
| 21 |
+
f'https://github.com/Pongpisit-Thanasutives/{path}.git',
|
| 22 |
+
f'{path}',
|
| 23 |
+
],
|
| 24 |
+
capture_output=True,
|
| 25 |
+
check=True,
|
| 26 |
+
)
|
| 27 |
+
sys.path.append(path)
|
| 28 |
+
with open(os.path.join(path, 'models', '__init__.py'), 'w') as f:
|
| 29 |
+
f.write('')
|
| 30 |
+
return path
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def get_model(path, weights):
|
| 34 |
+
from models import M_SFANet_UCF_QNRF
|
| 35 |
+
|
| 36 |
+
model = M_SFANet_UCF_QNRF.Model()
|
| 37 |
+
model.load_state_dict(
|
| 38 |
+
torch.load(weights, map_location=torch.device('cpu')))
|
| 39 |
+
return model.eval()
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def download_weights(
|
| 43 |
+
url='https://drive.google.com/uc?id=1fGuH4o0hKbgdP1kaj9rbjX2HUL1IH0oo',
|
| 44 |
+
out="Paper's_weights_UCF_QNRF.zip",
|
| 45 |
+
):
|
| 46 |
+
weights = "Paper's_weights_UCF_QNRF/best_M-SFANet*_UCF_QNRF.pth"
|
| 47 |
+
if os.path.exists(weights):
|
| 48 |
+
return weights
|
| 49 |
+
gdown.download(url, out)
|
| 50 |
+
subprocess.run(
|
| 51 |
+
['unzip', out],
|
| 52 |
+
capture_output=True,
|
| 53 |
+
check=True,
|
| 54 |
+
)
|
| 55 |
+
return weights
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def transform_image(img):
|
| 59 |
+
trans = transforms.Compose([
|
| 60 |
+
transforms.ToTensor(),
|
| 61 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
| 62 |
+
])
|
| 63 |
+
height, width = img.size[1], img.size[0]
|
| 64 |
+
height = round(height / 16) * 16
|
| 65 |
+
width = round(width / 16) * 16
|
| 66 |
+
img = cv2.resize(np.array(img), (width, height), cv2.INTER_CUBIC)
|
| 67 |
+
return trans(Image.fromarray(img))[None, :]
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def main():
|
| 71 |
+
st.write("Demo of [Encoder-Decoder Based Convolutional Neural Networks with Multi-Scale-Aware Modules for Crowd Counting](https://arxiv.org/abs/2003.05586)") # noqa
|
| 72 |
+
path = setup_env()
|
| 73 |
+
weights = download_weights()
|
| 74 |
+
model = get_model(path, weights)
|
| 75 |
+
|
| 76 |
+
image_file = st.file_uploader(
|
| 77 |
+
"Upload image", type=['png', 'jpg', 'jpeg'])
|
| 78 |
+
if image_file is not None:
|
| 79 |
+
image = Image.open(image_file).convert('RGB')
|
| 80 |
+
st.image(image)
|
| 81 |
+
density_map = model(transform_image(image))
|
| 82 |
+
density_map_img = density_map.detach().numpy()[0].transpose(1, 2, 0)
|
| 83 |
+
st.image(density_map_img / density_map_img.max())
|
| 84 |
+
st.write("Estimated count: ", torch.sum(density_map).item())
|
| 85 |
+
else:
|
| 86 |
+
st.write("Example image to use that you can drag and drop:")
|
| 87 |
+
st.image(Image.open('crowd.jpg').convert('RGB'))
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
main()
|
crowd.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gdown
|
| 2 |
+
numpy
|
| 3 |
+
opencv-python-headless
|
| 4 |
+
Pillow
|
| 5 |
+
streamlit
|
| 6 |
+
torch
|
| 7 |
+
torchvision
|
| 8 |
+
|