def video_thread():
# enable the thread to modify the global variable 'latest_video_frame': (this variable will be accessed by functions doing some sort of video analysis or video streaming)
global latest_video_frame
# create an instance of the RPI camera class:
camera = PiCamera()
# rotate the camera view 180 deg (I have the RPI camera mounted upside down):
camera.hflip = True
camera.vflip = True
# set resolution and frame rate:
camera.resolution = (640, 480)
camera.framerate = 30
# create a generator 'video_frame_generator' which will continuously capture video frames
# from the camera and save them one by one in the container 'generator_output': ('video_frame_generator' is an infinte iterator which on every iteration (every time 'next()' is called on it, like eg in a for loop) gets a video frame from the camera and saves it in 'generator_output'))
generator_output = PiRGBArray(camera, size=(640, 480))
video_frame_generator = camera.capture_continuous(generator_output, format="bgr", use_video_port=True)
# allow the camera to warm up:
time.sleep(0.1)
for item in video_frame_generator:
# get the numpy array representing the latest captured video frame from the camera
# and save it globally for everyone to access:
latest_video_frame = generator_output.array
# clear the output container so it can store the next frame in the next loop cycle:
# (please note that this is absolutely necessary!)
generator_output.truncate(0)
# delay for 0.033 sec (for ~ 30 Hz loop frequency):
time.sleep(0.033)
# stop the robot if the wifi connection is lost: (by telling the arduino to do so over serial)
评论列表
文章目录