Ejemplo n.º 1
0
def image_processing(graph, category_index, image_file_name, show_video_window):

    img = cv2.imread(image_file_name)
    image_expanded = np.expand_dims(img, axis=0)

    with graph.as_default():
        ops = tf.get_default_graph().get_operations()
        all_tensor_names = {output.name for op in ops for output in op.outputs}
        tensor_dict = {}
        for key in [
            'num_detections', 'detection_boxes', 'detection_scores',
            'detection_classes', 'detection_masks'
        ]:
            tensor_name = key + ':0'
            if tensor_name in all_tensor_names:
                tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
                    tensor_name)
        with tf.Session() as sess:
            output_dict = run_inference_for_single_image(image_expanded, sess, tensor_dict)

            vis_utils.visualize_boxes_and_labels_on_image_array(
                img,
                output_dict['detection_boxes'],
                output_dict['detection_classes'],
                output_dict['detection_scores'],
                category_index,
                instance_masks=output_dict.get('detection_masks'),
                use_normalized_coordinates=True,
                line_thickness=4)

            if show_video_window:
                cv2.imshow('ppe', img)
                cv2.waitKey(5000)
Ejemplo n.º 2
0
    def detect(self, frame):
        frame_expanded = np.expand_dims(frame, axis=0)

        # Perform the actual detection by running the model with the image as input
        (boxes, scores, classes,
         num) = self.sess.run([self.db, self.ds, self.dc, self.nd],
                              feed_dict={self.it: 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),
            self.category_index,
            use_normalized_coordinates=True,
            line_thickness=self.line_thickness,
            min_score_thresh=self.min_score_thresh,
            max_boxes_to_draw=self.max_objects)

        # publish what we think we saw and the locations
        objects = list(zip(boxes, scores, classes))
        objects.sort(key=lambda x: x[1],
                     reverse=true)  # sort in ascending order
        recogniseable_objects = []
        for i, obj in enumerate(objects):
            if obj[1] < self.min_score_thresh or i >= self.max_objects:
                break
            recogniseable_objects.append(
                get_object_position(classes[i], boxes[i]))

        return recogniseable_objects
def image_output(outputs, ctx):
    detection_boxes = outputs["detection_boxes"].reshape([-1, 4])
    detection_scores = outputs["detection_scores"].reshape([-1])
    detection_classes = np.int32((outputs["detection_classes"])).reshape([-1])
    ctx.image = ctx.image.convert('RGB')
    image_arr = np.array(ctx.image)

    LOG.info(
        'Visualization params: threshold %f, line_thickness %d, max_boxes %d, skip_labels %r, skip_scores %r'
        % (ctx.threshold, ctx.line_thickness, ctx.max_boxes, ctx.skip_labels,
           ctx.skip_scores))

    vis_utils.visualize_boxes_and_labels_on_image_array(
        image_arr,
        detection_boxes,
        detection_classes,
        detection_scores,
        category_index,
        use_normalized_coordinates=True,
        max_boxes_to_draw=ctx.max_boxes,
        min_score_thresh=ctx.threshold,
        agnostic_mode=False,
        line_thickness=ctx.line_thickness,
        skip_labels=ctx.skip_labels,
        skip_scores=ctx.skip_scores,
    )
    from_arr = Image.fromarray(image_arr)
    image_bytes = io.BytesIO()
    from_arr.save(image_bytes, format='PNG')

    outputs['output'] = image_bytes.getvalue()
    return outputs
Ejemplo n.º 4
0
def main():
  parser = argparse.ArgumentParser()
  parser.add_argument("--url", help='The url to send the request')
  parser.add_argument("--input_image", default='image1.jpg')
  parser.add_argument("--output_image", default='output.jpg')
  args = parser.parse_args()

  img = Image.open(args.input_image)
  img = img.resize((WIDTH, HEIGHT), Image.ANTIALIAS)
  img_np = np.array(img)

  res = requests.post(
    args.url,
    data=json.dumps({"instances": [{"inputs": img_np.tolist()}]}))
  if res.status_code != 200:
    print('Failed: {}'.format(res.text))
    return

  output_dict = json.loads(res.text).get('predictions')[0]

  vis_util.visualize_boxes_and_labels_on_image_array(
    img_np,
    np.array(output_dict['detection_boxes']),
    map(int, output_dict['detection_classes']),
    output_dict['detection_scores'],
    {},
    instance_masks=output_dict.get('detection_masks'),
    use_normalized_coordinates=True,
    line_thickness=8)

  output_image = Image.fromarray(img_np)
  output_image.save(args.output_image)
