opencv videocapture可以访问某些设备索引

发布于 2025-02-11 15:27:54 字数 2898 浏览 0 评论 0原文

我想在Python中使用不同的网络摄像头拍摄一系列图像,将它们堆叠,然后将它们保存在每个相机的1到14个文件夹中。堆叠是一个过程,每个像素的每个像素都可以求和,然后到最后除以图片的数量。

这是我的代码:

import numpy as np
import time
from time import strftime, localtime, gmtime
import subprocess
import sys
import glob


def install(package):
    subprocess.check_call([sys.executable, "-m", "pip", "install", package])


install("opencv-python")
import cv2


###################### Camport Access #####################################################################################


def list_ports():
# Test the ports and returns a tuple with the available ports and the ones that are working.

is_working = True
dev_port = 0
working_ports = []
available_ports = []
while is_working:
    camera = cv2.VideoCapture(dev_port)
    if not camera.isOpened():
        is_working = False
        print("Port %s is not working." % dev_port)
    else:
        is_reading, img = camera.read()
        w = camera.get(3)
        h = camera.get(4)
        if is_reading:
            print("Port %s is working and reads images (%s x %s)" % (dev_port, h, w))
            working_ports.append(dev_port)
        else:
            print("Port %s for camera ( %s x %s) is present but does not reads." % (dev_port, h, w))
            available_ports.append(dev_port)
    dev_port += 1
return available_ports, working_ports


available, camports = list_ports()
print(camports)


picture_count = 5
hours = int(input("amount of hours: "))
minute = localtime().tm_min

for i in range(stunden):

     hour = localtime().tm_hour
     print("starting hour: ", i)

     for j in camports:
         cam_port = j
         print("currently taking images at camport: ", j + 1)
         cam = cv2.VideoCapture(cam_port, cv2.CAP_DSHOW)
         # take picture here and safe it
         Images = []
         timestr = time.strftime("%d.%m.%Y-%H_%M_%S")

         for k in range(picture_count):
             cam.set(cv2.CAP_PROP_AUTOFOCUS, 0)
             time.sleep(0.5)
             result, image = cam.read()
             if result:
                 Images.append(image)
                 img_sum = np.zeros(np.shape(image))  # Sum of images
                 img_cnt = np.full(image.shape, picture_count)

                 for img in Images:
                     img_sum = img_sum + img.astype(float)  # Sum images
                 avg_img = img_sum / img_cnt  # Divide sum by count, for computing the average.
                 avg_img = np.round(avg_img).astype(np.uint8)  # Round an cast to uint8
             else:
                 print("failure at: ", j + 1, k + 1)

         cv2.imwrite(f"path\\{j + 1}\\pic {i + 1} {timestr}.tiff", avg_img)
     print("waiting for the next hour")

     while localtime().tm_min is not minute or localtime().tm_hour == hour:
       time.sleep(1)

访问摄像头时我有问题。我使用的相机与我使用的相机无关,第4、6、8、10、12、12和14个营地无法正常工作,并给我传达了每张照片都失败的信息。 (请参阅“失败:”)。 您有一个想法如何解决吗?

I want to take a series of Images in python with different webcams, stack them and afterwards save them in different folders counted from 1 to 14 for each camera. Stacking is a process by which each pixel of different pictures get summed up and by the end divided by the number of pictures.

Here is my code:

import numpy as np
import time
from time import strftime, localtime, gmtime
import subprocess
import sys
import glob


def install(package):
    subprocess.check_call([sys.executable, "-m", "pip", "install", package])


install("opencv-python")
import cv2


###################### Camport Access #####################################################################################


def list_ports():
# Test the ports and returns a tuple with the available ports and the ones that are working.

is_working = True
dev_port = 0
working_ports = []
available_ports = []
while is_working:
    camera = cv2.VideoCapture(dev_port)
    if not camera.isOpened():
        is_working = False
        print("Port %s is not working." % dev_port)
    else:
        is_reading, img = camera.read()
        w = camera.get(3)
        h = camera.get(4)
        if is_reading:
            print("Port %s is working and reads images (%s x %s)" % (dev_port, h, w))
            working_ports.append(dev_port)
        else:
            print("Port %s for camera ( %s x %s) is present but does not reads." % (dev_port, h, w))
            available_ports.append(dev_port)
    dev_port += 1
return available_ports, working_ports


available, camports = list_ports()
print(camports)


picture_count = 5
hours = int(input("amount of hours: "))
minute = localtime().tm_min

for i in range(stunden):

     hour = localtime().tm_hour
     print("starting hour: ", i)

     for j in camports:
         cam_port = j
         print("currently taking images at camport: ", j + 1)
         cam = cv2.VideoCapture(cam_port, cv2.CAP_DSHOW)
         # take picture here and safe it
         Images = []
         timestr = time.strftime("%d.%m.%Y-%H_%M_%S")

         for k in range(picture_count):
             cam.set(cv2.CAP_PROP_AUTOFOCUS, 0)
             time.sleep(0.5)
             result, image = cam.read()
             if result:
                 Images.append(image)
                 img_sum = np.zeros(np.shape(image))  # Sum of images
                 img_cnt = np.full(image.shape, picture_count)

                 for img in Images:
                     img_sum = img_sum + img.astype(float)  # Sum images
                 avg_img = img_sum / img_cnt  # Divide sum by count, for computing the average.
                 avg_img = np.round(avg_img).astype(np.uint8)  # Round an cast to uint8
             else:
                 print("failure at: ", j + 1, k + 1)

         cv2.imwrite(f"path\\{j + 1}\\pic {i + 1} {timestr}.tiff", avg_img)
     print("waiting for the next hour")

     while localtime().tm_min is not minute or localtime().tm_hour == hour:
       time.sleep(1)

I have a problem while accessing the cameras. Unattached to which camera I'm using the 4th, 6th, 8th, 10th, 12th and 14th camport won't work and give me the message that every picture made has failed. (see "failure at: ").
Do you have an idea how this could be fixed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文