billw2 / pikrellcam

Raspberry Pi motion vector detection program with OSD web interface.
GNU General Public License v3.0
262 stars 70 forks source link

pikrellcam stream can't be used with cv2 due to missing channels for cvtColor #64

Open fotisK69 opened 3 years ago

fotisK69 commented 3 years ago

Hi,

I am trying to get the stream from my pi camera using pikrellcam and read it via cv2 python module, but it seems that the stream can't be handled correctly because the cvtColor is complaining:

Traceback (most recent call last):
  File "...\Python\Python38\lib\multiprocessing\process.py", line 315, in _bootstrap
    self.run()
  File "...\Python\Python38\lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "...\Python\Python38\lib\multiprocessing\pool.py", line 109, in worker
    initializer(*initargs)
  File "...\fotisK69\dev\object_detector_app-master\object_detection_app.py", line 85, in worker
    frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

The stream it self works on a browser, so I do get the camera stream to my laptop! My setup is using 4 raspberry pi's zero w + pi camera (on each pi) and since the zero is not very strong in object detection due lack of cpu/gpu resources to do this on a laptop for 4 different camera streams.

The below code snippet is reading the stream overlaying some text and writing to a new file and show it in a window:

    print('Reading from webcam.')
    video_capture = WebcamVideoStream(src="http://<ip>:<port>",
                                      width=640,
                                      height=480).start()

    frame_width = 640
    frame_height = 480
    out = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc('M','J','P','G'), 10, (frame_width,frame_height))

    while True:
        frame = video_capture.read()
        input_q.put(frame)

        output_rgb = cv2.cvtColor(output_q.get(), cv2.COLOR_RGB2BGR)
        cv2.putText(output_rgb, "CPU=", (10, frame_height - 10), cv2.FONT_HERSHEY_PLAIN, 1, (255, 0, 0), 1, cv2.LINE_AA)
        out.write(output_rgb)
        cv2.imshow('Video', output_rgb)

I know that i can overlay text with pikrellcam too but the will not be related to each pi zero but should add other information. This is just an example.

I guess I have to configure pikrellcam do get a different stream quality, but I am just starting using pikrellcam. I was trying a different module like motion with the same pi camera and the stream what that solution was working with cv2.

Can you help please.