OlafenwaMoses / ImageAI

A python library built to empower developers to build applications and systems with self-contained Computer Vision capabilities
https://www.genxr.co/#products
MIT License
8.63k stars 2.19k forks source link

How to store store output or return output of video detection model #233

Open navdeep1604 opened 5 years ago

navdeep1604 commented 5 years ago

I have 100 of videos, I want run in batch and store result of each video (custom detected object) in a variable or file so that I can make later decision through further code( Like if person detcted then do this ). I am not able to understand that how to store data. For frame function only prints the data in console . I want to store result for each video

my code is below. Helpe me write to a text file or store into video_path vriable for each video

video_path=detector.detectCustomObjectsFromVideo(input_file_path=os.path.join(execution_path, "test.mp4"), save_detected_video=False,log_progress=False,

                      detection_timeout=1,per_frame_function=forFrame,return_detected_frame=True)

def forFrame(output_count,frame_number,output_array,returned_frame): print("FOR FRAME " , frame_number) print("Output for each object : ", output_array) print("Output count for unique objects : ", output_count) print("------------END OF A FRAME --------------")

OlafenwaMoses commented 5 years ago

From the forFrame() function, you can use OpenCV to save a the detected images by saving returned_frame as a video. The value returned_frame is a frame/picture of the video after it has been detected, with the bounding boxes and labels added.

navdeep1604 commented 5 years ago

Thanks Sir, Could you help me with little more explanation with code. I am bit new in adressing this. I would appreciate your help

OlafenwaMoses commented 5 years ago

Kindly see the example code linked below.

https://gist.github.com/OlafenwaMoses/a943de4a9c4125d9b2580061b726a6fa

sidb236 commented 5 years ago

Sir, in my case I want to store/ return the output_array of the per_frame_function. I tried returning the output_array as a dataframe using the user defined function "forFrame". I am getting error in this approach. I also tried returning the variables, frame_number, output_array and output_count directly. But the function isn't returning anything. Could you please tell me where I am going wrong and how to save this data? Thanks in advance.

OlafenwaMoses commented 5 years ago

What errors are you getting? Can you share your full code here?

sidb236 commented 5 years ago

Code: def forFrame(frame_number, output_array, output_count): print("FOR FRAME" , frame_number) print("Output for each object : ", output_array) print("Output count for unique objects : ", output_count) print("------------END OF A FRAME --------------") return frame_number, output_array, output_count

video_detector = VideoObjectDetection() video_detector.setModelTypeAsYOLOv3() video_detector.setModelPath('/yolo.h5') video_detector.loadModel()

custom_objects = detector.CustomObjects(car=True, bicycle=True, motorcycle=True, truck=True, bus=True)

video_detector.detectCustomObjectsFromVideo(custom_objects=custom_objects, input_file_path='/overpass.mp4', output_file_path='/overpass_test.mp4' , frames_per_second=10, per_frame_function = forFrame, minimum_percentage_probability=30, log_progress=True)

f_number=[] output=[] total_vehicles=[] f_number, output, output_count = forFrame(frame_number, output_array, output_count) f_number.append(f_number) output.append(output) total_vehicles.append(total_vehicles)

Error: name 'frame_number' is not defined

If the function forFrame is called without the arguements, Error: forFrame() missing 3 required positional arguments: 'frame_number', 'output_array', and 'output_count'

cagnulein commented 5 years ago

i've a similar issue:

i'm getting this error from cv2 when i try to save a detectedFrame:

OpenCV(4.1.0) /io/opencv/modules/imgcodecs/src/loadsave.cpp:662: error: (-2:Unspecified error) could not find a writer for the specified extension in function 'imwrite_'

I run this code:

detections = detector.detectObjectsFromVideo(input_file_path=os.path.join(execution_path , 'videos/'+videofilename), frames_per_second=25, frame_detection_interval=12, minimum_percentage_probability=50, save_detected_video=False, video_complete_function=forSeconds, per_frame_function=forFrame, return_detected_frame=True )

def forFrame(frame_number, output_array, output_count, returned_frame):
    global datevideo
    counter = 0

    for eachItem in output_count:
        #if(eachItem=="person"):
           try:
              name = "people/" + datevideo
              name += str(frame_number)
              cv2.imwrite(name, returned_frame)
           except Exception as e:
              print(e)
           return
cagnulein commented 5 years ago

ops i forgot to add the extension! Solved!

              name = "people/" + datevideo
              name += str(frame_number) + ".jpg"
OlafenwaMoses commented 5 years ago

Code: def forFrame(frame_number, output_array, output_count): print("FOR FRAME" , frame_number) print("Output for each object : ", output_array) print("Output count for unique objects : ", output_count) print("------------END OF A FRAME --------------") return frame_number, output_array, output_count

video_detector = VideoObjectDetection() video_detector.setModelTypeAsYOLOv3() video_detector.setModelPath('/yolo.h5') video_detector.loadModel()

custom_objects = detector.CustomObjects(car=True, bicycle=True, motorcycle=True, truck=True, bus=True)

video_detector.detectCustomObjectsFromVideo(custom_objects=custom_objects, input_file_path='/overpass.mp4', output_file_path='/overpass_test.mp4' , frames_per_second=10, per_frame_function = forFrame, minimum_percentage_probability=30, log_progress=True)

f_number=[] output=[] total_vehicles=[] f_number, output, output_count = forFrame(frame_number, output_array, output_count) f_number.append(f_number) output.append(output) total_vehicles.append(total_vehicles)

Error: name 'frame_number' is not defined

If the function forFrame is called without the arguements, Error: forFrame() missing 3 required positional arguments: 'frame_number', 'output_array', and 'output_count'

@sidb236 apologies for my late response. I have reviewed your code many times and can't figure out what's wrong. Have you tried to install the latest version of ImageAI and try the code again? Please do by running the command below.

pip3 install imageai --upgrade

sidb236 commented 5 years ago

Yes, I am using the latest version of imageai 2.1.4

alexpavlidespyrra commented 1 year ago

Hi all, I just looked at the code. It appears there is no return statement in detectObjectsFromVideo. I got the behaviour you want above by passing the output from video_complete_function to a variable which I passed to a return statement in detectObjectsFromVideo. I am a bit perplexed as to why it is designed this way. Someone in my team got around this by assigning the output of video_complete_function to a class instance variable, and although this works, its not the best way to get the data. @OlafenwaMoses is there a reason why you didn't want to directly return the output?