Ejemplo n.º 5
0
def detect_rcnn():
    if flask.request.method == "POST":
        if flask.request.files.get("image"):
            image = Image.open(flask.request.files["image"])
            image_np = load_image_into_numpy_array(image)
            #image_np_expanded = np.expand_dims(image_np, axis=0)
            output_dict = run_inference_for_single_image(
                image_np, detection_graph)
            category_index = {0: {"name": "pothole"}, 1: {"name": "pothole"}}
            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'),
                use_normalized_coordinates=True,
                line_thickness=8,
                skip_scores=True,
                skip_labels=True)
            img = Image.fromarray(image_np.astype("uint8"))
            img = img.resize((128, 128))
            rawBytes = io.BytesIO()
            img.save(rawBytes, "JPEG")
            rawBytes.seek(0)
            return rawBytes.getvalue()
        else:
            return "Could not find image"
    return "Please use POST method"
Ejemplo n.º 6
0
def detection(image_np, cap):
    stime = time.time()  # 计算起始时间
    # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
    image_np_expanded = np.expand_dims(image_np, axis=0)
    # print('image_np_expanded', image_np_expanded.shape)
    # print('image_np_expanded.ndim', image_np_expanded.ndim)

    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
    # Each box represents a part of the image where a particular object was detected.
    boxes = 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 = 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 the results of a 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)
    cv2.imshow('object detection', cv2.resize(image_np, (800, 600)))
    fps = 1 / (time.time() - stime)
    process_time = time.time() - stime
    if cap == 1:
        return image_np, process_time
    else:
        return image_np, fps
    def detect_objects(self, frame, visualize=False):
        small_frame = frame

        height, width, depth = frame.shape
        frame_size = (width, height)

        rgb_small_frame = small_frame[:, :, ::-1]

        # self.reframe_mask( frame_size) //사이즈 유동적으로 할 때

        # Only process every other frame of video to save time
        if self.time_to_run_inference():
            self.output_dict = self.run_inference(rgb_small_frame)
        if visualize:
            vis_util.visualize_boxes_and_labels_on_image_array(
                frame,
                self.output_dict['detection_boxes'],
                self.output_dict['detection_classes'],
                self.output_dict['detection_scores'],
                self.category_index,
                instance_masks=self.output_dict.get('detection_masks'),
                use_normalized_coordinates=True,
                min_score_thresh=self.threshold,
                line_thickness=1)

        return frame
Ejemplo n.º 8
0
def detect_objects(image_np, sess, detection_graph):
    # Expand dimensions since the model expects images to have shape: [mscoco_label_map.pbtxt, 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 a part of the image where a particular object was detected.
    boxes = 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 = 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 the results of a 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
