Video streaming from ip camera gets closed after a quite time ago "[mjpeg @ 0x97226c0] overread 1"

1k Views Asked by At

im trying to develop a surveillance camera program with python and opencv using an ESP32 with the OV2640 camera module, it connects with my local net and stream image to my local ip, this program should take photo periodically and apply cv2 functions to detect motion, then when it detects save image and sends an email with the attached image to me.

Everything works nice, but after some time (not same, could be 30mins or 3hrs) it crashes and closes the window, the problem says:

Image of the error

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import os.path
import cv2
import numpy as np


contadorImg = 0


cap = cv2.VideoCapturecapture = cv2.VideoCapture('http://192.168.1.78:80') 


ret, frame1 = cap.read()
ret, frame2 = cap.read()

while cap.isOpened():
    diff = cv2.absdiff(frame1, frame2)
    gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (5,5), 0)
    _, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)
    dilated = cv2.dilate(thresh, None, iterations=3)
    _,contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    for contour in contours:
        (x, y, w, h) = cv2.boundingRect(contour)

        if cv2.contourArea(contour) < 2300: #MIN AREA TO DETECT
            continue
        cv2.rectangle(frame1, (x, y), (x+w, y+h), (0, 255, 0), 2)
        cv2.putText(frame1, "Status: {}".format('Movement'), (10, 20), cv2.FONT_HERSHEY_SIMPLEX,
                    1, (0, 0, 255), 3)
        contadorImg = contadorImg + 1
        imagen = imagen = 'img' + str(contadorImg) + '.jpg'
        cv2.imwrite(imagen, frame1)

    #cv2.drawContours(frame1, contours, -1, (0, 255, 0), 2)



    if contadorImg%3 == 0 :                          #Sending just 1/3 of the taked images
        email = '*********'
        password = '********'
        send_to_email = '**********'
        subject = 'This is the subject'
        message = 'This is my message'  
        msg = MIMEMultipart()
        msg['From'] = email
        msg['To'] = send_to_email
        msg['Subject'] = subject

        msg.attach(MIMEText(message, 'plain'))
        # Setup the attachment
        attachment = open(imagen, "rb")
        part = MIMEBase('application', 'octet-stream')
        part.set_payload(attachment.read())
        encoders.encode_base64(part)

        part.add_header('Content-Disposition', "attachment; filename= %s" % imagen) 
        print ('i1:', imagen)



        # Attach the attachment to the MIMEMultipart object
        msg.attach(part)
        #time.sleep(1)
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.starttls()
        server.login(email, password)
        text = msg.as_string()
        server.sendmail(email, send_to_email, text)
        print('Mail Sent')
        server.quit()





    cv2.imshow("feed", frame1)
    frame1 = frame2
    ret, frame2 = cap.read()

    if cv2.waitKey(40) == 27:
        break

cv2.destroyAllWindows()
cap.release()




Thanks for read me.

0

There are 0 best solutions below