python - Object Detection using Tensorflow -
i following tensorflow object detection tutorial oxford-iiit pets dataset: https://github.com/tensorflow/models/blob/master/object_detection/g3doc/running_pets.md
i have generated "frozen_inference_graph.pb" latest checkpoint. how can test inference graph - "frozen_inference_graph.pb" , pet labels - "pet_label_map.pbtxt" on image.
i have tried using jupytor notebook nothing gets detected in image. have used following python code detecting "dog" , "cat" nothing gets detected. python code given below:
import os import cv2 import time import argparse import multiprocessing import numpy np import tensorflow tf utils import fps, webcamvideostream multiprocessing import queue, pool object_detection.utils import label_map_util object_detection.utils import visualization_utils vis_util path_to_ckpt = os.path.join('frozen_inference_graph.pb') path_to_labels = os.path.join('pet_label_map.pbtxt') num_classes = 37 label_map = label_map_util.load_labelmap(path_to_labels) categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=num_classes, use_display_name=true) category_index = label_map_util.create_category_index(categories) def detect_objects(image_np, sess, detection_graph): # expand dimensions since model expects images have shape: [1, none, none, 3] image_np_expanded = np.expand_dims(image_np, axis=0) image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # each box represents part of image particular object detected. boxes = detection_graph.get_tensor_by_name('detection_boxes:0') # each score represent how level of confidence each of objects. # score shown on result image, class label. scores = detection_graph.get_tensor_by_name('detection_scores:0') classes = detection_graph.get_tensor_by_name('detection_classes:0') num_detections = detection_graph.get_tensor_by_name('num_detections:0') # actual detection. (boxes, scores, classes, num_detections) = sess.run( [boxes, scores, classes, num_detections], feed_dict={image_tensor: image_np_expanded}) # visualization of results of detection. vis_util.visualize_boxes_and_labels_on_image_array( image_np, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, use_normalized_coordinates=true, line_thickness=8) return image_np def worker(input_q, output_q): # load (frozen) tensorflow model memory. detection_graph = tf.graph() detection_graph.as_default(): od_graph_def = tf.graphdef() tf.gfile.gfile(path_to_ckpt, 'rb') fid: serialized_graph = fid.read() od_graph_def.parsefromstring(serialized_graph) tf.import_graph_def(od_graph_def, name='') sess = tf.session(graph=detection_graph) frame = input_q.get() output_q.put(detect_objects(frame, sess, detection_graph)) sess.close() if __name__ == '__main__': parser = argparse.argumentparser() parser.add_argument('-src', '--source', dest='video_source', type=int, default=0, help='device index of camera.') parser.add_argument('-wd', '--width', dest='width', type=int, default=20, help='width of frames in video stream.') parser.add_argument('-ht', '--height', dest='height', type=int, default=20, help='height of frames in video stream.') parser.add_argument('-num-w', '--num-workers', dest='num_workers', type=int, default=2, help='number of workers.') parser.add_argument('-q-size', '--queue-size', dest='queue_size', type=int, default=5, help='size of queue.') args = parser.parse_args() logger = multiprocessing.log_to_stderr() logger.setlevel(multiprocessing.subdebug) input_q = queue(maxsize=args.queue_size) output_q = queue(maxsize=args.queue_size) pool = pool(args.num_workers, worker, (input_q, output_q)) frame = cv2.imread("image2.jpg"); input_q.put(frame) cv2.imshow('video', output_q.get()) cv2.waitkey(0) cv2.destroyallwindows()
any appreciated related running inference graph on actual image or debugging if nothing gets detected.
what outputs of boxes, scores , classes? can print them? if numbers them, maybe need change few lines in code visualize results.
for test, can use:
vis_util.save_image_array_as_png(image,'./outputimg.png') #print(image.shape) print('image saved') img=mpimg.imread('./outputimg.png') imgplot = plt.imshow(img) plt.show()
Comments
Post a Comment