Beispiel #1
0
def get_average_image_2(df_image):
    cnt=len(df_image.values.tolist())
    img_exp=None
    df2=df_image.copy(deep=True)
    df2.columns = ['fn', 'flg']
    df2['del']=0
    print(df_image.shape)
    for i, dt in enumerate(df_image.values.tolist()):
        path_img, flg = dt
        path_img=path_img.replace('/media/user1/Data/data/20201116/','/home/user1/yu_develop/202011161_sample/image/')
        
        if os.path.exists(path_img)==False:
            df2.iloc[i,2]=1
            continue
        if i%10==0:
          print(i,i/cnt,path_img)
        print(path_img)
        if img_exp is None:
            img, color_org = load_image_from_npy(path_img)
            img_exp=np.expand_dims(img,axis=0)
        if flg == 0 and i>0:
            img, color_org = load_image_from_npy(path_img)
            img_exp_i=np.expand_dims(img,axis=0)
            img_exp=np.concatenate([img_exp,img_exp_i],axis=0)
    print("img_exp shape",img_exp.shape)
    img_avg = np.mean(img_exp, axis=0)
    df3=df2[df2['del']==0]
    df3=df3.loc[:,['fn', 'flg']]
    return np.array(img_avg, dtype=np.uint8),df3
Beispiel #2
0
def get_human_lst():
    path = '/media/user1/Data/data/20201116/'
    patho = '/media/user1/Data/data/Data_out_20201116/'
    path_category_index = '../dt/category_index.npy'
    # "https://tfhub.dev/tensorflow/centernet/hourglass_1024x1024_kpts/1"
    # "https://tfhub.dev/tensorflow/centernet/resnet50v1_fpn_512x512_kpts/1"

    # category_index = np.load(path_category_index, allow_pickle='TRUE').item()
    path_mod = "https://tfhub.dev/tensorflow/efficientdet/d4/1"
    human_dt_lst = object_detection_from_npy_all_file(path, patho, path_mod,
                                                      path_category_index)
    pd.DataFrame(human_dt_lst).to_csv(f'{patho}/human_detect.csv',
                                      header=None,
                                      index=None)
    human_dt_lst = pd.read_csv(f'{patho}/human_detect.csv', header=None)
    image_sum = None
    for i, dt in enumerate(human_dt_lst.values.tolist()):
        path_img, flg = dt
        if i == 0:
            img, color_org = load_image_from_npy(path_img)
            image_sum = np.zeros(img.shape)
        if flg == 0:
            img, color_org = load_image_from_npy(path_img)
            image_sum = image_sum + img
    image_average = image_sum / (i + 1)
    image_average = np.array(image_average, dtype=np.uint8)
    image_average_fp = f'{patho}/image_average.npy'
    np.save(image_average_fp, image_average)

    image_average = np.load(image_average_fp)
    for i, dt in enumerate(human_dt_lst.values.tolist()):
        path_img, flg = dt
        if flg == 1:
            pathoful = path_img.replace(path.replace('/', '\\'), patho)
            img, color_org = load_image_from_npy(path_img)
            image_diff = img - image_average
            image_diff_fp = pathoful.replace('image.npy',
                                             'moving_detection_diff.png')
            # image_diff_fp=f'{pathoful}/moving_detection_diff.png'
            cv2.imwrite(image_diff_fp, image_diff)

            gray = cv2.cvtColor(image_average, cv2.COLOR_BGR2GRAY)
            avg = gray.copy().astype("float")
            img_detect, img_detect_concat, avg = detect_frame(img, avg)
            pil_img = Image.fromarray(img_detect.astype(np.uint8))

            image_diff_fp = pathoful.replace('image.npy',
                                             'moving_detection.png')
            pil_img.save(image_diff_fp)
Beispiel #3
0
def get_human_lst_each_diff():
    path = '/media/user1/Data/data/20201116/'
    patho = '/media/user1/Data/data/Data_out_20201116/'
    camlst=['cam0','cam1','cam2']
    for cam in camlst:
        df = pd.read_csv(f'{patho}/human_detect_{cam}.csv', header=None)
        df_human=df[df[1]==1]
        image_average_fp = f'{patho}/image_average_{cam}.npy'
        image_average=np.load(image_average_fp)
        cnt=len(df_human.values.tolist())
        for i, dt in enumerate(df_human.values.tolist()):
            path_img, flg = dt
            print(i,i/cnt,path_img)
            pathoful = path_img.replace(path, patho)
            img, color_org = load_image_from_npy(path_img)
            image_diff = img - image_average
            image_diff_fp = pathoful.replace('image.npy', 'moving_detection_diff.png')
            #print(image_diff_fp)
            cv2.imwrite(image_diff_fp, image_diff)
            gray = cv2.cvtColor(image_average, cv2.COLOR_BGR2GRAY)
            avg = gray.copy().astype("float")
            img_detect, img_detect_concat, avg = detect_frame(img, avg)
            pil_img = Image.fromarray(img_detect.astype(np.uint8))

            image_diff_fp = pathoful.replace('image.npy', 'moving_detection.png')
            #print(image_diff_fp)
            pil_img.save(image_diff_fp)
Beispiel #4
0
def get_average_image(df_image):
    image_sum = None
    cnt=len(df_image.values.tolist())
    for i, dt in enumerate(df_image.values.tolist()):
        path_img, flg = dt
        if i%100==0:
          print(i,i/cnt,path_img)
        if i == 0:
            img, color_org = load_image_from_npy(path_img)
            image_sum = np.zeros(img.shape)
        if flg == 0:
            img, color_org = load_image_from_npy(path_img)
            image_sum = image_sum + img
    image_average = image_sum / (i + 1)
    image_average = np.array(image_average, dtype=np.uint8)
    return image_average
