def draw_text_for_box(self,
                          text,
                          bbox,
                          font_scale=1.0,
                          color=None,
                          origin="above top left",
                          thickness=1):
        # If box height is passed scale relative to box height * font_scale
        font = cv2.FONT_HERSHEY_COMPLEX

        x0, y0, x1, y1 = bbox
        bbox_height = y1 - y0
        text_height = min(max(self._min_text_height, bbox_height * font_scale),
                          self._max_text_height)
        font_scale = cv2.getFontScaleFromHeight(font, int(text_height),
                                                thickness)
        (label_width,
         label_height), baseline = cv2.getTextSize(text, font, font_scale,
                                                   thickness)
        pad = 0.7
        box_pad = min(int((1 - pad) * label_width),
                      int((1 - pad) * label_height))
        label_height += box_pad
        position = (x0 if "left" in origin else x1,
                    y0 if "top" in origin else y1 - label_height - box_pad)
        if "above" in origin:
            position = (position[0], position[1] - label_height - box_pad)
        elif "below" in origin:
            position = (position[0], position[1] + label_height + box_pad)
        self.draw_text(text, position, font_scale, color, font, thickness)
def draw_bbox(frame, bboxlist, fps=False):
    for bbox in bboxlist:
        # draw a red rectangle around detected objects
        desc, score, left, top, right, bottom = bbox
        text = f"{desc}: {int(score*100)}%"
        fontScale = cv2.getFontScaleFromHeight(
            fontFace=cv2.FONT_HERSHEY_COMPLEX, pixelHeight=10, thickness=1)
        (text_width, text_height) = cv2.getTextSize(text,
                                                    cv2.FONT_HERSHEY_COMPLEX,
                                                    fontScale=fontScale,
                                                    thickness=1)[0]

        #labels
        cv2.rectangle(frame, (int(left), int(top - 6 - text_height)),
                      (int(left + text_width), int(top)), (0, 0, 255),
                      cv2.FILLED)
        cv2.putText(frame, text, (int(left), int(top - 5)),
                    cv2.FONT_HERSHEY_COMPLEX, fontScale, (255, 255, 255), 1,
                    cv2.LINE_AA)

        #bbox
        cv2.rectangle(frame, (int(left), int(top)), (int(right), int(bottom)),
                      (0, 0, 255),
                      thickness=2)

        if fps:
            # Display FPS on frame
            cv2.putText(frame, "FPS : " + str(int(fps)), (20, 80),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2,
                        cv2.LINE_AA)

    return frame
    def draw_text_for_box(self, text, bbox, font_scale=None, color=None, origin="above top left", thickness=1,
            bbox_scale=1.0 / 8.0):
        # If box height is passed scale relative to box height * font_scale
        font = cv2.FONT_HERSHEY_COMPLEX

        x0, y0, x1, y1 = bbox
        bbox_height = y1 - y0

        # Use 1/8th bbox or min as min_text_height
        text_height = min(max(self._min_text_height, bbox_height * bbox_scale), self._max_text_height)

        if not font_scale:
            # apt version of opencv doesn't have this function
            if hasattr(cv2, "getFontScaleFromHeight"):
                font_scale = cv2.getFontScaleFromHeight(font, int(text_height), thickness)
            else:  # Reasonable default for cv2.FONT_HERSHEY_COMPLEX
                font_ascii = (9 + 12 * 16) + (16 << 8) + (32 << 8)
                font_base_line = font_ascii & 15
                font_cap_line = (font_ascii >> 4) & 15
                font_scale = (int(text_height) - (thickness + 1) / 2.0) / (font_cap_line + font_base_line)

        (label_width, label_height), baseline = cv2.getTextSize(text, font, font_scale, thickness)
        pad = 0.7
        box_pad = min(int((1 - pad) * label_width), int((1 - pad) * label_height))
        label_height += box_pad
        position = (x0 if "left" in origin else x1, y0 if "top" in origin else y1 - label_height - box_pad)
        if "above" in origin:
            position = (position[0], position[1] - label_height - box_pad)
        elif "below" in origin:
            position = (position[0], position[1] + label_height + box_pad)
        self.draw_text(text, position, font_scale, color, font, thickness)
