我一直在尝试在 ASP.NET 中设置 MJPEG 流。我想从 URL 检索 MJEPG 流,并将我获得的每一帧发送到每个连接的客户端。我能够找到的示例只能从设置文件中读取,而不是从 URL 继续流,并通过MultiStreamContent发送整个文件。由于我逐帧检索,因此无法执行此操作。我想知道是否可以用 ASP.NET MVC 做我想做的事。我目前正在使用 AForge 视频从链接中检索 MJPEG 流。我的控制器类代码:
using System.Net.Http;
using System.Web.Http;
using AForge.Video;
namespace VideoPrototypeMVC.Controllers
{
public class CameraController : ApiController
{
int framecounter = 0;
MJPEGStream stream = new MJPEGStream();
[HttpGet]
public void GetVideoContent()
{
stream.Source = @"http://127.0.0.1:5002/stream";
stream.NewFrame += new NewFrameEventHandler(showFrame);
stream.Start();
MultipartContent content = new MultipartContent();
while (stream.IsRunning)
{
//Continues streaming should be here?
}
}
//Can be used to display of a frame …Run Code Online (Sandbox Code Playgroud) 我正在使用Flask和flask-restful生成一个MJPEG流.出于原因,我想在另一个Python程序中捕获此流,我使用OpenCV(3).问题是请求的第一帧进展顺利.另一方面,未正确接收请求的第二帧(延迟之后),并抛出错误:
[mpjpeg @ 0000017a86f524a0] Expected boundary '--' not found, instead found a line of 82 bytes
Run Code Online (Sandbox Code Playgroud)
多次.
我相信这是因为框架的边界是手动设置的.我将把违规代码放在下面.
MJPEG流生成:
## Controller for the streaming of content.
class StreamContent(Resource):
@classmethod
def setVis(self, vis):
self.savedVis = vis
def get(self):
return Response(gen(VideoCamera(self.savedVis)),
mimetype='multipart/x-mixed-replace; boundary=frame')
## Generate a new VideoCamera and stream the retrieved frames.
def gen(camera):
frame = camera.getFrame()
while frame != None:
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
time.sleep(0.07)
frame = camera.getFrame()
## Allows for the reading of video frames. …Run Code Online (Sandbox Code Playgroud) 所以我遇到了一些问题.我从外部API检索一些weatherdata.这将作为JSON返回并发送到Azure IoT中心.流分析将json处理成适当的格式,但我在这里遇到了问题.
元素:Current_Condition是一种数组格式.它总是在[0]位置有一个元素.我只需要从第一个位置获取该数组的数据,而不需要像id那样的过滤器.
这里是完整的数据
{
"deviceId": "aNewDevice",
"data": {
"data": {
"current_condition": [
{
"cloudcover": "0",
"FeelsLikeC": "0",
"FeelsLikeF": "32",
"humidity": "100",
"observation_time": "10:00 AM",
"precipMM": "0.0",
"pressure": "1020",
"temp_C": "2",
"temp_F": "36",
"visibility": "0",
"weatherCode": "143",
"weatherDesc": [ { "value": "Fog, Mist" } ],
"weatherIconUrl": [ { "value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0006_mist.png" } ],
"winddir16Point": "SSW",
"winddirDegree": "210",
"windspeedKmph": "7",
"windspeedMiles": "4"
}
],
"request": [
{
"query": "Nijmegen, Netherlands",
"type": "City"
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
还有一些关于我究竟需要做什么的解释(不只是一个例子或代码答案)将来会很好.(请求元素毕竟有同样的问题.)
提前致谢 :)