我想将 Picamera 在 Raspberry Pi 上拍摄的图像发送到我的 windows 计算机。
我写了一些代码,如下(它们在这里被简化了),但是它卡在 frame = footage_socket.recv_string()
了client.py
.
我没有收到任何错误,但它总是卡在代码中,就像它冻结一样,无法转到下一行。工作正常并连续server.py
打印。'test'
如果您查看jpg_as_text
,您可以看到编码文本。
server.py
:
import picamera
import socket
import threading
import zmq
import cv2
import base64
from picamera.array import PiRGBArray
if __name__ == "__main__":
addr = 'ip_address'
camera = picamera.PiCamera() # Camera initialization
camera.resolution = (640, 480)
camera.framerate = 7
rawCapture = PiRGBArray(camera, size=(640, 480))
# FPV initialization
context = zmq.Context()
footage_socket = context.socket(zmq.PUB)
footage_socket.connect('tcp://%s:5555'%addr)
print(addr)
font = cv2.FONT_HERSHEY_SIMPLEX
for frame in camera.capture_continuous( rawCapture,
format = "bgr",
use_video_port = True ):
image = frame.array
print('test')
image = cv2.resize(image, (640, 480)) # resize the frame
encoded, buffer = cv2.imencode('.jpg', image)
jpg_as_text = base64.b64encode(buffer)
footage_socket.send(jpg_as_text)
rawCapture.truncate(0)
client.py
:
from socket import *
import sys
import time
import threading as thread
import tkinter as tk
import math
import os
import cv2
import zmq
import base64
import numpy as np
if __name__ == "__main__":
context = zmq.Context()
footage_socket = context.socket(zmq.SUB)
footage_socket.bind('tcp://*:5555')
footage_socket.setsockopt_string(zmq.SUBSCRIBE, np.unicode(''))
font = cv2.FONT_HERSHEY_SIMPLEX
while 100:
try:
frame = footage_socket.recv_string() # This line of code is the problem.
print('next successfuly connected')
img = base64.b64decode(frame)
npimg = np.frombuffer(img, dtype=np.uint8)
source = cv2.imdecode(npimg, 1)
cv2.imshow("Stream", source)
cv2.waitKey(1)
except KeyboardInterrupt:
break
except:
pass