-
Notifications
You must be signed in to change notification settings - Fork 0
/
midtern_test.py
82 lines (75 loc) · 5.67 KB
/
midtern_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import cv2
import numpy as np
cap = cv2.VideoCapture(0) #내얼굴
#cap = cv2.VideoCapture(0) #ì¹´ë©”ë¼
temp = None #ë³µì‚¬í• temp변수를 while문 ë°–ì—ì„œ 초기화...
while True:
retval, frame = cap.read() # camera image를 ì½ì–´ì„œ frameì— ë°˜í™˜
#ì»¬ëŸ¬ë²”ìœ„ì— ë”°ë¥¸ ì˜ì—분í•
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # ì»¬ëŸ¬ë²”ìœ„ì— ë”°ë¥¸ ì˜ì—ë¶„í• ì„ ìœ„í•´ hsvë¡œ 변환
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 모í´ë¡œì§€ì—°ì‚°ì„ 위해 grayë¡œ 변환
lowerb = (0,40,0)
upperb = (20,180,255)
dst = cv2.inRange(hsv,lowerb,upperb) #컬러 ë²”ìœ„ì— ë”°ë¥¸ ë¶„í• ì˜ìƒ
#침ì‹, íŒ½ì°½ì„ ìœ„í•œ 모í´ë¡œì§€ ì—°ì‚°
kernel = cv2.getStructuringElement(shape=cv2.MORPH_ELLIPSE, ksize=(3, 3)) # 모í´ë¡œì§€ ì—°ì‚°ì˜ ê²°ê³¼ë¥¼ ì›ìœ¼ë¡œ 반환
opening = cv2.morphologyEx(dst,cv2.MORPH_OPEN,kernel,iterations=10) # hsv로 불러와서 OPENIG연산 10번 진행
closing = cv2.morphologyEx(opening,cv2.MORPH_CLOSE, kernel,iterations=10) # hsv로 불러와서 OPENIG연산 10번 진행
gradient = cv2.morphologyEx(closing,cv2.MORPH_GRADIENT,kernel,iterations=5) # 모í´ë¡œì§€ ì—°ì‚°ì˜ ê²°ê³¼ë¥¼ ì´ìš©í•´ Edges를 검출
#image ë‚´ì— ì›ì„ ì°¾ê³ roi를 ê·¸ë ¤ë„£ëŠ” 코드...
#circles = cv2.HoughCircles(gradient,method=cv2.HOUGH_GRADIENT,dp=1,minDist=50,param2=15) # í—ˆí”„ë³€í™˜ì„ ì´ìš©í•´ imageë‚´ì˜ ì›ì„ ì°¾ìŒ
#print('circles.shape',circles.shape) #ì°¾ì€ ì›ì˜ 좌표를 ì¶œë ¥
###2번째
mode = cv2.RETR_EXTERNAL #ì™¸ê³½ì„ ë§Œ 그리는 모드 ê·¼ë° frameì„ ë²—ì–´ë‚˜ë©´ 2ê°œì˜ ì™¸ê³½ì„ ì„ ê·¸ë ¸ìŒ...여튼 ì˜ì—검출ì—는 ë¬¸ì œê°€ 없었ìŒ.
method = cv2.CHAIN_APPROX_SIMPLE
contours, hierarchy = cv2.findContours(gradient,mode,method)
x,y,width,height = cv2.boundingRect(contours[0])
cv2.rectangle(frame,(x,y),(x+width,y+height),(0,0,255),3)
text = 'ROI'
org = (x,y-10)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame,text,org,font,1,(0,0,255),3)
# ì°¾ì€ ROI ì˜ì—ì—만 corners를 찾아준다...
##????ì°¾ì€ ROI ì˜ì—ë§Œì„ ë½‘ì•„ë‚´ëŠ” 코드...
roi = gray[y : y + height, x : x + width]## ì´ê±°ë¥¼ 어떻게 ìˆ˜ì •???????????????????????????????
K = 30 # cornerì˜ ê°œìˆ˜ë¥¼ 최대 30개로 ì„¤ì •
corners = cv2.goodFeaturesToTrack(roi, maxCorners=K, qualityLevel=0.05, minDistance=10, corners=None,mask=None)
print('corners', corners) # 코너 좌표 프린트(실수로 ì¢Œí‘œê°’ì´ ì°¾ì•„ì§)
count = 0 # 검출한 ì½”ë„ˆì˜ ê°œìˆ˜ë¥¼ 카운트하기 위한 변수 ì„ ì–¸+초기화
corners = corners.reshape(-1, 2) # ì´ êµ¬ë¬¸ì€ ì™œ 해주는지 잘 ëª¨ë¥´ê² ìŒ...
for a, b in corners:
cv2.circle(frame, (int(a+x), int(b+y)), 5, (0, 0, 255), -1) # 검출한 ì½”ë„ˆì˜ ê° ì¢Œí‘œì— ì ‘ê·¼í•´ ì›ì„ 그리기, 좌표 ì§€ì •ì—ì„œ 조금 ìƒê°ì„ ë§Žì´ í–ˆìœ¼ë‚˜ í•´ê²°...
count += 1 # ì›ì„ 그릴 때마다 countì˜ ê°œìˆ˜ë¥¼ 하나씩 가산(현재 코너ì ì˜ ìˆ˜ë¥¼ 카운트)
print('count', count) # 카운트 확ì¸ìš©...
if temp is not None:
count_temp = 0 # trakingì— ì„±ê³µí•œ corner ì˜ ê°œìˆ˜
for i in range(0, corners.shape[0]):
for j in range(0, corners.shape[1]):
if frame[int(i), int(j), 2] == temp[int(i), int(j), 2]:
count_temp += 1 # trakingì— ì„±ê³µí•œ cornerì˜ ê°œìˆ˜ë¥¼ count
print('count_temp', count_temp)
else:
break
org = 50, 50
# if count > 0.55 * count_temp:
if count > 0.5 * count_temp:
text = 'traking success'
org = (50, 50)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, text, org, font, 1, (255, 0, 0), 2)
else:
text1 = 'traking fail'
org1 = (50, 50)
font1 = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, text1, org1, font1, 1, (255, 0, 0), 2)
cv2.imshow('dst', dst) # ì»¬ëŸ¬ë²”ìœ„ì— ë”°ë¥¸ ì˜ì—ë¶„í• í™”ë©´ 표시
cv2.imshow('frame',frame) # 최종 frame
cv2.imshow('closing', closing) #open,close í›„ì˜ ì˜ìƒ
cv2.imshow('gradient',gradient ) #모í´ë¡œì§€ì—°ì‚° 후 edges만 추출...
temp = frame.copy() # cornersì˜ ì¢Œí‘œê°’ì„ ë³µì‚¬í•´ì˜´...
key = cv2.waitKey(25)
if key == 27:
break
if cap.isOpened():
cap.release()
cv2.destroyAllWindows()