网络摄像机流中的Raspberry Pi 3 Python和OpenCV人脸识别

网络摄像机流中的Raspberry Pi 3 Python和OpenCV人脸识别,第1张

网络摄像机流中的Raspberry Pi 3 Python和OpenCV人脸识别

最终,这不是管道,平台或权限的结果。在Raspberry Pi上生成并通过管道传输到python脚本的视频未得到正确处理。

我最终适应了这个picamera
python食谱

在Raspberry Pi上:(createStream.py)

import ioimport socketimport structimport timeimport picamera# Connect a client socket to my_server:8000 (change my_server to the# hostname of your server)client_socket = socket.socket()client_socket.connect(('10.0.0.3', 777))# Make a file-like object out of the connectionconnection = client_socket.makefile('wb')try:    with picamera.PiCamera() as camera:        camera.resolution = (1024, 768)        # Start a preview and let the camera warm up for 2 seconds        camera.start_preview()        time.sleep(2)        # Note the start time and construct a stream to hold image data        # temporarily (we could write it directly to connection but in this        # case we want to find out the size of each capture first to keep        # our protocol simple)        start = time.time()        stream = io.BytesIO()        for foo in camera.capture_continuous(stream, 'jpeg', use_video_port=True): # Write the length of the capture to the stream and flush to # ensure it actually gets sent connection.write(struct.pack('<L', stream.tell())) connection.flush() # Rewind the stream and send the image data over the wire stream.seek(0) connection.write(stream.read()) # Reset the stream for the next capture stream.seek(0) stream.truncate()    # Write a length of zero to the stream to signal we're done    connection.write(struct.pack('<L', 0))finally:    connection.close()    client_socket.close()

在处理流的计算机上:(processStream.py)

import ioimport socketimport structimport cv2import numpy as np# Start a socket listening for connections on 0.0.0.0:8000 (0.0.0.0 means# all interfaces)server_socket = socket.socket()server_socket.bind(('0.0.0.0', 777))server_socket.listen(0)# Accept a single connection and make a file-like object out of itconnection = server_socket.accept()[0].makefile('rb')try:    while True:        # Read the length of the image as a 32-bit unsigned int. If the        # length is zero, quit the loop        image_len = struct.unpack('<L', connection.read(struct.calcsize('<L')))[0]        if not image_len: break        # Construct a stream to hold the image data and read the image        # data from the connection        image_stream = io.BytesIO()        image_stream.write(connection.read(image_len))        # Rewind the stream, open it as an image with opencv and do some        # processing on it        image_stream.seek(0)        image = Image.open(image_stream)        data = np.fromstring(image_stream.getvalue(), dtype=np.uint8)        imagedisp = cv2.imdepre(data, 1)        cv2.imshow("frame",imagedisp)        cv2.waitKey(1)  #imshow will not output an image if you do not use waitKey        cv2.destroyAllWindows() #cleanup windows finally:    connection.close()    server_socket.close()

这种解决方案的结果与我在原始问题中引用的视频类似。较大的分辨率帧会增加订阅源的延迟,但是对于我的应用程序而言,这是可以容忍的。

首先,您需要运行processStream.py,然后在Raspberry Pi上执行createStream.py



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5617547.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存