Beispiel #5
0
 def get_moving_object_mask(self,img_path_moving,img_path_average):
     img, color_org = load_image_from_npy(img_path_moving)
     img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
     # image_average_fp = f'{patho}/image_average_{cam}_s.npy'
     image_average = np.load(img_path_average)
     # image_diff, image_diff2 = get_diff_of_two_image_2(img_gray, image_average_gray)
     # image1, image_diff2, image_diff = compute_difference_cv2(image_average_gray, img, color=[255, 0, 0])
     image_msk, image_org_msk = compute_difference_ssim(image_average, img, color=[1, 1, 1])
     return image_msk,img
Beispiel #6
0
def get_human_lst_each_diff_fun(path, patho, func=None, ext='cv2'):
    # path = '/home/user1/yu_develop/202011161_sample/image/'
    # patho = '/home/user1/yu_develop/202011161_sample/out/'
    camlst = ['cam0', 'cam1', 'cam2']
    if func is None:
        return None, None
    for cam in camlst:
        df = pd.read_csv(f'{patho}/human_detect_{cam}_s.csv', header=None)
        df_human = df[df[1] == 1]
        image_average_fp = f'{patho}/image_average_{cam}_s.npy'
        image_average = np.load(image_average_fp)

        #image_average = cv2.GaussianBlur(image_average, (5, 5), 0)
        image_average_gray = cv2.cvtColor(image_average, cv2.COLOR_BGR2GRAY)
        image_average = cv2.cvtColor(image_average, cv2.COLOR_BGR2RGB)
        cnt = len(df_human.values.tolist())
        for i, dt in enumerate(df_human.values.tolist()):
            path_img, flg = dt
            print(i, i / cnt, path_img)
            path_img = path_img.replace(
                '/media/user1/Data/data/20201116/',
                '/home/user1/yu_develop/202011161_sample/image/')
            pathoful = path_img.replace(path, patho)
            pathoful_fd = pathoful.replace('image.npy', '')
            os.makedirs(pathoful_fd, exist_ok=True)
            img, color_org = load_image_from_npy(path_img)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            # image_diff, image_diff2 = get_diff_of_two_image_2(img_gray, image_average_gray)
            # image1, image_diff2, image_diff = compute_difference_cv2(image_average_gray, img, color=[255, 0, 0])
            image_diff2, image_diff = func(image_average,
                                           img,
                                           color=[255, 0, 0])
            image_diff_fp = pathoful.replace('image.npy',
                                             f'image_org_plus_{ext}.png')
            cv2.imwrite(image_diff_fp, image_diff)

            image_diff_fp = pathoful.replace('image.npy', 'image.png')
            cv2.imwrite(image_diff_fp, img)

            image_diff_fp = pathoful.replace('image.npy',
                                             f'{ext}_moving_detection.png')
            cv2.imwrite(image_diff_fp, image_diff2)

            gray = cv2.cvtColor(image_average, cv2.COLOR_BGR2GRAY)
            avg = gray.copy().astype("float")
            img_detect, img_detect_concat, avg = detect_frame(img, avg)
            pil_img = Image.fromarray(img_detect.astype(np.uint8))

            image_diff_fp = pathoful.replace('image.npy',
                                             f'opencv_moving_detection.png')
            # print(image_diff_fp)
            pil_img.save(image_diff_fp)
Beispiel #7
0
def get_human_lst_each_diff_2():
    path = '/home/user1/yu_develop/202011161_sample/image/'
    patho = '/home/user1/yu_develop/202011161_sample/out/'
    camlst = ['cam0', 'cam1', 'cam2']
    for cam in camlst:
        df = pd.read_csv(f'{patho}/human_detect_{cam}_s.csv', header=None)
        df_human = df[df[1] == 1]
        image_average_fp = f'{patho}/image_average_{cam}_s.npy'
        image_average = np.load(image_average_fp)

        image_average = cv2.GaussianBlur(image_average, (5, 5), 0)
        image_average_gray = cv2.cvtColor(image_average, cv2.COLOR_BGR2GRAY)
        cnt = len(df_human.values.tolist())
        for i, dt in enumerate(df_human.values.tolist()):
            path_img, flg = dt
            print(i, i / cnt, path_img)
            path_img = path_img.replace(
                '/media/user1/Data/data/20201116/',
                '/home/user1/yu_develop/202011161_sample/image/')
            pathoful = path_img.replace(path, patho)
            os.makedirs(pathoful, exist_ok=True)
            img, color_org = load_image_from_npy(path_img)
            img = cv2.GaussianBlur(img, (5, 5), 0)
            img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            image_diff, image_diff2 = get_diff_of_two_image_2(
                img_gray, image_average_gray)
            image_diff_fp = pathoful.replace(
                'image.npy', 'moving_detection_diff_gaus_gray.png')
            cv2.imwrite(image_diff_fp, image_diff)
            # image_diff_fp = pathoful.replace('image.npy', 'moving_detection_diff.npy')
            #np.save(image_diff_fp, image_diff)

            image_diff_fp = pathoful.replace(
                'image.npy', 'moving_detection_diff_2_gaus_gray.png')
            cv2.imwrite(image_diff_fp, image_diff2)
            # image_diff_fp = pathoful.replace('image.npy', 'moving_detection_diff_2.npy')
            #np.save(image_diff_fp, image_diff2)

            gray = cv2.cvtColor(image_average, cv2.COLOR_BGR2GRAY)
            avg = gray.copy().astype("float")
            img_detect, img_detect_concat, avg = detect_frame(img, avg)
            pil_img = Image.fromarray(img_detect.astype(np.uint8))

            image_diff_fp = pathoful.replace('image.npy',
                                             'moving_detection_gaus.png')
            #print(image_diff_fp)
            pil_img.save(image_diff_fp)