Beispiel #4
0
def plot_contour_pairs(contours_pairs,
                       *,
                       image_shape=None,
                       image_height=None,
                       image_width=None):
    if image_shape is not None:
        canvas = np.full([image_shape[0], image_width[1], 3],
                         255,
                         dtype=np.uint8)
    elif image_height is not None and image_width is not None:
        canvas = np.full([image_height, image_width, 3], 255, dtype=np.uint8)
    else:
        raise AttributeError(
            "Provide either image_shape or image_height/image_width argument")
    colors = []
    text_canvas = canvas.copy()
    for contour, center, color in contours_pairs:
        canvas = cv2.drawContours(canvas, [contour], -1, (0, 0, 0), 1)

        if color not in colors:
            colors.append(color)

        # TODO color numbers on center
        font = cv2.FONT_HERSHEY_PLAIN
        font_px_size = 8
        text_canvas = cv2.putText(
            text_canvas, f"{colors.index(color)}", center, font,
            cv2.getFontScaleFromHeight(font, font_px_size), (0, 0, 0))
    kernel = np.ones([3, 3], dtype=np.uint8)
    canvas = cv2.morphologyEx(canvas, cv2.MORPH_OPEN, kernel=kernel)
    canvas[(text_canvas == (0, 0, 0)).all(axis=-1)] = (255, 0, 0)
    return canvas
def visualize_results(tracking_list,
                      height,
                      img):
    """visualize the tracking results.
       we only visualize the targets that have been confirmed.
    """
    img_copy = copy.deepcopy(img)
    img = transparent_region(img,
                             POINT1, POINT2, POINT3, POINT4,
                             (0, 0, 255))
    font_scale = cv2.getFontScaleFromHeight(fontFace=6,
                                            pixelHeight=int(height)) / 25

    num_tracker = len(tracking_list)

    if num_tracker > 0:
        for tracker in tracking_list:
            tentative = tracker.tentative

            # if target is in the danger region, system givea an alarm.
            center = tracker.measurement[:2]
            invasion = judge_invasion(center, POINT1, POINT2, POINT3, POINT4)
            print(invasion)
            if invasion:
                img = transparent_region(img_copy,
                                         POINT1, POINT2, POINT3, POINT4,
                                         (255, 0, 0))

            measurement = xyah2box(tracker.measurement)
            measurement = measurement.astype('int')
            label = tracker.index

            if not tentative:
                # print(label)
                # visualize bounding box and label
                retval, _ = cv2.getTextSize(str(label), 6, font_scale, 2)
                origin1 = measurement[0:2]
                origin2 = origin1 + retval
                text_origin = origin1 + np.array([0, retval[1]])
                cv2.rectangle(img, (origin1[0], origin1[1]),
                              (origin2[0], origin2[1]), (255, 192, 203),
                              thickness=-1)
                cv2.putText(img, str(label), (text_origin[0], text_origin[1]),
                            6, font_scale, (255, 255, 255), 1)

                cv2.rectangle(img, (measurement[0], measurement[1]),
                              (measurement[2], measurement[3]), (0, 0, 255),
                              thickness=2)

    return img
