2017-11-26 94 views
-1

私のIPカメラは少し不安定で、ランダムに切断されているようです。スクリプトを切断して何回か再接続しようとすると、おそらく5〜10秒待つことをスクリプトが判断できるようにしたい。私はいくつか試しましたが、何も動いていません。 RETがfalseの場合Python:自動的にIPカメラを再接続する

これが私の基本的なスクリプトは、スクリプトが終了している:

#!/usr/local/bin/python3 

import cv2 
import time 
import datetime 

print("start time: " + datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p")) 

cap = cv2.VideoCapture('rtsp://<ip><port>/live0.264') 

while(True): 
    # Capture frame-by-frame 
    ret, frame = cap.read() 

    # Confirm we have a valid image returned 
    if not ret: 
     print("disconnected!") 
     break 

    # Our operations on the frame come here 
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) 

    # Display the resulting frame 
    cv2.imshow('frame', gray) 
    if cv2.waitKey(1) & 0xFF == ord('q'): 
     break 

print("end time: " + time.strftime("%X")) 
# When everything is done, release the capture 
cap.release() 
cv2.destroyAllWindows() 

編集:私のネットワークが一時的にダウンしたことを、私は、スクリプトのようなイベントでは、カメラに再接続しようとするでしょうかそれと同様に何か。

+1

何が問題なのですか?エラーはありますか? –

+0

実際に私は解決策を知らないが、私はあなたのIPカメラを修正するためにhavbeと思う。 –

+0

の前に無作為に切り離されたIPカメラを見たことがない。これは安いカメラだ。私は、そのファームウェアやそれを切断する原因を何も制御できません。問題がカメラではなかったとしても、他の理由で切断された場合、スクリプトがカメラとの接続を回復できるようにしたい。 – brewcrazy

答えて

0

私はついにこの問題を解決することができました。うまくいけば、これは同じことをやろうとしている誰にとっても便利です。

実際には、モーションが検出されたときのモーション検出とビデオ録画のロジックを持つ、より複雑なスクリプトのシェルです。私はまだテストをしていますが、この基本的なロジック(と私の偽のIPカメラ)はすべてがうまく動作しています。

#!/usr/local/bin/python3 

import cv2 
import datetime 
import time 


def reset_attempts(): 
    return 50 


def process_video(attempts): 

    while(True): 
     (grabbed, frame) = camera.read() 

     if not grabbed: 
      print("disconnected!") 
      camera.release() 

      if attempts > 0: 
       time.sleep(5) 
       return True 
      else: 
       return False 


recall = True 
attempts = reset_attempts() 

while(recall): 
    camera = cv2.VideoCapture("rtsp://<ip><port>/live0.264") 

    if camera.isOpened(): 
     print("[INFO] Camera connected at " + 
       datetime.datetime.now().strftime("%m-%d-%Y %I:%M:%S%p")) 
     attempts = reset_attempts() 
     recall = process_video(attempts) 
    else: 
     print("Camera not opened " + 
       datetime.datetime.now().strftime("%m-%d-%Y %I:%M:%S%p")) 
     camera.release() 
     attempts -= 1 
     print("attempts: " + str(attempts)) 

     # give the camera some time to recover 
     time.sleep(5) 
     continue 
関連する問題