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.62k stars 2.19k forks source link

ValueError: An error occured. It may be that your input video is invalid. #391

Closed rEufrazio closed 4 years ago

rEufrazio commented 5 years ago

The code I am trying to run is the one below, and it is almost identical to the one available at the documentation for "Video and Live-Feed Detection and Analysis":

from imageai.Detection import VideoObjectDetection
#from imutils.video import WebcamVideoStream
from matplotlib import pyplot as plt
import cv2
import os

execution_path = r'C:\Users\rafael.e.araujo\Desktop\Arquivos\TCC\Python Scripts\object detection'
color_index = {'bus': 'red', 'handbag': 'steelblue', 'giraffe': 'orange', 'spoon': 'gray', 'cup': 'yellow', 'chair': 'green', 'elephant': 'pink', 'truck': 'indigo', 'motorcycle': 'azure', 'refrigerator': 'gold', 'keyboard': 'violet', 'cow': 'magenta', 'mouse': 'crimson', 'sports ball': 'raspberry', 'horse': 'maroon', 'cat': 'orchid', 'boat': 'slateblue', 'hot dog': 'navy', 'apple': 'cobalt', 'parking meter': 'aliceblue', 'sandwich': 'skyblue', 'skis': 'deepskyblue', 'microwave': 'peacock', 'knife': 'cadetblue', 'baseball bat': 'cyan', 'oven': 'lightcyan', 'carrot': 'coldgrey', 'scissors': 'seagreen', 'sheep': 'deepgreen', 'toothbrush': 'cobaltgreen', 'fire hydrant': 'limegreen', 'remote': 'forestgreen', 'bicycle': 'olivedrab', 'toilet': 'ivory', 'tv': 'khaki', 'skateboard': 'palegoldenrod', 'train': 'cornsilk', 'zebra': 'wheat', 'tie': 'burlywood', 'orange': 'melon', 'bird': 'bisque', 'dining table': 'chocolate', 'hair drier': 'sandybrown', 'cell phone': 'sienna', 'sink': 'coral', 'bench': 'salmon', 'bottle': 'brown', 'car': 'silver', 'bowl': 'maroon', 'tennis racket': 'palevilotered', 'airplane': 'lavenderblush', 'pizza': 'hotpink', 'umbrella': 'deeppink', 'bear': 'plum', 'fork': 'purple', 'laptop': 'indigo', 'vase': 'mediumpurple', 'baseball glove': 'slateblue', 'traffic light': 'mediumblue', 'bed': 'navy', 'broccoli': 'royalblue', 'backpack': 'slategray', 'snowboard': 'skyblue', 'kite': 'cadetblue', 'teddy bear': 'peacock', 'clock': 'lightcyan', 'wine glass': 'teal', 'frisbee': 'aquamarine', 'donut': 'mincream', 'suitcase': 'seagreen', 'dog': 'springgreen', 'banana': 'emeraldgreen', 'person': 'honeydew', 'surfboard': 'palegreen', 'cake': 'sapgreen', 'book': 'lawngreen', 'potted plant': 'greenyellow', 'toaster': 'ivory', 'stop sign': 'beige', 'couch': 'khaki'}
resized = False

def forFrame(frame_number, output_array, output_count, returned_frame):

    plt.clf()

    this_colors = []
    labels = []
    sizes = []

    counter = 0

    for eachItem in output_count:
        counter += 1
        labels.append(eachItem + " = " + str(output_count[eachItem]))
        sizes.append(output_count[eachItem])
        this_colors.append(color_index[eachItem])

    global resized

    if (resized == False):
        manager = plt.get_current_fig_manager()
        manager.resize(width=1000, height=500)
        resized = True

    plt.subplot(1, 2, 1)
    plt.title("Frame : " + str(frame_number))
    plt.axis("off")
    plt.imshow(returned_frame, interpolation="none")

    plt.subplot(1, 2, 2)
    plt.title("Analysis: " + str(frame_number))
    plt.pie(sizes, labels=labels, colors=this_colors, shadow=True, startangle=140, autopct="%1.1f%%")

    plt.pause(0.01)

#camera = WebcamVideoStream(src = 1).start()
camera = cv2.VideoCapture(0)

detector = VideoObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath(os.path.join(execution_path, "yolo.h5"))
detector.loadModel(detection_speed = "flash")

plt.show()

video_path = detector.detectObjectsFromVideo(camera_input = camera,
                                             save_detected_video = False,
                                             frames_per_second = 10,
                                             log_progress = True,
                                             minimum_percentage_probability = 30,
                                             per_frame_function = forFrame,
                                             return_detected_frame = True)

The error is as follows:

ValueError: An error occured. It may be that your input video is invalid. Ensure you specified a proper string value for 'output_file_path' is 'save_detected_video' is not False. Also ensure your per_frame, per_second, per_minute or video_complete_analysis function is properly configured to receive the right parameters.

I have already installed the latest version, v.2.1.5, and tried the YOLOv3 and TinyYOLOv3 models, both returning the same error. Also tried 2 different cameras.

At first I thought it might be the threading problem, as I was using the multithreaded solution from imutils, but it turns out that the same thing is happening with the cv2.VideoCapture.

Could you please help me out? I have no idea what is causing this problem.

rEufrazio commented 4 years ago

Could someone please help me here? I'm two weeks in and still have the same problem.

I recently discovered that this just happens when I use the returned_frame in any way, even if I assign it to another variable.

Already updated and downgraded the library and dependencies to every possible option, and tried using the other "for" methods, with no success.

If anyone could help me in any way I would be very grateful.

rEufrazio commented 4 years ago

I managed to solve the problem. It looks like the function will always display this message when anything wrong happens inside the forFrame() call. The error was being caused simply because I was calling the cv2.imshow() function incorrectly and not calling cv2.waitKey() inside the method.