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