cam_inst.read() returns past image

asked 2018-05-03 04:24:13 -0600

ERst gravatar image

I'm capturing and processing images iterately with in time interval. (3mins) code is written with python3.6, and run on Raspberry-pi (OS is Raspbian)

Whenever I run the code, it stop showing this Error.

ValueError Occured in del_ext/ del_ext skipped
OpenCV Error: Assertion failed (dsize.area() > 0 || (inv_scale_x > 0 && inv_scale_y > 0)) in resize, file /home/pi/opencv/opencv-3.4.0/modules/imgproc/src/resize.cpp, line 4045
Traceback (most recent call last):
  File "runpi.py", line 264, in <module>
    run(3.5,model_list)
  File "runpi.py", line 234, in run
    mat = temp.match(model_list)
  File "runpi.py", line 184, in match
    self.__resize(model.get_m_inform())
  File "runpi.py", line 147, in __resize
    self.mask = cv2.resize(self.mask, None, fx=reratio, fy=reratio, interpolation = inter_method)
cv2.error: /home/pi/opencv/opencv-3.4.0/modules/imgproc/src/resize.cpp:4045: error: (-215) dsize.area() > 0 || (inv_scale_x > 0 && inv_scale_y > 0) in function resize

And I found out that

  • It looks this error occurs only when running for more than an hour.
  • Recent taken images are out of date and the time gap is getting bigger with time pass

It looks kind of Video siganl is stored in RAM on R-pi and .read() returning out-dated image in RAM

What could be the solution for this problem?

Here is main part of the code

   def run(interval,model_list):
        cam0 = cv2.VideoCapture(0)  #Only cam0 is used. cam2 is just to record.
        camdir = "/home/pi/capstone/cam0/"
        cam2 = cv2.VideoCapture(1)
        cam2dir = "/home/pi/capstone/cam2/"
        runNo = 0
        acc_list = list()
        error_list = list()
        end = False
        while(end == False):
            print(runNo,"th run")

            img_name = "%s.jpg" %runNo
            frame, res = cam0.read()   #`res` is the image which will be processed
            cv2.imwrite(os.path.join(camdir,img_name),res)
            _ , cam2pic = cam2.read()
            cv2.imwrite(os.path.join(cam2dir,img_name),cam2pic)
            try:
                temp = Real(res)
                mat = temp.match(model_list)
                acc_list.append([mat,runNo])
                print("Accuracy=", mat)
            except ValueError:
                acc_list.append(["ValueError",runNo])
                error_list.append(["ValueError",runNo])
            except AttributeError:
                acc_list.append(["AttributeError", runNo])
                error_list.append(["AttributeError",runNo])
            except SmallObjectError:
                acc_list.append(["SmallObjectError", runNo])
                error_list.append(["SmallObjectError",runNo])
            runNo = runNo+1
            endNo = 40
            if(runNo/2 > endNo):
                end_check(res, end)
            elif(runNo > endNo):
                end = True
            sleep(interval*60)

        with open("acc_list.txt", "w") as output: #records for tracking errors
            output.write(str(acc_list))
        with open("err_list.txt", "w") as output:
            output.write(str(error_list))
        cam0.release()
        cam2.release()

    run(3.5,model_list)
edit retag flag offensive close merge delete

Comments

  • _ , cam2pic = cam2.read() do NOT throw away the return value, but check it. ALWAYS ! (it probably could not read an image, thus the error)
  • the v4l capture driver is using an internal queue (4 images, iirc). if you don't flush that often enough, you get stale images. probably your code is just too slow, to keep up. it also gets worse with multiple cameras (usb throughput and such)
berak gravatar imageberak ( 2018-05-03 04:32:07 -0600 )edit

@berak I thought cam2pic is written on the same adress in the loop. didn't it?

ERst gravatar imageERst ( 2018-05-03 04:51:43 -0600 )edit

@berak Could you explain how can I flush returns of cam2.read()? Is cam2pic=None at last in the loop enough?

ERst gravatar imageERst ( 2018-05-03 05:00:08 -0600 )edit

"flush" only means: read() fast enough.

berak gravatar imageberak ( 2018-05-03 05:04:10 -0600 )edit