Example #1
0
def save_heatmap(args):
    img = args[0]
    for idx, att in enumerate(args[1]):
        heatmap.add(img, att, alpha=0.4, save=f'{global_att_dir}/{global_epoch}_{args[2]}_{idx}.png', axis='off')
        #             all_boxes[j][i] = all_boxes[j][i][keep, :]

        misc_toc = time.time()
        nms_time = misc_toc - misc_tic

        sys.stdout.write('im_detect: {:d}/{:d} {:.3f}s {:.3f}s   \r' \
            .format(i + 1, num_images, detect_time, nms_time))
        sys.stdout.flush()

        output_dir = '/data/Salient_Image_data/output/' + input_img_path.split(
            '/')[-2]
        if not os.path.exists(output_dir): os.makedirs(output_dir)
        output_path = os.path.join(output_dir, input_img_path.split('/')[-1])

        heat_map_f = ndimage.filters.gaussian_filter(heat_map, sigma=40)
        heatmap.add(im2show, heat_map_f, alpha=0.7, save=output_path)
        # IPython.embed()

        #
        # if vis:
        #     # print('save image to r')
        #     cv2.imwrite('result.png', im2show)
        #
        #     # pdb.set_trace()
        #     #cv2.imshow('test', im2show)
        #     #cv2.waitKey(0)

    # with open(det_file, 'wb') as f:
    #     pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)
    #
    # print('Evaluating detections')
import heatmap
from scipy import ndimage
from skimage import io
import numpy as np

# read image
image_filename = '../data/face.png'
image = io.imread(image_filename)

# create heat map
x = np.zeros((101, 101))
x[50, 50] = 1
heat_map = ndimage.filters.gaussian_filter(x, sigma=16)


heatmap.add(image, heat_map, alpha=0.7, save='../data/face_heat_map.png')


for alpha in np.arange(0, 1.1, 0.1):
    print('alpha ' + str(alpha))
    heatmap.add(image, heat_map, alpha=alpha, save='../data/face_heat_map_' + str(alpha) + '.png', axis='off')
Example #4
0
def detect_referee(hg_matrix):
    global referee_position
    global input_ball
    global flag

    #İlk açılan pencereler
    win_Settings(0)

    #Hakem rengi belirlemek için
    low_color = np.array([35, 0, 150])
    high_color = np.array([85, 170, 255])

    #Daha önce oluşturulan saha içi maske
    area_mask = cv2.imread(field_mask_path)

    cap = cv2.VideoCapture(vid_filepath)
    #Hakemin ilk konumunu belirlemek ve konum bulmak için pencere boyutları
    resize_x = 40
    resize_y = 30
    frame = set_object_pos(cap)
    frame = init_frames(frame, resize_x, resize_y)

    x = 0
    y = 0
    w = 0
    h = 0

    #Top_view'e konum atmak için arraylar
    input_pts = []
    input_referee = [[0, 0], 'r']
    input_ball = [[0, 0], 'null']
    feet_coord = [0, 0]

    #Top_View boyutlarında ısı haritası oluşturma
    heatmap.create_heatmap()
    heat_key = True
    mask_key = False

    #-Hakemin her iki saniyedeki hızı
    print('Starting..\n\n')
    print(
        'Click anywhere on Main_Frame to determine the distance.\n L-Mouse: Choose point\n R-Mouse: Remove point'
    )
    second = 2
    rt = speed.RepeatedTimer(second, speed.flag)

    #----- Hakemin tespit edilip işaretlendiği kısım ---------
    while True:
        ret, frame = cap.read()

        if ret:
            area_masked = cv2.bitwise_and(frame, area_mask)
            cropped_frame_referee = area_masked[resized_r[0]:resized_r[1],
                                                resized_r[2]:resized_r[3]]

            #Maskeleme işlemleri
            masked = image_masks(cropped_frame_referee, low_color, high_color)

            image, contours, hierarchy = cv2.findContours(
                masked, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
            if (len(contours) <= 0):
                cv2.rectangle(cropped_frame_referee, (x, y), (x + w, y + h),
                              (0, 0, 255), 2)
                cv2.rectangle(frame, (x + resized_r[2], y + resized_r[0]),
                              (x + w + resized_r[2], y + h + resized_r[0]),
                              (0, 255, 0), 2)
            else:
                for contour in contours:
                    (x, y, w, h) = cv2.boundingRect(contour)

                    if ((w < 22 or h < 20) or (w > 40 or h > 45)): continue
                    feet_coord = [
                        float((x + resized_r[2]) + int(25 / 2.0)),
                        float((y + resized_r[0]) + 25)
                    ]
                    #                    feet_coord = [x + w/2, y + h)]
                    input_referee = [feet_coord, 'r']
                    cv2.rectangle(cropped_frame_referee, (x, y),
                                  (x + w, y + h), (0, 0, 255), 2)
                    cv2.rectangle(frame, (x + resized_r[2], y + resized_r[0]),
                                  (x + w + resized_r[2], y + h + resized_r[0]),
                                  (0, 255, 0), 2)

                    if (x != 0 and y <= 5): set_resized_r(0, frame_lenx)
                    if (y >= resize_x * 2 - h - 5):
                        set_resized_r(1, frame_lenx)
                    if (x <= 5 and y != 0): set_resized_r(2, frame_lenx)
                    if (x >= resize_y * 2 - w - 5):
                        set_resized_r(3, frame_lenx)

            cv2.setMouseCallback('Main_Frame', on_mouse_click,
                                 [x + resized_r[2], y + resized_r[0]])
            input_pts = [input_referee, input_ball]
            top_img, player_top_points = top_view.create_topview(
                hg_matrix, input_pts)
            heatmap_mask = heatmap.add(player_top_points[0][0])

            ref_speed = speed.cal_speed(time.localtime().tm_sec,
                                        player_top_points[0][0])
            top_img = drawoffside.drawspeed(top_img, player_top_points,
                                            ref_speed)

            if (player_top_points[1][1][0] == 'b'):
                rb_distance = distance.cal_distance(player_top_points)
                top_img = drawoffside.drawLine(top_img, player_top_points,
                                               rb_distance)

            cv2.imshow('Main_Frame', frame)
            cv2.imshow('Top_View', top_img)

            #Gereksiz kalabalık m tuşuna basınca gözükmesi ve kapanması için
            if mask_key:
                win_Settings(1)
                cv2.imshow('Mask_Frame', masked)
                cv2.imshow('Cropped_Frame', cropped_frame_referee)
            else:
                cv2.destroyWindow('Mask_Frame')
                cv2.destroyWindow('Cropped_Frame')

        else:
            print("ret, false, video bitti")
            win_Settings(2)
            colored_heatmap = heatmap.colorize(heatmap_mask)
            cv2.imshow('Heat_Map_Mask', heatmap_mask)
            cv2.imshow('Heat_Map_Color', colored_heatmap)

        #Tuş takımı
        key = cv2.waitKey(15) & 0xFF
        if key == ord('h'):
            if heat_key:
                win_Settings(2)
                colored_heatmap = heatmap.colorize(heatmap_mask)
                cv2.imshow('Heat_Map_Mask', heatmap_mask)
                cv2.imshow('Heat_Map_Color', colored_heatmap)
                heat_key = False
            else:
                heat_key = True
                cv2.destroyWindow('Heat_Map_Mask')
                cv2.destroyWindow('Heat_Map_Color')

        if key == ord('m'):
            if mask_key:
                mask_key = False
            else:
                mask_key = True

        if key == ord('q'):
            break

    rt.stop()
    cap.release()
    cv2.destroyAllWindows()