网络摄像机连接到一台单板计算机。使用OpenCV库,我每三分钟接收一次图像,并将其保存在脚本所在的目录中。我希望将照片发送到Web服务器,以便以后查看。要发送图像,我使用了库请求。结果是以下脚本:
import cv2
import time
import serial
import numpy as np
import time
import requests
cap = cv2.VideoCapture(0)
while(1):
filename1 = time.strftime('Y%m%d-%H%M%S')
def make_photo():
cap = cv2.VideoCapture(0)
for i in range(30):
cap.read()
ret, frame = cap.read()
a = cv2.imwrite(filename1 + '.png', frame)
cap.release()
cv2.destroyAllWindows()
url = 'http://example.com/post.php'
files = {'file': open(a, 'rb')}
r = requests.post(url, files = files)
print(r)
time.sleep(120)
def main():
make_photo()
if __name__ == '__main__':
main()
“ post.php”文件包含一个脚本,该脚本接受图像并将其存储在服务器上的目录中。运行python文件时,出现以下错误:
[ WARN:0] terminating async callback
Traceback (most recent call last):
File "C:/Users/A/PycharmProjects/d/python_mysql_connect2.py", line 39, in <module>
main()
File "C:/Users/A/PycharmProjects/d/python_mysql_connect2.py", line 36, in main
make_photo()
File "C:/Users/A/PycharmProjects/d/python_mysql_connect2.py", line 28, in make_photo
r = requests.post(url, files = files)
File "C:\Users\A\PycharmProjects\d\venv\lib\site-packages\requests\api.py", line 116, in post
return request('post', url, data=data, json=json, **kwargs)
File "C:\Users\A\PycharmProjects\d\venv\lib\site-packages\requests\api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\A\PycharmProjects\d\venv\lib\site-packages\requests\sessions.py", line 519, in request
prep = self.prepare_request(req)
File "C:\Users\A\PycharmProjects\d\venv\lib\site-packages\requests\sessions.py", line 462, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "C:\Users\A\PycharmProjects\d\venv\lib\site-packages\requests\models.py", line 316, in prepare
self.prepare_body(data, files, json)
File "C:\Users\A\PycharmProjects\d\venv\lib\site-packages\requests\models.py", line 504, in prepare_body
(body, content_type) = self._encode_files(files, data)
File "C:\Users\A\PycharmProjects\d\venv\lib\site-packages\requests\models.py", line 159, in _encode_files
fdata = fp.read()
OSError: [Errno 9] Bad file descriptor
[ WARN:1] terminating async callback
如何解决此问题?