如何使用curl从缓冲区上传图像?

时间:2013-11-17 20:32:47

标签: c opencv curl raspberry-pi

我使用Pierre Raufast blog中描述的软件成功捕获帧。 我尝试使用IplImage结构或原始缓冲区上传图像缓冲区(请查看此source codebuffer函数中的dstImagevideo_buffer_callback。我正在使用libcurl example中的修改代码。

cvMatBuff = cvEncodeImage(".jpg", py, 0);
/* Fill in the file upload field from buffer */ 
curl_formadd(&formpost,
           &lastptr,
           CURLFORM_COPYNAME, "file",
           CURLFORM_BUFFER, "nowy.jpg",
           CURLFORM_BUFFERPTR, cvMatBuff,
           CURLFORM_END);
curl = curl_easy_init();

不幸的是,执行后我的烧瓶应用程序出现了500错误。

192.168.0.16 - - [17/Nov/2013 21:02:22] "POST /faces HTTP/1.1" 500 -
----------------------------------------
Exception happened during processing of request from ('192.168.0.16', 48537)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/SocketServer.py", line 649, in __init__
    self.handle()
  File "/home/usr/flask/lib/python2.7/site-packages/werkzeug/serving.py", line 200, in handle
    rv = BaseHTTPRequestHandler.handle(self)
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
    self.handle_one_request()
  File "/home/usr/flask/lib/python2.7/site-packages/werkzeug/serving.py", line 235, in handle_one_request
    return self.run_wsgi()
  File "/home/usr/flask/lib/python2.7/site-packages/werkzeug/serving.py", line 194, in run_wsgi
    traceback.plaintext)
  File "/home/usr/flask/lib/python2.7/site-packages/werkzeug/utils.py", line 71, in __get__
    value = self.func(obj)
  File "/home/usr/flask/lib/python2.7/site-packages/werkzeug/debug/tbtools.py", line 359, in plaintext
    return u'\n'.join(self.generate_plaintext_traceback())
  File "/home/usr/flask/lib/python2.7/site-packages/werkzeug/debug/tbtools.py", line 353, in generate_plaintext_traceback
    frame.function_name
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 17: ordinal not in range(128)

当我从文件上传图片时,我的服务器端代码正常工作:

curl -F "file=@./medium.jpg" http://localhost:5000/faces

1 个答案:

答案 0 :(得分:0)

此外,您必须使用CURLFORM_BUFFERLENGTH, len将缓冲区长度显式传递给libcurl。

另外,我建议您使用CURLFORM_CONTENTTYPE, "image/jpeg"

传递内容类型

最后,您还必须注意将正确的缓冲区传递给libcurl。

cvEncodeImage返回CV_8UC1类型的单行矩阵。我不知道与cvEncodeImage(?)有任何内存对齐。所以你需要做的就是遍历cvMatBuff->data.ptr数组,然后逐行将它复制到一个大小为cvMatBuff->rows*cvMatBuff->cols的预分配缓冲区中,注意使用cvMatBuff->step跳转到下一行(步幅)。