make a loop for image processing

Question:

hi i use a code that take capture image from my webcam and do some image processing on image.
i need to repeat the total code consecutive n times. paraphrase take image and do image processing consecutively every five minutes.
thanks.

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)

i need to repeat consecutive every five minutes

Asked By: peter

||

Answers:

There are so many issues with your code I don’t know where to start. Let’s go for simple. I think you want a structure like this:

import numpy as np

def captureImage():
    print('Capturing image')
    # Code to capture image goes here, simulate a random one for now
    im = np.random.randint(0, 256, size=(256, 256, 3), dtype=np.uint8)
    return im

def processImage(im):
    print('Processing image')
    # Code to process image goes here, simulate processing with "np.mean()"
    mean = np.mean(im)
    print(f'Mean: {mean}')

N = 5
for i in range(N):
    im = captureImage()
    processImage(im)
    sleep(5)    # so you see results sooner
    # more like actual code... sleep(5*60)
Answered By: Mark Setchell
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.