카메라 프로세스를 항상 실행해야합니다.
이것이 평균 50ms의 결과를 얻을 수있는 유일한 방법입니다. 나는 모든 곳에서 해결책을 찾았다. 모션 센서 프로젝트로는 1 초가 너무 느 렸습니다.
@Dave Jones의 프로젝트는 그것을 수행하는 방법을 알아내는 데 도움이되었습니다.
단 2 개의 파일 :
항상 실행되는 데몬과 클라이언트.
데몬은 모든 카메라 설정을 설정하는 곳입니다.
picam-daemon.py
picam-client.py
python picam-daemon.py
import threading
import os, io, base64, time, socket, picamera, daemon
import daemon.runner
MAX_LENGTH = 50 # max length of any possible entry from "client"
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # setup socket
PORT = 10000 # port 10000
HOST = '127.0.0.1' # runs on local host
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # this allows us to override port, prevents error
serversocket.bind((HOST, PORT)) # lock server to this port and host
serversocket.listen(10) # max 10 clients
# Waits for commands, such as "snap" and "ack"
# Runs over "sockets"
def handle(clientsocket):
while 1:
buf = clientsocket.recv(MAX_LENGTH)
# Receive the SNAP command. Take a picture with PiCam.
if buf == 'snap':
start = time.time()
camera.capture('/home/pi/ir/picam-latest-snap.jpg')
finish = start - time.time()
print finish
print 'Picture Taken!'
if buf == 'ack':
print 'Ping: Hello!'
if len(buf) == 0: break
# Camera is always loaded here
# The "magic" is in the camThread, this allows a picture to be captured, then it gracefully closed the camera connection and reopens it. This produces very fast captures (54ms vs 1.5s!)
while 1:
# setup camera
camera = picamera.PiCamera()
camera.resolution = (640, 480)
#camera.zoom = (0.2, 0.2, 1.0, 1.0)
camera.exposure_mode = 'sports'
print('Camera server running')
# accept connections from outside, in order to receive commands
(clientsocket, address) = serversocket.accept()
ct = threading.Thread(target=handle, args=(clientsocket,))
ct.run() # this can be run(), because it can be scaled.
print 'Camera thread starting.'
camThread = threading.Thread()
while camThread.is_alive():
camThread.join(1)
camThread.run() # this must be start(), otherwise PiCam will crash. This is because PiCam cannot receive more than 1 connection.
print 'Camera thread ended'
camera.close() # Gracefully close PiCam if client disconnects
(두 번째 터미널에서) python picam-client.py
import socket
import sys
HOST = '127.0.0.1'
PORT = 10000
s = socket.socket()
s.connect((HOST, PORT))
print s
while 1:
msg = raw_input("Command To Send: ")
if msg == "close":
s.close()
sys.exit(0)
s.send(msg)
Google에서이 답변을 찾았 기 때문에이 답변을 게시하고 있습니다. 나는 하나를 찾을 수 없었기 때문에 일부 프로젝트를 둘러보고 무언가를 생각해 내야했습니다.