https://sourceforge.net/projects/opencvlibrary/files/opencv-win/
Hasil download di extract, lalu cari file cv2.pyd dan biasanya ada di lokasi sbb: vrsi 32bit dan 64bit
C:\Users\ACER\Downloads\opencv\build\python\2.7\x64
C:\Users\ACER\Downloads\opencv\build\python\2.7\x86
Copy salah satu sesuai dengan jenis OS masing-masing lalu copas ke lib python kita
Untuk versi saya saya copas ke :
C:\Python27\Lib\site-packages
Studi Kasus Menghitung Jumlah Kendaraan:
import cv2
bgsMOG = cv2.BackgroundSubtractorMOG2()
cap = cv2.VideoCapture("hitungjumkendaraan.avi")
counter =0
flag = 0
flaga = 0
flagb = 0
flagc = 0
flagd = 0
flage = 0
flagf = 0
flagg = 0
flagh = 0
flagi = 0
flagj = 0
flagk = 0
if cap:
while True:
ret, frame = cap.read()
if ret:
fgmask = bgsMOG.apply(frame, None, 0.01)
garis = cv2.line(frame,(750,600),(470,600),(200,200,0),2)
contours, hierarchy = cv2.findContours(fgmask,
cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
try: hierarchy = hierarchy[0]
except: hierarchy = []
for contour, hier in zip(contours, hierarchy):
(x,y,w,h) = cv2.boundingRect(contour)
if w > 9 and h > 14:
cv2.rectangle(frame, (x,y), (x+w,y+h), (180, 1, 0), 1)
x1=w/2
y1=h/2
cx=x+x1
cy=y+y1
centroid=(cx,cy)
titik = cv2.circle(frame,(int(cx),int(cy)),4,(0,255,0),-1)
if cy == 130:
if cy==130 and flag == flaga :
flaga= flag +1
counter=counter+1
elif cy == 130 and flag < flaga:
flaga = flag
counter = counter + 0
elif cy == 129:
if cy==129 and flag == flagb :
flagb= flag +1
counter=counter+1
elif cy == 129 and flag < flagb:
flagb = flag
counter = counter + 0
elif cy == 127:
if cy==127 and flag == flagd :
flagd= flag +1
counter=counter+1
elif cy == 127 and flag < flagd:
flagd = flag
counter = counter + 0
elif cy == 126:
if cy==126 and flag == flage :
flage= flag +1
counter=counter+1
elif cy == 126 and flag < flage:
flage = flag
counter = counter + 0
elif cy == 124:
if cy==124 and flag == flagg:
flagg= flag +1
counter=counter+1
elif cy == 124 and flag < flagg:
flagg = flag
counter = counter + 0
elif cy == 123:
if cy==123 and flag == flagh :
flagh= flag +1
counter=counter+1
elif cy == 123 and flag < flagh:
flagh = flag
counter = counter + 0
elif cy == 121:
if cy==121 and flag == flagj:
flagj= flag +1
counter=counter+1
elif cy == 121 and flag < flagj:
flagj = flag
counter = counter + 0
elif cy == 120:
if cy==120 and flag == flagk:
flagk= flag +1
counter=counter+1
elif cy == 120 and flag < flagk:
flagk = flag
counter = counter + 0
else:
counter=counter+0
cv2.putText(frame, str(counter),(10,150),
cv2.FONT_HERSHEY_SIMPLEX,2, (255, 0, 0), 1, True)
print (cy)
print (flaga)
cv2.imshow('Output', frame)
cv2.imshow('FGMASK', fgmask)
key = cv2.waitKey(100)
if key == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Tidak ada komentar:
Posting Komentar