首页 > 解决方案 > 如何通过代理服务器在 OpenCV 中打开视频流?

问题描述

有点白痴的问题。我的目标是在互联网上打开一个视频流作为 OpenCV 中的 VideoCapture 对象,我需要通过代理服务器连接,而不是直接连接。我需要为每个脚本设置代理服务器(我有 3-4 个脚本同时运行,它们都需要使用不同的代理),所以我不能使用环境变量。

我可以使用 requests 库来设置代理并执行 get 请求,但是如何让结果成为 cv2.VideoCapture 对象?如果结果只是一个文件,我可以理解这样做,但它是源源不断的。

我可以以某种方式为每个 python 脚本设置一个代理服务器,然后使用 OpenCV VideoCapture 进行连接吗?或者我是否必须使用请求包流功能并以某种方式遍历视频流的每一帧?我对请求包和 HTTP 协议都不太了解,所以如果您愿意回答(如果您愿意,请提前感谢!)请以 ELI5 方式回答 :)。谢谢!

到目前为止我的代码:

import cv2
import numpy as np
import requests  # <- not used right now, but I guess I should somehow?

cap = cv2.VideoCapture("https://a.traffic.cam.somewhere/mpegts") # <- this connects directly, but needs to connect via proxy instead.

# Check if the stream opened correctly.
if (cap.isOpened() == False): 
  print("\n\n\nUnable to read camera feed\n\n\n")
else:
  print("\n\n\nOpened successfully. Press Ctrl+C to stop.\n\n\n")

# here we do some OpenCV stuff to each frame

if ret == True:
  # overlay date and time to the upper corner of the frame
  frame = cv2.putText(frame,str(datetime.now()) + " EET",(50,50), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,255,255),2,cv2.LINE_AA)
  out.write(frame)
  # and show that frame on the screen
  cv2.imshow('frame', cv2.resize(frame, (int(frame_width*0.5), int(frame_height*0.5))))
  if cv2.waitKey(1) & 0xFF == ord('q'):
    break

# more code
# more code
# more code

标签: pythonopencvhttp-proxy

解决方案


推荐阅读