Exemple #1
0
    def __analize_frames_differences__(self, video_path, max_frames=50):
        frames, imgs_paths = self.__load_video_frames__(video_path, as_grey=False)
        
        #Original Dynamic Image
        if len(frames) > max_frames:
            frames2di = frames[0:max_frames]
        else:
            frames2di = frames
        imgPIL, img = getDynamicImage(frames2di)
        imgPIL = imgPIL.convert("RGB")
        img = np.array(imgPIL)
        cv2.imshow("Raw Dynamic Image", img)
        key = cv2.waitKey(0)

        candidate_frames, frames_indexes = self.__extract_candidate_frames_fromFramesList__(frames)

        #Dynamic Image from keyframes
        imgPIL, img = getDynamicImage(candidate_frames)
        imgPIL = imgPIL.convert("RGB")
        img = np.array(imgPIL)
        cv2.imshow("Dynamic Image from Keyframes", img)
        key = cv2.waitKey(0)

        # for i, cframe in enumerate(candidate_frames):
        #     text = imgs_paths[frames_indexes[i]]
        #     # show the image
        #     cv2.putText(cframe, "{}: {:.2f}".format(text, i), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 3)
        #     cv2.imshow("Image", cframe)
        #     key = cv2.waitKey(0)
        return frames, candidate_frames, frames_indexes
Exemple #2
0
    def __getitem_blur__(self, vid_name):
        dynamicImages = []
        nelem = 1
        sequence = os.listdir(vid_name)
        sequence.sort(key=lambda f: int("".join(filter(str.isdigit, f))))
        frames, paths = self.loadFramesSeq(vid_name, sequence)

        _, indices_segments = self.getVideoSegments(vid_name, idx)
        # print(len(frames),len(indices_segments))
        # ca2@i@VnTCC5TDK
        segment_idxs = []
        for i, idxs in enumerate(indices_segments):
            frames_in_segment = list(
                itemgetter(*idxs)(frames))  #np.asarray(frames)[idxs].tolist()
            blurrings = self.extractor.__compute_frames_blurring_fromList__(
                frames_in_segment, plot=False)
            indexes_candidates = self.extractor.__candidate_frames_blur_based__(
                frames_in_segment, blurrings, self.useKeyframes, nelem=nelem)

            if nelem > 1:
                indexes_candidates = list(
                    itemgetter(*indexes_candidates)(idxs))
                segment_idxs += indexes_candidates
            else:
                indexes_candidates = idxs[indexes_candidates[0]]
                segment_idxs.append(indexes_candidates)
        segment_idxs.sort()
        candidate_frames = list(itemgetter(*segment_idxs)(frames))

        imgPIL, img = dynamicImage.getDynamicImage(candidate_frames)
        imgPIL = self.spatial_transform(imgPIL.convert("RGB"))
        dynamicImages.append(imgPIL)
        return dynamicImages
Exemple #3
0
 def __getitem_diff__(self, vid_name):
     dynamicImages = []
     sequence = os.listdir(vid_name)
     sequence.sort(key=lambda f: int("".join(filter(str.isdigit, f))))
     video_segment, paths = self.loadFramesSeq(vid_name, sequence)
     candidate_frames, frames_indexes = self.extractor.__extract_candidate_frames_fromFramesList__(
         video_segment)
     # print('Frames indexes-{}/{}='.format(len(sequence),len(frames_indexes)))
     imgPIL, img = dynamicImage.getDynamicImage(candidate_frames)
     imgPIL = self.spatial_transform(imgPIL.convert("RGB"))
     dynamicImages.append(imgPIL)
     return dynamicImages
Exemple #4
0
    def __avg_dynamic_image__(self, frames, tempWindowLen):
        dimages = []
        indices = [x for x in range(0, len(frames), 1)]
        indices_segments = [indices[x:x + tempWindowLen] for x in range(0, len(indices), tempWindowLen)]

        for i, indices_segment in enumerate(indices_segments): #Generate segments using indices
            segment = np.asarray(frames)[indices_segment].tolist()
            
            imgPIL, img = getDynamicImage(segment)
            dimg = self.__format_dynamic_image__(imgPIL)
            dimg = cv2.cvtColor(dimg, cv2.COLOR_RGB2BGR)
            dimages.append(dimg)
        
        # avg = np.sum(np.array(dimages), axis=0)
        avg=0
        
        return dimages, avg