Ejemplo n.º 9
0
 def Detect(self, frame):
     self.frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
     self.frame_expanded = np.expand_dims(self.frame_rgb, axis=0)
     (boxes, scores, classes, num) = self.sess.run(
         [
             self.detection_boxes, self.detection_scores,
             self.detection_classes, self.num_detections
         ],
         feed_dict={self.image_tensor: self.frame_expanded})
     vis_util.visualize_boxes_and_labels_on_image_array(
         frame,
         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)
     scores = scores[0][:3]
     classes = classes[0][:3]
     name = ""
     seen = []
     for x in range(3):
         if (scores[x] > 0.6 and (not classes[x] in seen)):
             name += f'{self.map[classes[x]]}&'
             seen.append(classes[x])
     if (name):
         print('Object Detected saving image')
         self.Store_Search.SaveDetected(frame, name)
    def __displayDetectedImage(self, item):
        dcmFileName = item.data(0, Qt.UserRole)
        self.ui.statusBar.showMessage(dcmFileName)

        #预处理
        dcmFile = pydicom.read_file(dcmFileName)
        origin = dcmFile.pixel_array  # type:numpy.ndarray

        intercept = dcmFile.RescaleIntercept
        slope = dcmFile.RescaleSlope
        origin = origin * slope + intercept
        origin[origin < -100] = -100  # 去除低亮部分
        origin[origin > 750] = 750  # 去除高亮部分

        # 归一化到0-255
        origin = cv2.normalize(origin,
                               None,
                               0,
                               255,
                               norm_type=cv2.NORM_MINMAX,
                               dtype=cv2.CV_8UC3)
        image_np = np.expand_dims(origin, -1)  # 将origin增加一个维度
        image_np = np.repeat(image_np, 3, 2)  # 将二维矩阵重复三次

        #plt.imsave("output/undetected.jpg", image_np)

        NUM_CLASSES = 6
        PATH_TO_LABELS = 'label_map.pbtxt'
        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)

        result = item.data(1, Qt.UserRole)
        boxes = result[1]
        scores = result[2]
        classes = result[3]

        vis_util.visualize_boxes_and_labels_on_image_array(
            image_np,
            boxes,
            classes,
            scores,
            category_index,
            use_normalized_coordinates=True,
            line_thickness=1)

        #plt.imsave("output/detected.jpg", image_np)
        image = QImage(image_np, image_np.shape[1], image_np.shape[0],
                       QImage.Format_RGB888)

        self.curPixmap = QPixmap(image)
        self.on_actZoomFitH_triggered()

        self.ui.actZoomIn.setEnabled(True)
        self.ui.actZoomOut.setEnabled(True)
        self.ui.actZoomRealSize.setEnabled(True)
        self.ui.actZoomFitW.setEnabled(True)
        self.ui.actZoomFitH.setEnabled(True)
Ejemplo n.º 11
0
 def show_result_image(self, image_np, scores, boxes, classes):
     # Visualization of the results of a detection.
     import visualization_utils as vis_util
     from matplotlib import pyplot as plt
     vis_util.visualize_boxes_and_labels_on_image_array(
         image_np, boxes, classes.astype(np.int32),
         scores, self.category_index, use_normalized_coordinates=True,
         line_thickness=8)
     plt.figure(figsize=(12, 8))
     plt.imshow(image_np)
     plt.show()
Ejemplo n.º 12
0
def make_and_show_inference(img,
                            interpreter,
                            input_details,
                            output_details,
                            category_index,
                            nms=True,
                            score_thresh=0.6,
                            iou_thresh=0.5):
    """
    Generate and draw inference on image

    Parameters
    ----------
    img : Array of uint8
        Original Image to find predictions on.
    interpreter : tensorflow.lite.python.interpreter.Interpreter
        tflite model interpreter
    input_details : list
        input details of interpreter
    output_details : list
        output details of interpreter
    category_index : dict
        dictionary of labels
    nms : bool, optional
        To perform non-maximum suppression or not. The default is True.
    score_thresh : int, optional
        score above predicted class is accepted. The default is 0.6.
    iou_thresh : int, optional
        Intersection Over Union Threshold. The default is 0.5.

    Returns
    -------
    NONE
    """
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img_rgb = cv2.resize(img_rgb, (300, 300), cv2.INTER_AREA)
    img_rgb = img_rgb.reshape([1, 300, 300, 3])

    interpreter.set_tensor(input_details[0]['index'], img_rgb)
    interpreter.invoke()

    output_dict = get_output_dict(img_rgb, interpreter, output_details, nms,
                                  iou_thresh, score_thresh)
    # Visualization of the results of a detection.
    vis_util.visualize_boxes_and_labels_on_image_array(
        img,
        output_dict['detection_boxes'],
        output_dict['detection_classes'],
        output_dict['detection_scores'],
        category_index,
        use_normalized_coordinates=True,
        min_score_thresh=score_thresh,
        line_thickness=3)
