如何使用猎鹰服务器流式传输视频(动态jpeg)?

时间:2018-12-03 13:59:19

标签: python-3.x opencv video-streaming falconframework

所需的输出

输入:使用OpenCV或REST摄像机URL的摄像机源。 (此问题无关)

输出:经过一些OpenCV处理后流jpeg图像


到目前为止,我已经基于$destinationFolder = 'PATH OF THE DESTINATION FOLDER' $sourceFile = 'FULL PATH AND FILENAME OF THE SOURCE FILE' # split the filename into a basename and an extension variable $baseName = [System.IO.Path]::GetFileNameWithoutExtension($sourceFile) $extension = [System.IO.Path]::GetExtension($sourceFile) # you could also do it like this: # $fileInfo = Get-Item -Path $sourceFile # $baseName = $fileInfo.BaseName # $extension = $fileInfo.Extension # get an array of all filenames (name only) of the files with a similar name already present in the destination folder $allFiles = @(Get-ChildItem $destinationFolder -File -Filter "$baseName*$extension" | Select-Object -ExpandProperty Name) # construct the new filename $newName = $baseName + $extension $count = 1 while ($allFiles -contains $newName) { # add a sequence number in brackets to the end of the basename until it is unique in the destination folder $newName = "{0}({1}){2}" -f $baseName, $count++, $extension } # construct the new full path and filename for the destination of your Copy-Item command $targetFile = Join-Path -Path $destinationFolder -ChildPath $newName Copy-Item -Path $sourceFile -Destination $targetFile tutorial

完成了以下操作

输入:图片文件作为POST请求

输出:带有图像路径的GET请求端点

Falcon

响应如下:

  

HTTP / 1.1 201已创建

     

连接:关闭

     

日期:2018年12月3日星期一13:08:14 GMT

     

服务器:gunicorn / 19.7.1

     

内容长度:0

     

内容类型:application / json; charset = UTF-8

     

位置:/images/e69a83ee-b369-47c3-8b1c-60ab7bf875ec.jpg

上面的代码有2个问题:

  1. 我首先获取数据流并将其保存在文件中,然后在OpenCV中读取它以执行其他一些操作,这有些过头了,应该容易修复,但是我不知道如何
  2. 此服务不流式传输JPG。我所有的GET请求网址都可以在浏览器中打开,以查看不理想的图片

那么,如何读取import mimetypes import os import re import uuid import cv2 import io import falcon from falcon import media import json import msgpack class Collection(object): def __init__(self, image_store): self._image_store = image_store def on_get(self, req, resp): # TODO: Modify this to return a list of href's based on # what images are actually available. doc = { 'images': [ { 'href': '/images/1eaf6ef1-7f2d-4ecc-a8d5-6e8adba7cc0e.png' } ] } resp.data = msgpack.packb(doc, use_bin_type=True) resp.content_type = falcon.MEDIA_MSGPACK resp.status = falcon.HTTP_200 def on_post(self, req, resp): name = self._image_store.save(req.stream, req.content_type) # Unnecessary Hack to read the saved file in OpenCV image = cv2.imread("images/" + name) new_image = do_something_with_image(image) _ = cv2.imwrite("images/" + name, new_image) resp.status = falcon.HTTP_201 resp.location = '/images/' + name class Item(object): def __init__(self, image_store): self._image_store = image_store def on_get(self, req, resp, name): resp.content_type = mimetypes.guess_type(name)[0] resp.stream, resp.stream_len = self._image_store.open(name) class ImageStore(object): _CHUNK_SIZE_BYTES = 4096 _IMAGE_NAME_PATTERN = re.compile( '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.[a-z]{2,4}$' ) def __init__(self, storage_path, uuidgen=uuid.uuid4, fopen=io.open): self._storage_path = storage_path self._uuidgen = uuidgen self._fopen = fopen def save(self, image_stream, image_content_type): ext = mimetypes.guess_extension(image_content_type) # Issue with this code, Not returning the extension so hard coding it in next line ext = ".jpg" name = '{uuid}{ext}'.format(uuid=self._uuidgen(), ext=ext) image_path = os.path.join(self._storage_path, name) with self._fopen(image_path, 'wb') as image_file: while True: chunk = image_stream.read(self._CHUNK_SIZE_BYTES) if not chunk: break image_file.write(chunk) return name def open(self, name): # Always validate untrusted input! if not self._IMAGE_NAME_PATTERN.match(name): raise IOError('File not found') image_path = os.path.join(self._storage_path, name) stream = self._fopen(image_path, 'rb') stream_len = os.path.getsize(image_path) return stream, stream_len def create_app(image_store): api = falcon.API() api.add_route('/images', Collection(image_store)) api.add_route('/images/{name}', Item(image_store)) api.add_sink(handle_404, '') return api def get_app(): storage_path = os.environ.get('LOOK_STORAGE_PATH', './images') image_store = ImageStore(storage_path) return create_app(image_store) 数据作为numpy数组?更重要的是,我需要进行哪些更改才能从此服务流式传输图像?

  

P.S。很长的歉意

1 个答案:

答案 0 :(得分:1)

我找到了一个很好用的解决方案。有关更多信息,请查看this beautiful code

learning_curves(estimator=RFReg, X=X_train, y=y_size, train_sizes= train_sizes)