2

我对此真的很陌生,所以如果问题看起来很幼稚,请原谅我。我尝试使用 opencv 在 aws ec2 实例上从我的网络摄像头进行视频捕获。我在谷歌上尝试了很多方法,但仍然无法完成。

这是我的代码

> import numpy as np import tensorflow as tf import math
> #from keras.models import load_model
> #from keras.utils import CustomObjectScope
> #from keras.initializers import glorot_uniform
> #import keras import cv2 from flask import Flask, request, jsonify, render_template from werkzeug import secure_filename from pathlib
> import Path import pickle TEMPLATES_AUTO_RELOAD = True
> 
> @app.route('/trial') def trial():
>     #import server
>     cap=cv2.VideoCapture(-1)
>     clientsocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
>     clientsocket.connect(('localhost',8089))
>     if cap is None or not cap.isOpened():
>        return render_template('cv/objectTracking.html', prediction_text='Employee Salary should be $ {}')
> 
>     while True:
>         ret,frame=cap.read()
>         # Serialize frame
>         data = pickle.dumps(frame)
> 
>         # Send message length first
>         message_size = struct.pack("L", len(data)) ### CHANGED
> 
>         # Then data
>         clientsocket.sendall(message_size + data)

服务器.py

import pickle
import socket
import struct

import cv2

HOST = ''
PORT = 8089

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Socket created')

s.bind((HOST, PORT))
print('Socket bind complete')
s.listen(10)
print('Socket now listening')

conn, addr = s.accept()

data = b'' ### CHANGED
payload_size = struct.calcsize("L") ### CHANGED

while True:

    # Retrieve message size
    while len(data) < payload_size:
        data += conn.recv(4096)

    packed_msg_size = data[:payload_size]
    data = data[payload_size:]
    msg_size = struct.unpack("L", packed_msg_size)[0] ### CHANGED

    # Retrieve all data based on message size
    while len(data) < msg_size:
        data += conn.recv(4096)

    frame_data = data[:msg_size]
    data = data[msg_size:]

    # Extract frame
    frame = pickle.loads(frame_data)

    # Display
    cv2.imshow('frame', frame)
    waitKey(1)

我首先运行代码 python server.py 而不是 app.py 。当我点击 url "/trial" 时出现错误,因为 cap.isOpened 是错误的。我很困惑如何继续我尝试了许多不同的方法。任何帮助将不胜感激。谢谢

4

0 回答 0