Ejemplo n.º 13
0
def draw_boxes(frame, bundled_np, min_score_thresh=0.5):
    [boxes, classes, scores] = bundled_np
    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=min_score_thresh)
    return frame
Ejemplo n.º 14
0
 def draw_detection_result(self, image_np, boxes, classes, scores, category, predict_chars=None):
     vis_util.visualize_boxes_and_labels_on_image_array(
         image_np,
         np.squeeze(boxes),
         np.squeeze(classes).astype(np.int32),
         np.squeeze(scores),
         category,
         max_boxes_to_draw=100,
         use_normalized_coordinates=True,
         line_thickness=5,
         predict_chars=predict_chars
     )
Ejemplo n.º 15
0
    def get_classification(self, image):
        """Determines the color of the traffic light in the image

        Args:
            image (cv::Mat): image containing the traffic light

        Returns:
            int: ID of traffic light color (specified in styx_msgs/TrafficLight)

        """
        image_np_expanded = np.expand_dims(image, axis=0)
        # rospy.logwarn('path %s', self.path)

        with self.detection_graph.as_default():
            with tf.Session(graph=self.detection_graph) as sess:
                t0 = rospy.Time.now()
                out = self.sess.run([self.detection_boxes, self.detection_scores,
                                     self.detection_classes, self.num_detections],
                                    feed_dict={self.image_tensor: image_np_expanded})
                dt = rospy.Time.now() - t0
                rospy.loginfo('Classification CPU Time (s): %f', dt.to_sec())

        boxes, scores, classes, num = out

        # create np arrays
        boxes = np.squeeze(boxes)
        scores = np.squeeze(scores)
        classes = np.squeeze(classes).astype(np.int32)

        vis_util.visualize_boxes_and_labels_on_image_array(
            image, boxes, classes, scores,
            self.category_index,
            use_normalized_coordinates=True,
            line_thickness=4)

        if self.save_images == True and self.saved_image_counter <= self.saved_image_limit:
            if not (os.path.exists("./tl_images_infer")):
                os.mkdir("./tl_images_infer")
            cv2.imwrite("./tl_images_infer/infer_image{0:0>4}.jpeg".format(self.saved_image_counter),cv2.cvtColor(image,cv2.COLOR_RGB2BGR))
            self.saved_image_counter += 1

        self.current_light = TrafficLight.UNKNOWN

        if scores is not None and scores[0] > CLASSIFICATION_THRESHOLD:  # If highest score is above 50% it's a hit
            if classes[0] == 1:
                self.current_light = TrafficLight.RED
            elif classes[0] == 2:
                self.current_light = TrafficLight.YELLOW
            elif classes[0] == 3:
                self.current_light = TrafficLight.GREEN

        return self.current_light
Ejemplo n.º 16
0
def draw_boxes(config, frame: FrameType, bundled_data: bytes) -> FrameType:
    min_score_thresh = config.args['min_score_thresh']
    [boxes, classes, scores] = pickle.loads(bundled_data)
    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=min_score_thresh)
    return frame
 def visualize_detection_result(image_np, boxes, classes, scores, category):
     vis_util.visualize_boxes_and_labels_on_image_array(
         image_np,
         np.squeeze(boxes),
         np.squeeze(classes).astype(np.int32),
         np.squeeze(scores),
         category,
         max_boxes_to_draw=100,
         use_normalized_coordinates=True,
         line_thickness=5,
         min_score_thresh=.4,
         predict_chars=None)
     img_show = PIL.Image.fromarray(image_np)
     img_show.show()
