def draw_bounding_box_on_image(current_frame_number,
                               image,
                               ymin,
                               xmin,
                               ymax,
                               xmax,
                               color='red',
                               thickness=4,
                               display_str_list=(),
                               use_normalized_coordinates=True):
    """Adds a bounding box to an image.

  Each string in display_str_list is displayed on a separate line above the
  bounding box in black text on a rectangle filled with the input 'color'.
  If the top of the bounding box extends to the edge of the image, the strings
  are displayed below the bounding box.

  Args:
    image: a PIL.Image object.
    ymin: ymin of bounding box.
    xmin: xmin of bounding box.
    ymax: ymax of bounding box.
    xmax: xmax of bounding box.
    color: color to draw bounding box. Default is red.
    thickness: line thickness. Default value is 4.
    display_str_list: list of strings to display in box
                      (each to be shown on its own line).
    use_normalized_coordinates: If True (default), treat coordinates
      ymin, xmin, ymax, xmax as relative to the image.  Otherwise treat
      coordinates as absolute.
  """
    csv_line = ""  # to create new csv line consists of vehicle type, predicted_speed, color and predicted_direction
    update_csv = False  # update csv for a new vehicle that are passed from ROI - just one new line for each vehicles
    is_vehicle_detected = [0]
    draw = ImageDraw.Draw(image)
    im_width, im_height = image.size
    if use_normalized_coordinates:
        (left, right, top, bottom) = (xmin * im_width, xmax * im_width,
                                      ymin * im_height, ymax * im_height)
    else:
        (left, right, top, bottom) = (xmin, xmax, ymin, ymax)
    draw.line([(left, top), (left, bottom), (right, bottom), (right, top),
               (left, top)],
              width=thickness,
              fill=color)

    predicted_speed = "n.a."  # means not available, it is just initialization
    predicted_direction = "n.a."  # means not available, it is just initialization

    image_temp = numpy.array(image)
    detected_vehicle_image = image_temp[int(top):int(bottom),
                                        int(left):int(right)]

    if (
            bottom > ROI_POSITION
    ):  # if the vehicle get in ROI area, vehicle predicted_speed predicted_color algorithms are called - 200 is an arbitrary value, for my case it looks very well to set position of ROI line at y pixel 200
        predicted_direction, predicted_speed, is_vehicle_detected, update_csv = speed_prediction.predict_speed(
            top, bottom, right, left, current_frame_number,
            detected_vehicle_image, ROI_POSITION)

    predicted_color = color_recognition_api.color_recognition(
        detected_vehicle_image)

    try:
        font = ImageFont.truetype('arial.ttf', 16)
    except IOError:
        font = ImageFont.load_default()

    # If the total height of the display strings added to the top of the bounding
    # box exceeds the top of the image, stack the strings below the bounding box
    # instead of above.
    display_str_list[0] = predicted_color + " " + display_str_list[0]
    csv_line = predicted_color + "," + str(predicted_direction) + "," + str(
        predicted_speed)  # csv line created
    display_str_heights = [font.getsize(ds)[1] for ds in display_str_list]

    # Each display_str has a top and bottom margin of 0.05x.
    total_display_str_height = (1 + 2 * 0.05) * sum(display_str_heights)

    if top > total_display_str_height:
        text_bottom = top
    else:
        text_bottom = bottom + total_display_str_height

    # Reverse list and print from bottom to top.
    for display_str in display_str_list[::-1]:
        text_width, text_height = font.getsize(display_str)
        margin = np.ceil(0.05 * text_height)
        draw.rectangle([(left, text_bottom - text_height - 2 * margin),
                        (left + text_width, text_bottom)],
                       fill=color)
        draw.text((left + margin, text_bottom - text_height - margin),
                  display_str,
                  fill='black',
                  font=font)
        text_bottom -= text_height - 2 * margin
        return is_vehicle_detected, csv_line, update_csv
Пример #2
0
                                i.setDone()
                        if i.timedOut():
                            index=carsVelocity.index(i)
                            carsVelocity.pop(index)
                            del i

                    if new==True: #If nothing is detected,create new
                        p=vehicles.Car(pid,cx,cy,max_p_age)
                        carsVelocity.append(p)
                        pid+=1
