def run(self): """The starting point for the thread.""" conf_thres = 0.3 nms_thres = 0.45 self.data = schema.annotation_file() self.data['analysts'].append('Machine Generated') self.model.to(self.device).eval() dataloader = load_images(self.image_directory, batch_size=1, img_size=self.image_size) for count, (img_path, img) in enumerate(dataloader): entry = schema.annotation_file_entry() with torch.no_grad(): image_name = os.path.split(img_path[0])[1] img = torch.from_numpy(img).unsqueeze(0).to(self.device) pred = self.model(img) pred = pred[pred[:, :, 4] > conf_thres] if pred: detections = non_max_suppression(pred.unsqueeze(0), conf_thres, nms_thres) entry = self.scale_detections(img_path[0], detections[0]) if entry['annotations']: self.data['images'][image_name] = entry self.progress.emit(count + 1, image_name, 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') 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 load_from_directory(self): """(Slot) Load image data from directory.""" if self.dirty_data_check(): directory = (QtWidgets.QFileDialog.getExistingDirectory( self, 'Select Directory', self.image_directory)) if directory != '': self.load_config(directory) self.image_directory = directory self.data = schema.annotation_file() self.populate_labels() self.mask = None self.load_image_list() self.pb_mask.setEnabled(True) self.pb_annotater.setEnabled(True) self.set_dirty(False) self.label_image_directory.setText(self.image_directory)
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)