Ejemplo n.º 18
0
def object_detaction_infer2(inference_url,imageFile,signature_name,jobParams):
    # method 1
    # image = Image.open(BytesIO(imageFile)).convert("RGB")
    # (im_width, im_height) = image.size
    # image_data = np.array(image.getdata()).reshape((im_height, im_width, 3)).astype(np.uint8)

    # method 2
    img_np_arr = np.frombuffer(imageFile, np.uint8)
    image_data = cv2.imdecode(img_np_arr, cv2.IMREAD_COLOR)

    image_data_yolo_list = image_data[np.newaxis, :].tolist()
    headers = {"Content-type": "application/json","Host":"{}-predictor-default.kfserving-pod.example.com".format(inference_url.split("/endpoints/v3/v1/models/")[1].split(":")[0])}
    service_ip = query_service_domain('istio-ingressgateway.istio-system.svc.cluster.local')
    inference_url = "http://{}/v1/models/".format(service_ip)+inference_url.split("/endpoints/v3/v1/models/")[1]
    r = requests.post(inference_url,headers=headers,
                      data=json.dumps({"signature_name": "serving_default","instances":image_data_yolo_list}),
                      )
    if r.status_code!=200:
        logging.error(r.content)
    r = r.json()
    output_dict = r['predictions'][0]
    class_name_path = re.sub("^/home", "/dlwsdata/work", os.path.join(jobParams["model_base_path"], "class_names.json"))
    if not os.path.exists(class_name_path):
        class_name_path = "/DLWorkspace/src/utils/coco.names"
    category_index = read_class_names2(class_name_path)
    if "logits" in output_dict:
        length = len(output_dict['logits'])
        return {"data":[[v["name"],output_dict["logits"][k-1] if k<=length else [v["name"],None] ]for k,v in category_index.items()],"type":"classify"}
    else:
        output_dict['num_detections'] = int(output_dict['num_detections'])
        output_dict['detection_classes'] = np.array([int(class_id) for class_id in output_dict['detection_classes']])
        output_dict['detection_boxes'] = np.array(output_dict['detection_boxes'])
        output_dict['detection_scores'] = np.array(output_dict['detection_scores'])
        visualization_utils.visualize_boxes_and_labels_on_image_array(
            image_data,
            output_dict['detection_boxes'],
            output_dict['detection_classes'],
            output_dict['detection_scores'],
            category_index,
            instance_masks=output_dict.get('detection_masks'),
            use_normalized_coordinates=True,
            line_thickness=1,
        )
        Image.fromarray(image_data).show()
        image = Image.fromarray(image_data)
        imgByteArr = io.BytesIO()
        image.save(imgByteArr,format='JPEG')
        imgByteArr = imgByteArr.getvalue()
        imgByteArr = base64.b64encode(imgByteArr)
        return {"data":imgByteArr,"type":"detection"}
