我正在尝试实时发送我的MacBook Air网络摄像头视频,并使用另一台计算机上的Python接收它。这样做的动机是为了能够将实时图像识别任务卸载到服务器上。服务器需要在Python中访问实时视频帧,以便我可以将帧传递给我的图像识别算法(深度神经网络)。
我已成功地使用https://github.com/atuldo/videoStream来完成此操作,该方法使用套接字库以字符串格式发送视频帧。但是,此方法会导致非常低的帧速率(请参见下面的代码片段)。
尝试1:
我已成功地使用https://github.com/atuldo/videoStream来完成此操作,该方法使用套接字库以字符串格式发送视频帧。但是,此方法会导致非常低的帧速率(请参见下面的代码片段)。
尝试1:
from flask import Flask, render_template, Response
from camera import VideoCamera
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
def gen(camera):
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
@app.route('/video_feed')
def video_feed():
return Response(gen(VideoCamera()),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
我还使用了这个https://github.com/log0/video_streaming_with_flask_example来使用flask将视频流传输到网页。在使用谷歌浏览器查看网页时,与我的第一种解决方案相比,它具有更好的帧速率。然而,这个解决方案无法在Python中接收视频,并且需要我从网页中读取视频,但我不确定如何去做。
尝试2(我用cv2替换了原始GitHub代码中的pygame以访问我的Mac摄像头):
### Server
import socket
from threading import *
import socket
import pygame
import pygame.camera
from pygame.locals import *
import cv2
def server():
#host_name = socket.gethostname()
host_name = 'localhost'
print "\nServer started at " + str(socket.gethostbyname(host_name)) + " at port " + str(90)
#port = 90
port = 1024
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(("",port))
serversocket.listen(10)
#pygame.camera.init()
#cam = pygame.camera.Camera(0,(640,480),"RGB")
#cam.start()
#img = pygame.Surface((640,480))
video = cv2.VideoCapture(0)
while True:
connection, address = serversocket.accept()
print "GOT_CONNECTION"
#cam.get_image(img)
#data = pygame.image.tostring(img,"RGB")
success, image = video.read()
data = cv2.imencode('.jpg', image)[1].tostring()
connection.sendall(data)
connection.close()
server()
###Reciever (snippet, for full code please refer to the github link above)
clientsocket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect((self.ipAddress, self.port))
received = []
while True:
recvd_data = clientsocket.recv(230400)
if not recvd_data:
break
else:
received.append(recvd_data)
dataset = ''.join(received)
#image = pygame.image.fromstring(dataset,(640, 480),"RGB") # convert received image from string
nparr = np.fromstring(dataset, np.uint8)
image = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR)
cv2.imwrite('foo.jpg', image)
#pygame.image.save(image, "foo.jpg")
self.ids.image_source.reload()
视频流将显示在网页上,如下所示:<html>
<head>
<title>Video Streaming Demonstration</title>
</head>
<body>
<h1>Video Streaming Demonstration</h1>
<img id="bg" src="{{ url_for('video_feed') }}">
</body>
</html>
我该如何使用这两种方法中的任一种来获得高帧率,或者是否有更好的方法?