【问题标题】:Is there a way to control a webcam focus in pygame?有没有办法在pygame中控制网络摄像头的焦点?
【发布时间】:2012-11-15 23:08:44
【问题描述】:

我有两个网络摄像头,我想用它们拍照。以下代码实现了这一点:

import pygame
import pygame.camera
from datetime import datetime
import Image
import threading
import time

class Camera (threading.Thread):
    def __init__(self, camera):
        self.camera = pygame.camera.Camera(camera,(2304,1536))
        self.stop = False
        threading.Thread.__init__(self)

    def run(self):
        self.camera.start()
        srf = self.camera.get_image()
        img = pygame.image.tostring(srf, 'RGB')
        img = Image.fromstring('RGB', srf.get_size(), img)

        img.save('%s.png'%datetime.now(), 'PNG')

s = datetime.now()

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

cam1 = Camera("/dev/video0")
cam2 = Camera("/dev/video1")

cam1.start()
cam2.start()

cam1.join()
cam2.join()

print datetime.now() - s

但我需要设置焦点。

我找到了这些命令行:

apt-get install uvcdynctrl
uvcdynctrl --device=/dev/video1 --clist
uvcdynctrl --device=/dev/video1 --get='Focus, Auto'
uvcdynctrl --device=/dev/video1 --set='Focus, Auto' 0
uvcdynctrl --device=/dev/video1 --set='Focus (absolute)' 20

我虽然可以使用以下内容:

import os
os.system('command to set the focus')

但是在我的(罗技)相机上它不起作用,我从命令行(Ubuntu 12.04,python 2.7)得到这个输出:

uvcdynctrl --device=/dev/video1 --set='Focus, Auto' 0

[libwebcam] Unsupported V4L2_CID_EXPOSURE_AUTO control with a non-contiguous 
  range of choice IDs found
[libwebcam] Invalid or unsupported V4L2 control encountered: ctrl_id = 0x009A0901, name = 'Exposure, Auto'
[libwebcam] Unsupported V4L2_CID_EXPOSURE_AUTO control with a non-contiguous 
  range of choice IDs found
[libwebcam] Invalid or unsupported V4L2 control encountered: ctrl_id = 0x009A0901, name = 'Exposure, Auto'

编辑:

摄像头是Logitech HD Pro Webcam C920 并且正在运行:

uvcdynctrl --device=/dev/video1 --clist

给出输出:

[libwebcam] Unsupported V4L2_CID_EXPOSURE_AUTO control with a non-contiguous 
  range of choice IDs found
[libwebcam] Invalid or unsupported V4L2 control encountered: ctrl_id = 0x009A0901, name = 'Exposure, Auto'
[libwebcam] Unsupported V4L2_CID_EXPOSURE_AUTO control with a non-contiguous 
  range of choice IDs found
[libwebcam] Invalid or unsupported V4L2 control encountered: ctrl_id = 0x009A0901, name = 'Exposure, Auto'
Listing available controls for device /dev/video1:
  Brightness
  Contrast
  Saturation
  White Balance Temperature, Auto
  Gain
  Power Line Frequency
  White Balance Temperature
  Sharpness
  Backlight Compensation
  Exposure (Absolute)
  Exposure, Auto Priority
  Pan (Absolute)
  Tilt (Absolute)
  Focus (absolute)
  Focus, Auto
  Zoom, Absolute

【问题讨论】:

  • 要做的第一件事 - 下载最新版本并从源代码构建。这似乎是一个活跃的项目。 sourceforge.net/p/libwebcam/wiki/Home
  • 这不取决于网络摄像头的型号吗?您可以首先提及您遇到问题的网络摄像头的具体型号。可能是这个模型根本不允许你手动调整焦点。
  • 型号为罗技HD Pro Webcam C920,支持对焦功能。
  • @avip 我已经完成了你的建议并得到了同样的结果。
  • Delta,您在 python 中同时从两个摄像头捕获图像是否获得了积极的结果?如果是,您是否对上面的 python 脚本进行了更改?

标签: python webcam pygame


【解决方案1】:

执行这个

sudo apt-get install v4l-utils

在python中

os.system('v4l2-ctl -d 0 -c focus_auto=0')
os.system('v4l2-ctl -d 0 -c focus_absolute=250')

【讨论】:

    猜你喜欢
    • 2021-05-30
    • 2011-10-15
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多