Ejemplo n.º 19
0
 def process(self, image_np):
     with self.od_graph_def.as_default():
         with tf.Session(graph=self.graph) as sess:
             # Definite input and output Tensors for detection_graph
             image_tensor = detection_graph.get_tensor_by_name(
                 'image_tensor:0')
             # Each box represents a part of the image where a particular object was detected.
             detection_boxes = 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.
             detection_scores = detection_graph.get_tensor_by_name(
                 'detection_scores:0')
             detection_classes = detection_graph.get_tensor_by_name(
                 'detection_classes:0')
             num_detections = detection_graph.get_tensor_by_name(
                 'num_detections:0')
             # cap = cv2.VideoCapture(0)
             #window = cv2.namedWindow('frame')
             # Capture frame-by-frame
             # ret, image_np = cap.read()
             if image_np is None:
                 return
             # 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.
             (boxes, scores, classes,
              num) = sess.run([
                  detection_boxes, detection_scores, detection_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,
                 np.squeeze(boxes),
                 np.squeeze(classes).astype(np.int32),
                 np.squeeze(scores),
                 category_index,
                 use_normalized_coordinates=True,
                 line_thickness=4,
                 min_score_thresh=0.6)
             # Display the resulting frame
             cv2.imshow(self.frame, image_np)
             try:
                 self.image_pub.publish(
                     self.bridge.cv2_to_imgmsg(image_np, "bgr8"))
             except CvBridgeError as e:
                 print e
Ejemplo n.º 20
0
    def generate(image_tensor, boxes, scores, classes, num_detections):
        ret, frame = vid_source.read()
        # Peformance fix
        process_this_frame = True
        # tensor code
        while ret:

            if process_this_frame:

                #image_np = client._load_image_into_numpy_array(frame)
                image_np_expanded = np.expand_dims(frame, axis=0)

                (boxes_t, scores_t, classes_t,
                 num_detections_t) = client.sess.run(
                     [boxes, scores, classes, num_detections],
                     feed_dict={image_tensor: image_np_expanded})

                image, labels = vis_util.visualize_boxes_and_labels_on_image_array(
                    frame,
                    np.squeeze(boxes_t),
                    np.squeeze(classes_t).astype(np.int32),
                    np.squeeze(scores_t),
                    client.category_index,
                    use_normalized_coordinates=True,
                    line_thickness=8)
                # print(labels)
                speak_labels(labels)
                #image_pil = Image.fromarray(np.uint8(frame)).convert('RGB')

                payload = cv2.imencode('.jpg', frame)[1].tobytes()
                yield (b'--frame\r\n'
                       b'Content-Type: image/jpeg\r\n\r\n' + payload + b'\r\n')
            process_this_frame = not process_this_frame
            ret, frame = vid_source.read()
Ejemplo n.º 21
0
def gen_classified_image(trash_classifier, category_index, img, min_score_thresh):
    # Open image then convert to numpy array
    img_np = load_image_into_numpy_array(img)

    # Get bounding boxes
    (boxes, scores, classes, num) = trash_classifier.get_classification(img_np)

    # Draw bounding boxes on image
    img_out_np = visualize_boxes_and_labels_on_image_array(
        image = img_np,
        boxes = np.squeeze(boxes),
        classes = np.squeeze(classes),
        scores = np.squeeze(scores),
        category_index = category_index,
        use_normalized_coordinates = True,
        skip_labels = False,
        agnostic_mode = False,
        min_score_thresh=min_score_thresh
    )

    # Save image with bounding boxes
    img_out = Image.fromarray(img_out_np)

    byteio = io.BytesIO()
    img_out.save(byteio, format='jpeg')
    imbytes = byteio.getvalue()
    imageBytesEnc = bytearray(imbytes)
    score_vals = scores[0]
    detection_count = len(score_vals[score_vals > min_score_thresh])
    return imageBytesEnc, detection_count, num[0]
Ejemplo n.º 22
0
def img_inference(img_path):
    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            # Definite input and output Tensors for detection_graph
            image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
            # Each box represents a part of the image where a particular object was detected.
            detection_boxes = 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.
            detection_scores = detection_graph.get_tensor_by_name(
                'detection_scores:0')
            detection_classes = detection_graph.get_tensor_by_name(
                'detection_classes:0')
            num_detections = detection_graph.get_tensor_by_name(
                'num_detections:0')
            image = PIL.Image.open(img_path)
            image_np = load_image_into_numpy_array(image)
            image_np_expanded = np.expand_dims(image_np, axis=0)

            (boxes, scores, classes,
             num) = sess.run([
                 detection_boxes, detection_scores, detection_classes,
                 num_detections
             ],
                             feed_dict={image_tensor: image_np_expanded})

            pci = calculate_pci(scores, classes)
            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,
                min_score_thresh=0.5,
                use_normalized_coordinates=True,
                line_thickness=8)

            plt.figure(figsize=IMAGE_SIZE)
            plt.title('Pavement Condition Index (PCI): ' + str(round(pci)))
            plt.imshow(image_np)
            name = img_path.split('/')[-1].split('.')[0]

            op_name = PATH + '/imgs/' + name + '_OUTPUT.png'
            cv2.imwrite(op_name, image_np)
# pci = calculate_pci(boxes[0])
    return (op_name, pci)
Ejemplo n.º 23
0
    def get_classification(self, image):
        """Determines the color of the traffic light in the image

        Args:
            image (cv::Mat): image containing the traffic light

        Returns:
            int: ID of traffic light color (specified in styx_msgs/TrafficLight)

        """
        #TODO implement light color prediction

        image_np_expanded = np.expand_dims(image, axis=0)

        (boxes, scores, classes,
         num) = self.sess.run([
             self.detection_boxes, self.detection_scores,
             self.detection_classes, self.num_detections
         ],
                              feed_dict={self.image_tensor: image_np_expanded})

        boxes = np.squeeze(boxes)
        scores = np.squeeze(scores)
        classes = np.squeeze(classes).astype(np.int32)

        vis_util.visualize_boxes_and_labels_on_image_array(
            image,
            boxes,
            classes,
            scores,
            self.category_index,
            use_normalized_coordinates=True,
            line_thickness=6)
        self.boxed_image = image
        if (scores[0] > 0.2):
            rospy.logwarn("Classes: {}, Scores: {}".format(
                classes[0], scores[0]))
            if (classes[0] == 1):
                return TrafficLight.RED
            elif (classes[0] == 2):
                return TrafficLight.YELLOW
            elif (classes[0] == 3):
                return TrafficLight.GREEN

        return TrafficLight.UNKNOWN
