ImageAI
ImageAI copied to clipboard
can't open camera by index Cannot open camera
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:[email protected]] 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)
`
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)