Skip to content

Commit cb7e157

Browse files
Add files via upload
1 parent 07139c6 commit cb7e157

11 files changed

+839
-0
lines changed

Object_detection.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
## Evan's very own Object Detection program using Tensforflow MobileNet-SSD model
2+
3+
## Some of this will be copied from Google's example at
4+
## https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb
5+
6+
## and some will be copied from this guy's example at
7+
## https://github.com/datitran/object_detector_app/blob/master/object_detection_app.py
8+
9+
## but I will change it to make it more understandable to me.
10+
11+
12+
# Import packages
13+
import os
14+
import cv2
15+
import numpy as np
16+
import tensorflow as tf
17+
import sys
18+
19+
# This is needed since the notebook is stored in the object_detection folder.
20+
sys.path.append("..")
21+
22+
from utils import label_map_util
23+
from utils import visualization_utils as vis_util
24+
25+
# Name of the directory containing the object detection module we're using
26+
MODEL_NAME = 'card_inference_graph'
27+
IMAGE_NAME = 'test3.jpg'
28+
29+
30+
# Grab path to current working directory
31+
CWD_PATH = os.getcwd()
32+
33+
# Path to frozen detection graph .pb file, which contains the model that is used
34+
# for object detection.
35+
PATH_TO_CKPT = os.path.join(CWD_PATH,MODEL_NAME,'frozen_inference_graph.pb')
36+
#ckpt_path = 'C://Users/Evan/Documents/Object_Detection_stuff/tensorflow/models/research/object_detection/raccoon_inference_graph/frozen_inference_graph.pb'
37+
#PATH_TO_CKPT = ckpt_path.encode('utf8')
38+
39+
# Path to label map file
40+
PATH_TO_LABELS = os.path.join(CWD_PATH,'training','card-labelmap.pbtxt')
41+
#PATH_TO_LABELS = os.path.join(CWD_PATH,MODEL_NAME,'object-detection.pbtxt')
42+
#label_path = 'C://Users/Evan/Documents/Object_Detection_stuff/tensorflow/models/research/object_detection/training/objectdetection.pbtxt'
43+
#PATH_TO_LABELS = label_path.encode('utf8')
44+
# Path to image
45+
PATH_TO_IMAGE = os.path.join(CWD_PATH,IMAGE_NAME)
46+
47+
# Number of classes the object detector can identify
48+
NUM_CLASSES = 6
49+
50+
## Load the label map.
51+
# Label maps map indices to category names, so that when our convolution
52+
# network predicts `5`, we know that this corresponds to `airplane`.
53+
# Here we use internal utility functions, but anything that returns a
54+
# dictionary mapping integers to appropriate string labels would be fine
55+
56+
# EVAN YOU NEED TO LOOK AT THESE FILES AND FIGURE OUT WHAT THEY'RE DOING BECAUSE
57+
# THIS SEEMS KIND OF EXCESSIVE
58+
59+
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
60+
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
61+
category_index = label_map_util.create_category_index(categories)
62+
63+
# Load the Tensorflow model into memory.
64+
# EVAN, the with statement basically makes it all close down after it's done
65+
# loading. Not sure what it does or means really.
66+
detection_graph = tf.Graph()
67+
with detection_graph.as_default():
68+
od_graph_def = tf.GraphDef()
69+
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
70+
serialized_graph = fid.read()
71+
od_graph_def.ParseFromString(serialized_graph)
72+
tf.import_graph_def(od_graph_def, name='')
73+
74+
sess = tf.Session(graph=detection_graph)
75+
76+
## EVAN, I think this section sort of defines what the outputs of the model
77+
## are going to be
78+
79+
## Define input and output tensors for detection_graph
80+
81+
# Input tensor is the image
82+
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
83+
84+
## Output tensors
85+
# Each box represents a part of the image where a particular object was detected
86+
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
87+
88+
# Each score represents level of confidence for each of the objects.
89+
# The score is shown on the result image, together with the class label.
90+
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
91+
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
92+
93+
# Number of objects detected
94+
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
95+
96+
# Load image, convert to RGB (which is needed by Tensorflow model), and
97+
# expand image dimensions to have shape: [1, None, None, 3]
98+
# i.e. a single-column array, where each item in the column has the pixel RGB value
99+
image = cv2.imread(PATH_TO_IMAGE)
100+
#image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
101+
image_rgb = image
102+
image_rgb_expanded = np.expand_dims(image_rgb, axis=0)
103+
104+
# Perform the actual detection by running the model with the image as input
105+
(boxes, scores, classes, num) = sess.run(
106+
[detection_boxes, detection_scores, detection_classes, num_detections],
107+
feed_dict={image_tensor: image_rgb_expanded})
108+
109+
# Draw the results of the detection (aka 'visulaize the results')
110+
## EVAN, you need to figure out what this STUPID FRICKIN visualization utility
111+
## is doing so you can get rid of it
112+
113+
vis_util.visualize_boxes_and_labels_on_image_array(
114+
image_rgb,
115+
np.squeeze(boxes),
116+
np.squeeze(classes).astype(np.int32),
117+
np.squeeze(scores),
118+
category_index,
119+
use_normalized_coordinates=True,
120+
line_thickness=8)
121+
122+
# All the results have been drawn on image_rgb. Convert back to BGR and display.
123+
#final_image = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2BGR)
124+
cv2.imshow('Ayy', image_rgb)
125+
126+
cv2.waitKey(0)
127+
cv2.destroyAllWindows()
128+

