質問内容
指定した間だけプログラムを実行させるにはどうしたらいいですか。
例:5分間プログラムを実行させ、5分経つとプログラムを終了する。
下記のプログラムに加えたいです。
作成したプログラム
Python
1import cv2 2import sys 3import numpy as np 4import pygame.mixer 5import time 6from concurrent.futures import ThreadPoolExecutor 7 8xmin,xmax = 112, 483 # 左上,右下のX座標 9ymin,ymax = 172, 408 # 左上,右下のy座標 10 11def dilation(dilationSize, kernelSize, img): # 膨張した画像にして返す 12 kernel = np.ones((kernelSize, kernelSize), np.uint8) 13 element = cv2.getStructuringElement(cv2.MORPH_RECT, (2 * dilationSize + 1, 2 * dilationSize + 1), (dilationSize, dilationSize)) 14 dilation_img = cv2.dilate(img, kernel, element) 15 return dilation_img 16 17 18def detect(gray_diff, thresh_diff=100, dilationSize=9, kernelSize=20): # 一定面積以上の物体を検出 19 retval, black_diff = cv2.threshold(gray_diff, thresh_diff, 255, cv2.THRESH_BINARY) # 2値化 20 dilation_img = dilation(dilationSize, kernelSize, black_diff) # 膨張処理 21 img = dilation_img.copy() 22 image, contours, hierarchy = cv2.findContours(dilation_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE,offset=(xmin,ymin)) # 境界線検出 23 24 ball_pos = [] 25 26 for cnt in contours: # 重心位置を計算 27 M = cv2.moments(cnt, False) 28 x, y = int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]) 29 ball_pos.append((x, y)) 30 31 return ball_pos, img 32 33def displayCircle(image, ballList, thickness=5): 34 cv2.rectangle(frame,(52,201),(491,290),(0,255,0),3) #長方形の描画 35 rect = np.array([[44, 283], [52, 201], [486, 204], [491, 290]]) 36 37 for i in range(len(ballList)): 38 x = int(ballList[i][0]) 39 y = int(ballList[i][1]) 40 41 if cv2.pointPolygonTest(rect,(x,y),False) >= 0: 42 # 線の中にボールの中心が入っている赤色で表示。 43 cv2.circle(image,(x,y),10,(0,0,255),thickness) 44 45 pool = ThreadPoolExecutor(4) 46 pool.submit(sound) 47 48 return image 49 50def sound(): 51 pygame.mixer.init() 52 pygame.mixer.music.load('music.mp3') 53 pygame.mixer.music.play(1) # loop count 54 55 time.sleep(2) #2秒再生 56 pygame.mixer.music.stop() #停止 57 58def resizeImage(image, w=2, h=2): 59 height = image.shape[0] 60 width = image.shape[1] 61 resizedImage = cv2.resize(image, (int(width / w), int(height / h))) 62 return resizedImage 63 64 65def blackToColor(bImage): 66 colorImage = np.array((bImage, bImage, bImage)) 67 colorImage = colorImage.transpose(1, 2, 0) 68 return colorImage 69 70video = cv2.VideoCapture(0) # videoファイルを読み込む 71# fourcc = cv2.VideoWriter_fourcc(*'MJPG') 72fourcc = cv2.VideoWriter_fourcc(*'XVID') 73c = time.time() 74 75if not video.isOpened(): # ファイルがオープンできない場合の処理. 76 print("Could not open video") 77 sys.exit() 78 79vidw = video.get(cv2.CAP_PROP_FRAME_WIDTH) 80vidh = video.get(cv2.CAP_PROP_FRAME_HEIGHT) 81out = cv2.VideoWriter("tennis.mp4", fourcc, 20.0,(int(vidw), int(vidh))) # 出力先のファイルを開く 82 83 84ok, frame = video.read() # 最初のフレームを読み込む 85if not ok: 86 print('Cannot read video file') 87 sys.exit() 88 89frame_pre = frame.copy() 90frame_pre 91while True: 92 ok, frame = video.read() # フレームを読み込む 93 if not ok: 94 break 95 96 frame_next = frame.copy() 97 98 color_diff = cv2.absdiff(frame_next[ymin:ymax, xmin:xmax], frame_pre[ymin:ymax, xmin:xmax]) # フレーム間の差分計算 99 gray_diff = cv2.cvtColor(color_diff, cv2.COLOR_BGR2GRAY) # グレースケール変換 100 retval, black_diff = cv2.threshold(gray_diff,30, 255, cv2.THRESH_BINARY) 101 102 ball, dilation_img = detect(gray_diff) 103 104 frame = displayCircle(frame, ball, 2) #フレーム加工 105 106 cv2.imshow("Tracking", frame) # フレームを画面表示 107 108 out.write(frame) 109 110 frame_pre = frame_next.copy() # 次のフレームの読み込み 111 frame_pre[ymin:ymax, xmin:xmax] 112 113 k = cv2.waitKey(10) & 0xff # ESCを押したら中止 114 if k == 27: 115 break 116 117video.release() 118out.release() 119cv2.destroyAllWindows()

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/03 15:32