Ejemplo n.º 24
0
def run_detection(test_image_path, threshold):
    with tf.Session() as sess:
        detection_graph = tf.get_default_graph()
        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
        detection_boxes = detection_graph.get_tensor_by_name(
            'detection_boxes:0')
        detection_scores = detection_graph.get_tensor_by_name(
            'detection_scores:0')
        detection_classes = detection_graph.get_tensor_by_name(
            'detection_classes:0')
        num_detections = detection_graph.get_tensor_by_name('num_detections:0')
        # weights = detection_graph.get_tensor_by_name('FeatureExtractor/MobilenetV1/Conv2d_0/weights:0')
        # print("images",test_image_path)
        for image_path in test_image_path:
            # print("images")
            image = Image.open(image_path)
            image_np = load_image_into_numpy_array(image)
            # print(np.shape(image_np))
            image_np_expanded = np.expand_dims(image_np, axis=0)
            # print(np.shape(image_np_expanded))
            (boxes, scores, classes,
             num) = sess.run([
                 detection_boxes, detection_scores, detection_classes,
                 num_detections
             ],
                             feed_dict={image_tensor: image_np_expanded})

            print("======================")
            print("Number of Detection ", num)
            print("Classes  : ", classes)
            print("Scores : ", scores)

            viz_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,
                min_score_thresh=threshold)
            plt.figure(figsize=IMAGE_SIZE)
            plt.imshow(image_np)
            plt.show()
Ejemplo n.º 25
0
 def _visualize_detections(self, image, boxes, classes, scores):
     # Visualization of the results of a detection.
     return vis_util.visualize_boxes_and_labels_on_image_array(
         image,
         boxes,
         classes,
         scores,
         self.category_index,
         use_normalized_coordinates=True,
         line_thickness=4)
Ejemplo n.º 26
0
def run_detection_camera():
    cam = cv2.VideoCapture(0)

    with tf.Session() as sess:
        detection_graph = tf.get_default_graph()
        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
        detection_boxes = detection_graph.get_tensor_by_name(
            'detection_boxes:0')
        detection_scores = detection_graph.get_tensor_by_name(
            'detection_scores:0')
        detection_classes = detection_graph.get_tensor_by_name(
            'detection_classes:0')
        num_detections = detection_graph.get_tensor_by_name('num_detections:0')
        # weights = detection_graph.get_tensor_by_name('FeatureExtractor/MobilenetV1/Conv2d_0/weights:0')

        while (True):
            ret, frame = cam.read()
            # image_np = load_image_into_numpy_array_frame(frame)
            # print(np.shape(image_np))
            image_np_expanded = np.expand_dims(frame, axis=0)
            (boxes, scores, classes,
             num) = sess.run([
                 detection_boxes, detection_scores, detection_classes,
                 num_detections
             ],
                             feed_dict={image_tensor: image_np_expanded})

            viz_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)

            cv2.imshow('Object Detection', frame)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

    cam.release()
    cv2.destroyAllWindows()
