【问题标题】:Python Video Streaming beginner error in initializing the cameraPython Video Streaming 初学者在初始化相机时出错
【发布时间】:2014-09-22 20:36:10
【问题描述】:

我想在我的树莓派 USB 摄像头和我的笔记本电脑之间制作视频流

客户:

import io
import socket
import struct
import time
import pygame.camera

# Connect a client socket to my_server:8000 (change my_server to the
# hostname of your server)
client_socket = socket.socket()
client_socket.connect(('192.168.1.12', 8000))

# Make a file-like object out of the connection
connection = client_socket.makefile('wb')
try:
    with pygame.camera.Camera ()as camera:
        camera.resolution = (640, 480)
        # 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'):
            # 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())
            # If we've been capturing for more than 30 seconds, quit
            if time.time() - start > 30:
                break
            # 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()

但我有这个

错误:`Traceback(最近一次调用最后一次):

  File "camera.py", line 15, in <module> with pygame.camera.Camera ()as camera:
  File "/usr/lib/python2.7/dist-packages/pygame/camera.py", line 100, in __init__
    _check_init()
  File "/usr/lib/python2.7/dist-packages/pygame/camera.py", line 86, in _check_init raise     ValueError("Need to call camera.init() before using.")
  ValueError: Need to call camera.init() before using.

我是这种编程语言的新手,所以我不明白如何集成相机以使用它及其功能 我在网上找到了一些教程,但我的代码中没有错误的部分 `

【问题讨论】:

    标签: python camera video-streaming pydev


    【解决方案1】:

    这是因为您在调用 Camera() 之前没有初始化 pygame

    首先确保你导入所有你需要的东西:

    import pygame
    import pygame.camera
    from pygame.locals import *
    

    现在你必须初始化 pygame 和 pygame.camera:

    pygame.init()
    pygame.camera.init()
    

    最后像你一样创建Camera对象:

    with pygame.camera.Camera ()as camera:
       blablabla...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-28
      • 1970-01-01
      • 2017-03-29
      • 1970-01-01
      • 2017-09-20
      • 2013-05-19
      相关资源
      最近更新 更多