0

我对树莓派和 python 很陌生。我想从 python 脚本配置树莓派相机(曝光时间和快门速度)。使用 opencv,它使用 Picamera 模块工作。这是示例。

with picamera.PiCamera() as camera:
    camera.start_preview()
    camera.shutter_speed= 50000
    time.sleep(2)
    camera.capture_sequence([
        'image1.jpg',
        'image2.jpg',
        'image3.jpg',
        ])
    camera.stop_preview()

但在我的主要代码中,我使用的是 imutils 视频流。现在我不知道如何将这两者整合在一起。如果有人可以在这里指导我,那将是很大的帮助。

# Import packages
import os
import cv2
import numpy as np
import sys
import time
from threading import Thread
import importlib.util
from datetime import datetime
import threading 
from PIL import Image, ImageTk
from imutils.video import VideoStream
import imutils
import tkinter as tk



# Define VideoStream class to handle streaming of video from webcam in separate processing thread

class VideoStream:
    """Camera object that controls video streaming from the Picamera"""
    def __init__(self,resolution=(250,175),framerate=30):
        # Initialize the PiCamera and the camera image stream
    
        self.stream = cv2.VideoCapture(0)
        ret = self.stream.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG'))
        ret = self.stream.set(3,resolution[0])
        ret = self.stream.set(4,resolution[1])
        
        # Read first frame from the stream
        (self.grabbed, self.frame) = self.stream.read()

    # Variable to control when the camera is stopped
        self.stopped = False

    def start(self):
    # Start the thread that reads frames from the video stream
        Thread(target=self.update,args=()).start()
        return self

    def update(self):
        # Keep looping indefinitely until the thread is stopped
        while True:
            # If the camera is stopped, stop the thread
            if self.stopped:
                # Close camera resources
                self.stream.release()
                return

            # Otherwise, grab the next frame from the stream
            (self.grabbed, self.frame) = self.stream.read()

    def read(self):
    # Return the most recent frame
        return self.frame

    def stop(self):
    # Indicate that the camera and thread should be stopped
        self.stopped = True
    
    
imW, imH = 840, 480

# Initialize video stream
videostream = VideoStream(resolution=(imW,imH),framerate=30).start()
time.sleep(1)

while True:
    # Grab frame from video stream
    frame = videostream.read()
    #print (type(frame.shape))
    frame = cv2.flip(frame,1) 
    cv2.imshow("Test", frame)

        # Press any< key to quit
    if cv2.waitKey(1) > -1:
        #call("sudo shutdown -h now", shell=True)
        break

cv2.destroyAllWindows()
videostream.stop()
4

0 回答 0