EdjeElectronics / TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10

How to train a TensorFlow Object Detection Classifier for multiple object detection on Windows
Apache License 2.0
2.92k stars 1.3k forks source link

Extracting coordinates instead of drawing a box #69

Open psi43 opened 6 years ago

psi43 commented 6 years ago

EDIT: Nevermind, got it to work!

Hey, first off, great tutorial, thank you so much.

I got it to run on ubuntu 16.04 as well with ease but I have a problem. I'm running on a CLI Ubuntu server, so instead of using an image as output, I'd just like to have the coordinates of the boxes.

I looked into the Object_detection_image.py and found where the boxes are being drawn, but it uses a function named visualize_boxes_and_labels_on_image_array to draw them. If I try to ouput the np.squeeze(boxes), it returns this:

[[0.5897823  0.35585764 0.87036747 0.5124078 ]
 [0.6508235  0.13419046 0.85757935 0.2114587 ]
 [0.64070517 0.14992228 0.8580698  0.23488007]
 ...
 [0.         0.         0.         0.        ]
 [0.         0.         0.         0.        ]
 [0.         0.         0.         0.        ]]

Is there a way to just get the coordinates from that?

Thank you for your time!

EDIT: Okay, I added a new function to the visualization_utils.py that returns the "ymin, ymax, xmin, xmax" variables, used in other functions of that file to draw the boxes. The problem is, they look like this: [[0.5897822976112366, 0.8703674674034119, 0.35585764050483704, 0.5124077796936035], [0.6508234739303589, 0.8575793504714966, 0.13419045507907867, 0.2114586979150772]] I was expecting coordinates. These seem like percentages.

EDIT: Okay, I got it to work.

Sn0wl3r0ker commented 4 years ago

add this to the utils/visualization_utils.py

def return_coordinates(
    image,
    boxes,
    classes,
    scores,
    category_index,
    instance_masks=None,
    instance_boundaries=None,
    keypoints=None,
    use_normalized_coordinates=False,
    max_boxes_to_draw=20,
    min_score_thresh=.5,
    agnostic_mode=False,
    line_thickness=4,
    groundtruth_box_visualization_color='black',
    skip_scores=False,
    skip_labels=False):
  # Create a display string (and color) for every box location, group any boxes
  # that correspond to the same location.
  box_to_display_str_map = collections.defaultdict(list)
  box_to_color_map = collections.defaultdict(str)
  box_to_instance_masks_map = {}
  box_to_instance_boundaries_map = {}
  box_to_score_map = {}
  box_to_keypoints_map = collections.defaultdict(list)
  if not max_boxes_to_draw:
    max_boxes_to_draw = boxes.shape[0]
  for i in range(min(max_boxes_to_draw, boxes.shape[0])):
    if scores is None or scores[i] > min_score_thresh:
      box = tuple(boxes[i].tolist())
      if instance_masks is not None:
        box_to_instance_masks_map[box] = instance_masks[i]
      if instance_boundaries is not None:
        box_to_instance_boundaries_map[box] = instance_boundaries[i]
      if keypoints is not None:
        box_to_keypoints_map[box].extend(keypoints[i])
      if scores is None:
        box_to_color_map[box] = groundtruth_box_visualization_color
      else:
        display_str = ''
        if not skip_labels:
          if not agnostic_mode:
            if classes[i] in category_index.keys():
              class_name = category_index[classes[i]]['name']
            else:
              class_name = 'N/A'
            display_str = str(class_name)
        if not skip_scores:
          if not display_str:
            display_str = '{}%'.format(int(100*scores[i]))
          else:
            display_str = '{}: {}%'.format(display_str, int(100*scores[i]))
        box_to_display_str_map[box].append(display_str)
        box_to_score_map[box] = scores[i]
        if agnostic_mode:
          box_to_color_map[box] = 'DarkOrange'
        else:
          box_to_color_map[box] = STANDARD_COLORS[
              classes[i] % len(STANDARD_COLORS)]

  # Draw all boxes onto image.
  coordinates_list = []
  counter_for = 0
  for box, color in box_to_color_map.items():
    ymin, xmin, ymax, xmax = box
    height, width, channels = image.shape
    ymin = int(ymin*height)
    ymax = int(ymax*height)
    xmin = int(xmin*width)
    xmax = int(xmax*width)
    coordinates_list.append([ymin, ymax, xmin, xmax, (box_to_score_map[box]*100)])
    counter_for = counter_for + 1

  return coordinates_list

add this to Object_detection_dir.py

coordinates = vis_util.return_coordinates(
                        image,
                        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.80)

as well as this:

textfile = open("json/"+filename_string+".json", "a")
                    textfile.write(json.dumps(coordinates))
                    textfile.write("\n")

I think this should be all. Hi! This is not a issue.I add this simple loop to make the code detect all .JPG file the the testimg folder and export the .json file, img with boxes in json, json/testimg folder. Hope this can help someone who wants it.... P.S. i'm still a noob, so hope u can give me some advise

1.import some models:

import glob
import re

2.markdown default IMAGE PATH: "# IMAGE_NAME =" "# PATH_TO_IMAGE = os.path.join(CWD_PATH,IMAGE_NAME)"

3.And heres the change:

for filename in sorted(glob.glob("testimg/"+ "*.JPG")):
    print("\n")
    print("Start parsing "+filename+"...")
    image = cv2.imread(filename)
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image_expanded = np.expand_dims(image_rgb, axis=0)
    filenameJ = re.sub(r".*?\/", "", filename)
    open("json/"+filenameJ+".json", "a").write(json.dumps(coordinates)+"\n"

    for coordinate in coordinates:
         (y1, y2, x1, x2, acc, classification) = coordinate
         height = y2-y1
         width = x2-x1
         crop = image[y1:y1+height, x1:x1+width]
         cv2.imwrite("json/"+filename, image)
         print(coordinate)
jix1710 commented 4 years ago

I just solved the issue ... looks like the label is not updated in the for loop .. so if there are multiple labels in the same frame, it will return the latest one only ... I've edited the last few lines in vis_util.return_coordinates function to be like this:

# Draw all boxes onto image.
  coordinates_list = []
  counter_for = 0
  for box, color in box_to_color_map.items():
    ymin, xmin, ymax, xmax = box
    height, width, channels = image.shape
    ymin = int(ymin*height)
    ymax = int(ymax*height)
    xmin = int(xmin*width)
    xmax = int(xmax*width)
    coordinates_list.append([ymin, ymax, xmin, xmax, (box_to_score_map[box]*100),display_strs[counter_for]])
    counter_for = counter_for + 1

  return coordinates_list 

@psi43 There is a weird contradiction I came across when there is multi object detection ... I'm doing rock, paper, scissor detection ... so if I use vis_util.return_coordinates to return classes .. it will return 2 different coordinates .. but it will print the same class (which is wrong) .. but when it comes to using the drawing functionality in vis_util .. it will draw the 2 boxes, but each label is different (which is true) looks like the function doesn't take more than one class per frame note: ignore the false detection as I've trained on few images this is the detection: image

and this is the classification:

[231, 404, 352, 616, 99.99584555625916, 'scissor']
[159, 424, 33, 216, 92.08916425704956, 'scissor']

representing y1, y2, x1, x2, accuracy, classification respectively

hii my out IMAGENAME BOXCENTRLPOINT Y-MP C-A
0 cap_000 [[616.0] [821.0] 'VO: 87%'] this is my return coordinates . but i have to 'vo' in a separate column and accrue '87%' is separate column. how is it possible? How is change in visualization.until code?

psi43 commented 4 years ago

@jix1710 I'm sorry, but I don't understand what you're trying to ask :(.

jix1710 commented 4 years ago

@jix1710 I'm sorry, but I don't understand what you're trying to ask :(. 'VO: 87%' this coordinates to separate just ex== 'vo' ,'87%' ihave this type output

jix1710 commented 4 years ago

add this to the utils/visualization_utils.py

def return_coordinates(
    image,
    boxes,
    classes,
    scores,
    category_index,
    instance_masks=None,
    instance_boundaries=None,
    keypoints=None,
    use_normalized_coordinates=False,
    max_boxes_to_draw=20,
    min_score_thresh=.5,
    agnostic_mode=False,
    line_thickness=4,
    groundtruth_box_visualization_color='black',
    skip_scores=False,
    skip_labels=False):
  # Create a display string (and color) for every box location, group any boxes
  # that correspond to the same location.
  box_to_display_str_map = collections.defaultdict(list)
  box_to_color_map = collections.defaultdict(str)
  box_to_instance_masks_map = {}
  box_to_instance_boundaries_map = {}
  box_to_score_map = {}
  box_to_keypoints_map = collections.defaultdict(list)
  if not max_boxes_to_draw:
    max_boxes_to_draw = boxes.shape[0]
  for i in range(min(max_boxes_to_draw, boxes.shape[0])):
    if scores is None or scores[i] > min_score_thresh:
      box = tuple(boxes[i].tolist())
      if instance_masks is not None:
        box_to_instance_masks_map[box] = instance_masks[i]
      if instance_boundaries is not None:
        box_to_instance_boundaries_map[box] = instance_boundaries[i]
      if keypoints is not None:
        box_to_keypoints_map[box].extend(keypoints[i])
      if scores is None:
        box_to_color_map[box] = groundtruth_box_visualization_color
      else:
        display_str = ''
        if not skip_labels:
          if not agnostic_mode:
            if classes[i] in category_index.keys():
              class_name = category_index[classes[i]]['name']
            else:
              class_name = 'N/A'
            display_str = str(class_name)
        if not skip_scores:
          if not display_str:
            display_str = '{}%'.format(int(100*scores[i]))
          else:
            display_str = '{}: {}%'.format(display_str, int(100*scores[i]))
        box_to_display_str_map[box].append(display_str)
        box_to_score_map[box] = scores[i]
        if agnostic_mode:
          box_to_color_map[box] = 'DarkOrange'
        else:
          box_to_color_map[box] = STANDARD_COLORS[
              classes[i] % len(STANDARD_COLORS)]

  # Draw all boxes onto image.
  coordinates_list = []
  counter_for = 0
  for box, color in box_to_color_map.items():
    ymin, xmin, ymax, xmax = box
    height, width, channels = image.shape
    ymin = int(ymin*height)
    ymax = int(ymax*height)
    xmin = int(xmin*width)
    xmax = int(xmax*width)
    coordinates_list.append([ymin, ymax, xmin, xmax, (box_to_score_map[box]*100)])
    counter_for = counter_for + 1

  return coordinates_list

add this to Object_detection_dir.py

coordinates = vis_util.return_coordinates(
                        image,
                        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.80)

as well as this:

textfile = open("json/"+filename_string+".json", "a")
                    textfile.write(json.dumps(coordinates))
                    textfile.write("\n")

I think this should be all.

how is possible to find the object detection output data is right ya wrong?

jix1710 commented 4 years ago

How to call all Images open at once time ? in object detection modle

Sn0wl3r0ker commented 4 years ago

How to call all Images open at once time ? in object detection modle

Hey man! i don't know if it's right. But i think u can try my "for loop" method to call all images in the target folder.

add this to the utils/visualization_utils.py

def return_coordinates(
    image,
    boxes,
    classes,
    scores,
    category_index,
    instance_masks=None,
    instance_boundaries=None,
    keypoints=None,
    use_normalized_coordinates=False,
    max_boxes_to_draw=20,
    min_score_thresh=.5,
    agnostic_mode=False,
    line_thickness=4,
    groundtruth_box_visualization_color='black',
    skip_scores=False,
    skip_labels=False):
  # Create a display string (and color) for every box location, group any boxes
  # that correspond to the same location.
  box_to_display_str_map = collections.defaultdict(list)
  box_to_color_map = collections.defaultdict(str)
  box_to_instance_masks_map = {}
  box_to_instance_boundaries_map = {}
  box_to_score_map = {}
  box_to_keypoints_map = collections.defaultdict(list)
  if not max_boxes_to_draw:
    max_boxes_to_draw = boxes.shape[0]
  for i in range(min(max_boxes_to_draw, boxes.shape[0])):
    if scores is None or scores[i] > min_score_thresh:
      box = tuple(boxes[i].tolist())
      if instance_masks is not None:
        box_to_instance_masks_map[box] = instance_masks[i]
      if instance_boundaries is not None:
        box_to_instance_boundaries_map[box] = instance_boundaries[i]
      if keypoints is not None:
        box_to_keypoints_map[box].extend(keypoints[i])
      if scores is None:
        box_to_color_map[box] = groundtruth_box_visualization_color
      else:
        display_str = ''
        if not skip_labels:
          if not agnostic_mode:
            if classes[i] in category_index.keys():
              class_name = category_index[classes[i]]['name']
            else:
              class_name = 'N/A'
            display_str = str(class_name)
        if not skip_scores:
          if not display_str:
            display_str = '{}%'.format(int(100*scores[i]))
          else:
            display_str = '{}: {}%'.format(display_str, int(100*scores[i]))
        box_to_display_str_map[box].append(display_str)
        box_to_score_map[box] = scores[i]
        if agnostic_mode:
          box_to_color_map[box] = 'DarkOrange'
        else:
          box_to_color_map[box] = STANDARD_COLORS[
              classes[i] % len(STANDARD_COLORS)]

  # Draw all boxes onto image.
  coordinates_list = []
  counter_for = 0
  for box, color in box_to_color_map.items():
    ymin, xmin, ymax, xmax = box
    height, width, channels = image.shape
    ymin = int(ymin*height)
    ymax = int(ymax*height)
    xmin = int(xmin*width)
    xmax = int(xmax*width)
    coordinates_list.append([ymin, ymax, xmin, xmax, (box_to_score_map[box]*100)])
    counter_for = counter_for + 1

  return coordinates_list

add this to Object_detection_dir.py

coordinates = vis_util.return_coordinates(
                        image,
                        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.80)

as well as this:

textfile = open("json/"+filename_string+".json", "a")
                    textfile.write(json.dumps(coordinates))
                    textfile.write("\n")

I think this should be all. Hi! This is not a issue.I add this simple loop to make the code detect all .JPG file the the testimg folder and export the .json file, img with boxes in json, json/testimg folder. Hope this can help someone who wants it.... P.S. i'm still a noob, so hope u can give me some advise

1.import some models:

import glob
import re

2.markdown default IMAGE PATH: "# IMAGE_NAME =" "# PATH_TO_IMAGE = os.path.join(CWD_PATH,IMAGE_NAME)"

3.And heres the change:

for filename in sorted(glob.glob("testimg/"+ "*.JPG")):
  print("\n")
  print("Start parsing "+filename+"...")
  image = cv2.imread(filename)
  image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  image_expanded = np.expand_dims(image_rgb, axis=0)
  filenameJ = re.sub(r".*?\/", "", filename)
  open("json/"+filenameJ+".json", "a").write(json.dumps(coordinates)+"\n"

  for coordinate in coordinates:
       (y1, y2, x1, x2, acc, classification) = coordinate
       height = y2-y1
       width = x2-x1
       crop = image[y1:y1+height, x1:x1+width]
       cv2.imwrite("json/"+filename, image)
       print(coordinate)
jix1710 commented 4 years ago

thx bro .And heres the change:

for filename in sorted(glob.glob("testimg/"+ "*.JPG")):

print("\n")

print("Start parsing "+filename+"...")

image = cv2.imread(filename)

image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

image_expanded = np.expand_dims(image_rgb, axis=0)

this is run for me but how all image show after detection? bs of ths folder in 30 image but only one image is showme output

On Wed, 15 Jul 2020 at 10:41, Sn0wl3r0ker notifications@github.com wrote:

如何一次調用所有圖像? 在物體檢測模型中

Hey man! i don't know if it's right. But i think u can try my "for loop" method to call all images in the target folder.

add this to the utils/visualization_utils.py

def return_coordinates(

image,

boxes,

classes,

scores,

category_index,

instance_masks=None,

instance_boundaries=None,

keypoints=None,

use_normalized_coordinates=False,

max_boxes_to_draw=20,

min_score_thresh=.5,

agnostic_mode=False,

line_thickness=4,

groundtruth_box_visualization_color='black',

skip_scores=False,

skip_labels=False):

Create a display string (and color) for every box location, group any boxes

that correspond to the same location.

box_to_display_str_map = collections.defaultdict(list)

box_to_color_map = collections.defaultdict(str)

box_to_instance_masks_map = {}

box_to_instance_boundaries_map = {}

box_to_score_map = {}

box_to_keypoints_map = collections.defaultdict(list)

if not max_boxes_to_draw:

max_boxes_to_draw = boxes.shape[0]

for i in range(min(max_boxes_to_draw, boxes.shape[0])):

if scores is None or scores[i] > min_score_thresh:

  box = tuple(boxes[i].tolist())

  if instance_masks is not None:

    box_to_instance_masks_map[box] = instance_masks[i]

  if instance_boundaries is not None:

    box_to_instance_boundaries_map[box] = instance_boundaries[i]

  if keypoints is not None:

    box_to_keypoints_map[box].extend(keypoints[i])

  if scores is None:

    box_to_color_map[box] = groundtruth_box_visualization_color

  else:

    display_str = ''

    if not skip_labels:

      if not agnostic_mode:

        if classes[i] in category_index.keys():

          class_name = category_index[classes[i]]['name']

        else:

          class_name = 'N/A'

        display_str = str(class_name)

    if not skip_scores:

      if not display_str:

        display_str = '{}%'.format(int(100*scores[i]))

      else:

        display_str = '{}: {}%'.format(display_str, int(100*scores[i]))

    box_to_display_str_map[box].append(display_str)

    box_to_score_map[box] = scores[i]

    if agnostic_mode:

      box_to_color_map[box] = 'DarkOrange'

    else:

      box_to_color_map[box] = STANDARD_COLORS[

          classes[i] % len(STANDARD_COLORS)]

Draw all boxes onto image.

coordinates_list = []

counter_for = 0

for box, color in box_to_color_map.items():

ymin, xmin, ymax, xmax = box

height, width, channels = image.shape

ymin = int(ymin*height)

ymax = int(ymax*height)

xmin = int(xmin*width)

xmax = int(xmax*width)

coordinates_list.append([ymin, ymax, xmin, xmax, (box_to_score_map[box]*100)])

counter_for = counter_for + 1

return coordinates_list

add this to Object_detection_dir.py

coordinates = vis_util.return_coordinates(

                    image,

                    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.80)

as well as this:

textfile = open("json/"+filename_string+".json", "a")

                textfile.write(json.dumps(coordinates))

                textfile.write("\n")

I think this should be all. Hi! This is not a issue.I add this simple loop to make the code detect all .JPG file the the testimg folder and export the .json file, img with boxes in json, json/testimg folder. Hope this can help someone who wants it.... P.S. i'm still a noob, so hope u can give me some advise

1.import some models:

import glob

import re

2.markdown default IMAGE PATH: "# IMAGE_NAME =" "# PATH_TO_IMAGE = os.path.join(CWD_PATH,IMAGE_NAME)"

3.And heres the change:

for filename in sorted(glob.glob("testimg/"+ "*.JPG")):

print("\n")

print("Start parsing "+filename+"...")

image = cv2.imread(filename)

image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

image_expanded = np.expand_dims(image_rgb, axis=0)

filenameJ = re.sub(r".*?\/", "", filename)

open("json/"+filenameJ+".json", "a").write(json.dumps(coordinates)+"\n"

for coordinate in coordinates:

   (y1, y2, x1, x2, acc, classification) = coordinate

   height = y2-y1

   width = x2-x1

   crop = image[y1:y1+height, x1:x1+width]

   cv2.imwrite("json/"+filename, image)

   print(coordinate)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/EdjeElectronics/TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10/issues/69#issuecomment-658549575, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOQW3D2SG7CMQSCAR3KNKH3R3U3BPANCNFSM4FFF37GQ .

jix1710 commented 4 years ago

Start parsing test_images\Cap_6.jpg...

Start parsing test_images\Cap_7.jpg...

Start parsing test_images\Cap_8.jpg...

Start parsing test_images\Cap_9.jpg... [502.5, 864.0, 'VO: 99%']

how is all image output cap_9 type?

On Wed, 15 Jul 2020 at 16:13, jix italiya jlitaliya82@gmail.com wrote:

thx bro .And heres the change:

for filename in sorted(glob.glob("testimg/"+ "*.JPG")):

print("\n")

print("Start parsing "+filename+"...")

image = cv2.imread(filename)

image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

image_expanded = np.expand_dims(image_rgb, axis=0)

this is run for me but how all image show after detection? bs of ths folder in 30 image but only one image is showme output

On Wed, 15 Jul 2020 at 10:41, Sn0wl3r0ker notifications@github.com wrote:

如何一次調用所有圖像? 在物體檢測模型中

Hey man! i don't know if it's right. But i think u can try my "for loop" method to call all images in the target folder.

add this to the utils/visualization_utils.py

def return_coordinates(

image,

boxes,

classes,

scores,

category_index,

instance_masks=None,

instance_boundaries=None,

keypoints=None,

use_normalized_coordinates=False,

max_boxes_to_draw=20,

min_score_thresh=.5,

agnostic_mode=False,

line_thickness=4,

groundtruth_box_visualization_color='black',

skip_scores=False,

skip_labels=False):

Create a display string (and color) for every box location, group any boxes

that correspond to the same location.

box_to_display_str_map = collections.defaultdict(list)

box_to_color_map = collections.defaultdict(str)

box_to_instance_masks_map = {}

box_to_instance_boundaries_map = {}

box_to_score_map = {}

box_to_keypoints_map = collections.defaultdict(list)

if not max_boxes_to_draw:

max_boxes_to_draw = boxes.shape[0]

for i in range(min(max_boxes_to_draw, boxes.shape[0])):

if scores is None or scores[i] > min_score_thresh:

  box = tuple(boxes[i].tolist())

  if instance_masks is not None:

    box_to_instance_masks_map[box] = instance_masks[i]

  if instance_boundaries is not None:

    box_to_instance_boundaries_map[box] = instance_boundaries[i]

  if keypoints is not None:

    box_to_keypoints_map[box].extend(keypoints[i])

  if scores is None:

    box_to_color_map[box] = groundtruth_box_visualization_color

  else:

    display_str = ''

    if not skip_labels:

      if not agnostic_mode:

        if classes[i] in category_index.keys():

          class_name = category_index[classes[i]]['name']

        else:

          class_name = 'N/A'

        display_str = str(class_name)

    if not skip_scores:

      if not display_str:

        display_str = '{}%'.format(int(100*scores[i]))

      else:

        display_str = '{}: {}%'.format(display_str, int(100*scores[i]))

    box_to_display_str_map[box].append(display_str)

    box_to_score_map[box] = scores[i]

    if agnostic_mode:

      box_to_color_map[box] = 'DarkOrange'

    else:

      box_to_color_map[box] = STANDARD_COLORS[

          classes[i] % len(STANDARD_COLORS)]

Draw all boxes onto image.

coordinates_list = []

counter_for = 0

for box, color in box_to_color_map.items():

ymin, xmin, ymax, xmax = box

height, width, channels = image.shape

ymin = int(ymin*height)

ymax = int(ymax*height)

xmin = int(xmin*width)

xmax = int(xmax*width)

coordinates_list.append([ymin, ymax, xmin, xmax, (box_to_score_map[box]*100)])

counter_for = counter_for + 1

return coordinates_list

add this to Object_detection_dir.py

coordinates = vis_util.return_coordinates(

                    image,

                    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.80)

as well as this:

textfile = open("json/"+filename_string+".json", "a")

                textfile.write(json.dumps(coordinates))

                textfile.write("\n")

I think this should be all. Hi! This is not a issue.I add this simple loop to make the code detect all .JPG file the the testimg folder and export the .json file, img with boxes in json, json/testimg folder. Hope this can help someone who wants it.... P.S. i'm still a noob, so hope u can give me some advise

1.import some models:

import glob

import re

2.markdown default IMAGE PATH: "# IMAGE_NAME =" "# PATH_TO_IMAGE = os.path.join(CWD_PATH,IMAGE_NAME)"

3.And heres the change:

for filename in sorted(glob.glob("testimg/"+ "*.JPG")):

print("\n")

print("Start parsing "+filename+"...")

image = cv2.imread(filename)

image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

image_expanded = np.expand_dims(image_rgb, axis=0)

filenameJ = re.sub(r".*?\/", "", filename)

open("json/"+filenameJ+".json", "a").write(json.dumps(coordinates)+"\n"

for coordinate in coordinates:

  (y1, y2, x1, x2, acc, classification) = coordinate

  height = y2-y1

  width = x2-x1

  crop = image[y1:y1+height, x1:x1+width]

  cv2.imwrite("json/"+filename, image)

  print(coordinate)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/EdjeElectronics/TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10/issues/69#issuecomment-658549575, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOQW3D2SG7CMQSCAR3KNKH3R3U3BPANCNFSM4FFF37GQ .

jix1710 commented 4 years ago

Hi everyone, I have some problems, basically i am making a project for smart refrigerator where i use object detection to know what's inside the fridge, i use two classes for a start, bottle and can, I use 1 bottle and 1 can as sample, thankfully it can classify bottle and can in the frame/image but my prob is that for the code below:

coordinates_list.append([ymin, ymax, xmin, xmax, (box_to_score_map[box]*100), str(class_name)

for the class name when it show this:

[358, 705, 990, 1256, 99.93228912353516, 'Bottle'] [341, 708, 284, 469, 99.69232678413391, 'Bottle']

I think it overwrite the class for can, if i remove the bottle out from the frame/image, then only the 'Can' shows at the class_name

I attached an image for the bottle and can detection Thank you in advance result

how to solve this problem? because of same problem with me? any solution

jix1710 commented 4 years ago

I just solved the issue ... looks like the label is not updated in the for loop .. so if there are multiple labels in the same frame, it will return the latest one only ... I've edited the last few lines in vis_util.return_coordinates function to be like this:

# Draw all boxes onto image.
  coordinates_list = []
  counter_for = 0
  for box, color in box_to_color_map.items():
    ymin, xmin, ymax, xmax = box
    height, width, channels = image.shape
    ymin = int(ymin*height)
    ymax = int(ymax*height)
    xmin = int(xmin*width)
    xmax = int(xmax*width)
    coordinates_list.append([ymin, ymax, xmin, xmax, (box_to_score_map[box]*100),display_strs[counter_for]])
    counter_for = counter_for + 1

  return coordinates_list 

how to solve this problem ? i have use this code ([ymin, ymax, xmin, xmax, (box_to_score_map[box]*100),display_strs[counter_for]]) but output coordinates same class name not to multi class find

jix1710 commented 4 years ago

i have collected some 300 different classes of objects. But when I run the program, only 1 images were detected, the remaining 299 images were not detected. and only show 1 ige coordinates Please let me know, why the rest of the images were not detecting.. REPLY

On Wed, 15 Jul 2020 at 16:33, jix italiya jlitaliya82@gmail.com wrote:

Start parsing test_images\Cap_6.jpg...

Start parsing test_images\Cap_7.jpg...

Start parsing test_images\Cap_8.jpg...

Start parsing test_images\Cap_9.jpg... [502.5, 864.0, 'VO: 99%']

how is all image output cap_9 type?

On Wed, 15 Jul 2020 at 16:13, jix italiya jlitaliya82@gmail.com wrote:

thx bro .And heres the change:

for filename in sorted(glob.glob("testimg/"+ "*.JPG")):

print("\n")

print("Start parsing "+filename+"...")

image = cv2.imread(filename)

image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

image_expanded = np.expand_dims(image_rgb, axis=0)

this is run for me but how all image show after detection? bs of ths folder in 30 image but only one image is showme output

On Wed, 15 Jul 2020 at 10:41, Sn0wl3r0ker notifications@github.com wrote:

如何一次調用所有圖像? 在物體檢測模型中

Hey man! i don't know if it's right. But i think u can try my "for loop" method to call all images in the target folder.

add this to the utils/visualization_utils.py

def return_coordinates(

image,

boxes,

classes,

scores,

category_index,

instance_masks=None,

instance_boundaries=None,

keypoints=None,

use_normalized_coordinates=False,

max_boxes_to_draw=20,

min_score_thresh=.5,

agnostic_mode=False,

line_thickness=4,

groundtruth_box_visualization_color='black',

skip_scores=False,

skip_labels=False):

Create a display string (and color) for every box location, group any boxes

that correspond to the same location.

box_to_display_str_map = collections.defaultdict(list)

box_to_color_map = collections.defaultdict(str)

box_to_instance_masks_map = {}

box_to_instance_boundaries_map = {}

box_to_score_map = {}

box_to_keypoints_map = collections.defaultdict(list)

if not max_boxes_to_draw:

max_boxes_to_draw = boxes.shape[0]

for i in range(min(max_boxes_to_draw, boxes.shape[0])):

if scores is None or scores[i] > min_score_thresh:

  box = tuple(boxes[i].tolist())

  if instance_masks is not None:

    box_to_instance_masks_map[box] = instance_masks[i]

  if instance_boundaries is not None:

    box_to_instance_boundaries_map[box] = instance_boundaries[i]

  if keypoints is not None:

    box_to_keypoints_map[box].extend(keypoints[i])

  if scores is None:

    box_to_color_map[box] = groundtruth_box_visualization_color

  else:

    display_str = ''

    if not skip_labels:

      if not agnostic_mode:

        if classes[i] in category_index.keys():

          class_name = category_index[classes[i]]['name']

        else:

          class_name = 'N/A'

        display_str = str(class_name)

    if not skip_scores:

      if not display_str:

        display_str = '{}%'.format(int(100*scores[i]))

      else:

        display_str = '{}: {}%'.format(display_str, int(100*scores[i]))

    box_to_display_str_map[box].append(display_str)

    box_to_score_map[box] = scores[i]

    if agnostic_mode:

      box_to_color_map[box] = 'DarkOrange'

    else:

      box_to_color_map[box] = STANDARD_COLORS[

          classes[i] % len(STANDARD_COLORS)]

Draw all boxes onto image.

coordinates_list = []

counter_for = 0

for box, color in box_to_color_map.items():

ymin, xmin, ymax, xmax = box

height, width, channels = image.shape

ymin = int(ymin*height)

ymax = int(ymax*height)

xmin = int(xmin*width)

xmax = int(xmax*width)

coordinates_list.append([ymin, ymax, xmin, xmax, (box_to_score_map[box]*100)])

counter_for = counter_for + 1

return coordinates_list

add this to Object_detection_dir.py

coordinates = vis_util.return_coordinates(

                    image,

                    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.80)

as well as this:

textfile = open("json/"+filename_string+".json", "a")

                textfile.write(json.dumps(coordinates))

                textfile.write("\n")

I think this should be all. Hi! This is not a issue.I add this simple loop to make the code detect all .JPG file the the testimg folder and export the .json file, img with boxes in json, json/testimg folder. Hope this can help someone who wants it.... P.S. i'm still a noob, so hope u can give me some advise

1.import some models:

import glob

import re

2.markdown default IMAGE PATH: "# IMAGE_NAME =" "# PATH_TO_IMAGE = os.path.join(CWD_PATH,IMAGE_NAME)"

3.And heres the change:

for filename in sorted(glob.glob("testimg/"+ "*.JPG")):

print("\n")

print("Start parsing "+filename+"...")

image = cv2.imread(filename)

image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

image_expanded = np.expand_dims(image_rgb, axis=0)

filenameJ = re.sub(r".*?\/", "", filename)

open("json/"+filenameJ+".json", "a").write(json.dumps(coordinates)+"\n"

for coordinate in coordinates:

     (y1, y2, x1, x2, acc, classification) = coordinate

     height = y2-y1

     width = x2-x1

     crop = image[y1:y1+height, x1:x1+width]

     cv2.imwrite("json/"+filename, image)

     print(coordinate)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/EdjeElectronics/TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10/issues/69#issuecomment-658549575, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOQW3D2SG7CMQSCAR3KNKH3R3U3BPANCNFSM4FFF37GQ .

jix1710 commented 4 years ago

hiii sir plz help me

"for filename in sorted(glob.glob("testing/"+ "*.JPG")):"any idla sorted function use any other functions use? because of sorted functions use to list of short...i have no use sorted functions..use any another functions?

On Mon, 17 Aug 2020 at 10:37, jix italiya jlitaliya82@gmail.com wrote:

i have collected some 300 different classes of objects. But when I run the program, only 1 images were detected, the remaining 299 images were not detected. and only show 1 ige coordinates Please let me know, why the rest of the images were not detecting.. REPLY

On Wed, 15 Jul 2020 at 16:33, jix italiya jlitaliya82@gmail.com wrote:

Start parsing test_images\Cap_6.jpg...

Start parsing test_images\Cap_7.jpg...

Start parsing test_images\Cap_8.jpg...

Start parsing test_images\Cap_9.jpg... [502.5, 864.0, 'VO: 99%']

how is all image output cap_9 type?

On Wed, 15 Jul 2020 at 16:13, jix italiya jlitaliya82@gmail.com wrote:

thx bro .And heres the change:

for filename in sorted(glob.glob("testimg/"+ "*.JPG")):

print("\n")

print("Start parsing "+filename+"...")

image = cv2.imread(filename)

image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

image_expanded = np.expand_dims(image_rgb, axis=0)

this is run for me but how all image show after detection? bs of ths folder in 30 image but only one image is showme output

On Wed, 15 Jul 2020 at 10:41, Sn0wl3r0ker notifications@github.com wrote:

如何一次調用所有圖像? 在物體檢測模型中

Hey man! i don't know if it's right. But i think u can try my "for loop" method to call all images in the target folder.

add this to the utils/visualization_utils.py

def return_coordinates(

image,

boxes,

classes,

scores,

category_index,

instance_masks=None,

instance_boundaries=None,

keypoints=None,

use_normalized_coordinates=False,

max_boxes_to_draw=20,

min_score_thresh=.5,

agnostic_mode=False,

line_thickness=4,

groundtruth_box_visualization_color='black',

skip_scores=False,

skip_labels=False):

Create a display string (and color) for every box location, group any boxes

that correspond to the same location.

box_to_display_str_map = collections.defaultdict(list)

box_to_color_map = collections.defaultdict(str)

box_to_instance_masks_map = {}

box_to_instance_boundaries_map = {}

box_to_score_map = {}

box_to_keypoints_map = collections.defaultdict(list)

if not max_boxes_to_draw:

max_boxes_to_draw = boxes.shape[0]

for i in range(min(max_boxes_to_draw, boxes.shape[0])):

if scores is None or scores[i] > min_score_thresh:

  box = tuple(boxes[i].tolist())

  if instance_masks is not None:

    box_to_instance_masks_map[box] = instance_masks[i]

  if instance_boundaries is not None:

    box_to_instance_boundaries_map[box] = instance_boundaries[i]

  if keypoints is not None:

    box_to_keypoints_map[box].extend(keypoints[i])

  if scores is None:

    box_to_color_map[box] = groundtruth_box_visualization_color

  else:

    display_str = ''

    if not skip_labels:

      if not agnostic_mode:

        if classes[i] in category_index.keys():

          class_name = category_index[classes[i]]['name']

        else:

          class_name = 'N/A'

        display_str = str(class_name)

    if not skip_scores:

      if not display_str:

        display_str = '{}%'.format(int(100*scores[i]))

      else:

        display_str = '{}: {}%'.format(display_str, int(100*scores[i]))

    box_to_display_str_map[box].append(display_str)

    box_to_score_map[box] = scores[i]

    if agnostic_mode:

      box_to_color_map[box] = 'DarkOrange'

    else:

      box_to_color_map[box] = STANDARD_COLORS[

          classes[i] % len(STANDARD_COLORS)]

Draw all boxes onto image.

coordinates_list = []

counter_for = 0

for box, color in box_to_color_map.items():

ymin, xmin, ymax, xmax = box

height, width, channels = image.shape

ymin = int(ymin*height)

ymax = int(ymax*height)

xmin = int(xmin*width)

xmax = int(xmax*width)

coordinates_list.append([ymin, ymax, xmin, xmax, (box_to_score_map[box]*100)])

counter_for = counter_for + 1

return coordinates_list

add this to Object_detection_dir.py

coordinates = vis_util.return_coordinates(

                    image,

                    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.80)

as well as this:

textfile = open("json/"+filename_string+".json", "a")

                textfile.write(json.dumps(coordinates))

                textfile.write("\n")

I think this should be all. Hi! This is not a issue.I add this simple loop to make the code detect all .JPG file the the testimg folder and export the .json file, img with boxes in json, json/testimg folder. Hope this can help someone who wants it.... P.S. i'm still a noob, so hope u can give me some advise

1.import some models:

import glob

import re

2.markdown default IMAGE PATH: "# IMAGE_NAME =" "# PATH_TO_IMAGE = os.path.join(CWD_PATH,IMAGE_NAME)"

3.And heres the change:

for filename in sorted(glob.glob("testimg/"+ "*.JPG")):

print("\n")

print("Start parsing "+filename+"...")

image = cv2.imread(filename)

image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

image_expanded = np.expand_dims(image_rgb, axis=0)

filenameJ = re.sub(r".*?\/", "", filename)

open("json/"+filenameJ+".json", "a").write(json.dumps(coordinates)+"\n"

for coordinate in coordinates:

    (y1, y2, x1, x2, acc, classification) = coordinate

    height = y2-y1

    width = x2-x1

    crop = image[y1:y1+height, x1:x1+width]

    cv2.imwrite("json/"+filename, image)

    print(coordinate)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/EdjeElectronics/TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10/issues/69#issuecomment-658549575, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOQW3D2SG7CMQSCAR3KNKH3R3U3BPANCNFSM4FFF37GQ .

jix1710 commented 4 years ago

I just solved the issue ... looks like the label is not updated in the for loop .. so if there are multiple labels in the same frame, it will return the latest one only ... I've edited the last few lines in vis_util.return_coordinates function to be like this:

# Draw all boxes onto image.
  coordinates_list = []
  counter_for = 0
  for box, color in box_to_color_map.items():
    ymin, xmin, ymax, xmax = box
    height, width, channels = image.shape
    ymin = int(ymin*height)
    ymax = int(ymax*height)
    xmin = int(xmin*width)
    xmax = int(xmax*width)
    coordinates_list.append([ymin, ymax, xmin, xmax, (box_to_score_map[box]*100),display_strs[counter_for]])
    counter_for = counter_for + 1

  return coordinates_list 

i have same code for u to use me but class name is same for evertime ? plz help me...... i have use to coordinates_list.append([ymin, ymax, xmin, xmax, (box_to_score_map[box]*100),display_str[counter_for]])

Raman1121 commented 4 years ago

Hello all I added the piece of code suggested by @psi43 but I am getting this error. AttributeError: module 'object_detection.utils.visualization_utils' has no attribute 'return_coordinates'

Can anyone help me with this? I am adding the code in the right location in the visualization_utils file.

jix1710 commented 3 years ago

how to deploy this model to productions in own server ?? plz help me

On Sat, 5 Sep 2020 at 17:22, Raman Dutt notifications@github.com wrote:

Hello all I added the piece of code suggested by @psi43 https://github.com/psi43 but I am getting this error. AttributeError: module 'object_detection.utils.visualization_utils' has no attribute 'return_coordinates'

Can anyone help me with this? I am adding the code in the right location in the visualization_utils file.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/EdjeElectronics/TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10/issues/69#issuecomment-687598934, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOQW3DZFPYG6ZRXES65T7O3SEIRAPANCNFSM4FFF37GQ .

Aakanksha3010 commented 3 years ago

I ran the OpenPose on Colab and also got JSON files of the video which contains the key points however I'm confused as to how to use these JSON files in order to calculate rep counts and other pose evaluation techniques. Please help me out !

this is my code link: https://colab.research.google.com/drive/1tl0NvOSGLzP0vEpKLjlYEHVtEuJs77it?usp=sharing

vinilreddy36 commented 3 years ago

I just solved the issue ... looks like the label is not updated in the for loop .. so if there are multiple labels in the same frame, it will return the latest one only ... I've edited the last few lines in vis_util.return_coordinates function to be like this:

# Draw all boxes onto image.
  coordinates_list = []
  counter_for = 0
  for box, color in box_to_color_map.items():
    ymin, xmin, ymax, xmax = box
    height, width, channels = image.shape
    ymin = int(ymin*height)
    ymax = int(ymax*height)
    xmin = int(xmin*width)
    xmax = int(xmax*width)
    coordinates_list.append([ymin, ymax, xmin, xmax, (box_to_score_map[box]*100),display_strs[counter_for]])
    counter_for = counter_for + 1

  return coordinates_list 

i have same code for u to use me but class name is same for evertime ? plz help me...... i have use to coordinates_list.append([ymin, ymax, xmin, xmax, (box_to_score_map[box]*100),display_str[counter_for]])

add this, it will solve ur problem.

#Draw all boxes onto image.
 coordinates_list = []
 counter_for = 0
 for box, color in box_to_color_map.items():
   ymin, xmin, ymax, xmax = box
   height, width, channels = image.shape
   ymin = int(ymin*height)
   ymax = int(ymax*height)
   xmin = int(xmin*width)
   xmax = int(xmax*width)
   if classes[counter_for] in category_index.keys():
     class_name = category_index[classes[counter_for]]['name']
   else:
     class_name = 'N/A'
   coordinates_list.append([ymin, ymax, xmin, xmax, (box_to_score_map[box]*100), str(class_name)])
   counter_for = counter_for + 1
 return coordinates_list
unrivalle commented 3 years ago

coordinates = vis_util.return_coordinates(

it is not not working could you please share the latest code . I am getting AttributeError: module 'object_detection.utils.visualization_utils' has no attribute 'return_coordinates'.

vinilreddy36 commented 3 years ago

coordinates = vis_util.return_coordinates(

it is not not working could you please share the latest code . I am getting AttributeError: module 'object_detection.utils.visualization_utils' has no attribute 'return_coordinates'.

did u add this to your utils/visualization_utils.py?

def return_coordinates(
    image,
    boxes,
    classes,
    scores,
    category_index,
    instance_masks=None,
    instance_boundaries=None,
    keypoints=None,
    use_normalized_coordinates=False,
    max_boxes_to_draw=20,
    min_score_thresh=.5,
    agnostic_mode=False,
    line_thickness=4,
    groundtruth_box_visualization_color='black',
    skip_scores=False,
    skip_labels=False):
  # Create a display string (and color) for every box location, group any boxes
  # that correspond to the same location.
  box_to_display_str_map = collections.defaultdict(list)
  box_to_color_map = collections.defaultdict(str)
  box_to_instance_masks_map = {}
  box_to_instance_boundaries_map = {}
  box_to_score_map = {}
  box_to_keypoints_map = collections.defaultdict(list)
  if not max_boxes_to_draw:
    max_boxes_to_draw = boxes.shape[0]
  for i in range(min(max_boxes_to_draw, boxes.shape[0])):
    if scores is None or scores[i] > min_score_thresh:
      box = tuple(boxes[i].tolist())
      if instance_masks is not None:
        box_to_instance_masks_map[box] = instance_masks[i]
      if instance_boundaries is not None:
        box_to_instance_boundaries_map[box] = instance_boundaries[i]
      if keypoints is not None:
        box_to_keypoints_map[box].extend(keypoints[i])
      if scores is None:
        box_to_color_map[box] = groundtruth_box_visualization_color
      else:
        display_str = ''
        if not skip_labels:
          if not agnostic_mode:
            if classes[i] in category_index.keys():
              class_name = category_index[classes[i]]['name']
            else:
              class_name = 'N/A'
            display_str = str(class_name)
        if not skip_scores:
          if not display_str:
            display_str = '{}%'.format(int(100*scores[i]))
          else:
            display_str = '{}: {}%'.format(display_str, int(100*scores[i]))
        box_to_display_str_map[box].append(display_str)
        box_to_score_map[box] = scores[i]
        if agnostic_mode:
          box_to_color_map[box] = 'DarkOrange'
        else:
          box_to_color_map[box] = STANDARD_COLORS[
              classes[i] % len(STANDARD_COLORS)]

  # Draw all boxes onto image.
  coordinates_list = []
  counter_for = 0
  for box, color in box_to_color_map.items():
    ymin, xmin, ymax, xmax = box
    height, width, channels = image.shape
    ymin = int(ymin*height)
    ymax = int(ymax*height)
    xmin = int(xmin*width)
    xmax = int(xmax*width)
    if classes[counter_for] in category_index.keys():
     class_name = category_index[classes[counter_for]]['name']
   else:
     class_name = 'N/A'
   coordinates_list.append([ymin, ymax, xmin, xmax, (box_to_score_map[box]*100), str(class_name)])
   counter_for = counter_for + 1
 return coordinates_list
unrivalle commented 3 years ago

Thanks .

On Wed, Feb 24, 2021 at 6:01 PM vinilreddy36 notifications@github.com wrote:

coordinates = vis_util.return_coordinates(

it is not not working could you please share the latest code . I am getting AttributeError: module 'object_detection.utils.visualization_utils' has no attribute 'return_coordinates'.

did u add this to your utils/visualization_utils.py?

def return_coordinates( image, boxes, classes, scores, category_index, instance_masks=None, instance_boundaries=None, keypoints=None, use_normalized_coordinates=False, max_boxes_to_draw=20, min_score_thresh=.5, agnostic_mode=False, line_thickness=4, groundtruth_box_visualization_color='black', skip_scores=False, skip_labels=False):

Create a display string (and color) for every box location, group any boxes

that correspond to the same location.

box_to_display_str_map = collections.defaultdict(list) box_to_color_map = collections.defaultdict(str) box_to_instance_masks_map = {} box_to_instance_boundaries_map = {} box_to_score_map = {} box_to_keypoints_map = collections.defaultdict(list) if not max_boxes_to_draw: max_boxes_to_draw = boxes.shape[0] for i in range(min(max_boxes_to_draw, boxes.shape[0])): if scores is None or scores[i] > min_score_thresh: box = tuple(boxes[i].tolist()) if instance_masks is not None: box_to_instance_masks_map[box] = instance_masks[i] if instance_boundaries is not None: box_to_instance_boundaries_map[box] = instance_boundaries[i] if keypoints is not None: box_to_keypoints_map[box].extend(keypoints[i]) if scores is None: box_to_color_map[box] = groundtruth_box_visualization_color else: display_str = '' if not skip_labels: if not agnostic_mode: if classes[i] in category_index.keys(): class_name = category_index[classes[i]]['name'] else: class_name = 'N/A' display_str = str(class_name) if not skip_scores: if not display_str: display_str = '{}%'.format(int(100scores[i])) else: display_str = '{}: {}%'.format(display_str, int(100scores[i])) box_to_display_str_map[box].append(display_str) box_to_score_map[box] = scores[i] if agnostic_mode: box_to_color_map[box] = 'DarkOrange' else: box_to_color_map[box] = STANDARD_COLORS[ classes[i] % len(STANDARD_COLORS)]

Draw all boxes onto image.

coordinates_list = [] counter_for = 0 for box, color in box_to_color_map.items(): ymin, xmin, ymax, xmax = box height, width, channels = image.shape ymin = int(yminheight) ymax = int(ymaxheight) xmin = int(xminwidth) xmax = int(xmaxwidth) if classes[counter_for] in category_index.keys(): class_name = category_index[classes[counter_for]]['name'] else: class_name = 'N/A' coordinates_list.append([ymin, ymax, xmin, xmax, (box_to_score_map[box]*100), str(class_name)]) counter_for = counter_for + 1 return coordinates_list

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/EdjeElectronics/TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10/issues/69#issuecomment-785043324, or unsubscribe https://github.com/notifications/unsubscribe-auth/AN5IJSPCOCXVGEWG4NUOGVLTATWR5ANCNFSM4FFF37GQ .

unrivalle commented 3 years ago

Could you please help me to creat an Flask application for custom object detection model .

On Wed, Feb 24, 2021 at 11:55 PM Souravs Subudhi unrivalledsaurav@gmail.com wrote:

Thanks .

On Wed, Feb 24, 2021 at 6:01 PM vinilreddy36 notifications@github.com wrote:

coordinates = vis_util.return_coordinates(

it is not not working could you please share the latest code . I am getting AttributeError: module 'object_detection.utils.visualization_utils' has no attribute 'return_coordinates'.

did u add this to your utils/visualization_utils.py?

def return_coordinates( image, boxes, classes, scores, category_index, instance_masks=None, instance_boundaries=None, keypoints=None, use_normalized_coordinates=False, max_boxes_to_draw=20, min_score_thresh=.5, agnostic_mode=False, line_thickness=4, groundtruth_box_visualization_color='black', skip_scores=False, skip_labels=False):

Create a display string (and color) for every box location, group any boxes

that correspond to the same location.

box_to_display_str_map = collections.defaultdict(list) box_to_color_map = collections.defaultdict(str) box_to_instance_masks_map = {} box_to_instance_boundaries_map = {} box_to_score_map = {} box_to_keypoints_map = collections.defaultdict(list) if not max_boxes_to_draw: max_boxes_to_draw = boxes.shape[0] for i in range(min(max_boxes_to_draw, boxes.shape[0])): if scores is None or scores[i] > min_score_thresh: box = tuple(boxes[i].tolist()) if instance_masks is not None: box_to_instance_masks_map[box] = instance_masks[i] if instance_boundaries is not None: box_to_instance_boundaries_map[box] = instance_boundaries[i] if keypoints is not None: box_to_keypoints_map[box].extend(keypoints[i]) if scores is None: box_to_color_map[box] = groundtruth_box_visualization_color else: display_str = '' if not skip_labels: if not agnostic_mode: if classes[i] in category_index.keys(): class_name = category_index[classes[i]]['name'] else: class_name = 'N/A' display_str = str(class_name) if not skip_scores: if not display_str: display_str = '{}%'.format(int(100scores[i])) else: display_str = '{}: {}%'.format(display_str, int(100scores[i])) box_to_display_str_map[box].append(display_str) box_to_score_map[box] = scores[i] if agnostic_mode: box_to_color_map[box] = 'DarkOrange' else: box_to_color_map[box] = STANDARD_COLORS[ classes[i] % len(STANDARD_COLORS)]

Draw all boxes onto image.

coordinates_list = [] counter_for = 0 for box, color in box_to_color_map.items(): ymin, xmin, ymax, xmax = box height, width, channels = image.shape ymin = int(yminheight) ymax = int(ymaxheight) xmin = int(xminwidth) xmax = int(xmaxwidth) if classes[counter_for] in category_index.keys(): class_name = category_index[classes[counter_for]]['name'] else: class_name = 'N/A' coordinates_list.append([ymin, ymax, xmin, xmax, (box_to_score_map[box]*100), str(class_name)]) counter_for = counter_for + 1 return coordinates_list

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/EdjeElectronics/TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10/issues/69#issuecomment-785043324, or unsubscribe https://github.com/notifications/unsubscribe-auth/AN5IJSPCOCXVGEWG4NUOGVLTATWR5ANCNFSM4FFF37GQ .

unrivalle commented 3 years ago

Thanks! Really thanks for your support .

On Mon, Mar 1, 2021 at 12:16 PM Ayushman Tyagi notifications@github.com wrote:

I could not use return-coordinates method because of Attribute error, so i used the return_coordinates method inside the visualize_boxes_and_labels_on_image_array() and made it return the coordinates instead of image.

def visualize_boxes_and_labels_on_image_array( image, boxes, classes, scores, category_index, instance_masks=None, instance_boundaries=None, keypoints=None, keypoint_scores=None, keypoint_edges=None, track_ids=None, use_normalized_coordinates=False, max_boxes_to_draw=20, min_score_thresh=.5, agnostic_mode=False, line_thickness=4, mask_alpha=.4, groundtruth_box_visualization_color='black', skip_boxes=False, skip_scores=False, skip_labels=False, skip_track_ids=False): """Overlay labeled boxes on an image with formatted scores and label names.

This function groups boxes that correspond to the same location and creates a display string for each detection and overlays these on the image. Note that this function modifies the image in place, and returns that same image.

Args: image: uint8 numpy array with shape (img_height, img_width, 3) boxes: a numpy array of shape [N, 4] classes: a numpy array of shape [N]. Note that class indices are 1-based, and match the keys in the label map. scores: a numpy array of shape [N] or None. If scores=None, then this function assumes that the boxes to be plotted are groundtruth boxes and plot all boxes as black with no classes or scores. category_index: a dict containing category dictionaries (each holding category index id and category name name) keyed by category indices. instance_masks: a uint8 numpy array of shape [N, image_height, image_width], can be None. instance_boundaries: a numpy array of shape [N, image_height, image_width] with values ranging between 0 and 1, can be None. keypoints: a numpy array of shape [N, num_keypoints, 2], can be None. keypoint_scores: a numpy array of shape [N, num_keypoints], can be None. keypoint_edges: A list of tuples with keypoint indices that specify which keypoints should be connected by an edge, e.g. [(0, 1), (2, 4)] draws edges from keypoint 0 to 1 and from keypoint 2 to 4. track_ids: a numpy array of shape [N] with unique track ids. If provided, color-coding of boxes will be determined by these ids, and not the class indices. use_normalized_coordinates: whether boxes is to be interpreted as normalized coordinates or not. max_boxes_to_draw: maximum number of boxes to visualize. If None, draw all boxes. min_score_thresh: minimum score threshold for a box or keypoint to be visualized. agnostic_mode: boolean (default: False) controlling whether to evaluate in class-agnostic mode or not. This mode will display scores but ignore classes. line_thickness: integer (default: 4) controlling line width of the boxes. mask_alpha: transparency value between 0 and 1 (default: 0.4). groundtruth_box_visualization_color: box color for visualizing groundtruth boxes skip_boxes: whether to skip the drawing of bounding boxes. skip_scores: whether to skip score when drawing a single detection skip_labels: whether to skip label when drawing a single detection skip_track_ids: whether to skip track id when drawing a single detection

Returns: uint8 numpy array with shape (img_height, img_width, 3) with overlaid boxes. """ Create a display string (and color) for every box location, group any boxes that correspond to the same location.

box_to_display_str_map = collections.defaultdict(list) box_to_color_map = collections.defaultdict(str) box_to_instance_masks_map = {} box_to_instance_boundaries_map = {} box_to_keypoints_map = collections.defaultdict(list) box_to_keypoint_scores_map = collections.defaultdict(list) box_to_track_ids_map = {} if not max_boxes_to_draw: max_boxes_to_draw = boxes.shape[0] for i in range(boxes.shape[0]): if max_boxes_to_draw == len(box_to_color_map): break if scores is None or scores[i] > min_score_thresh: box = tuple(boxes[i].tolist()) if instance_masks is not None: box_to_instance_masks_map[box] = instance_masks[i] if instance_boundaries is not None: box_to_instance_boundaries_map[box] = instance_boundaries[i] if keypoints is not None: box_to_keypoints_map[box].extend(keypoints[i]) if keypoint_scores is not None: box_to_keypoint_scores_map[box].extend(keypoint_scores[i]) if track_ids is not None: box_to_track_ids_map[box] = track_ids[i] if scores is None: box_to_color_map[box] = groundtruth_box_visualization_color else: display_str = '' if not skip_labels: if not agnostic_mode: if classes[i] in six.viewkeys(category_index): class_name = category_index[classes[i]]['name'] else: class_name = 'N/A' display_str = str(class_name) if not skip_scores: if not display_str: display_str = '{}%'.format(round(100

scores[i])) else: display_str = '{}: {}%'.format(display_str, round(100 scores[i])) if not skip_track_ids and track_ids is not None: if not display_str: display_str = 'ID {}'.format(track_ids[i]) else: display_str = '{}: ID {}'.format(display_str, track_ids[i]) box_to_display_str_map[box].append(display_str) if agnostic_mode: box_to_color_map[box] = 'DarkOrange' elif track_ids is not None: prime_multipler = _get_multiplier_for_color_randomness() box_to_color_map[box] = STANDARD_COLORS[ (prime_multipler * track_ids[i]) % len(STANDARD_COLORS)] else: box_to_color_map[box] = STANDARD_COLORS[ classes[i] % len(STANDARD_COLORS)] Draw all boxes onto image.

for box, color in box_to_color_map.items(): ymin, xmin, ymax, xmax = box if instance_masks is not None: draw_mask_on_image_array( image, box_to_instance_masks_map[box], color=color, alpha=mask_alpha ) if instance_boundaries is not None: draw_mask_on_image_array( image, box_to_instance_boundaries_map[box], color='red', alpha=1.0 ) draw_bounding_box_on_image_array( image, ymin, xmin, ymax, xmax, color=color, thickness=0 if skip_boxes else line_thickness, display_str_list=box_to_display_str_map[box], use_normalized_coordinates=use_normalized_coordinates) if keypoints is not None: keypoint_scores_for_box = None if box_to_keypoint_scores_map: keypoint_scores_for_box = box_to_keypoint_scores_map[box] draw_keypoints_on_image_array( image, box_to_keypoints_map[box], keypoint_scores_for_box, min_score_thresh=min_score_thresh, color=color, radius=line_thickness / 2, use_normalized_coordinates=use_normalized_coordinates, keypoint_edges=keypoint_edges, keypoint_edge_color=color, keypoint_edge_width=line_thickness // 2) coordinates =return_coordinates(image, boxes, classes, scores, category_index,use_normalized_coordinates=True, line_thickness=8, min_score_thresh=0.80)

return coordinates

** But when i run this method, it returns a ndarray type obejct. [[[255 255 255] [255 255 255] [255 255 255] ... [255 255 255] [255 255 255] [255 255 255]]

[[255 255 255] [255 255 255] [255 255 255] ... [255 255 255] [255 255 255] [255 255 255]]

[[255 255 255] [255 255 255] [255 255 255] ... [255 255 255] [255 255 255] [255 255 255]]

...

[[250 254 255] [248 252 251] [229 235 225] ... [255 255 255] [255 255 255] [255 255 255]]

[[234 236 235] [249 250 254] [254 255 255] ... [246 246 246] [241 241 241] [255 255 255]]

[[254 255 246] [232 234 231] [251 251 255] ... [255 255 255] [255 255 255] [255 255 255]]]

***and i get another error when i am trying to write the output to a file json/test.json

File "C:\Users\aytya\anaconda3\lib\json\encoder.py", line 179, in default raise TypeError(f'Object of type {o.class.name} '

TypeError: Object of type ndarray is not JSON serializable

In my project i want to detect object which has 2 classes i.e red box and blue box and i want their coordinates in the image. Please help me find these coordinates /

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/EdjeElectronics/TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10/issues/69#issuecomment-787695420, or unsubscribe https://github.com/notifications/unsubscribe-auth/AN5IJSJQCXMC5CWD6BZJQ23TBMZ35ANCNFSM4FFF37GQ .

ghost commented 3 years ago

I read this thread and it's quite interesting to crop bounding boxes after training of tensorflow object detection API. Can anyone please put all the chunks at one place that worked for you, step by step. It would look like a tutorial and new readers will be able to easily implement. Thanks.

wael-mahdi commented 3 years ago

hiii...friends ..I am working on a project(computer vision-video tracking-multi objects) i need help with how I can put unique id and fixed for each object in frames I know detected contours and draw box rectangles for each object but I cannot fix id for each one. second, my order if anyone have a dataset called DUKE MTMC I need it in my project thanx a lot for all

ghost commented 2 years ago

You do not need to add any additional programs (return_coordinates) to determine the objects coordinates. just use dnn.NMSBoxes command in opencv as follow:

# This code return objects coordination in a variable as matrix (signs) and their labels (labels):

idxs = cv2.dnn.NMSBoxes(boxes, scores, 0.5, 1.5)

# define a array as matrix
signs = []
labels = []
for i in range(len(idxs)):
        signs.append(i)
        labels.append(i)

# ensure at least one detection exists
if len(idxs) > 0:
    # loop over the indexes we are keeping
    for i in idxs.flatten():
        # extract the bounding box coordinates
        ymin = int((boxes[0][i][0] * height))
        xmin = int((boxes[0][i][1] * width))
        ymax = int((boxes[0][i][2] * height))
        xmax = int((boxes[0][i][3] * width))
        signs[i] = [ymin,ymax,xmin,xmax]
        labels[i] = int(classes[0][i])

print(signs)
print(labels)

@Mehran970 🙏🙏🙏 Thanks a lot. It worked for multiple objects in a frame. You made my day. Thank you so much.