Exemple #5
0
    def __getitem__(self, idx):
        vid_name = self.videos[idx]
        # print(vid_name, self.numFrames[idx])
        label = self.labels[idx]
        dynamicImages = []
        ipts = []
        # preprocessing_time = 0.0
        video_segments, _ = self.getVideoSegments(
            vid_name, idx
        )  # bbox_segments: (1, 16, 6)= (no segments,no frames segment,info
        paths = []
        rgb_central_frames = []
        for i, sequence in enumerate(video_segments):
            video_segments[i], pths = self.loadFramesSeq(vid_name, sequence)
            paths.append(pths)

        for sequence in video_segments:
            # start_time = time.time()
            imgPIL, img = dynamicImage.getDynamicImage(sequence)
            rgb = sequence[int(len(sequence) / 2)]
            rgb_pil = Image.fromarray(np.uint8(rgb)).convert('RGB')
            rgb_central_frames.append(self.rgb_transform(rgb_pil))
            # end_time = time.time()
            # preprocessing_time += (end_time - start_time)
            dynamicImages.append(np.array(imgPIL))
            ipts.append(self.spatial_transform(imgPIL.convert("RGB")))
        ipts = torch.stack(ipts, dim=0)  #torch.Size([bs, ndi, ch, h, w])
        rgb_central_frames = torch.stack(rgb_central_frames, dim=0)
        # m_bboxes = bbox_from_di(dynamicImages, num_imgs=5, plot=False)
        m_bboxes = [0]
        gt_bboxes, one_box = None, None
        if self.dataset == 'ucfcrime2local':
            gt_bboxes, one_box = load_bbox_gt(vid_name, label, paths[0])
        else:
            one_box = m_bboxes
            # print('m_bboxes: ', len(one_box))
            # gt_bboxes, one_box = [-1, -1, -1, -1], [0, 0, 224, 224]

        one_box = torch.from_numpy(np.array(one_box)).float()

        return (ipts, idx, dynamicImages, one_box, rgb_central_frames), label
Exemple #6
0
def main():
    extractor = FrameExtractor(len_window=5)
    # datasetAll = [os.path.join(constants.PATH_HOCKEY_FRAMES_VIOLENCE, '24'),
    #                 os.path.join(constants.PATH_VIF_FRAMES, '1', 'Violence', 'crowd_violence__Man_Utd_vs_Roma_Crowd_Trouble__uncychris__ZGI5vlDMpJA'),
    #                 os.path.join(constants.PATH_RWF_2000_FRAMES, 'train', 'Fight', '_2RYnSFPD_U_0')]
    
    # datasetAll = [os.path.join(constants.PATH_RWF_2000_FRAMES, 'train', 'Fight', '_2RYnSFPD_U_0'),
    #                 os.path.join(constants.PATH_RWF_2000_FRAMES, 'train', 'Fight', '_q5Nwh4Z6ao_1'),
    #                 os.path.join(constants.PATH_RWF_2000_FRAMES, 'train', 'NonFight','i2sLegg2JPA_1')]

    # images_folder = os.path.join(constants.PATH_RWF_2000_FRAMES, 'train', 'Fight','GafFu4IZtIA_0')
    # datasetAll, labelsAll, numFramesAll = hockeyLoadData(shuffle=True)
    # datasetAll, labelsAll, numFramesAll, splitsLen = vifLoadData(constants.PATH_VIF_FRAMES)
    train_names, train_labels, train_num_frames, test_names, test_labels, test_num_frames = rwf_load_data()
    datasetAll = train_names + test_names
    
    bb = []
    nelem = 1
    seqLen = 15
    dynamicImgLen = [5, 10, 15, 20]
    for k, video_path in enumerate(datasetAll):
        print()
        
        _, video_name = os.path.split(video_path)

        frames, imgs_paths = extractor.__load_video_frames__(video_path) # Load all frames
        print(len(imgs_paths), video_path)
        indices_segments = extractor.__getVideoSegments__(video_path, seqLen=seqLen) # split all video in shots of seqLen frames
        # print(len(video_segments), video_segments)
        print(indices_segments)
        segment_idxs = []
        for i,idxs in enumerate(indices_segments):

            frames_in_segment = list(itemgetter(*idxs)(frames)) #np.asarray(frames)[idxs].tolist()
            blurrings = extractor.__compute_frames_blurring_fromList__(frames_in_segment, plot=False)
            # print('blurrings=',blurrings)
            indexes_candidates = extractor.__candidate_frames_blur_based__(frames_in_segment, blurrings, 'blur-min', nelem=nelem)
            # print('indexes_candidates1=',indexes_candidates, type(indexes_candidates))

            if nelem > 1:
                indexes_candidates = list(itemgetter(*indexes_candidates)(idxs))
                segment_idxs += indexes_candidates
                print('Segment i{}-(len={}/candidates={})'.format(i+1,len(frames_in_segment), len(indexes_candidates)))
            else:
                indexes_candidates = idxs[indexes_candidates[0]]
                segment_idxs.append(indexes_candidates)
                print('Segment i{}-(len={}/candidates={})'.format(i+1,len(frames_in_segment), 1))
            
            print('indexes_candidates=',indexes_candidates)
            

        segment_idxs.sort()
        candidate_frames = list(itemgetter(*segment_idxs)(frames))
        print('CAndidate frames  len({}), indexes={}'.format(len(candidate_frames),segment_idxs))

        dyn_image, _ = getDynamicImage(frames[0:30]) #all frames
        dyn_image = extractor.__format_dynamic_image__(dyn_image)
        cv2.imshow("dyn_image", dyn_image)
        key = cv2.waitKey(0)

        dyn_image_cand, _ = getDynamicImage(candidate_frames) #candidate frames
        dyn_image_cand = extractor.__format_dynamic_image__(dyn_image_cand)
        cv2.imshow("dyn_image_cand", dyn_image_cand)
        key = cv2.waitKey(0)
