-
-
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 | |
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)
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...
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
@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.
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 @[email protected]
@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?
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')
@KeitelDOG Did you get successful in only detecting the humans in the frame?
@wahas-mughal yes, I had it working like this, to shows only detected humans, by using a custom Map with only person label. The performance is still the same, as in background the model still detect all classes, but the custom map only tells it to show person result. It's still in my old Macbook with poor performance, and someday I'll check in the my new Macbook to see how useful the performance can get.
import tensorflow.compat.v1 as tf