質問内容
指定した間だけプログラムを実行させるにはどうしたらいいですか。
例:5分間プログラムを実行させ、5分経つとプログラムを終了する。
下記のプログラムに加えたいです。
作成したプログラム
Python
import cv2 import sys import numpy as np import pygame.mixer import time from concurrent.futures import ThreadPoolExecutor xmin,xmax = 112, 483 # 左上,右下のX座標 ymin,ymax = 172, 408 # 左上,右下のy座標 def dilation(dilationSize, kernelSize, img): # 膨張した画像にして返す kernel = np.ones((kernelSize, kernelSize), np.uint8) element = cv2.getStructuringElement(cv2.MORPH_RECT, (2 * dilationSize + 1, 2 * dilationSize + 1), (dilationSize, dilationSize)) dilation_img = cv2.dilate(img, kernel, element) return dilation_img def detect(gray_diff, thresh_diff=100, dilationSize=9, kernelSize=20): # 一定面積以上の物体を検出 retval, black_diff = cv2.threshold(gray_diff, thresh_diff, 255, cv2.THRESH_BINARY) # 2値化 dilation_img = dilation(dilationSize, kernelSize, black_diff) # 膨張処理 img = dilation_img.copy() image, contours, hierarchy = cv2.findContours(dilation_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE,offset=(xmin,ymin)) # 境界線検出 ball_pos = [] for cnt in contours: # 重心位置を計算 M = cv2.moments(cnt, False) x, y = int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]) ball_pos.append((x, y)) return ball_pos, img def displayCircle(image, ballList, thickness=5): cv2.rectangle(frame,(52,201),(491,290),(0,255,0),3) #長方形の描画 rect = np.array([[44, 283], [52, 201], [486, 204], [491, 290]]) for i in range(len(ballList)): x = int(ballList[i][0]) y = int(ballList[i][1]) if cv2.pointPolygonTest(rect,(x,y),False) >= 0: # 線の中にボールの中心が入っている赤色で表示。 cv2.circle(image,(x,y),10,(0,0,255),thickness) pool = ThreadPoolExecutor(4) pool.submit(sound) return image def sound(): pygame.mixer.init() pygame.mixer.music.load('music.mp3') pygame.mixer.music.play(1) # loop count time.sleep(2) #2秒再生 pygame.mixer.music.stop() #停止 def resizeImage(image, w=2, h=2): height = image.shape[0] width = image.shape[1] resizedImage = cv2.resize(image, (int(width / w), int(height / h))) return resizedImage def blackToColor(bImage): colorImage = np.array((bImage, bImage, bImage)) colorImage = colorImage.transpose(1, 2, 0) return colorImage video = cv2.VideoCapture(0) # videoファイルを読み込む # fourcc = cv2.VideoWriter_fourcc(*'MJPG') fourcc = cv2.VideoWriter_fourcc(*'XVID') c = time.time() if not video.isOpened(): # ファイルがオープンできない場合の処理. print("Could not open video") sys.exit() vidw = video.get(cv2.CAP_PROP_FRAME_WIDTH) vidh = video.get(cv2.CAP_PROP_FRAME_HEIGHT) out = cv2.VideoWriter("tennis.mp4", fourcc, 20.0,(int(vidw), int(vidh))) # 出力先のファイルを開く ok, frame = video.read() # 最初のフレームを読み込む if not ok: print('Cannot read video file') sys.exit() frame_pre = frame.copy() frame_pre while True: ok, frame = video.read() # フレームを読み込む if not ok: break frame_next = frame.copy() color_diff = cv2.absdiff(frame_next[ymin:ymax, xmin:xmax], frame_pre[ymin:ymax, xmin:xmax]) # フレーム間の差分計算 gray_diff = cv2.cvtColor(color_diff, cv2.COLOR_BGR2GRAY) # グレースケール変換 retval, black_diff = cv2.threshold(gray_diff,30, 255, cv2.THRESH_BINARY) ball, dilation_img = detect(gray_diff) frame = displayCircle(frame, ball, 2) #フレーム加工 cv2.imshow("Tracking", frame) # フレームを画面表示 out.write(frame) frame_pre = frame_next.copy() # 次のフレームの読み込み frame_pre[ymin:ymax, xmin:xmax] k = cv2.waitKey(10) & 0xff # ESCを押したら中止 if k == 27: break video.release() out.release() cv2.destroyAllWindows()
まだ回答がついていません
会員登録して回答してみよう