Exemple #7
0
def main():
    segment_len = 5
    splits = [0, 5, 10, 15, 20, 30]

    video_segments = []

    # segment, central_frame = load_video_segment(os.path.join(constants.PATH_VIF_FRAMES, '1/Violence','fans_violence__Slovak_chauvinism_nationalism_extremism_intolerance_and_all_form'), init_frame=0, num_frames=segment_len)
    segment, central_frame = load_video_segment(os.path.join(
        constants.PATH_VIF_FRAMES, '1/NonViolence',
        'peaceful_football_crowds__MaximsNewsNetwork_SUDAN_ELECTIONS_YASIR_ARMAN_OMAR_AL_BASHIR_UNM'
    ),
                                                init_frame=0,
                                                num_frames=segment_len)
    # segment, central_frame = load_video_segment(os.path.join(constants.PATH_HOCKEY_FRAMES_VIOLENCE, '127'), init_frame=0, num_frames=segment_len)
    # segment, central_frame = load_video_segment(os.path.join(constants.PATH_UCFCRIME2LOCAL_FRAMES_VIOLENCE, 'Arrest002-VSplit1'), init_frame=10, num_frames=segment_len)
    print('Segment Len=', len(segment))
    imgPIL, img = getDynamicImage(segment)
    imcv = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
    gray = cv2.cvtColor(imcv, cv2.COLOR_BGR2GRAY)
    print('Image shape=', gray.shape)  #(288, 360)

    # Calculate gradient RGB
    # sobelx = cv2.Sobel(imcv, cv2.CV_64F, 1, 0, ksize=5)
    # sobely = cv2.Sobel(imcv, cv2.CV_64F, 0, 1, ksize=5)
    # mag, angle = cv2.cartToPolar(sobelx, sobelx, angleInDegrees=True)
    # print('Magnitude: ',mag.shape, '-Angles: ', angle.shape)

    pixels_per_cell = (8, 8)
    n_orientations = 128
    cells_per_block = (1, 1)
    fd_rgb, hog_image_rgb = hog(imcv,
                                orientations=n_orientations,
                                pixels_per_cell=pixels_per_cell,
                                cells_per_block=cells_per_block,
                                visualize=True,
                                multichannel=True,
                                feature_vector=False)
    fd_gray, hog_image_gray = hog(gray,
                                  orientations=n_orientations,
                                  pixels_per_cell=pixels_per_cell,
                                  cells_per_block=cells_per_block,
                                  visualize=True,
                                  multichannel=False,
                                  feature_vector=False)
    print('**hog_image_rgb: ', hog_image_rgb.shape, '-hog_image_gray: ',
          hog_image_gray.shape)
    print('****descriptor_rgb: ', fd_rgb.shape, '-descriptor_gray: ',
          fd_gray.shape)
    rows = 1
    cols = 3

    # plt.subplot(rows,cols,1),plt.imshow(imcv,cmap = 'gray')
    # plt.title('Original'), plt.xticks([]), plt.yticks([])
    # plt.subplot(rows,cols,2),plt.imshow(sobelx,cmap = 'gray')
    # plt.title('Sobel X'), plt.xticks([]), plt.yticks([])
    # plt.subplot(rows,cols,3),plt.imshow(sobely,cmap = 'gray')
    # plt.title('Sobel Y'), plt.xticks([]), plt.yticks([])
    # plt.subplot(rows,cols,4),plt.imshow(hog_image_rgb,cmap = 'gray')
    # plt.title('SKImg HOG'), plt.xticks([]), plt.yticks([])

    # gray_sobelx8u = cv2.Sobel(gray, cv2.CV_8U, 1, 0, ksize=5)
    # gray_sobely8u = cv2.Sobel(gray, cv2.CV_8U, 0, 1, ksize=5)
    # plt.subplot(rows,cols,5),plt.imshow(gray,cmap = 'gray')
    # plt.title('Original_Gray'), plt.xticks([]), plt.yticks([])
    # plt.subplot(rows,cols,6),plt.imshow(gray_sobelx8u,cmap = 'gray')
    # plt.title('Sobel CV_8U x'), plt.xticks([]), plt.yticks([])
    # plt.subplot(rows,cols,7),plt.imshow(gray_sobely8u,cmap = 'gray')
    # plt.title('Sobel CV_8U y'), plt.xticks([]), plt.yticks([])

    # Calculate gradient GRAY
    gray_sobelx64 = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=5)
    gray_sobely64 = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=5)
    magnitudes_gray, orientations_gray = cv2.cartToPolar(gray_sobelx64,
                                                         gray_sobely64,
                                                         angleInDegrees=True)
    print('Magnitude_gray: ', magnitudes_gray.shape, '-Angles_gray: ',
          orientations_gray.shape)
    print('Angles. Max={}, Min={}'.format(np.amax(orientations_gray),
                                          np.amin(orientations_gray)))

    plt.subplot(rows, cols, 1), plt.imshow(gray, cmap='gray')
    plt.title('Original_Gray'), plt.xticks([]), plt.yticks([])
    plt.subplot(rows, cols, 2), plt.imshow(central_frame)
    plt.title('Frame'), plt.xticks([]), plt.yticks([])
    # plt.subplot(rows,cols,6),plt.imshow(gray_sobelx64,cmap = 'gray')
    # plt.title('Sobel CV_64F x'), plt.xticks([]), plt.yticks([])
    # plt.subplot(rows,cols,7),plt.imshow(gray_sobely64,cmap = 'gray')
    # plt.title('Sobel CV_64F y'), plt.xticks([]), plt.yticks([])
    plt.subplot(rows, cols, 3), plt.imshow(hog_image_gray, cmap='gray')
    plt.title('SKImg HOG'), plt.xticks([]), plt.yticks([])
    plt.show()

    entropies = compute_entropy(fd_gray, entropy_type='own', threshold=0)
    heat_map = sb.heatmap(entropies, annot=False)
    plt.title('Entropy')
    plt.show()