ybaik / dense-optical-flow

Dense optical flow test code
0 stars 4 forks source link

ValueError: cannot convert float NaN to integer #1

Open sarmadm opened 5 years ago

sarmadm commented 5 years ago

Hi

Could you please help me with this error , I'm trying to read images from a folder and save the output into another folder , but this error shows :

ValueError Traceback (most recent call last)

in 52 optical_flow = cv2.DualTVL1OpticalFlow_create() 53 flow = optical_flow.calc(img_curr, img_next, None) ---> 54 bgr = flow2bgr_middlebury(flow) 55 cv2.imshow('flow', bgr) 56

~\Documents\deep_L_projects\optical_flow\utils\flow_process.py in flow2bgr_middlebury(flow) 103 a = np.arctan2(-v[y, x], -u[y, x]) / np.pi 104 fk = (a + 1) / 2 * (ncols - 1) --> 105 k0 = int(fk) 106 k1 = (k0 + 1) % ncols 107 f = fk - k0

ValueError: cannot convert float NaN to integer

`

import sys import cv2 import os

packpath = 'C:/Users/sarmad/Documents/deep_L_projects/optical_flow/'

if not(packpath in sys.path): sys.path.append(packpath)

import flow_process import flow_io import flow_eval import os import pandas as pd import numpy as np import random import cv2 import glob import sys

from utils.flow_process import flow2bgr_middlebury from utils.flow_io import read_flo, read_flow_kitti, write_flo from utils.flow_eval import epe_rate, epe_average

input_path = 'C:/Users/sarmad/Documents/dev/dev_02/in' output_path = 'C:/Users/sarmad/Documents/dev/dev_02/out/'

video_dir = os.listdir(input_path) for ind in video_dir: vid_dir = os.path.join(input_path, ind) save_dir = os.path.join(output_path, ind) print(vid_dir) images = glob.glob(os.path.join(vid_dir,'*')) print ("Processing {}: {} files... ".format(vid_dir, len(images))), sys.stdout.flush() img_curr = cv2.cvtColor(cv2.imread(images[0]),cv2.COLOR_BGR2GRAY)

    for ret, frames2 in enumerate(images):
            img_next = cv2.cvtColor(cv2.imread(frames2),cv2.COLOR_BGR2GRAY)

            if len(img_curr.shape) != 2:
                img_curr = cv2.cvtColor(img_curr, cv2.COLOR_BGR2GRAY)
                img_next = cv2.cvtColor(img_next, cv2.COLOR_BGR2GRAY)

            optical_flow = cv2.DualTVL1OpticalFlow_create()
            flow = optical_flow.calc(img_curr, img_next, None)
            bgr = flow2bgr_middlebury(flow)
            cv2.imshow('flow', bgr)

#  optical_flow = cv2.FarnebackOpticalFlow_create()

# do coloring

cv2.waitKey(0)

`

ybaik commented 5 years ago

Please give me your example image.

I think there is NaN value of a when a = np.arctan2(-v[y, x], -u[y, x]) / np.pi. Please add a routine to change 'NaN' to '0' after the above function.

Thanks, Youngki

2019년 1월 27일 (일) 오전 8:24, sarmadm notifications@github.com님이 작성:

Hi

Could you please help me with this error , I'm trying to read images from a folder and save the output into another folder , but this error shows :

ValueError Traceback (most recent call last) in 52 optical_flow = cv2.DualTVL1OpticalFlow_create() 53 flow = optical_flow.calc(img_curr, img_next, None) ---> 54 bgr = flow2bgr_middlebury(flow) 55 cv2.imshow('flow', bgr) 56

~\Documents\deep_L_projects\optical_flow\utils\flow_process.py in flow2bgr_middlebury(flow) 103 a = np.arctan2(-v[y, x], -u[y, x]) / np.pi 104 fk = (a + 1) / 2 * (ncols - 1) --> 105 k0 = int(fk) 106 k1 = (k0 + 1) % ncols 107 f = fk - k0

ValueError: cannot convert float NaN to integer

`

import sys import cv2 import os

packpath = 'C:/Users/sarmad/Documents/deep_L_projects/optical_flow/'

if not(packpath in sys.path): sys.path.append(packpath)

import flow_process import flow_io import flow_eval import os import pandas as pd import numpy as np import random import cv2 import glob import sys

from utils.flow_process import flow2bgr_middlebury from utils.flow_io import read_flo, read_flow_kitti, write_flo from utils.flow_eval import epe_rate, epe_average

input_path = 'C:/Users/sarmad/Documents/dev/dev_02/in' output_path = 'C:/Users/sarmad/Documents/dev/dev_02/out/'

video_dir = os.listdir(input_path) for ind in video_dir: vid_dir = os.path.join(input_path, ind) save_dir = os.path.join(output_path, ind) print(vid_dir) images = glob.glob(os.path.join(vid_dir,'*')) print ("Processing {}: {} files... ".format(vid_dir, len(images))), sys.stdout.flush() img_curr = cv2.cvtColor(cv2.imread(images[0]),cv2.COLOR_BGR2GRAY)

for ret, frames2 in enumerate(images):
        img_next = cv2.cvtColor(cv2.imread(frames2),cv2.COLOR_BGR2GRAY)

        if len(img_curr.shape) != 2:
            img_curr = cv2.cvtColor(img_curr, cv2.COLOR_BGR2GRAY)
            img_next = cv2.cvtColor(img_next, cv2.COLOR_BGR2GRAY)

        optical_flow = cv2.DualTVL1OpticalFlow_create()
        flow = optical_flow.calc(img_curr, img_next, None)
        bgr = flow2bgr_middlebury(flow)
        cv2.imshow('flow', bgr)

optical_flow = cv2.FarnebackOpticalFlow_create()

do coloring

cv2.waitKey(0)

`

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ybaik/dense-optical-flow/issues/1, or mute the thread https://github.com/notifications/unsubscribe-auth/ALaD_Lk2afVW9LPLrv4P37XRGdFU4he-ks5vHOOogaJpZM4aUcUo .

sarmadm commented 5 years ago

Thanks very much for answering , I sent you email please see it

Regards

ybaik commented 5 years ago

I fixed the issue. Thank you!

sarmadm commented 5 years ago

HI

Thanks for your help , I have updated flow_process.py but it is still showing this error:

` Processing C:/Users/Documents/deep_L_projects/optical_flow/dense/in/1: 32 files...

ValueError Traceback (most recent call last)

in 48 optical_flow = cv2.DualTVL1OpticalFlow_create() 49 flow = optical_flow.calc(img_curr, img_next, None) ---> 50 bgr = flow2bgr_middlebury(flow) 51 cv2.imshow('flow', bgr) 52 ~\Documents\deep_L_projects\optical_flow\utils\flow_process.py in flow2bgr_middlebury(flow) 103 if not is_valid(ud, vd): 104 continue --> 105 106 rad = mag[y, x] 107 a = np.arctan2(-v[y, x], -u[y, x]) / np.pi ValueError: cannot convert float NaN to integer `
ybaik commented 5 years ago

Please use "optical_flow = cv2.DualTVL1OpticalFlow_create()" at the outside of the loop. And you can use flow2bgr() instead of flow2bgr_middlebury().

ybaik commented 5 years ago

I couldn't reproduce the error. Please let me know the pair image names when the error is observed.