Beispiel #6
0
def draw_axis_y(img,
                rect,
                range_y,
                label,
                num_ticks=5,
                color=(0, 0, 0),
                thickness=5):
    left, right, top, bottom = rect
    height = bottom - top

    left_start = left - 10

    low, high = range_y
    ticks = np.linspace(low, high, num_ticks + 2)[1:-1]

    cv2.line(img, (left_start, top + 10), (left_start, bottom - 10),
             color,
             thickness=thickness)

    font_face = cv2.FONT_HERSHEY_SIMPLEX
    font_scale = cv2.getFontScaleFromHeight(
        font_face, int(round(height / (num_ticks + 3))))

    for t in ticks:
        y = mapto(t, low, high, bottom, top)
        y = int(round(y))
        lab = str(int(round(t)))
        (w, h), baseline = cv2.getTextSize(lab, font_face, font_scale, 2)
        cv2.line(img, (left_start - 5, y), (left_start, y),
                 color,
                 thickness=thickness)
        cv2.putText(img,
                    lab, (left_start - 30 - w // 2, y + baseline),
                    font_face,
                    0.9,
                    color,
                    thickness=2)

    imgnew = np.zeros((img.shape[1], img.shape[0]), dtype='uint8')
    cv2.putText(imgnew,
                label, (img.shape[0] - top - 130, left - 100),
                font_face,
                font_scale * 0.7,
                255,
                thickness=2)
    imgnew = cv2.rotate(imgnew, cv2.ROTATE_90_COUNTERCLOCKWISE)

    img[imgnew > 0] = color

    return img
Beispiel #7
0
def draw_attributes(img_path, df):
    """Write predicted face attributes on the image
    """
    races = ['A', 'W', 'B', 'I', 'O']
    ages = ['C', 'Adst', 'T', 'Y Ad', 'Ad', 'Old Ad', 'Se']
    img = cv2.imread(img_path)
    # img  = cv2.cvtColor(color, cv2.COLOR_BGR2RGB)
    data = {
        'Face': 0,
        'Male': 0,
        'Female': 0,
        'A': 0,
        'W': 0,
        'B': 0,
        'I': 0,
        'O': 0,
        'C': 0,
        'Adst': 0,
        'T': 0,
        'Y Ad': 0,
        'Ad': 0,
        'Old Ad': 0,
        'Se': 0
    }
    for row in df.iterrows():
        top, right, bottom, left = row[1][13:].astype(int)
        data['Face'] = data['Face'] + 1
        if row[1]['Gender'] <= 0.5:
            gender = 'M'
            data['Male'] = data['Male'] + 1
        else:
            gender = 'F'
            data['Female'] = data['Female'] + 1
        race = races[int(np.argmax(row[1][1:6]))]
        age = ages[int(np.argmax(row[1][6:13]))]
        data[race] = data[race] + 1
        data[age] = data[age] + 1
        text_showed = f"{age} {race} {gender}"
        # No face boundary now
        #cv2.rectangle(img, (left, top), (right, bottom), (0, 0, 255), 2)
        font = cv2.FONT_HERSHEY_DUPLEX
        img_width = img.shape[1]
        font_scale = cv2.getFontScaleFromHeight(font, img.shape[1], 1)
        cv2.putText(img, text_showed, (left + 6, bottom - 6), font, 0.5,
                    (0, 0, 0), 2)
        cv2.putText(img, text_showed, (left + 6, bottom - 6), font, 0.5,
                    (255, 255, 255), 1)
    data['img'] = img
    return data
Beispiel #8
0
def draw_text(img):
    text = 'hello lion'
    bottom_left_corner = (100, 400)
    font_face = cv2.FONT_HERSHEY_COMPLEX
    font_scale = 1.5
    cv2.putText(img,
                text,
                bottom_left_corner,
                font_face,
                font_scale,
                color=(0, 255, 0),
                thickness=2)

    # calculate the font_scale for the desired pixel height
    pixel_height = 20
    font_scale = cv2.getFontScaleFromHeight(font_face,
                                            pixel_height,
                                            thickness=2)
    print(f'desired font_scale: {font_scale}')

    cv2.putText(img,
                text,
                bottom_left_corner,
                font_face,
                font_scale,
                color=(255, 0, 0),
                thickness=2)

    # now center the text
    print(f"# {img.shape}")  # (480, 440, 3)
    img_height, img_width = img.shape[:2]  # start, stop, step: [0:2:0] or [:2]
    print(f"img height: {img_height}; img width: {img_width}")
    text_size, base_line = cv2.getTextSize(text,
                                           font_face,
                                           font_scale,
                                           thickness=2)
    text_width, text_height = text_size
    print(
        f'text width: {text_width}; text height: {text_height}; base line: {base_line}'
    )
    x_center = (img_width - text_width) // 2
    y_position = (img_height - base_line - 10)  # 10 pixel above bottom
    cv2.putText(img,
                text, (x_center, y_position),
                font_face,
                font_scale,
                color=(0, 0, 255),
                thickness=2)
Beispiel #9
0
def draw_alphanum(bg_img, colour, letter):
    font = cv.FONT_HERSHEY_DUPLEX
    thickness = 10
    pixel_size = (int(bg_img.shape[0]/4), int(bg_img.shape[1]/4))
    try:
        font_scale = cv.getFontScaleFromHeight(font, pixel_size[1], thickness)
    except AttributeError as err:
        font_scale = 5.071428571428571

    font_size, ret = cv.getTextSize(letter, font, font_scale, thickness)
    textX = int((bg_img.shape[1] - font_size[0]) / 2)
    textY = int((bg_img.shape[0] + font_size[1]) / 2)
    
    cv.putText(bg_img, letter, (textX, textY), font, font_scale, colour, thickness)
    bottom_left = (textX, textY)
    top_right = (textX+font_size[0], textY-font_size[1])
    return (*bottom_left, *top_right)
Beispiel #10
0
def draw_attr(image,
              attr_dict,
              loc,
              line_height,
              font=cv2.FONT_HERSHEY_SIMPLEX,
              thickness=1,
              linespace=None):
    if linespace is None:
        linespace = int(line_height / 2)
    font_scale = cv2.getFontScaleFromHeight(font, line_height, thickness)
    text_org_x = int(loc[0])
    text_org_y = int(loc[1] - len(attr_dict.keys()) *
                     (line_height + linespace) / 2 + (line_height + linespace))
    for key, val in attr_dict.items():
        if val is None:
            val = float('nan')
        text = '{}: {:.2f}'.format(key, val)
        cv2.putText(image,
                    text, (text_org_x, text_org_y),
                    font,
                    font_scale,
                    255,
                    thickness=thickness)
        text_org_y += (line_height + linespace)
Beispiel #11
0
cv2.putText(imageText, text, (20, 350), fontFace, fontScale, fontColor,
            fontThickness, cv2.LINE_AA)

# Display the image
#cv2.imshow("Image Text",imageText)
#cv2.waitKey(0)
cv2.imwrite("../results/imageText.png", imageText)

#Issues in Text annotation

#Solution 1: Get font size from pixel height of text

pixelHeight = 20

# Calculate the fontScale
fontScale = cv2.getFontScaleFromHeight(fontFace, pixelHeight, fontThickness)
print("fontScale = {}".format(fontScale))

imageTextFontScale = image.copy()
cv2.putText(imageTextFontScale, text, (20, 350), fontFace, fontScale,
            fontColor, fontThickness, cv2.LINE_AA)
# Display the image
#cv2.imshow("Image Text FontScale",imageTextFontScale)
#cv2.waitKey(0)
cv2.imwrite("../results/imageTextFontScale.png", imageTextFontScale)

#Solution 2: Get height and width of text

imageGetTextSize = image.copy()
imageHeight, imageWidth = imageGetTextSize.shape[:2]
Beispiel #12
0
def get_plotting_params(caps_2d, cap_3d, ang_names=[]):

    height_angle = 175
    spacing_angle = 40
    spacing_videos = 20

    n_angles = len(ang_names)

    params_2d = [get_video_params_cap(c) for c in caps_2d]
    height_2d = max([p['height'] for p in params_2d])
    widths_2d = [
        round(p['width'] * height_2d / p['height']) for p in params_2d
    ]

    param_3d = get_video_params_cap(cap_3d)
    height_3d = param_3d['height']
    width_3d = param_3d['width']

    start_3d = height_2d + spacing_videos
    start_angles = start_3d + height_3d + spacing_videos + spacing_angle

    width_total = sum(widths_2d)
    height_total = height_2d + spacing_videos + height_3d + spacing_videos + \
        height_angle * n_angles + spacing_angle*(n_angles+1)

    nframes = min([p['nframes'] for p in params_2d])
    nframes = min(nframes, param_3d['nframes'])

    fps = param_3d['fps']

    height_font = spacing_angle // 2
    font_face = cv2.FONT_HERSHEY_SIMPLEX
    font_scale = cv2.getFontScaleFromHeight(font_face, height_font)
    font_scale = int(round(font_scale))
    font_color = (0, 0, 0)
    font_thickness = 2

    mid_3d = int((width_total - width_3d) / 2)

    d = {
        'height_angle': height_angle,
        'spacing_angle': spacing_angle,
        'spacing_videos': spacing_videos,
        'height_2d': height_2d,
        'widths_2d': widths_2d,
        'start_3d': start_3d,
        'width_3d': width_3d,
        'mid_3d': mid_3d,
        'height_3d': height_3d,
        'nframes': nframes,
        'fps': fps,
        'width_total': width_total,
        'height_total': height_total,
        'start_3d': start_3d,
        'start_angles': start_angles,
        'height_font': height_font,
        'font_face': font_face,
        'font_scale': font_scale,
        'font_color': font_color,
        'font_thickness': font_thickness,
    }

    return d
Beispiel #13
0
 def getFontScaleFromHeight(fontFace, pixelHeight, thickness=1):
     return cv.getFontScaleFromHeight(fontFace, pixelHeight, thickness)
           thickness=-1,
           lineType=cv2.LINE_AA)

