Skip to content

Instantly share code, notes, and snippets.

@ochilab
Last active June 17, 2025 10:00
Show Gist options
  • Save ochilab/53dada16037c9f5e0018baf27aa0fb87 to your computer and use it in GitHub Desktop.
Save ochilab/53dada16037c9f5e0018baf27aa0fb87 to your computer and use it in GitHub Desktop.
OpenCVを利用してネットワークカメラの映像をHTTPで取得する
import cv2
# カメラ情報を設定
username = ""
password = ""
ip_address = "192.168.1.2" # カメラのIPアドレス
port = 80 #httpの場合、 httpsなら445
# カメラによってURLの形式は異なるため、マニュアルやメーカーの仕様書を確認(httpか、httpsか)
stream_url = f"http://{username}:{password}@{ip_address}:{port}/axis-cgi/mjpg/video.cgi"
# カメラから映像を取得
cap = cv2.VideoCapture(stream_url)
if not cap.isOpened():
print("カメラに接続できませんでした")
exit()
while True:
ret, frame = cap.read()
if not ret:
print("フレームを取得できません")
break
# 映像を表示
cv2.imshow('Network Camera', frame)
# 'q'キーで終了
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 後処理
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment