def bbox_created(self, rect, show_assistant=True, meta=None): """(Slot) save the newly created bbox and display it.""" if rect.width() > 0 and rect.height() > 0: self.set_dirty(True) if self.current_file_name not in self.data['images']: template = schema.annotation_file_entry() self.data['images'][self.current_file_name] = template rec = self.data['images'][self.current_file_name] metadata = schema.annotation() metadata['created_by'] = 'human' metadata['bbox']['xmin'] = rect.left( ) / self.graphicsView.img_size[0] metadata['bbox']['xmax'] = rect.right( ) / self.graphicsView.img_size[0] metadata['bbox']['ymin'] = rect.top( ) / self.graphicsView.img_size[1] metadata['bbox']['ymax'] = rect.bottom( ) / self.graphicsView.img_size[1] if meta is not None: metadata['label'] = meta['label'] metadata['truncated'] = meta['truncated'] metadata['occluded'] = meta['occluded'] metadata['difficult'] = meta['difficult'] rec['annotations'].append(metadata) self.display_annotation_data() self.selected_row = self.tw_labels.rowCount() - 1 self.tw_labels.selectRow(self.selected_row) self.license.request() self.display_bboxes() if show_assistant: self.show_assistant()
def scale_detections(self, image, detections): img = cv2.imread(image) # The amount of padding that was added scale = self.image_size / max(img.shape) pad_x = max(img.shape[0] - img.shape[1], 0) * scale pad_y = max(img.shape[1] - img.shape[0], 0) * scale # Image height and width after padding is removed unpad_h = self.image_size - pad_y unpad_w = self.image_size - pad_x entry = schema.annotation_file_entry() for x_min, y_min, x_max, y_max, conf, cls_conf, cls_pred in detections: annotation = schema.annotation() annotation['created_by'] = 'machine' # Rescale coordinates to original dimensions box_h = ((y_max - y_min) / unpad_h) * img.shape[0] box_w = ((x_max - x_min) / unpad_w) * img.shape[1] y_min = (((y_min - pad_y // 2) / unpad_h) * img.shape[0]).round().item() x_min = (((x_min - pad_x // 2) / unpad_w) * img.shape[1]).round().item() x_max = (x_min + box_w).round().item() y_max = (y_min + box_h).round().item() x_min = max(x_min, 0) y_min = max(y_min, 0) x_max = max(x_max, 0) y_max = max(y_max, 0) annotation['bbox']['xmin'] = x_min / img.shape[1] annotation['bbox']['xmax'] = x_max / img.shape[1] annotation['bbox']['ymin'] = y_min / img.shape[0] annotation['bbox']['ymax'] = y_max / img.shape[0] annotation['label'] = self.classes[int(cls_pred.item())] entry['annotations'].append(annotation) return entry
def run(self): """The starting point for the thread.""" self.stop = False self.data = schema.annotation_file() self.data['analysts'].append('Machine Generated') if self.model is None: self.model = tf.saved_model.load(self.model_dir) self.model_loaded.emit() for count, img in enumerate(self.image_list): if count >= self.starting_image: if self.stop: break file_name = os.path.join(self.image_directory, img) if os.path.exists(file_name): image = Image.open(file_name) # 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 = np.array(image) # Expand dimensions since the model expects images # to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims(image_np, axis=0) # Actual detection. dets = self.model(image_np_expanded) entry = schema.annotation_file_entry() scores = dets['detection_scores'][0].numpy() boxes = dets['detection_boxes'][0].numpy() classes = dets['detection_classes'][0].numpy() for index, score in enumerate(scores): if score >= self.threshold: annotation = schema.annotation() annotation['created_by'] = 'machine' annotation['confidence'] = float(score) bbox = boxes[index] annotation['bbox']['xmin'] = float(bbox[1]) annotation['bbox']['xmax'] = float(bbox[3]) annotation['bbox']['ymin'] = float(bbox[0]) annotation['bbox']['ymax'] = float(bbox[2]) class_number = int(classes[index]) if class_number in self.label_map: label = self.label_map[class_number] else: label = 'unknown' annotation['label'] = label entry['annotations'].append(annotation) if len(entry['annotations']) > 0: self.data['images'][img] = entry image.close() self.progress.emit(count + 1, img, entry) self.finished.emit(self.data)
def run(self): """The starting point for the thread.""" self.stop = False self.data = schema.annotation_file() self.data['analysts'].append('Machine Generated') with self.detection_graph.as_default(): graph_def = self.detection_graph.as_graph_def() with tf.io.gfile.GFile(self.inference_graph, 'rb') as fid: serialized_graph = fid.read() graph_def.ParseFromString(serialized_graph) tf.import_graph_def(graph_def, name='') self.model_loaded.emit() with tf.Session(graph=self.detection_graph) as sess: # Definite input and output Tensors for detection_graph 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. d_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. d_scores = (self.detection_graph. get_tensor_by_name('detection_scores:0')) d_classes = (self.detection_graph. get_tensor_by_name('detection_classes:0')) num_detections = (self.detection_graph. get_tensor_by_name('num_detections:0')) for count, img in enumerate(self.image_list): if count >= self.starting_image: if self.stop: break file_name = os.path.join(self.image_directory, img) if os.path.exists(file_name): image = Image.open(file_name) # 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 = np.array(image) # Expand dimensions since the model expects images # to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims(image_np, axis=0) # Actual detection. fd = {image_tensor: image_np_expanded} (boxes, scores, classes, num) = sess.run([d_boxes, d_scores, d_classes, num_detections], feed_dict=fd) boxes = np.squeeze(boxes) scores = np.squeeze(scores) classes = np.squeeze(classes) entry = schema.annotation_file_entry() for i in range(len(scores)): if scores[i] >= self.threshold: annotation = schema.annotation() annotation['created_by'] = 'machine' annotation['confidence'] = float(scores[i]) bbox = boxes[i] annotation['bbox']['xmin'] = float(bbox[1]) annotation['bbox']['xmax'] = float(bbox[3]) annotation['bbox']['ymin'] = float(bbox[0]) annotation['bbox']['ymax'] = float(bbox[2]) if classes[i] in self.label_map: label = self.label_map[classes[i]] else: label = 'unknown' # label = self.category_index[classes[i]]['name'] annotation['label'] = label entry['annotations'].append(annotation) if len(entry['annotations']) > 0: self.data['images'][img] = entry image.close() self.progress.emit(count + 1, img, entry) self.finished.emit(self.data)
def run(self): """The starting point for the thread.""" self.data = schema.annotation_file() self.data['analysts'].append('Machine Generated') counter = 0 with self.detection_graph.as_default(): graph_def = self.detection_graph.as_graph_def() with tf.io.gfile.GFile(self.inference_graph, 'rb') as fid: serialized_graph = fid.read() graph_def.ParseFromString(serialized_graph) tf.import_graph_def(graph_def, name='') with tf.Session(graph=self.detection_graph) as sess: # Definite input and output Tensors for detection_graph # image_tensor = (self.detection_graph. # get_tensor_by_name('image_tensor:0')) #input_name = "image_tensor" input_name = "input_1" # input_1 image_tensor = (self.detection_graph.get_tensor_by_name( '{}:0'.format(input_name))) # TODO: automate with # https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/graph_transforms/README.md#inspecting-graphs #output_names = ["detection_boxes", "detection_scores", "detection_classes", "num_detections"] output_names = [ "filtered_detections/map/TensorArrayStack/TensorArrayGatherV3", "filtered_detections/map/TensorArrayStack_1/TensorArrayGatherV3", "filtered_detections/map/TensorArrayStack_2/TensorArrayGatherV3" ] # Each box represents a part of the image where a # particular object was detected. output_name = output_names[0] d_boxes = (self.detection_graph.get_tensor_by_name( '{}:0'.format(output_name))) # Each score represent how level of confidence for each of # the objects. Score is shown on the result image, # together with the class label. output_name = output_names[1] d_scores = (self.detection_graph.get_tensor_by_name( '{}:0'.format(output_name))) output_name = output_names[2] d_classes = (self.detection_graph.get_tensor_by_name( '{}:0'.format(output_name))) if (len(output_names) > 3): output_name = output_names[3] num_detections = (self.detection_graph.get_tensor_by_name( '{}:0'.format(output_name))) for img in self.image_list: file_name = os.path.join(self.image_directory, img) image = Image.open(file_name) # 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 = np.array(image, dtype="float32") height, width, channels = image_np.shape if (self.imagenet_normalize): image_np = imagenet_normalize(image_np) if (self.to_bgr): image_np = rgb_to_bgr(image_np) # Expand dimensions since the model expects images # to have shape: [1, None, None, 3] (channels last) image_np_expanded = np.expand_dims(image_np, axis=0) # Actual detection. if (len(output_names) == 3): fd = {image_tensor: image_np_expanded} (boxes, scores, classes) = sess.run([d_boxes, d_scores, d_classes], feed_dict=fd) elif (len(output_names) > 3): fd = {image_tensor: image_np_expanded} (boxes, scores, classes, num) = sess.run( [d_boxes, d_scores, d_classes, num_detections], feed_dict=fd) boxes = np.squeeze(boxes) scores = np.squeeze(scores) classes = np.squeeze(classes) #print(classes) #print(scores) entry = schema.annotation_file_entry() for i in range(len(scores)): if scores[i] >= self.threshold: annotation = schema.annotation() annotation['created_by'] = 'machine' bbox = boxes[i] # y first # annotation['bbox']['xmin'] = float(bbox[1]) # annotation['bbox']['xmax'] = float(bbox[3]) # annotation['bbox']['ymin'] = float(bbox[0]) # annotation['bbox']['ymax'] = float(bbox[2]) # x first annotation['bbox']['xmin'] = float(bbox[0]) / width annotation['bbox']['xmax'] = float(bbox[2]) / width annotation['bbox']['ymin'] = float( bbox[1]) / height annotation['bbox']['ymax'] = float( bbox[3]) / height if classes[i] in self.label_map: label = self.label_map[classes[i]] else: label = 'unknown' # label = self.category_index[classes[i]]['name'] annotation['label'] = label entry['annotations'].append(annotation) if len(entry['annotations']) > 0: # order from top left to bottom right entry['annotations'] = sorted( entry['annotations'], key=lambda x: x['bbox']['xmin']) entry['annotations'] = sorted( entry['annotations'], key=lambda x: x['bbox']['ymin']) self.data['images'][img] = entry image.close() counter += 1 self.progress.emit(counter, img, entry) self.finished.emit(self.data)