cv2.ellipse(img, (300, 300), (100, 40),
            0,
            0,
            300, (100, 100, 255),
            thickness=3)
cv2.ellipse(img, (300, 300), (100, 40), 80, 0, 300, (100, 255, 0), thickness=3)

cv2.rectangle(img, (100, 150), (300, 400), (200, 200, 200), thickness=3)

#For Text
text = "Hello Im Musk"
#fontSize = 1.5
fontStyle = cv2.FONT_HERSHEY_SIMPLEX
fontColor = (0, 255, 0)
fontThickness = 2

fontSize = cv2.getFontScaleFromHeight(fontStyle, 30, 2)

cv2.putText(img, text, (100, 450), fontStyle, fontSize, fontColor,
            fontThickness)
testSize, baseLine = cv2.getTextSize(text, fontStyle, fontSize, fontThickness)

print(testSize, baseLine)

cv2.imshow("Hello", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Beispiel #15
0
while (True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Handles the mirroring of the current frame
    frame = cv2.flip(frame, 1)

    # Our operations on the frame come here

    # Saves image of the current frame in jpg file
    # name = 'frame' + str(currentFrame) + '.jpg'
    # cv2.imwrite(name, frame)

    # Display the resulting frame
    cv2.imshow('Camara', frame)
    cv2.getFontScaleFromHeight(5, 5, 1)
    cv2.moveWindow('Camara', 300, 100)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    # To stop duplicate images
    currentFrame += 1

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

# Potential Error:
# OpenCV: Cannot Use FaceTime HD Kamera
# OpenCV: camera failed to properly initialize!
# Segmentation fault: 11
Beispiel #16
0
#add box around face
img = cv2.imread(os.getcwd() + '/images/boy.jpg')
cv2.rectangle(img, (170, 50), (300, 200), (255, 0, 255),
              thickness=2,
              lineType=cv2.LINE_8)

#get image size
imgHeight, imgWidth = img.shape[:2]

#define text properties
text = 'asian boi'
fontFace = cv2.FONT_HERSHEY_COMPLEX
fontColor = (250, 10, 10)
textHeight = 20
thickness = 1
fontScale = cv2.getFontScaleFromHeight(fontFace, textHeight, thickness)
textSize, baseLine = cv2.getTextSize(text, fontFace, fontScale, thickness)
textWidth, textHeight = textSize

#center text (remember (0,0) is top left
x_coord = (imgWidth - textWidth) // 2
y_coord = (imgHeight - baseLine - 10)

#draw text box
color = (255, 255, 255)
bottom_left = (x_coord, y_coord + baseLine)
top_right = (x_coord + textWidth, y_coord - textHeight)
cv2.rectangle(img, bottom_left, top_right, color, thickness=-1)

#draw the text
cv2.putText(img, text, (x_coord, y_coord), fontFace, fontScale, fontColor,
Beispiel #17
0
    # different color for every skeleton pair
    hsv_tuples = [(x / len(skeleton_pair), 1., 1.)
                  for x in range(len(skeleton_pair))]
    colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
    colors = list(map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), colors))

    # connect skeleton pair
    for i, pair in enumerate(skeleton_pair):
        if pair[0] in valid_idx_list and pair[1] in valid_idx_list:
            point1 = (preds_decode[pair[0]][0], preds_decode[pair[0]][1])
            point2 = (preds_decode[pair[1]][0], preds_decode[pair[1]][1])
            cv2.line(man, point1, point2, color=colors[i], thickness=3, lineType=4)

    # draw key point
    fontScale = cv2.getFontScaleFromHeight(fontFace=6,
                                           pixelHeight=img_height) / 27
    for key in valid_idx_list:
        if 4 < key < 12:
            retval, _ = cv2.getTextSize(text=skeleton['key_points'][key],
                                        fontFace=1,
                                        fontScale=fontScale,
                                        thickness=1)
            origin1 = (preds_decode[key][0] + 5, preds_decode[key][1] - 5 - retval[1])
            origin2 = (preds_decode[key][0] + 5 + retval[0], preds_decode[key][1] - 5)
            text_origin = (preds_decode[key][0] + 5, preds_decode[key][1] - 5)

            if origin2[0] > img_width:
                origin1 = (preds_decode[key][0] + 5 - retval[0], preds_decode[key][1] - 5 - retval[1])
                origin2 = (preds_decode[key][0] + 5, preds_decode[key][1] - 5)
                text_origin = (preds_decode[key][0] + 5 - retval[0], preds_decode[key][1] - 5)