現在、python3.5.5とOpenCV3.4.3を使用し、ハフ変換でのリアルタイム円検出と各円の出現時間を個別に計測できるシステムを作製しようと考えています。作成したプログラムは以下の通りです。
python
1from datetime import datetime 2import math 3import numpy as np 4import cv2 5 6FIND_DIST = 10 7targets = [] 8 9def inp_points(): 10 pts = [] 11 while True: 12 s = (x1,y1) 13 if len(s) < 2: 14 break 15 16 pts.append((x1,y1)) 17 return pts 18 19id = 1 20cap = cv2.VideoCapture(0) 21while True: 22 now_time = datetime.now() 23 ret, frame = cap.read() 24 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 25 gray = cv2.GaussianBlur(gray, (33,33), 1) 26 circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 60, param1=10, param2=85, minRadius=10, maxRadius=80) 27 if circles is not None: 28 circles = np.uint16(np.around(circles)) 29 for i in circles[0,:]: 30 cv2.circle(frame,(i[0],i[1]),i[2],(255,255,0),2) 31 cv2.circle(frame,(i[0],i[1]),2,(0,0,255),3) 32 x1 = i[0] 33 y1 = i[1] 34 35 pts = inp_points() 36 if len(pts) <= 0: 37 break 38 39 for tgt in targets: 40 tgt['bIn'] = False 41 42 new_tgts = [] 43 44 for pt in pts: 45 bIn = False 46 for tgt in targets: 47 48 if math.sqrt( math.pow(pt[0] - tgt['pos'][0], 2) + math.pow(pt[1] - tgt['pos'][1], 2)) <= FIND_DIST: 49 tgt['bIn'] = True 50 tgt['pos'] = pt 51 bIn = True 52 53 if not bIn: 54 new_tgt = {'id':id,'bIn':True,'pos':pt,'in_time':now_time} 55 new_tgts.append(new_tgt) 56 id += 1 57 58 for tgt in targets: 59 if tgt['bIn']: 60 new_tgts.append(tgt) 61 else: 62 print('{}sec'.format((now_time - tgt['in_time']).total_seconds())) 63 64 65 cv2.imshow('preview', frame) 66 key = cv2.waitKey(10) 67 if key == ord("q"): 68 break 69 70cv2.destroyAllWindows() 71コード
動かそうとしたところ、パソコンが固まってしまいました。調べたところ、パソコンが固まってしまう原因として、CPUの不足が原因とありました。しかし、あまりITに関して詳しくなく実際の原因が何なのかわからない状態です。使用しているシステムプロセッサはIntel(R) Core(TM) i5-4310M CPU @ 2.70GHz 2.70GHz、RAMは8.0GB、64ビットオペレーティングシステムを使用しています。です。助けていただけるとありがたいです。
回答4件