Skip to content

Instantly share code, notes, and snippets.

@madhawav
Last active March 20, 2024 10:26
Show Gist options
  • Star 43 You must be signed in to star a gist
  • Fork 18 You must be signed in to fork a gist
  • Save madhawav/1546a4b99c8313f06c0b2d7d7b4a09e2 to your computer and use it in GitHub Desktop.
Save madhawav/1546a4b99c8313f06c0b2d7d7b4a09e2 to your computer and use it in GitHub Desktop.
# Code adapted from Tensorflow Object Detection Framework
# https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb
# Tensorflow Object Detection Detector
import numpy as np
import tensorflow as tf
import cv2
import time
class DetectorAPI:
def __init__(self, path_to_ckpt):
self.path_to_ckpt = path_to_ckpt
self.detection_graph = tf.Graph()
with self.detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(self.path_to_ckpt, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
self.default_graph = self.detection_graph.as_default()
self.sess = tf.Session(graph=self.detection_graph)
# Definite input and output Tensors for detection_graph
self.image_tensor = self.detection_graph.get_tensor_by_name('image_tensor:0')
# Each box represents a part of the image where a particular object was detected.
self.detection_boxes = self.detection_graph.get_tensor_by_name('detection_boxes:0')
# Each score represent how level of confidence for each of the objects.
# Score is shown on the result image, together with the class label.
self.detection_scores = self.detection_graph.get_tensor_by_name('detection_scores:0')
self.detection_classes = self.detection_graph.get_tensor_by_name('detection_classes:0')
self.num_detections = self.detection_graph.get_tensor_by_name('num_detections:0')
def processFrame(self, image):
# Expand dimensions since the trained_model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image, axis=0)
# Actual detection.
start_time = time.time()
(boxes, scores, classes, num) = self.sess.run(
[self.detection_boxes, self.detection_scores, self.detection_classes, self.num_detections],
feed_dict={self.image_tensor: image_np_expanded})
end_time = time.time()
print("Elapsed Time:", end_time-start_time)
im_height, im_width,_ = image.shape
boxes_list = [None for i in range(boxes.shape[1])]
for i in range(boxes.shape[1]):
boxes_list[i] = (int(boxes[0,i,0] * im_height),
int(boxes[0,i,1]*im_width),
int(boxes[0,i,2] * im_height),
int(boxes[0,i,3]*im_width))
return boxes_list, scores[0].tolist(), [int(x) for x in classes[0].tolist()], int(num[0])
def close(self):
self.sess.close()
self.default_graph.close()
if __name__ == "__main__":
model_path = '/path/to/faster_rcnn_inception_v2_coco_2017_11_08/frozen_inference_graph.pb'
odapi = DetectorAPI(path_to_ckpt=model_path)
threshold = 0.7
cap = cv2.VideoCapture('/path/to/input/video')
while True:
r, img = cap.read()
img = cv2.resize(img, (1280, 720))
boxes, scores, classes, num = odapi.processFrame(img)
# Visualization of the results of a detection.
for i in range(len(boxes)):
# Class 1 represents human
if classes[i] == 1 and scores[i] > threshold:
box = boxes[i]
cv2.rectangle(img,(box[1],box[0]),(box[3],box[2]),(255,0,0),2)
cv2.imshow("preview", img)
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
break
@chrisfauerbach
Copy link

Madhawa - I found your medium post tonight on 'people detection'. I'm building out a project, with code awfully similar. Looking at the code on line 76-80, your application is still 'finding' everything right? but only highlighting people? or am I missing something?

@jiapei100
Copy link

Where can I adjust batch_size ?
I got the following ERROR messages:

Limit:                  3022389248
InUse:                  1819312384
MaxInUse:               2296556288
NumAllocs:                    2107
MaxAllocSize:           1704329216

2018-09-26 01:26:45.888177: W tensorflow/core/common_runtime/bfc_allocator.cc:275] **************_____***********************************************__________________________________
2018-09-26 01:26:45.888213: W tensorflow/core/framework/op_kernel.cc:1273] OP_REQUIRES failed at transpose_op.cc:199 : Resource exhausted: OOM when allocating tensor with shape[300,4032,17,17] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
Traceback (most recent call last):
  File "~/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1292, in _do_call
    return fn(*args)
  File "~/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1277, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "~/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1367, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.ResourceExhaustedError: OOM when allocating tensor with shape[300,4032,17,17] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
         [[{{node MaxPool2D/MaxPool-0-TransposeNHWCToNCHW-LayoutOptimizer}} = Transpose[T=DT_FLOAT, Tperm=DT_INT32, _device="/job:localhost/replica:0/task:0/device:GPU:0"](CropAndResize, PermConstNHWCToNCHW-LayoutOptimizer)]]
Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.

         [[{{node SecondStagePostprocessor/BatchMultiClassNonMaxSuppression/map/while/MultiClassNonMaxSuppression/ClipToWindow_69/Gather/Gather_2/_625}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_11273...r/Gather_2", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](^_cloopSecondStagePostprocessor/BatchMultiClassNonMaxSuppression/map/while/MultiClassNonMaxSuppression/SortByField/Assert/Assert/data_0/_31)]]
Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./tensorflow-human-detection.py", line 72, in <module>
    boxes, scores, classes, num = odapi.processFrame(img)
  File "./tensorflow-human-detection.py", line 43, in processFrame
    feed_dict={self.image_tensor: image_np_expanded})
  File "~/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 887, in run
    run_metadata_ptr)
  File "~/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1110, in _run
    feed_dict_tensor, options, run_metadata)
  File "~/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1286, in _do_run
    run_metadata)
  File "~/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1308, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.ResourceExhaustedError: OOM when allocating tensor with shape[300,4032,17,17] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
         [[{{node MaxPool2D/MaxPool-0-TransposeNHWCToNCHW-LayoutOptimizer}} = Transpose[T=DT_FLOAT, Tperm=DT_INT32, _device="/job:localhost/replica:0/task:0/device:GPU:0"](CropAndResize, PermConstNHWCToNCHW-LayoutOptimizer)]]
Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.

         [[{{node SecondStagePostprocessor/BatchMultiClassNonMaxSuppression/map/while/MultiClassNonMaxSuppression/ClipToWindow_69/Gather/Gather_2/_625}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_11273...r/Gather_2", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](^_cloopSecondStagePostprocessor/BatchMultiClassNonMaxSuppression/map/while/MultiClassNonMaxSuppression/SortByField/Assert/Assert/data_0/_31)]]
Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.

@mzkaramat
Copy link

Thanks for the tutorial, it is very informative. Is there better way to retrain the model only on the human instead of getting all classes as output?

@KeitelDOG
Copy link

Very good codes. Is there a way that I can filter classes to use only person, car. I know their class value are 1 for person, 3 for car, but how coul I possibly make this happen here in line 33 :

self.detection_classes = self.detection_graph.get_tensor_by_name('detection_classes:0')

@AnalyticsInAction
Copy link

AnalyticsInAction commented Mar 23, 2019

Runs fine for me, but freezes when I try and quit out with "q". Issue is resolved when I append the following code to the end of the script to destroy the windows.

cap.release()
cv2.destroyAllWindows()

@vchacham
Copy link

vchacham commented Jun 5, 2019

How do I get rid of this error?
AttributeError: module 'tensorflow' has no attribute 'GraphDef'
I have added graph.proto file to the tensorflow/core/framework/graph.proto
But still getting the same error
Please help me.. Thank you


AttributeError Traceback (most recent call last)
in ()
54 if name == "main":
55 model_path = '/path/to/faster_rcnn_inception_v2_coco_2017_11_08/frozen_inference_graph.pb'
---> 56 odapi = DetectorAPI(path_to_ckpt=model_path)
57 threshold = 0.7
58 cap = cv2.VideoCapture('/path/to/input/video')

in init(self, path_to_ckpt)
7 self.detection_graph = tf.Graph()
8 with self.detection_graph.as_default():
----> 9 od_graph_def = tf.GraphDef()
10 with tf.gfile.GFile(self.path_to_ckpt, 'rb') as fid:
11 serialized_graph = fid.read()

AttributeError: module 'tensorflow' has no attribute 'GraphDef'

@Raymundo1
Copy link

Very good codes. Is there a way that I can filter classes to use only person, car. I know their class value are 1 for person, 3 for car, but how coul I possibly make this happen here in line 33 :

self.detection_classes = self.detection_graph.get_tensor_by_name('detection_classes:0')

change line 78 to the specific class you want

@kindofausername
Copy link

Great code. thanks!

@enesSenVolantX
Copy link

How do I get rid of this error?
AttributeError: module 'tensorflow' has no attribute 'GraphDef'
I have added graph.proto file to the tensorflow/core/framework/graph.proto
But still getting the same error
Please help me.. Thank you

AttributeError Traceback (most recent call last)
in ()
54 if name == "main":
55 model_path = '/path/to/faster_rcnn_inception_v2_coco_2017_11_08/frozen_inference_graph.pb'
---> 56 odapi = DetectorAPI(path_to_ckpt=model_path)
57 threshold = 0.7
58 cap = cv2.VideoCapture('/path/to/input/video')

in init(self, path_to_ckpt)
7 self.detection_graph = tf.Graph()
8 with self.detection_graph.as_default():
----> 9 od_graph_def = tf.GraphDef()
10 with tf.gfile.GFile(self.path_to_ckpt, 'rb') as fid:
11 serialized_graph = fid.read()

AttributeError: module 'tensorflow' has no attribute 'GraphDef'

did you resolve this issue? is it about version of tensorflow?

@xstast24
Copy link

AttributeError: module 'tensorflow' has no attribute 'GraphDef'

did you resolve this issue? is it about version of tensorflow?

  1. Make sure you are loading correct model named "frozen_inference_graph.pb" and not the other *.pb file.
  2. This code works for tensorflow 1.x versions. If you have tensorflow 2.x then you need to call this:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

@heerparmar15
Copy link

AttributeError: module 'tensorflow' has no attribute 'GraphDef'

did you resolve this issue? is it about version of tensorflow?

  1. Make sure you are loading correct model named "frozen_inference_graph.pb" and not the other *.pb file.
  2. This code works for tensorflow 1.x versions. If you have tensorflow 2.x then you need to call this:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

I want to run this code on google colab. How do i give the frozen_inference_graph.pb path to the code there?

@hndr91
Copy link

hndr91 commented Apr 21, 2020

Very good codes. Is there a way that I can filter classes to use only person, car. I know their class value are 1 for person, 3 for car, but how coul I possibly make this happen here in line 33 :

self.detection_classes = self.detection_graph.get_tensor_by_name('detection_classes:0')

Hei @KeitelDOG how to find out the index of the class?

@KeitelDOG
Copy link

Very good codes. Is there a way that I can filter classes to use only person, car. I know their class value are 1 for person, 3 for car, but how coul I possibly make this happen here in line 33 :

self.detection_classes = self.detection_graph.get_tensor_by_name('detection_classes:0')

Hei @KeitelDOG how to find out the index of the class?

@hndr91 you will find it in the data directory of tensorflow models in oddl directory of the User. For Example:

/Users/YourUser/oddl/models/research/object_detection/data/mscoco_complete_label_map.pbtxt.
Maybe it depends on where you add your models, don't remember. But if you can't find them directly, you may SEARCH in oddl directoryu for extension .pbtxt.

Here is some class values:

item {
  name: "background"
  id: 0
  display_name: "background"
}
item {
  name: "/m/01g317"
  id: 1
  display_name: "person"
}
item {
  name: "/m/0199g"
  id: 2
  display_name: "bicycle"
}
item {
  name: "/m/0k4j"
  id: 3
  display_name: "car"
}
item {
  name: "/m/04_sv"
  id: 4
  display_name: "motorcycle"
}
item {
  name: "/m/05czz6l"
  id: 5
  display_name: "airplane"
}
item {
  name: "/m/01bjv"
  id: 6
  display_name: "bus"
}
item {
  name: "/m/07jdr"
  id: 7
  display_name: "train"
}
item {
  name: "/m/07r04"
  id: 8
  display_name: "truck"
}
item {
  name: "/m/019jd"
  id: 9
  display_name: "boat"
}
item {
  name: "/m/015qff"
  id: 10
  display_name: "traffic light"
}
item {
  name: "/m/01pns0"
  id: 11
  display_name: "fire hydrant"
}
item {
  name: "12"
  id: 12
  display_name: "12"
}
item {
  name: "/m/02pv19"
  id: 13
  display_name: "stop sign"
}
item {
  name: "/m/015qbp"
  id: 14
  display_name: "parking meter"
}
item {
  name: "/m/0cvnqh"
  id: 15
  display_name: "bench"
}
item {
  name: "/m/015p6"
  id: 16
  display_name: "bird"
}
item {
  name: "/m/01yrx"
  id: 17
  display_name: "cat"
}
item {
  name: "/m/0bt9lr"
  id: 18
  display_name: "dog"
}
item {
  name: "/m/03k3r"
  id: 19
  display_name: "horse"
}
item {
  name: "/m/07bgp"
  id: 20
  display_name: "sheep"
}
item {
  name: "/m/01xq0k1"
  id: 21
  display_name: "cow"
}
item {
  name: "/m/0bwd_0j"
  id: 22
  display_name: "elephant"
}
item {
  name: "/m/01dws"
  id: 23
  display_name: "bear"
}
item {
  name: "/m/0898b"
  id: 24
  display_name: "zebra"
}
item {
  name: "/m/03bk1"
  id: 25
  display_name: "giraffe"
}
item {
  name: "26"
  id: 26
  display_name: "26"
}
item {
  name: "/m/01940j"
  id: 27
  display_name: "backpack"
}
item {
  name: "/m/0hnnb"
  id: 28
  display_name: "umbrella"
}
item {
  name: "29"
  id: 29
  display_name: "29"
}
item {
  name: "30"
  id: 30
  display_name: "30"
}
item {
  name: "/m/080hkjn"
  id: 31
  display_name: "handbag"
}
item {
  name: "/m/01rkbr"
  id: 32
  display_name: "tie"
}
item {
  name: "/m/01s55n"
  id: 33
  display_name: "suitcase"
}
item {
  name: "/m/02wmf"
  id: 34
  display_name: "frisbee"
}
item {
  name: "/m/071p9"
  id: 35
  display_name: "skis"
}
item {
  name: "/m/06__v"
  id: 36
  display_name: "snowboard"
}
item {
  name: "/m/018xm"
  id: 37
  display_name: "sports ball"
}
item {
  name: "/m/02zt3"
  id: 38
  display_name: "kite"
}
item {
  name: "/m/03g8mr"
  id: 39
  display_name: "baseball bat"
}
item {
  name: "/m/03grzl"
  id: 40
  display_name: "baseball glove"
}
item {
  name: "/m/06_fw"
  id: 41
  display_name: "skateboard"
}
item {
  name: "/m/019w40"
  id: 42
  display_name: "surfboard"
}
item {
  name: "/m/0dv9c"
  id: 43
  display_name: "tennis racket"
}
item {
  name: "/m/04dr76w"
  id: 44
  display_name: "bottle"
}
item {
  name: "45"
  id: 45
  display_name: "45"
}
item {
  name: "/m/09tvcd"
  id: 46
  display_name: "wine glass"
}
item {
  name: "/m/08gqpm"
  id: 47
  display_name: "cup"
}
item {
  name: "/m/0dt3t"
  id: 48
  display_name: "fork"
}
item {
  name: "/m/04ctx"
  id: 49
  display_name: "knife"
}
item {
  name: "/m/0cmx8"
  id: 50
  display_name: "spoon"
}
item {
  name: "/m/04kkgm"
  id: 51
  display_name: "bowl"
}
item {
  name: "/m/09qck"
  id: 52
  display_name: "banana"
}
item {
  name: "/m/014j1m"
  id: 53
  display_name: "apple"
}
item {
  name: "/m/0l515"
  id: 54
  display_name: "sandwich"
}
item {
  name: "/m/0cyhj_"
  id: 55
  display_name: "orange"
}
item {
  name: "/m/0hkxq"
  id: 56
  display_name: "broccoli"
}
item {
  name: "/m/0fj52s"
  id: 57
  display_name: "carrot"
}
item {
  name: "/m/01b9xk"
  id: 58
  display_name: "hot dog"
}
item {
  name: "/m/0663v"
  id: 59
  display_name: "pizza"
}
item {
  name: "/m/0jy4k"
  id: 60
  display_name: "donut"
}
item {
  name: "/m/0fszt"
  id: 61
  display_name: "cake"
}
item {
  name: "/m/01mzpv"
  id: 62
  display_name: "chair"
}
item {
  name: "/m/02crq1"
  id: 63
  display_name: "couch"
}
item {
  name: "/m/03fp41"
  id: 64
  display_name: "potted plant"
}
item {
  name: "/m/03ssj5"
  id: 65
  display_name: "bed"
}
item {
  name: "66"
  id: 66
  display_name: "66"
}
item {
  name: "/m/04bcr3"
  id: 67
  display_name: "dining table"
}
item {
  name: "68"
  id: 68
  display_name: "68"
}
item {
  name: "69"
  id: 69
  display_name: "69"
}
item {
  name: "/m/09g1w"
  id: 70
  display_name: "toilet"
}
item {
  name: "71"
  id: 71
  display_name: "71"
}
item {
  name: "/m/07c52"
  id: 72
  display_name: "tv"
}
item {
  name: "/m/01c648"
  id: 73
  display_name: "laptop"
}
item {
  name: "/m/020lf"
  id: 74
  display_name: "mouse"
}
item {
  name: "/m/0qjjc"
  id: 75
  display_name: "remote"
}
item {
  name: "/m/01m2v"
  id: 76
  display_name: "keyboard"
}
item {
  name: "/m/050k8"
  id: 77
  display_name: "cell phone"
}
item {
  name: "/m/0fx9l"
  id: 78
  display_name: "microwave"
}
item {
  name: "/m/029bxz"
  id: 79
  display_name: "oven"
}
item {
  name: "/m/01k6s3"
  id: 80
  display_name: "toaster"
}
item {
  name: "/m/0130jx"
  id: 81
  display_name: "sink"
}
item {
  name: "/m/040b_t"
  id: 82
  display_name: "refrigerator"
}
item {
  name: "83"
  id: 83
  display_name: "83"
}
item {
  name: "/m/0bt_c3"
  id: 84
  display_name: "book"
}
item {
  name: "/m/01x3z"
  id: 85
  display_name: "clock"
}
item {
  name: "/m/02s195"
  id: 86
  display_name: "vase"
}
item {
  name: "/m/01lsmm"
  id: 87
  display_name: "scissors"
}
item {
  name: "/m/0kmg4"
  id: 88
  display_name: "teddy bear"
}
item {
  name: "/m/03wvsk"
  id: 89
  display_name: "hair drier"
}
item {
  name: "/m/012xff"
  id: 90
  display_name: "toothbrush"
}

@hndr91
Copy link

hndr91 commented Apr 22, 2020

Very good codes. Is there a way that I can filter classes to use only person, car. I know their class value are 1 for person, 3 for car, but how coul I possibly make this happen here in line 33 :

self.detection_classes = self.detection_graph.get_tensor_by_name('detection_classes:0')

Hei @KeitelDOG how to find out the index of the class?

@hndr91 you will find it in the data directory of tensorflow models in oddl directory of the User. For Example:

/Users/YourUser/oddl/models/research/object_detection/data/mscoco_complete_label_map.pbtxt.
Maybe it depends on where you add your models, don't remember. But if you can't find them directly, you may SEARCH in oddl directoryu for extension .pbtxt.

Here is some class values:

item {
  name: "background"
  id: 0
  display_name: "background"
}
item {
  name: "/m/01g317"
  id: 1
  display_name: "person"
}
item {
  name: "/m/0199g"
  id: 2
  display_name: "bicycle"
}
item {
  name: "/m/0k4j"
  id: 3
  display_name: "car"
}
item {
  name: "/m/04_sv"
  id: 4
  display_name: "motorcycle"
}
item {
  name: "/m/05czz6l"
  id: 5
  display_name: "airplane"
}
item {
  name: "/m/01bjv"
  id: 6
  display_name: "bus"
}
item {
  name: "/m/07jdr"
  id: 7
  display_name: "train"
}
item {
  name: "/m/07r04"
  id: 8
  display_name: "truck"
}
item {
  name: "/m/019jd"
  id: 9
  display_name: "boat"
}
item {
  name: "/m/015qff"
  id: 10
  display_name: "traffic light"
}
item {
  name: "/m/01pns0"
  id: 11
  display_name: "fire hydrant"
}
item {
  name: "12"
  id: 12
  display_name: "12"
}
item {
  name: "/m/02pv19"
  id: 13
  display_name: "stop sign"
}
item {
  name: "/m/015qbp"
  id: 14
  display_name: "parking meter"
}
item {
  name: "/m/0cvnqh"
  id: 15
  display_name: "bench"
}
item {
  name: "/m/015p6"
  id: 16
  display_name: "bird"
}
item {
  name: "/m/01yrx"
  id: 17
  display_name: "cat"
}
item {
  name: "/m/0bt9lr"
  id: 18
  display_name: "dog"
}
item {
  name: "/m/03k3r"
  id: 19
  display_name: "horse"
}
item {
  name: "/m/07bgp"
  id: 20
  display_name: "sheep"
}
item {
  name: "/m/01xq0k1"
  id: 21
  display_name: "cow"
}
item {
  name: "/m/0bwd_0j"
  id: 22
  display_name: "elephant"
}
item {
  name: "/m/01dws"
  id: 23
  display_name: "bear"
}
item {
  name: "/m/0898b"
  id: 24
  display_name: "zebra"
}
item {
  name: "/m/03bk1"
  id: 25
  display_name: "giraffe"
}
item {
  name: "26"
  id: 26
  display_name: "26"
}
item {
  name: "/m/01940j"
  id: 27
  display_name: "backpack"
}
item {
  name: "/m/0hnnb"
  id: 28
  display_name: "umbrella"
}
item {
  name: "29"
  id: 29
  display_name: "29"
}
item {
  name: "30"
  id: 30
  display_name: "30"
}
item {
  name: "/m/080hkjn"
  id: 31
  display_name: "handbag"
}
item {
  name: "/m/01rkbr"
  id: 32
  display_name: "tie"
}
item {
  name: "/m/01s55n"
  id: 33
  display_name: "suitcase"
}
item {
  name: "/m/02wmf"
  id: 34
  display_name: "frisbee"
}
item {
  name: "/m/071p9"
  id: 35
  display_name: "skis"
}
item {
  name: "/m/06__v"
  id: 36
  display_name: "snowboard"
}
item {
  name: "/m/018xm"
  id: 37
  display_name: "sports ball"
}
item {
  name: "/m/02zt3"
  id: 38
  display_name: "kite"
}
item {
  name: "/m/03g8mr"
  id: 39
  display_name: "baseball bat"
}
item {
  name: "/m/03grzl"
  id: 40
  display_name: "baseball glove"
}
item {
  name: "/m/06_fw"
  id: 41
  display_name: "skateboard"
}
item {
  name: "/m/019w40"
  id: 42
  display_name: "surfboard"
}
item {
  name: "/m/0dv9c"
  id: 43
  display_name: "tennis racket"
}
item {
  name: "/m/04dr76w"
  id: 44
  display_name: "bottle"
}
item {
  name: "45"
  id: 45
  display_name: "45"
}
item {
  name: "/m/09tvcd"
  id: 46
  display_name: "wine glass"
}
item {
  name: "/m/08gqpm"
  id: 47
  display_name: "cup"
}
item {
  name: "/m/0dt3t"
  id: 48
  display_name: "fork"
}
item {
  name: "/m/04ctx"
  id: 49
  display_name: "knife"
}
item {
  name: "/m/0cmx8"
  id: 50
  display_name: "spoon"
}
item {
  name: "/m/04kkgm"
  id: 51
  display_name: "bowl"
}
item {
  name: "/m/09qck"
  id: 52
  display_name: "banana"
}
item {
  name: "/m/014j1m"
  id: 53
  display_name: "apple"
}
item {
  name: "/m/0l515"
  id: 54
  display_name: "sandwich"
}
item {
  name: "/m/0cyhj_"
  id: 55
  display_name: "orange"
}
item {
  name: "/m/0hkxq"
  id: 56
  display_name: "broccoli"
}
item {
  name: "/m/0fj52s"
  id: 57
  display_name: "carrot"
}
item {
  name: "/m/01b9xk"
  id: 58
  display_name: "hot dog"
}
item {
  name: "/m/0663v"
  id: 59
  display_name: "pizza"
}
item {
  name: "/m/0jy4k"
  id: 60
  display_name: "donut"
}
item {
  name: "/m/0fszt"
  id: 61
  display_name: "cake"
}
item {
  name: "/m/01mzpv"
  id: 62
  display_name: "chair"
}
item {
  name: "/m/02crq1"
  id: 63
  display_name: "couch"
}
item {
  name: "/m/03fp41"
  id: 64
  display_name: "potted plant"
}
item {
  name: "/m/03ssj5"
  id: 65
  display_name: "bed"
}
item {
  name: "66"
  id: 66
  display_name: "66"
}
item {
  name: "/m/04bcr3"
  id: 67
  display_name: "dining table"
}
item {
  name: "68"
  id: 68
  display_name: "68"
}
item {
  name: "69"
  id: 69
  display_name: "69"
}
item {
  name: "/m/09g1w"
  id: 70
  display_name: "toilet"
}
item {
  name: "71"
  id: 71
  display_name: "71"
}
item {
  name: "/m/07c52"
  id: 72
  display_name: "tv"
}
item {
  name: "/m/01c648"
  id: 73
  display_name: "laptop"
}
item {
  name: "/m/020lf"
  id: 74
  display_name: "mouse"
}
item {
  name: "/m/0qjjc"
  id: 75
  display_name: "remote"
}
item {
  name: "/m/01m2v"
  id: 76
  display_name: "keyboard"
}
item {
  name: "/m/050k8"
  id: 77
  display_name: "cell phone"
}
item {
  name: "/m/0fx9l"
  id: 78
  display_name: "microwave"
}
item {
  name: "/m/029bxz"
  id: 79
  display_name: "oven"
}
item {
  name: "/m/01k6s3"
  id: 80
  display_name: "toaster"
}
item {
  name: "/m/0130jx"
  id: 81
  display_name: "sink"
}
item {
  name: "/m/040b_t"
  id: 82
  display_name: "refrigerator"
}
item {
  name: "83"
  id: 83
  display_name: "83"
}
item {
  name: "/m/0bt_c3"
  id: 84
  display_name: "book"
}
item {
  name: "/m/01x3z"
  id: 85
  display_name: "clock"
}
item {
  name: "/m/02s195"
  id: 86
  display_name: "vase"
}
item {
  name: "/m/01lsmm"
  id: 87
  display_name: "scissors"
}
item {
  name: "/m/0kmg4"
  id: 88
  display_name: "teddy bear"
}
item {
  name: "/m/03wvsk"
  id: 89
  display_name: "hair drier"
}
item {
  name: "/m/012xff"
  id: 90
  display_name: "toothbrush"
}

Thanks, @KietelDOG
So, according to the label information, is it possible to modified the dict. in case I don't need all the label?

@KeitelDOG
Copy link

@hndr91 you can create your own file that has chosen classes person_label_map.pbtx:

item {
  name: "/m/01g317"
  id: 1
  display_name: "person"
}

And use it instead of the default one, don't remember how exactly. But if you're after some speed, know that the pre-trained models already use all classes. If you want to check person only, you have to train a new model to detect only person, so it will be faster. Even if you use person only label, the model still check for all classes for each frame.

@hndr91
Copy link

hndr91 commented Apr 29, 2020

thanks @KeitelDOG it much clear now

@umeshkumar99
Copy link

How do I get rid of this error?
AttributeError: module 'tensorflow' has no attribute 'GraphDef'
I have added graph.proto file to the tensorflow/core/framework/graph.proto
But still getting the same error
Please help me.. Thank you

AttributeError Traceback (most recent call last)
in ()
54 if name == "main":
55 model_path = '/path/to/faster_rcnn_inception_v2_coco_2017_11_08/frozen_inference_graph.pb'
---> 56 odapi = DetectorAPI(path_to_ckpt=model_path)
57 threshold = 0.7
58 cap = cv2.VideoCapture('/path/to/input/video')

in init(self, path_to_ckpt)
7 self.detection_graph = tf.Graph()
8 with self.detection_graph.as_default():
----> 9 od_graph_def = tf.GraphDef()
10 with tf.gfile.GFile(self.path_to_ckpt, 'rb') as fid:
11 serialized_graph = fid.read()

AttributeError: module 'tensorflow' has no attribute 'GraphDef'
Import Tensorflow using below command
import tensorflow.compat.v1 as tf

@umeshkumar99
Copy link

import tensorflow.compat.v1 as tf

@dhgokul
Copy link

dhgokul commented Jul 16, 2020

Issue : AttributeError: module 'tensorflow' has no attribute 'GraphDef'
use the below code for version issue (TF 2.X)
od_graph_def = tf.compat.v1.GraphDef()
with tf.io.gfile.GFile(self.path_to_ckpt, 'rb') as fid:

Issue : AttributeError: module 'tensorflow' has no attribute 'Session'
use the below code for version issue (TF 2.X)
self.sess = tf.compat.v1.Session(graph=self.detection_graph)

@devanshkaria88
Copy link

Madhawa - I found your medium post tonight on 'people detection'. I'm building out a project, with code awfully similar. Looking at the code on line 76-80, your application is still 'finding' everything right? but only highlighting people? or am I missing something?

exactly... I think so too...

@BrandonGene
Copy link

How do I get rid of this error?

File "C:\test\human_detection.py", line 64, in
odapi = DetectorAPI(path_to_ckpt=model_path)

File "C:\test\human_detection.py", line 19, in init
serialized_graph = fid.read()

File "C:\Users\D02008001.conda\envs\tensorflow\lib\site-packages\tensorflow\python\lib\io\file_io.py", line 117, in read
self._preread_check()

File "C:\Users\D02008001.conda\envs\tensorflow\lib\site-packages\tensorflow\python\lib\io\file_io.py", line 80, in _preread_check
compat.path_to_str(self.__name), 1024 * 512)

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa8 in position 114: invalid start byte

@KeitelDOG
Copy link

@BrandonGene can't really know why, but I would bet on your file specs, since you're using Windows.

The problem could be BOM, Byte-Order Mark. Depending on the IDE you use, UTF-8 files could be saved with BOM, and failing at a UTF8 decoder that can't handle it.

If it's BOM the problem, I'm not sure, then save your file as simple UTF8 (normallly, not With BOM). Might wanna have an advance IDE like Sublime to do it, or IDE that uses normal one by default.

If it's not BOM the problem, the you can still try another IDE that can handle your file normally. Also it could be incomplete file, check the file size online and offline, or the md5 or sha1 hash value and check that you have the same file that can't be decoded.

@Malik675
Copy link

Thank you for this
But can any one share with me this type of project that only download and run directly .please I need help @engineerabbas675@gmail.com

@BrandonGene
Copy link

@BrandonGene can't really know why, but I would bet on your file specs, since you're using Windows.

The problem could be BOM, Byte-Order Mark. Depending on the IDE you use, UTF-8 files could be saved with BOM, and failing at a UTF8 decoder that can't handle it.

If it's BOM the problem, I'm not sure, then save your file as simple UTF8 (normallly, not With BOM). Might wanna have an advance IDE like Sublime to do it, or IDE that uses normal one by default.

If it's not BOM the problem, the you can still try another IDE that can handle your file normally. Also it could be incomplete file, check the file size online and offline, or the md5 or sha1 hash value and check that you have the same file that can't be decoded.

Thanks, I solved it!
Can I use this model to learn more complex human images?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment