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

can't open camera by index Cannot open camera #736

Open sb3114 opened 2 years ago

sb3114 commented 2 years ago

hi, i am using : pi 64 OS bullesye imageai 2.1.6 opencv-python 4.5.5.62 python 3.7.7

while trying to do object dection on live video, opencv is able to access camera only once. if i stop the code and run a second time i get following error. i am able to access multiple times camera using opencv directly before trying with imageai.
Also when i am stopping the video capture with ctrl + z there is no output video.

(virtenv3.7) pi@pi:~/ml/detection $ python testLive.py [ WARN:0@5.691] global /tmp/pip-install-m36v8df1/opencv-python/opencv/modules/videoio/src/cap_v4l.cpp (889) open VIDEOIO(V4L2:/dev/video0): can't open camera by index Cannot open camera

here is my code: `from imageai.Detection import VideoObjectDetection import os import cv2

execution_path = os.getcwd()

camera = cv2.VideoCapture(0)

detector = VideoObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath(os.path.join(execution_path , "resnet50_coco_best_v2.1.0.h5"))
detector.loadModel()

video_path = detector.detectObectsFromVideo(
                camera_input=camera,
                output_file_path=os.path.join(execution_path, "camera_detected_video"),
                frames_per_second=20, log_progress=True, minimum_percentage_probability=40)
print(video_path)

`

YonLiud commented 2 years ago

A known question in OpenCV

camera = cv2.VideoCapture(0)

The 0 you mentioned is not always the index of the camera, it may be assigned to 1 or 2 or anything else, so a possible solution is changing the 0 to anything else, try changing it to random numbers and eventually land on the corresponding id, it shouldn't be more than 5 IIRC

cam_idx = 1
camera = cv2.VideoCapture(cam_idx)