Object_detection_image.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
######## Image Object Detection Using Tensorflow-trained Classifier #########
2+
#
3+
# Author: Evan Juras
4+
# Date: 1/15/18
5+
# Description:
6+
# This program uses a TensorFlow-trained classifier to perform object detection.
7+
# It loads the classifier uses it to perform object detection on an image.
8+
# It draws boxes and scores around the objects of interest in the image.
9+
10+
## Some of the code is copied from Google's example at
11+
## https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb
12+
13+
## and some is copied from Dat Tran's example at
14+
## https://github.com/datitran/object_detector_app/blob/master/object_detection_app.py
15+
16+
## but I changed it to make it more understandable to me.
17+
18+
# Import packages
19+
import os
20+
import cv2
21+
import numpy as np
22+
import tensorflow as tf
23+
import sys
24+
25+
# This is needed since the notebook is stored in the object_detection folder.
26+
sys.path.append("..")
27+
28+
# Import utilites
29+
from utils import label_map_util
30+
from utils import visualization_utils as vis_util
31+
32+
# Name of the directory containing the object detection module we're using
33+
MODEL_NAME = 'inference_graph'
34+
IMAGE_NAME = 'test1.jpg'
35+
36+
# Grab path to current working directory
37+
CWD_PATH = os.getcwd()
38+
39+
# Path to frozen detection graph .pb file, which contains the model that is used
40+
# for object detection.
41+
PATH_TO_CKPT = os.path.join(CWD_PATH,MODEL_NAME,'frozen_inference_graph.pb')
42+
43+
# Path to label map file
44+
PATH_TO_LABELS = os.path.join(CWD_PATH,'training','labelmap.pbtxt')
45+
46+
# Path to image
47+
PATH_TO_IMAGE = os.path.join(CWD_PATH,IMAGE_NAME)
48+
49+
# Number of classes the object detector can identify
50+
NUM_CLASSES = 6
51+
52+
# Load the label map.
53+
# Label maps map indices to category names, so that when our convolution
54+
# network predicts `5`, we know that this corresponds to `king`.
55+
# Here we use internal utility functions, but anything that returns a
56+
# dictionary mapping integers to appropriate string labels would be fine
57+
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
58+
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
59+
category_index = label_map_util.create_category_index(categories)
60+
61+
# Load the Tensorflow model into memory.
62+
detection_graph = tf.Graph()
63+
with detection_graph.as_default():
64+
od_graph_def = tf.GraphDef()
65+
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
66+
serialized_graph = fid.read()
67+
od_graph_def.ParseFromString(serialized_graph)
68+
tf.import_graph_def(od_graph_def, name='')
69+
70+
sess = tf.Session(graph=detection_graph)
71+
72+
# Define input and output tensors (i.e. data) for the object detection classifier
73+
74+
# Input tensor is the image
75+
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
76+
77+
# Output tensors are the detection boxes, scores, and classes
78+
# Each box represents a part of the image where a particular object was detected
79+
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
80+
81+
# Each score represents level of confidence for each of the objects.
82+
# The score is shown on the result image, together with the class label.
83+
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
84+
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
85+
86+
# Number of objects detected
87+
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
88+
89+
# Load image using OpenCV and
90+
# expand image dimensions to have shape: [1, None, None, 3]
91+
# i.e. a single-column array, where each item in the column has the pixel RGB value
92+
image = cv2.imread(PATH_TO_IMAGE)
93+
image_expanded = np.expand_dims(image, axis=0)
94+
95+
# Perform the actual detection by running the model with the image as input
96+
(boxes, scores, classes, num) = sess.run(
97+
[detection_boxes, detection_scores, detection_classes, num_detections],
98+
feed_dict={image_tensor: image_expanded})
99+
100+
# Draw the results of the detection (aka 'visulaize the results')
101+
102+
vis_util.visualize_boxes_and_labels_on_image_array(
103+
image,
104+
np.squeeze(boxes),
105+
np.squeeze(classes).astype(np.int32),
106+
np.squeeze(scores),
107+
category_index,
108+
use_normalized_coordinates=True,
109+
line_thickness=8,
110+
min_score_thresh=0.80)
111+
112+
# All the results have been drawn on image. Now display the image.
113+
cv2.imshow('Object detector', image)
114+
115+
# Press any key to close the image
116+
cv2.waitKey(0)
117+
118+
# Clean up
119+
cv2.destroyAllWindows()

