【问题标题】:make a loop for image processing制作一个视频捕捉循环,延迟 5 分钟
【发布时间】:2022-11-18 18:43:45
【问题描述】:

您好,我使用的代码可以从我的网络摄像头获取图像并对图像进行一些图像处理。 我需要连续 n 次重复总代码。换句话说,每五分钟连续拍摄图像并进行图像处理。 谢谢。

import time
import cv2

videoCaptureObject = cv2.VideoCapture(0)

result = True
while(result):
  ret,frame = videoCaptureObject.read()
  cv2.imwrite("NewPicture.jpg",frame)
result = False
videoCaptureObject.release()
import numpy as np

image = cv2.imread('Newpicture.jpg')

blur = cv2.GaussianBlur(image, (3,3), 0)
gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)

thresh = cv2.threshold(gray, 220, 255, cv2.THRESH_BINARY_INV)[1]

x, y, w, h = cv2.boundingRect(thresh)           #  Replaced code
# 
left = (x, np.argmax(thresh[:, x]))             # 
right = (x+w-1, np.argmax(thresh[:, x+w-1]))    # 
top = (np.argmax(thresh[y, :]), y)              # 
bottom = (np.argmax(thresh[y+h-1, :]), y+h-1)   # 

cv2.circle(image, left, 8, (0, 50, 255), -1)
cv2.circle(image, right, 8, (0, 255, 255), -1)
cv2.circle(image, top, 8, (255, 50, 0), -1)
cv2.circle(image, bottom, 8, (255, 255, 0), -1)

print('left: {}'.format(left))
print('right: {}'.format(right))
print('top: {}'.format(top))
print('bottom: {}'.format(bottom))
cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey()
time.sleep(300)

我需要每五分钟连续重复一次

【问题讨论】:

  • 为什么导入cv2两次?为什么你的 while 循环没有缩进?当您每 300 秒只需要一张图像时,为什么要连续读取视频并将 JPEG 保存到文件系统 30 次/秒?当你还没有创建任何窗口时,为什么要销毁所有窗口?你怎么希望在不导入计时或睡眠模块的情况下暂停 5 分钟?

标签: python image


【解决方案1】:

您的代码有很多问题,我不知道从哪里开始。让我们简单点。我想你想要这样的结构:

import ...

def captureImage():
    ...
    ...
    return im

def processImage(im):
    ...
    ...


for i in range(N):
    im = captureImage()
    processImage(im)
    sleep(5*60)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多