Ejemplo n.º 27
0
def main(_):
    # step 2: send a request
    options = [('grpc.max_send_message_length', 1000 * 1024 * 1024),
               ('grpc.max_receive_message_length', 1000 * 1024 * 1024)]
    channel = grpc.insecure_channel(FLAGS.server, options=options)
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
    request = predict_pb2.PredictRequest()
    request.model_spec.name = 'model'
    request.model_spec.signature_name = 'serving_default'

    # step 1: prepare input
    img = cv2.imread(FLAGS.image)
    h, w = img.shape[:2]
    if compress:
        ratio = w / h
        h1 = height
        w1 = round(h1 * ratio)
        scaled_img = cv2.resize(img, (w1, h1), interpolation=cv2.INTER_AREA)
        tensor = tf.contrib.util.make_tensor_proto(scaled_img,
                                                   shape=[1] +
                                                   list(scaled_img.shape))
    else:
        tensor = tf.contrib.util.make_tensor_proto(img,
                                                   shape=[1] + list(img.shape))

    request.inputs['inputs'].CopyFrom(tensor)
    start = time.time()

    # step 3: get the results
    result_future = stub.Predict.future(request, 10.0)  # 10 secs timeout
    result = result_future.result()

    stop = time.time()
    print('time is ', stop - start)

    NUM_CLASSES = 30
    label_map = label_map_util.load_labelmap('annotations/label_map.pbtxt')
    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)

    boxes = result.outputs['detection_boxes'].float_val
    classes = result.outputs['detection_classes'].float_val
    scores = result.outputs['detection_scores'].float_val

    result = vis_util.visualize_boxes_and_labels_on_image_array(
        img,
        np.reshape(boxes, [100, 4]),
        np.squeeze(classes).astype(np.int32),
        np.squeeze(scores),
        category_index,
        use_normalized_coordinates=True,
        line_thickness=8)

    cv2.imwrite('result.jpg', result)
Ejemplo n.º 28
0
def get_labeled_image(image_path, path_to_labels, num_classes, boxes, classes,
                      scores):
    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)
    image = Image.open(image_path)
    image_np = load_image_into_numpy_array(image)
    image_process = vis_util.visualize_boxes_and_labels_on_image_array(
        image_np, boxes, classes, scores, category_index)
    return image_process
def camThread():
    # Wait for a coherent pair of frames: depth and color
    frames = pipeline.wait_for_frames()
    depth_frame = frames.get_depth_frame()
    color_frame = frames.get_color_frame()
    if not depth_frame or not color_frame:
        return

    # Convert images to numpy arrays
    depth_image = np.asanyarray(depth_frame.get_data())
    color_image = np.asanyarray(color_frame.get_data())
    height = color_image.shape[0]
    width = color_image.shape[1]

    frame_expanded = np.expand_dims(color_image, 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')
    img = vis_util.visualize_boxes_and_labels_on_image_array(
        color_image,
        np.squeeze(boxes),
        np.squeeze(classes).astype(np.int32),
        np.squeeze(scores),
        category_index,
        use_normalized_coordinates=True,
        line_thickness=2,
        min_score_thresh=0.55,
        depth_frame=depth_frame,
        height=height,
        width=width)

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
                 GL_UNSIGNED_BYTE, cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glColor3f(1.0, 1.0, 1.0)
    glEnable(GL_TEXTURE_2D)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glBegin(GL_QUADS)
    glTexCoord2d(0.0, 1.0)
    glVertex3d(-1.0, -1.0, 0.0)
    glTexCoord2d(1.0, 1.0)
    glVertex3d(1.0, -1.0, 0.0)
    glTexCoord2d(1.0, 0.0)
    glVertex3d(1.0, 1.0, 0.0)
    glTexCoord2d(0.0, 0.0)
    glVertex3d(-1.0, 1.0, 0.0)
    glEnd()
    glFlush()
    glutSwapBuffers()
Ejemplo n.º 30
0
def _draw_detections(image_np, detections, category_index):
  """Draws detections on to the image.

  Args:
    image_np: Image in the form of uint8 numpy array.
    detections: a dictionary that contains the detection outputs.
    category_index: contains the mapping between indexes and the category names.

  Returns:
    Does not return anything but draws the boxes on the
  """
  vis_util.visualize_boxes_and_labels_on_image_array(
      image_np,
      detections['detection_boxes'],
      detections['detection_classes'],
      detections['detection_scores'],
      category_index,
      use_normalized_coordinates=True,
      max_boxes_to_draw=1000,
      min_score_thresh=.0,
      agnostic_mode=False)