Object_detection_video.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
######## Video Object Detection Using Tensorflow-trained Classifier #########
2+
#
3+
# Author: Evan Juras
4+
# Date: 1/16/18
5+
# Description:
6+
# This program uses a TensorFlow-trained classifier to perform object detection.
7+
# It loads the classifier uses it to perform object detection on a video.
8+
# It draws boxes and scores around the objects of interest in each frame
9+
# of the video.
10+
11+
## Some of the code is copied from Google's example at
12+
## https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb
13+
14+
## and some is copied from Dat Tran's example at
15+
## https://github.com/datitran/object_detector_app/blob/master/object_detection_app.py
16+
17+
## but I changed it to make it more understandable to me.
18+
19+
# Import packages
20+
import os
21+
import cv2
22+
import numpy as np
23+
import tensorflow as tf
24+
import sys
25+
26+
# This is needed since the notebook is stored in the object_detection folder.
27+
sys.path.append("..")
28+
29+
# Import utilites
30+
from utils import label_map_util
31+
from utils import visualization_utils as vis_util
32+
33+
# Name of the directory containing the object detection module we're using
34+
MODEL_NAME = 'inference_graph'
35+
VIDEO_NAME = 'test.mov'
36+
37+
# Grab path to current working directory
38+
CWD_PATH = os.getcwd()
39+
40+
# Path to frozen detection graph .pb file, which contains the model that is used
41+
# for object detection.
42+
PATH_TO_CKPT = os.path.join(CWD_PATH,MODEL_NAME,'frozen_inference_graph.pb')
43+
44+
# Path to label map file
45+
PATH_TO_LABELS = os.path.join(CWD_PATH,'training','labelmap.pbtxt')
46+
47+
# Path to video
48+
PATH_TO_VIDEO = os.path.join(CWD_PATH,VIDEO_NAME)
49+
50+
# Number of classes the object detector can identify
51+
NUM_CLASSES = 6
52+
53+
# Load the label map.
54+
# Label maps map indices to category names, so that when our convolution
55+
# network predicts `5`, we know that this corresponds to `king`.
56+
# Here we use internal utility functions, but anything that returns a
57+
# dictionary mapping integers to appropriate string labels would be fine
58+
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
59+
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
60+
category_index = label_map_util.create_category_index(categories)
61+
62+
# Load the Tensorflow model into memory.
63+
detection_graph = tf.Graph()
64+
with detection_graph.as_default():
65+
od_graph_def = tf.GraphDef()
66+
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
67+
serialized_graph = fid.read()
68+
od_graph_def.ParseFromString(serialized_graph)
69+
tf.import_graph_def(od_graph_def, name='')
70+
71+
sess = tf.Session(graph=detection_graph)
72+
73+
# Define input and output tensors (i.e. data) for the object detection classifier
74+
75+
# Input tensor is the image
76+
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
77+
78+
# Output tensors are the detection boxes, scores, and classes
79+
# Each box represents a part of the image where a particular object was detected
80+
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
81+
82+
# Each score represents level of confidence for each of the objects.
83+
# The score is shown on the result image, together with the class label.
84+
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
85+
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
86+
87+
# Number of objects detected
88+
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
89+
90+
# Open video file
91+
video = cv2.VideoCapture(PATH_TO_VIDEO)
92+
93+
while(video.isOpened()):
94+
95+
# Acquire frame and expand frame dimensions to have shape: [1, None, None, 3]
96+
# i.e. a single-column array, where each item in the column has the pixel RGB value
97+
ret, frame = video.read()
98+
frame_expanded = np.expand_dims(frame, axis=0)
99+
100+
# Perform the actual detection by running the model with the image as input
101+
(boxes, scores, classes, num) = sess.run(
102+
[detection_boxes, detection_scores, detection_classes, num_detections],
103+
feed_dict={image_tensor: frame_expanded})
104+
105+
# Draw the results of the detection (aka 'visulaize the results')
106+
vis_util.visualize_boxes_and_labels_on_image_array(
107+
frame,
108+
np.squeeze(boxes),
109+
np.squeeze(classes).astype(np.int32),
110+
np.squeeze(scores),
111+
category_index,
112+
use_normalized_coordinates=True,
113+
line_thickness=8,
114+
min_score_thresh=0.80)
115+
116+
# All the results have been drawn on the frame, so it's time to display it.
117+
cv2.imshow('Object detector', frame)
118+
119+
# Press 'q' to quit
120+
if cv2.waitKey(1) == ord('q'):
121+
break
122+
123+
# Clean up
124+
video.release()
125+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)