"""
                #cv2.circle(frame,(cx,cy),5,(0,0,255),-1)
                #img=cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
                if (fline.is_passing(cy, cx)):
                    crop_vehicle = frame[y:y + h, x:x + w]
                    color = color_recognition_api.color_recognition(
                        crop_vehicle)
                    cv2.imwrite("firstPass.jpg", crop_vehicle)
                    #cv2.imshow("cropped_first", crop_vehicle)

                if (sline.is_passing(cy, cx)):
                    crop_vehicle = frame[y + int(h / 2):y + h, x:x + w]
                    #color = color_recognition_api.color_recognition(crop_vehicle)
                    #cv2.imshow("cropped_second", crop_vehicle)
                    cv2.imwrite("secondPass.jpg", crop_vehicle)
                    """licPlate = Main.main(frame2)
                    if(licPlate != ""):
                        cv2.imwrite("detected.jpg", crop_vehicle)
                        file = open("LicensePlates.txt", "a")
                        file.write("{}\n".format(licPlate))
                        file.close()
                        print("Detected license plate: " + str(licPlate) + "\n")"""
Пример #3
0
                ####Tracking######
                m = cv2.moments(cnt)
                cx = int(m['m10'] / m['m00'])
                cy = int(m['m01'] / m['m00'])
                x, y, w, h = cv2.boundingRect(cnt)
                crop_vehicle = frame[y:y + h, x:x + w]
                cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

                result, plateCoordinates = Main.main(crop_vehicle)
                if result != "":

                    print("License Plate: " + str(result) + "\n")

                    color = color_recognition_api.color_recognition(
                        frame[int(plateCoordinates[2][1] -
                                  150):int(plateCoordinates[0][1] - 100),
                              int(plateCoordinates[1][0]
                                  ):int(plateCoordinates[3][0])])
                    print("Color: " + str(color) + "\n")
                    print(
                        "-----------------------------------------------------------------\n"
                    )

                    if previous != result:
                        print("Send...\n")
                        r = requests.post(
                            "https://fathomless-plains-27484.herokuapp.com/api/v1/s3M5aCMtypyas8fs1VPHhw/passage",
                            data={
                                'car_num': result,
                                'color': color,
                                'camera_id': 1
Пример #4
0
def draw_bounding_box_on_image(current_frame_number,
                               image,
                               ymin,
                               xmin,
                               ymax,
                               xmax,
                               color='red',
                               thickness=4,
                               display_str_list=(),
                               use_normalized_coordinates=True):

    csv_line = ""
    update_csv = False  # update csv for a new vehicle that are passed from ROI - just one new line for each vehicles
    is_vehicle_detected = [0]
    draw = ImageDraw.Draw(image)
    im_width, im_height = image.size
    if use_normalized_coordinates:
        (left, right, top, bottom) = (xmin * im_width, xmax * im_width,
                                      ymin * im_height, ymax * im_height)
    else:
        (left, right, top, bottom) = (xmin, xmax, ymin, ymax)
    draw.line([(left, top), (left, bottom), (right, bottom), (right, top),
               (left, top)],
              width=thickness,
              fill=color)

    image_temp = numpy.array(image)
    detected_vehicle_image = image_temp[int(top):int(bottom),
                                        int(left):int(right)]

    if (bottom > ROI_POSITION_TOP and bottom < ROI_POSITION_BOTTOM and right <
        (ROI_POSITION_RIGHT + ERROR_FOCTOR_RL) and left >
        (ROI_POSITION_LEFT - ERROR_FOCTOR_RL)):
        is_vehicle_detected = counting.predict_count(
            top, bottom, right, left, current_frame_number,
            detected_vehicle_image, ROI_POSITION_TOP, ROI_POSITION_BOTTOM,
            ROI_POSITION_RIGHT, ROI_POSITION_LEFT, ERROR_FOCTOR_BT)

    predicted_color = color_recognition_api.color_recognition(
        detected_vehicle_image)

    try:
        font = ImageFont.truetype('arial.ttf', 16)
    except IOError:
        font = ImageFont.load_default()

    # If the total height of the display strings added to the top of the bounding
    # box exceeds the top of the image, stack the strings below the bounding box
    # instead of above.
    display_str_list[0] = predicted_color + " " + display_str_list[0]
    csv_line = predicted_color
    display_str_heights = [font.getsize(ds)[1] for ds in display_str_list]

    # Each display_str has a top and bottom margin of 0.05x.
    total_display_str_height = (1 + 2 * 0.05) * sum(display_str_heights)

    if top > total_display_str_height:
        text_bottom = top
    else:
        text_bottom = bottom + total_display_str_height

    # Reverse list and print from bottom to top.
    for display_str in display_str_list[::-1]:
        text_width, text_height = font.getsize(display_str)
        margin = np.ceil(0.05 * text_height)
        draw.rectangle([(left, text_bottom - text_height - 2 * margin),
                        (left + text_width, text_bottom)],
                       fill=color)
        draw.text((left + margin, text_bottom - text_height - margin),
                  display_str,
                  fill='black',
                  font=font)
        text_bottom -= text_height - 2 * margin
        return is_vehicle_detected