├── LICENSE
├── README.md
├── data
├── images
│ ├── test
│ │ ├── seq_000001.jpg
│ │ ├── seq_000002.jpg
│ │ ├── seq_000003.jpg
│ │ ├── seq_000004.jpg
│ │ ├── seq_000005.jpg
│ │ ├── seq_000006.jpg
│ │ ├── seq_000007.jpg
│ │ ├── seq_000008.jpg
│ │ ├── seq_000009.jpg
│ │ └── seq_000010.jpg
│ ├── train
│ │ ├── seq_000011.jpg
│ │ ├── seq_000013.jpg
│ │ ├── seq_000014.jpg
│ │ ├── seq_000015.jpg
│ │ ├── seq_000016.jpg
│ │ ├── seq_000017.jpg
│ │ ├── seq_000019.jpg
│ │ ├── seq_000020.jpg
│ │ ├── seq_000021.jpg
│ │ ├── seq_000022.jpg
│ │ ├── seq_000023.jpg
│ │ ├── seq_000025.jpg
│ │ ├── seq_000026.jpg
│ │ ├── seq_000027.jpg
│ │ ├── seq_000028.jpg
│ │ ├── seq_000029.jpg
│ │ ├── seq_000031.jpg
│ │ ├── seq_000032.jpg
│ │ ├── seq_000033.jpg
│ │ ├── seq_000034.jpg
│ │ ├── seq_000035.jpg
│ │ ├── seq_000036.jpg
│ │ ├── seq_000037.jpg
│ │ ├── seq_000038.jpg
│ │ ├── seq_000039.jpg
│ │ └── seq_000040.jpg
│ └── val
│ │ ├── seq_000012.jpg
│ │ ├── seq_000018.jpg
│ │ ├── seq_000024.jpg
│ │ └── seq_000030.jpg
└── utils
│ ├── faster_rcnn.config
│ ├── train.record
│ └── val.record
├── main.py
├── pretrained-results
├── result1.png
└── result2.png
├── requirements.txt
└── results
├── result0001.jpg
├── result0002.jpg
├── result0003.jpg
├── result0004.jpg
├── result0005.jpg
├── result0006.jpg
└── result0007.jpg
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 Darpan Jain
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # CROWD COUNTING
2 |
3 | ## Contents
4 |
5 | - [What do we have here?](#introduction)
6 | - [Why pretrained models don't work?](#using-pre-trained-models)
7 | - [What now?](#training-a-custom-model)
8 | - [Does it work?](#results)
9 | - [What all do I need?](#prerequisites)
10 | - [Let's do this!](#usage)
11 |
12 | ***
13 |
14 | ### Introduction
15 |
16 | This repository contains the code of performing the task of implementing a people counter from an overhead video surveillance camera by using Transfer Learning.
17 | ***
18 |
19 | ### Using pre-trained models
20 |
21 | - Tensorflow’s Object Detection API provides pre-trained models for object detection, which are capable of detecting around 90 classes (objects) with `person` being one of the classes.
22 |
23 | - On giving test images to a pretrained model, the inference results were not as per requirements. On some instances the model detected the entire image as a person and also missing out on some fairly obvious ones.
24 |
25 |
26 | 
27 |
28 | - Clearly, using the pre-trained models is not the way to go. So, ~~Pre-Trained Models~~ :confused:
29 | ***
30 |
31 | ### Training a custom model
32 |
33 | A custom model had to trained for accurate implementation. The following steps were taken for the same:
34 |
35 | 1. **Annotated training data** had to be prepared before being given to the model fot training.
36 | 1. ***[LabelImg](https://github.com/tzutalin/labelImg)*** was used for this purpose, to draw bounding boxes around objects of interest in the training images.
37 | 1. LabelImg gives output as **xml** files containing coordinates of each of the bounding boxes in an image and the associated label of the object.
38 | 1. All the xml files were converted to a ***train.csv*** and then into a ***train.record*** format. TFRecord format is required by Tensorflow to perform training of a custom model.
39 | 1. Similarly a ***val.record*** was created for validation data.
40 | 1. The architecture of the model is based on the **Faster RCNN** algorithm, which is an efficient and popular object detection algorithm which uses deep convolutional networks.
41 | 1. The config file of the model was modified. The last 90 neuron classification layer of the network was removed and replaced with a new layer that gives output for only one class i.e. person.
42 | 1. The **config file** for the same can be found in `./data/utils/faster_rcnn.config`
43 | 1. After training the model, the checkpoint model is saved as `model.pb` file.
44 | 1. This model can now be deployed and used for obtaining inferences on crowd images.
45 |
46 | - The model can be found on this drive link: [Custom Model](https://drive.google.com/open?id=1IBgEyaASf10KUFTCbky9mtruUpyoqDWR)
47 |
48 | - Download and place the model in `./data/utils` before executing main.py.
49 | ***
50 |
51 | ### Results
52 | Upon running `main.py`, the results are as shown below. (Refer `./results`)
53 |
54 |
55 | 
56 |
57 |
58 | 
59 |
60 | ***Note:*** Since the model was trained on only **30** annotated images, the accuracy can be significantly increased by using a larger dataset to build the model.
61 | ***
62 |
63 |
64 | ### Prerequisites
65 | All the required dependencies can be install by running the command `pip install -r requirements.txt`
66 | ***
67 |
68 | ### Usage
69 |
70 | - Place the images (.jpg) you need to run inference on in `./data/images/test`
71 | - Run `main.py`
72 | - Results will be saved in `./results`
73 |
74 | ***
75 |
--------------------------------------------------------------------------------
/data/images/test/seq_000001.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/test/seq_000001.jpg
--------------------------------------------------------------------------------
/data/images/test/seq_000002.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/test/seq_000002.jpg
--------------------------------------------------------------------------------
/data/images/test/seq_000003.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/test/seq_000003.jpg
--------------------------------------------------------------------------------
/data/images/test/seq_000004.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/test/seq_000004.jpg
--------------------------------------------------------------------------------
/data/images/test/seq_000005.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/test/seq_000005.jpg
--------------------------------------------------------------------------------
/data/images/test/seq_000006.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/test/seq_000006.jpg
--------------------------------------------------------------------------------
/data/images/test/seq_000007.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/test/seq_000007.jpg
--------------------------------------------------------------------------------
/data/images/test/seq_000008.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/test/seq_000008.jpg
--------------------------------------------------------------------------------
/data/images/test/seq_000009.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/test/seq_000009.jpg
--------------------------------------------------------------------------------
/data/images/test/seq_000010.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/test/seq_000010.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000011.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000011.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000013.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000013.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000014.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000014.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000015.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000015.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000016.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000016.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000017.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000017.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000019.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000019.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000020.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000020.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000021.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000021.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000022.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000022.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000023.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000023.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000025.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000025.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000026.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000026.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000027.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000027.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000028.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000028.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000029.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000029.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000031.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000031.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000032.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000032.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000033.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000033.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000034.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000034.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000035.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000035.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000036.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000036.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000037.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000037.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000038.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000038.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000039.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000039.jpg
--------------------------------------------------------------------------------
/data/images/train/seq_000040.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/train/seq_000040.jpg
--------------------------------------------------------------------------------
/data/images/val/seq_000012.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/val/seq_000012.jpg
--------------------------------------------------------------------------------
/data/images/val/seq_000018.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/val/seq_000018.jpg
--------------------------------------------------------------------------------
/data/images/val/seq_000024.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/val/seq_000024.jpg
--------------------------------------------------------------------------------
/data/images/val/seq_000030.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/images/val/seq_000030.jpg
--------------------------------------------------------------------------------
/data/utils/faster_rcnn.config:
--------------------------------------------------------------------------------
1 | model {
2 | faster_rcnn {
3 | num_classes: 1
4 | image_resizer {
5 | keep_aspect_ratio_resizer {
6 | min_dimension: 600
7 | max_dimension: 1024
8 | }
9 | }
10 | feature_extractor {
11 | type: "faster_rcnn_resnet50"
12 | first_stage_features_stride: 16
13 | }
14 | first_stage_anchor_generator {
15 | grid_anchor_generator {
16 | height_stride: 16
17 | width_stride: 16
18 | scales: 0.25
19 | scales: 0.5
20 | scales: 1.0
21 | scales: 2.0
22 | aspect_ratios: 0.5
23 | aspect_ratios: 1.0
24 | aspect_ratios: 2.0
25 | }
26 | }
27 | first_stage_box_predictor_conv_hyperparams {
28 | op: CONV
29 | regularizer {
30 | l2_regularizer {
31 | weight: 0.0
32 | }
33 | }
34 | initializer {
35 | truncated_normal_initializer {
36 | stddev: 0.00999999977648
37 | }
38 | }
39 | }
40 | first_stage_nms_score_threshold: 0.0
41 | first_stage_nms_iou_threshold: 0.699999988079
42 | first_stage_max_proposals: 100
43 | first_stage_localization_loss_weight: 2.0
44 | first_stage_objectness_loss_weight: 1.0
45 | initial_crop_size: 14
46 | maxpool_kernel_size: 2
47 | maxpool_stride: 2
48 | second_stage_box_predictor {
49 | mask_rcnn_box_predictor {
50 | fc_hyperparams {
51 | op: FC
52 | regularizer {
53 | l2_regularizer {
54 | weight: 0.0
55 | }
56 | }
57 | initializer {
58 | variance_scaling_initializer {
59 | factor: 1.0
60 | uniform: true
61 | mode: FAN_AVG
62 | }
63 | }
64 | }
65 | use_dropout: false
66 | dropout_keep_probability: 1.0
67 | }
68 | }
69 | second_stage_post_processing {
70 | batch_non_max_suppression {
71 | score_threshold: 0.300000011921
72 | iou_threshold: 0.600000023842
73 | max_detections_per_class: 100
74 | max_total_detections: 100
75 | }
76 | score_converter: SOFTMAX
77 | }
78 | second_stage_localization_loss_weight: 2.0
79 | second_stage_classification_loss_weight: 1.0
80 | }
81 | }
82 | train_config {
83 | batch_size: 1
84 | data_augmentation_options {
85 | random_horizontal_flip {
86 | }
87 | }
88 | optimizer {
89 | momentum_optimizer {
90 | learning_rate {
91 | manual_step_learning_rate {
92 | initial_learning_rate: 0.000300000014249
93 | schedule {
94 | step: 900000
95 | learning_rate: 2.99999992421e-05
96 | }
97 | schedule {
98 | step: 1200000
99 | learning_rate: 3.00000010611e-06
100 | }
101 | }
102 | }
103 | momentum_optimizer_value: 0.899999976158
104 | }
105 | use_moving_average: false
106 | }
107 | gradient_clipping_by_norm: 10.0
108 | fine_tune_checkpoint: "/home/darpan/people_counter/models/model.ckpt"
109 | from_detection_checkpoint: true
110 | num_steps: 500
111 | }
112 | train_input_reader {
113 | label_map_path: "/home/darpan/people_counter/data/label.pbtxt"
114 | tf_record_input_reader {
115 | input_path: "/home/darpan/people_counter/data/train.record"
116 | }
117 | }
118 | eval_config {
119 | num_examples: 4
120 | max_evals: 10
121 | use_moving_averages: false
122 | }
123 | eval_input_reader {
124 | label_map_path: "/home/darpan/people_counter/data/label.pbtxt"
125 | shuffle: false
126 | num_readers: 1
127 | tf_record_input_reader {
128 | input_path: "/home/darpan/people_counter/data/eval.record"
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/data/utils/train.record:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/utils/train.record
--------------------------------------------------------------------------------
/data/utils/val.record:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/data/utils/val.record
--------------------------------------------------------------------------------
/main.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | import tensorflow as tf
3 | import cv2
4 | import time
5 | import glob
6 | from progressbar import *
7 |
8 | widgets = [Bar('>'), ' ', ETA(), ' ', ReverseBar('<')]
9 | pbar = ProgressBar(widgets=widgets, maxval=10000000)
10 |
11 | class People_Counter:
12 |
13 | def __init__(self, path):
14 | self.path = path
15 | self.detection_graph = tf.Graph()
16 | with self.detection_graph.as_default():
17 | od_graph_def = tf.GraphDef()
18 | with tf.gfile.GFile(self.path, 'rb') as fid:
19 | serialized_graph = fid.read()
20 | od_graph_def.ParseFromString(serialized_graph)
21 | tf.import_graph_def(od_graph_def, name='')
22 |
23 | self.default_graph = self.detection_graph.as_default()
24 | self.sess = tf.Session(graph=self.detection_graph)
25 |
26 | self.image_tensor = self.detection_graph.get_tensor_by_name('image_tensor:0') # Defining tensors for the graph
27 | self.detection_boxes = self.detection_graph.get_tensor_by_name('detection_boxes:0') # Each box denotes part of image with a person detected
28 | self.detection_scores = self.detection_graph.get_tensor_by_name('detection_scores:0') # Score represents the confidence for the detected person
29 | self.detection_classes = self.detection_graph.get_tensor_by_name('detection_classes:0')
30 | self.num_detections = self.detection_graph.get_tensor_by_name('num_detections:0')
31 |
32 | def detect(self, image):
33 | image_np_expanded = np.expand_dims(image, axis=0)
34 | (boxes, scores, classes, num) = self.sess.run(
35 | [self.detection_boxes, self.detection_scores, self.detection_classes, self.num_detections],
36 | feed_dict={self.image_tensor: image_np_expanded}) # Using the model for detection
37 |
38 | im_height, im_width,_ = image.shape
39 | boxes_list = [None for i in range(boxes.shape[1])]
40 | for i in range(boxes.shape[1]):
41 | boxes_list[i] = (int(boxes[0,i,0] * im_height),
42 | int(boxes[0,i,1]*im_width),
43 | int(boxes[0,i,2] * im_height),
44 | int(boxes[0,i,3]*im_width))
45 |
46 | return boxes_list, scores[0].tolist(), [int(x) for x in classes[0].tolist()], int(num[0])
47 |
48 | def close(self):
49 | self.sess.close()
50 | self.default_graph.close()
51 |
52 | if __name__ == "__main__":
53 | model_path = './data/utils/my_model.pb'
54 | peop_counter = People_Counter(path=model_path)
55 | threshold = 0.4
56 | no=1
57 | for n in pbar(glob.glob("./data/images/test/*.jpg")):
58 | count=0
59 | img = cv2.imread(n)
60 | img = cv2.resize(img, (640, 480))
61 |
62 | boxes, scores, classes, num = peop_counter.detect(img)
63 |
64 | for i in range(len(boxes)):
65 | if classes[i] == 1 and scores[i] > threshold:
66 | box = boxes[i]
67 | cv2.rectangle(img,(box[1],box[0]),(box[3],box[2]),(255,0,0),2)
68 | count+=1
69 | cv2.putText(img,'Count = '+str(count),(10,400),cv2.FONT_HERSHEY_SIMPLEX, 1.25,(255,255,0),2,cv2.LINE_AA)
70 | cv2.imwrite("./results/result%04i.jpg" %no, img)
71 | no+=1
72 | print("\n\t\t\tSuccessfully saved all results!\n")
73 |
--------------------------------------------------------------------------------
/pretrained-results/result1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/pretrained-results/result1.png
--------------------------------------------------------------------------------
/pretrained-results/result2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/pretrained-results/result2.png
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | tensorflow==2.9.3
2 | numpy==1.22.0
3 | opencv_contrib_python==4.2.0.32
4 | progressbar2==3.39.2
5 |
--------------------------------------------------------------------------------
/results/result0001.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/results/result0001.jpg
--------------------------------------------------------------------------------
/results/result0002.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/results/result0002.jpg
--------------------------------------------------------------------------------
/results/result0003.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/results/result0003.jpg
--------------------------------------------------------------------------------
/results/result0004.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/results/result0004.jpg
--------------------------------------------------------------------------------
/results/result0005.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/results/result0005.jpg
--------------------------------------------------------------------------------
/results/result0006.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/results/result0006.jpg
--------------------------------------------------------------------------------
/results/result0007.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darpan-jain/crowd-counting-using-tensorflow/14e5d1523851868a7d0616e47b40913887c88d4d/results/result0007.jpg
--------------------------------------------------------------------------------