def detect(self, image): with self.detection_graph.as_default(): with tf.Session(graph=self.detection_graph) as sess: # Expand dimensions since the model expects images to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims(image, axis=0) 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. 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. scores = self.detection_graph.get_tensor_by_name( 'detection_scores:0') classes = self.detection_graph.get_tensor_by_name( 'detection_classes:0') num_detections = self.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 the results of a detection. vis_util.visualize_boxes_and_labels_on_image_array( image, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), self.category_index, use_normalized_coordinates=True, line_thickness=8) plt.imshow(image) plt.show()
def show_inference(model, image, category_index): # the array based representation of the image will be used later in order to prepare the # result image with boxes and labels on it. image_np = image # Actual detection. output_dict = run_inference_for_single_image(model, image_np) # Visualization of the results of a detection. vis_util.visualize_boxes_and_labels_on_image_array( image_np, output_dict['detection_boxes'], output_dict['detection_classes'], output_dict['detection_scores'], category_index, instance_masks=output_dict.get('detection_masks_reframed', None), use_normalized_coordinates=True, line_thickness=8) detected_objects = {} current = 0 while current < len(output_dict['detection_scores'] ) and output_dict['detection_scores'][current] > 0.5: current_obj_index = output_dict['detection_classes'][current] # int obj_name = category_index[current_obj_index]['name'] # string location = output_dict['detection_boxes'][ current] # pass in 1x4 np array x = int(np.average((location[1], location[3])) * image_np.shape[1]) y = int(np.average((location[0], location[2])) * image_np.shape[0]) location = (x, y) if obj_name not in detected_objects: detected_objects[obj_name] = [] detected_objects[obj_name].append(location) current += 1 # print(detected_objects) # imshow here # cv2.imshow('video', image_np) # cv2.waitKey(1) return detected_objects
ret = video.set(4, 720) while (True): ret, frame = video.read() frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frame_expanded = np.expand_dims(frame_rgb, axis=0) # Perform the actual detection by running the model with the image as input (boxes, scores, classes, num) = sess.run( [detection_boxes, detection_scores, detection_classes, num_detections], feed_dict={image_tensor: frame_expanded}) # Draw the results of the detection (aka 'visulaize the results') vis_util.visualize_boxes_and_labels_on_image_array( frame, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, use_normalized_coordinates=True, line_thickness=8, min_score_thresh=0.60) cv2.imshow("Object_detection", frame) print(image_tensor) print(detection_boxes) print(detection_classes) print(num_detections) if cv2.waitKey(1) == ord('q'): break video.release() cv2.destroyAllWindows()
def visualize_detection_results(result_dict, tag, global_step, categories, summary_dir='', export_dir='', agnostic_mode=False, show_groundtruth=False, groundtruth_box_visualization_color='black', min_score_thresh=.5, max_num_predictions=20, skip_scores=False, skip_labels=False, keep_image_id_for_visualization_export=False): """Visualizes detection results and writes visualizations to image summaries. This function visualizes an image with its detected bounding boxes and writes to image summaries which can be viewed on tensorboard. It optionally also writes images to a directory. In the case of missing entry in the label map, unknown class name in the visualization is shown as "N/A". Args: result_dict: a dictionary holding groundtruth and detection data corresponding to each image being evaluated. The following keys are required: 'original_image': a numpy array representing the image with shape [1, height, width, 3] or [1, height, width, 1] 'detection_boxes': a numpy array of shape [N, 4] 'detection_scores': a numpy array of shape [N] 'detection_classes': a numpy array of shape [N] The following keys are optional: 'groundtruth_boxes': a numpy array of shape [N, 4] 'groundtruth_keypoints': a numpy array of shape [N, num_keypoints, 2] Detections are assumed to be provided in decreasing order of score and for display, and we assume that scores are probabilities between 0 and 1. tag: tensorboard tag (string) to associate with image. global_step: global step at which the visualization are generated. categories: a list of dictionaries representing all possible categories. Each dict in this list has the following keys: 'id': (required) an integer id uniquely identifying this category 'name': (required) string representing category name e.g., 'cat', 'dog', 'pizza' 'supercategory': (optional) string representing the supercategory e.g., 'animal', 'vehicle', 'food', etc summary_dir: the output directory to which the image summaries are written. export_dir: the output directory to which images are written. If this is empty (default), then images are not exported. agnostic_mode: boolean (default: False) controlling whether to evaluate in class-agnostic mode or not. show_groundtruth: boolean (default: False) controlling whether to show groundtruth boxes in addition to detected boxes groundtruth_box_visualization_color: box color for visualizing groundtruth boxes min_score_thresh: minimum score threshold for a box to be visualized max_num_predictions: maximum number of detections to visualize skip_scores: whether to skip score when drawing a single detection skip_labels: whether to skip label when drawing a single detection keep_image_id_for_visualization_export: whether to keep image identifier in filename when exported to export_dir Raises: ValueError: if result_dict does not contain the expected keys (i.e., 'original_image', 'detection_boxes', 'detection_scores', 'detection_classes') """ detection_fields = fields.DetectionResultFields input_fields = fields.InputDataFields if not set([ input_fields.original_image, detection_fields.detection_boxes, detection_fields.detection_scores, detection_fields.detection_classes, ]).issubset(set(result_dict.keys())): raise ValueError('result_dict does not contain all expected keys.') if show_groundtruth and input_fields.groundtruth_boxes not in result_dict: raise ValueError( 'If show_groundtruth is enabled, result_dict must contain ' 'groundtruth_boxes.') tf.logging.info('Creating detection visualizations.') category_index = label_map_util.create_category_index(categories) image = np.squeeze(result_dict[input_fields.original_image], axis=0) if image.shape[2] == 1: # If one channel image, repeat in RGB. image = np.tile(image, [1, 1, 3]) detection_boxes = result_dict[detection_fields.detection_boxes] detection_scores = result_dict[detection_fields.detection_scores] detection_classes = np.int32( (result_dict[detection_fields.detection_classes])) detection_keypoints = result_dict.get(detection_fields.detection_keypoints) detection_masks = result_dict.get(detection_fields.detection_masks) detection_boundaries = result_dict.get( detection_fields.detection_boundaries) # Plot groundtruth underneath detections if show_groundtruth: groundtruth_boxes = result_dict[input_fields.groundtruth_boxes] groundtruth_keypoints = result_dict.get( input_fields.groundtruth_keypoints) vis_utils.visualize_boxes_and_labels_on_image_array( image=image, boxes=groundtruth_boxes, classes=None, scores=None, category_index=category_index, keypoints=groundtruth_keypoints, use_normalized_coordinates=False, max_boxes_to_draw=None, groundtruth_box_visualization_color= groundtruth_box_visualization_color) vis_utils.visualize_boxes_and_labels_on_image_array( image, detection_boxes, detection_classes, detection_scores, category_index, instance_masks=detection_masks, instance_boundaries=detection_boundaries, keypoints=detection_keypoints, use_normalized_coordinates=False, max_boxes_to_draw=max_num_predictions, min_score_thresh=min_score_thresh, agnostic_mode=agnostic_mode, skip_scores=skip_scores, skip_labels=skip_labels) if export_dir: if keep_image_id_for_visualization_export and result_dict[ fields.InputDataFields().key]: export_path = os.path.join( export_dir, 'export-{}-{}.png'.format( tag, result_dict[fields.InputDataFields().key])) else: export_path = os.path.join(export_dir, 'export-{}.png'.format(tag)) vis_utils.save_image_array_as_png(image, export_path) summary = tf.Summary(value=[ tf.Summary.Value(tag=tag, image=tf.Summary.Image( encoded_image_string=vis_utils. encode_image_array_as_png_str(image))) ]) summary_writer = tf.summary.FileWriterCache.get(summary_dir) summary_writer.add_summary(summary, global_step) tf.logging.info('Detection visualizations written to summary with tag %s.', tag)
def getPrediction(self): # Load the Tensorflow model into memory. # Load image using OpenCV and # expand image dimensions to have shape: [1, None, None, 3] # i.e. a single-column array, where each item in the column has the pixel RGB value sess = tf.Session(graph=self.detection_graph) image = cv2.imread(self.PATH_TO_IMAGE) image_expanded = np.expand_dims(image, axis=0) # Perform the actual detection by running the model with the image as input (boxes, scores, classes, num) = sess.run([ self.detection_boxes, self.detection_scores, self.detection_classes, self.num_detections ], feed_dict={self.image_tensor: image_expanded}) result = scores.flatten() res = [] for idx in range(0, len(result)): if result[idx] > .40: res.append(idx) top_classes = classes.flatten() # Selecting class 2 and 3 # top_classes = top_classes[top_classes > 1] res_list = [top_classes[i] for i in res] class_final_names = [self.class_names_mapping[x] for x in res_list] top_scores = [e for l2 in scores for e in l2 if e > 0.30] # final_output = list(zip(class_final_names, top_scores)) # print(final_output) # new_classes = classes.flatten() new_scores = scores.flatten() new_boxes = boxes.reshape(300, 4) # get all boxes from an array max_boxes_to_draw = new_boxes.shape[0] # this is set as a default but feel free to adjust it to your needs min_score_thresh = .30 # iterate over all objects found # boundingBox = {} # for i in range(min(max_boxes_to_draw, new_boxes.shape[0])): # if new_scores is None or new_scores[i] > min_score_thresh: # boundingBox[class_final_names[i]] = new_boxes[i] # print("Bounding Boxes of", class_final_names[i], new_boxes[i]) listOfOutput = [] for (name, score, i) in zip(class_final_names, top_scores, range(min(max_boxes_to_draw, new_boxes.shape[0]))): valDict = {} valDict["className"] = name valDict["confidence"] = str(score) if new_scores is None or new_scores[i] > min_score_thresh: val = list(new_boxes[i]) valDict["yMin"] = str(val[0]) valDict["xMin"] = str(val[1]) valDict["yMax"] = str(val[2]) valDict["xMax"] = str(val[3]) listOfOutput.append(valDict) # new_boxes = boxes.reshape(100,4) # print(new_boxes) # print(type(new_boxes)) # print(new_boxes.shape) # print(boxes.shape) # Draw the results of the detection (aka 'visulaize the results') vis_util.visualize_boxes_and_labels_on_image_array( image, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), self.category_index, use_normalized_coordinates=True, line_thickness=8, min_score_thresh=0.60) output_filename = 'output4.jpg' cv2.imwrite(output_filename, image) opencodedbase64 = encodeImageIntoBase64("output4.jpg") # json_image = dict(zip(img_dict, image_64_encode_list)) # print(open_output_image) # plt.savefig(PATH + '\\' + arr.split('.')[0] + '_labeled.jpg') # cv2.waitKey(0) # cv2.destroyAllWindows() # # # All the results have been drawn on image. Now display the image. #cv2.imshow('Object detector', image) # # # Press any key to close the image #cv2.waitKey(0) # # # Clean up #cv2.destroyAllWindows() listOfOutput.append({"image": opencodedbase64.decode('utf-8')}) return listOfOutput