2024-05-06 00:26:54 +00:00
|
|
|
{
|
|
|
|
"cells": [
|
2024-05-06 00:30:22 +00:00
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-06-10 05:47:41 +00:00
|
|
|
"execution_count": 1,
|
2024-05-06 00:30:22 +00:00
|
|
|
"id": "f90cb956-250b-454e-855e-fefcd0ff880a",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"## Import Library"
|
|
|
|
]
|
|
|
|
},
|
2024-05-06 00:26:54 +00:00
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-06-10 05:47:41 +00:00
|
|
|
"execution_count": 2,
|
2024-05-06 00:26:54 +00:00
|
|
|
"id": "93b77493-0a01-4421-b2a0-380991740ff6",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"import numpy as np\n",
|
|
|
|
"import cv2\n",
|
|
|
|
"import pandas as pd\n",
|
2024-05-21 11:24:53 +00:00
|
|
|
"\n",
|
|
|
|
"jumlah_kenderaan = 0\n",
|
|
|
|
"kenderaan_kiri = 0\n",
|
|
|
|
"kenderaan_kanan = 0"
|
2024-05-06 00:26:54 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-06-10 05:47:41 +00:00
|
|
|
"execution_count": 3,
|
2024-05-21 11:24:53 +00:00
|
|
|
"id": "8d8696ea-4af1-4aa9-96cb-ff4538242ab5",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"## Deklarasi Variable"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-06-10 05:47:41 +00:00
|
|
|
"execution_count": 4,
|
2024-05-06 00:26:54 +00:00
|
|
|
"id": "80b4ff7c-1f3b-4e1d-896c-d88c0966f33e",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "stdout",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
2024-06-10 05:47:41 +00:00
|
|
|
"6868.0 30.03550936578534 848 478\n"
|
2024-05-06 00:26:54 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
2024-06-10 05:47:41 +00:00
|
|
|
"cap = cv2.VideoCapture('video/video.mp4')\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"frames_count, fps, width, height = cap.get(cv2.CAP_PROP_FRAME_COUNT), cap.get(cv2.CAP_PROP_FPS), cap.get(\n",
|
|
|
|
" cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)\n",
|
|
|
|
"width = int(width)\n",
|
|
|
|
"height = int(height)\n",
|
|
|
|
"print(frames_count, fps, width, height)\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
"# membuat data frame pandas dengan jumlah baris sama dengan jumlah frame\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"df = pd.DataFrame(index=range(int(frames_count)))\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
"df.index.name = \"Frame\" # frame dalam bahasa indonesia\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
|
|
|
"framenumber = 0 # mencatat frame saat ini\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
"carscrossedup = 0 # mencatat mobil yang melintasi atas\n",
|
|
|
|
"carscrosseddown = 0 # mencatat mobil yang melintasi bawah\n",
|
|
|
|
"carids = [] # list kosong untuk menambah id mobil\n",
|
|
|
|
"caridscrossed = [] # list kosong untuk menambah id mobil yang telah melintasi\n",
|
|
|
|
"totalcars = 0 # mencatat total mobil\n",
|
|
|
|
"\n",
|
|
|
|
"fgbg = cv2.createBackgroundSubtractorMOG2() # membuat subtractor latar belakang MOG2\n",
|
|
|
|
"\n",
|
|
|
|
"# informasi untuk memulai menyimpan file video\n",
|
|
|
|
"ret, frame = cap.read() # impor gambar\n",
|
|
|
|
"ratio = .5 # rasio pengubah ukuran\n",
|
|
|
|
"image = cv2.resize(frame, (0, 0), None, ratio, ratio) # ubah ukuran gambar\n",
|
|
|
|
"width2, height2, channels = image.shape\n",
|
|
|
|
"# video = cv2.VideoWriter('penghitung_kendaraan.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, (height2, width2), 1)"
|
2024-05-06 00:26:54 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-06-10 05:47:41 +00:00
|
|
|
"execution_count": 5,
|
2024-05-21 11:24:53 +00:00
|
|
|
"id": "1c2cd208-3d36-4e1d-9376-5e1d223bd021",
|
2024-05-06 00:26:54 +00:00
|
|
|
"metadata": {},
|
2024-05-06 00:30:22 +00:00
|
|
|
"outputs": [],
|
2024-05-21 11:24:53 +00:00
|
|
|
"source": [
|
|
|
|
"## Proses Video "
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-07-10 04:18:35 +00:00
|
|
|
"execution_count": 6,
|
2024-05-21 11:24:53 +00:00
|
|
|
"id": "1c53be2e-1fc0-46af-b8dd-84cd6b1dffdb",
|
|
|
|
"metadata": {},
|
2024-06-10 05:47:41 +00:00
|
|
|
"outputs": [],
|
2024-05-06 00:26:54 +00:00
|
|
|
"source": [
|
|
|
|
"while True:\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" ret, frame = cap.read() # impor gambar\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" if ret: # jika ada frame lanjutkan kode\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" image = cv2.resize(frame, (0, 0), None, ratio, ratio) # ubah ukuran gambar\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # konversi gambar ke warna abu-abu\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" fgmask = fgbg.apply(gray) # menggunakan pengurangan latar belakang MOG2\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # menerapkan tingkat kesulitan pada fgmask untuk mencoba mengisolasi mobil\n",
|
|
|
|
" # perlu mencoba berbagai pengaturan hingga mobil mudah diidentifikasi\n",
|
|
|
|
" kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) # membuat kernel untuk operasi morfologi\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" closing = cv2.morphologyEx(fgmask, cv2.MORPH_CLOSE, kernel)\n",
|
|
|
|
" opening = cv2.morphologyEx(closing, cv2.MORPH_OPEN, kernel)\n",
|
|
|
|
" dilation = cv2.dilate(opening, kernel)\n",
|
|
|
|
" retvalbin, bins = cv2.threshold(dilation, 220, 255, cv2.THRESH_BINARY) # menghapus shadow\n",
|
|
|
|
"\n",
|
|
|
|
" # membuat kontur\n",
|
|
|
|
" contours, hierarchy = cv2.findContours(bins, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2:]\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # menggunakan konveks hull untuk membuat poligon kait dengan kontur\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" hull = [cv2.convexHull(c) for c in contours]\n",
|
|
|
|
"\n",
|
|
|
|
" # menggambar kontur\n",
|
|
|
|
" cv2.drawContours(image, hull, -1, (0, 255, 0), 3)\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # garis dibuat untuk menghentikan penghitungan kontur, diperlukan karena mobil jauh menjadi kontur satu\n",
|
|
|
|
" lineypos = 100\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" cv2.line(image, (0, lineypos), (width, lineypos), (255, 0, 0), 5)\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # garis y posisi dibuat untuk menghitung kontur\n",
|
|
|
|
" lineypos2 = 125\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" cv2.line(image, (0, lineypos2), (width, lineypos2), (0, 255, 0), 5)\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # area minimal untuk kontur agar tidak dihitung sebagai rumit\n",
|
|
|
|
" minarea = 400\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # area maksimal untuk kontur, dapat cukup besar untuk bus\n",
|
|
|
|
" maxarea = 40000\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # vektor untuk x dan y lokasi tengah kontur dalam frame saat ini\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" cxx = np.zeros(len(contours))\n",
|
|
|
|
" cyy = np.zeros(len(contours))\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" for i in range(len(contours)): # melakukan iterasi pada semua kontur dalam frame saat ini\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # menggunakan hierarki untuk hanya menghitung kontur induk (kontur yang tidak berada dalam kontur lain)\n",
|
|
|
|
" if hierarchy[0, i, 3] == -1:\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" area = cv2.contourArea(contours[i]) # menghitung luas kontur\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" if minarea < area < maxarea: # menggunakan area sebagai garis pembatas untuk kontur\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
|
|
|
" # menghitung centroid dari kontur\n",
|
|
|
|
" cnt = contours[i]\n",
|
|
|
|
" M = cv2.moments(cnt)\n",
|
|
|
|
" cx = int(M['m10'] / M['m00'])\n",
|
|
|
|
" cy = int(M['m01'] / M['m00'])\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" if cy > lineypos: # menghapus kontur yang berada di atas garis (y dimulai dari atas)\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # mengambil titik koordinat untuk membuat kotak lingkaran\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" x, y, w, h = cv2.boundingRect(cnt)\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # membuat kotak lingkaran dari kontur\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # Menambahkan teks centroid untuk memverifikasi pada tahap selanjutnya\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" cv2.putText(image, str(cx) + \",\" + str(cy), (cx + 10, cy + 10), cv2.FONT_HERSHEY_SIMPLEX,\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" 0.3, (0, 0, 255), 1)\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
|
|
|
" cv2.drawMarker(image, (cx, cy), (0, 0, 255), cv2.MARKER_STAR, markerSize=5, thickness=1,\n",
|
|
|
|
" line_type=cv2.LINE_AA)\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # menambahkan centroid yang telah memenuhi kriteria ke dalam list centroid\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" cxx[i] = cx\n",
|
|
|
|
" cyy[i] = cy\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # menghapus nol dalam vector centroid yang tidak dihitung (centroid yang tidak dikirim ke dataframe)\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" cxx = cxx[cxx != 0]\n",
|
|
|
|
" cyy = cyy[cyy != 0]\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # list kosong untuk nanti mencatat indeks centroid yang dikirim ke dataframe\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" minx_index2 = []\n",
|
|
|
|
" miny_index2 = []\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # jumlah maksimum yang diizinkan untuk centroid dalam frame saat ini untuk dikaitkan dengan centroid dari frame sebelumnya\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" maxrad = 25\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # bagian berikut mengelola centroid dan mengasignasinya ke id mobil lama atau id mobil baru\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # jika terdapat centroid dalam area yang ditentukan\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" if len(cxx): # jika ada centroid dalam area yang ditentukan\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" if not carids: # jika daftar carids kosong\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" for i in range(len(cxx)): # melakukan loop sebanyak centroid yang ada\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" carids.append(i) # menambahkan id mobil ke dalam daftar kosong\n",
|
|
|
|
" df[str(carids[i])] = \"\" # menambahkan kolom ke dalam dataframe berdasarkan id mobil\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # mengisi nilai centroid pada frame saat ini dan id mobil yang sesuai\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" df.at[int(framenumber), str(carids[i])] = [cxx[i], cyy[i]]\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" totalcars = carids[i] + 1 # menambahkan 1 pada jumlah mobil\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" else: # jika sudah ada id mobil\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" dx = np.zeros((len(cxx), len(carids))) # array untuk menghitung deltanya\n",
|
|
|
|
" dy = np.zeros((len(cyy), len(carids))) # array untuk menghitung deltanya\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" for i in range(len(cxx)): # melakukan loop sebanyak centroid yang ada\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" for j in range(len(carids)): # melakukan loop sebanyak id mobil yang ada\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # mengambil centroid dari frame sebelumnya untuk id mobil tertentu\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" oldcxcy = df.iloc[int(framenumber - 1)][str(carids[j])]\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # mengambil centroid dari frame sekarang yang tidak selalu sesuai dengan centroid dari frame sebelumnya\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" curcxcy = np.array([cxx[i], cyy[i]])\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" if not oldcxcy: # jika centroid dari frame sebelumnya kosong karena mobil keluar layar\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" continue # lanjutkan ke id mobil selanjutnya\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" else: # hitung deltanya untuk dibandingkan dengan centroid dari frame sekarang\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
|
|
|
" dx[i, j] = oldcxcy[0] - curcxcy[0]\n",
|
|
|
|
" dy[i, j] = oldcxcy[1] - curcxcy[1]\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" for j in range(len(carids)): # melakukan loop sebanyak id mobil yang ada\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" jumlahjumlah = np.abs(dx[:, j]) + np.abs(dy[:, j]) # menghitung jumlah delta wrt id mobil tertentu\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # mencari indeks id mobil yang memiliki nilai minimum dan ini indeks yang tepat\n",
|
|
|
|
" indeksindextrue = np.argmin(np.abs(jumlahjumlah))\n",
|
|
|
|
" minx_index = indeksindextrue\n",
|
|
|
|
" miny_index = indeksindextrue\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # mengambil nilai delta untuk id mobil yang dipilih\n",
|
|
|
|
" deltadeltadx = dx[minx_index, j]\n",
|
|
|
|
" deltadeltady = dy[miny_index, j]\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" if deltadeltadx == 0 and deltadeltady == 0 and np.all(dx[:, j] == 0) and np.all(dy[:, j] == 0):\n",
|
|
|
|
" # periksa apakah nilai minimum adalah 0 dan periksa apakah semua delta adalah nol karena ini adalah kumpulan kosong\n",
|
|
|
|
" # delta dapat berupa nol jika centroid tidak berpindah\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" continue # lanjutkan ke id mobil selanjutnya\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
|
|
|
" else:\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # jika nilai delta kurang dari radius maksimum maka tambahkan centroid ke id mobil yang sesuai\n",
|
|
|
|
" if np.abs(deltadeltadx) < maxrad and np.abs(deltadeltady) < maxrad:\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # menambahkan centroid ke id mobil yang sudah ada\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" df.at[int(framenumber), str(carids[j])] = [cxx[minx_index], cyy[miny_index]]\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" minx_index2.append(minx_index) # menambahkan indeks centroid yang sudah ditambahkan ke id mobil lain\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" miny_index2.append(miny_index)\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" for i in range(len(cxx)): # melakukan loop sebanyak centroid yang ada\n",
|
|
|
|
"\n",
|
|
|
|
" # jika centroid tidak ada dalam list minindex maka mobil baru perlu ditambahkan\n",
|
|
|
|
" if i not in minx_index2 and miny_index2:\n",
|
|
|
|
"\n",
|
|
|
|
" df[str(totalcars)] = \"\" # membuat kolom baru untuk mobil baru yang tercatat\n",
|
|
|
|
" totalcars = totalcars + 1 # menambahkan jumlah mobil yang tercatat\n",
|
|
|
|
" t = totalcars - 1 # t adalah placeholder untuk jumlah mobil\n",
|
|
|
|
" carids.append(t) # menambahkan id mobil ke list id mobil\n",
|
|
|
|
" df.at[int(framenumber), str(t)] = [cxx[i], cyy[i]] # menambahkan centroid ke mobil yang sudah ada\n",
|
|
|
|
"\n",
|
|
|
|
" elif curcxcy[0] and not oldcxcy and not minx_index2 and not miny_index2:\n",
|
|
|
|
" # jika centroid saat ini ada namun centroid sebelumnya tidak ada\n",
|
|
|
|
" # mobil baru perlu ditambahkan jika minindex2 kosong\n",
|
|
|
|
"\n",
|
|
|
|
" df[str(totalcars)] = \"\" # membuat kolom baru untuk mobil baru yang tercatat\n",
|
|
|
|
" totalcars = totalcars + 1 # menambahkan jumlah mobil yang tercatat\n",
|
|
|
|
" t = totalcars - 1 # t adalah placeholder untuk jumlah mobil\n",
|
|
|
|
" carids.append(t) # menambahkan id mobil ke list id mobil\n",
|
|
|
|
" df.at[int(framenumber), str(t)] = [cxx[i], cyy[i]] # menambahkan centroid ke mobil yang sudah ada\n",
|
|
|
|
"\n",
|
|
|
|
" # Bagian di bawah menglabel centroid yang ada di layar\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" currentcars = 0 # mobil yang ada di layar\n",
|
|
|
|
" currentcarsindex = [] # indeks id mobil yang ada di layar\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" for i in range(len(carids)): # melakukan loops sebanyak jumlah id mobil\n",
|
|
|
|
"\n",
|
|
|
|
" # memeriksa frame saat ini untuk mengetahui id mobil yang sedang aktif\n",
|
|
|
|
" # dengan memeriksa adanya centroid pada frame saat ini untuk id mobil tertentu\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" if df.at[int(framenumber), str(carids[i])] != '':\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" currentcars = currentcars + 1 # menambahkan mobil yang ada di layar\n",
|
|
|
|
" currentcarsindex.append(i) # menambahkan id mobil yang ada di layar\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" for i in range(currentcars): # melakukan loops sebanyak jumlah mobil yang ada di layar\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # mengambil centroid untuk id mobil tertentu pada frame saat ini\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" curcent = df.iloc[int(framenumber)][str(carids[currentcarsindex[i]])]\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # mengambil centroid untuk id mobil tertentu pada frame sebelumnya\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" oldcent = df.iloc[int(framenumber - 1)][str(carids[currentcarsindex[i]])]\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" if curcent: # jika ada centroid pada frame saat ini\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # Teks di layar untuk centroid saat ini\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" cv2.putText(image, \"Centroid\" + str(curcent[0]) + \",\" + str(curcent[1]),\n",
|
|
|
|
" (int(curcent[0]), int(curcent[1])), cv2.FONT_HERSHEY_SIMPLEX, .5, (0, 255, 255), 2)\n",
|
|
|
|
"\n",
|
|
|
|
" cv2.putText(image, \"ID:\" + str(carids[currentcarsindex[i]]), (int(curcent[0]), int(curcent[1] - 15)),\n",
|
|
|
|
" cv2.FONT_HERSHEY_SIMPLEX, .5, (0, 255, 255), 2)\n",
|
|
|
|
"\n",
|
|
|
|
" cv2.drawMarker(image, (int(curcent[0]), int(curcent[1])), (0, 0, 255), cv2.MARKER_STAR, markerSize=5,\n",
|
|
|
|
" thickness=1, line_type=cv2.LINE_AA)\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # Periksa apakah centroid lama ada\n",
|
|
|
|
" # Tambahkan kotak radius dari centroid lama ke centroid saat ini untuk visualisasi\n",
|
|
|
|
" if oldcent:\n",
|
|
|
|
" xmulai = oldcent[0] - maxrad\n",
|
|
|
|
" ymulai = oldcent[1] - maxrad\n",
|
|
|
|
" xakhir = oldcent[0] + maxrad\n",
|
|
|
|
" yakhir = oldcent[1] + maxrad\n",
|
|
|
|
" cv2.rectangle(image, (int(xmulai), int(ymulai)), (int(xakhir), int(yakhir)), (0, 125, 0), 1)\n",
|
|
|
|
"\n",
|
|
|
|
" # Periksa apakah centroid lama di bawah garis dan centroid baru di atas garis\n",
|
|
|
|
" # Untuk menghitung mobil dan memastikan mobil tidak dihitung dua kali\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" if oldcent[1] >= lineypos2 and curcent[1] <= lineypos2 and carids[\n",
|
|
|
|
" currentcarsindex[i]] not in caridscrossed:\n",
|
|
|
|
"\n",
|
|
|
|
" carscrossedup = carscrossedup + 1\n",
|
2024-05-21 11:24:53 +00:00
|
|
|
" kenderaan_kiri = carscrossedup\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" cv2.line(image, (0, lineypos2), (width, lineypos2), (0, 0, 255), 5)\n",
|
|
|
|
" caridscrossed.append(\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" currentcarsindex[i]) # Tambahkan id mobil ke daftar mobil yang dihitung untuk mencegah penghitungan dua kali\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # Periksa apakah centroid lama di atas garis dan centroid baru di bawah garis\n",
|
|
|
|
" # Untuk menghitung mobil dan memastikan mobil tidak dihitung dua kali\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" elif oldcent[1] <= lineypos2 and curcent[1] >= lineypos2 and carids[\n",
|
|
|
|
" currentcarsindex[i]] not in caridscrossed:\n",
|
|
|
|
"\n",
|
|
|
|
" carscrosseddown = carscrosseddown + 1\n",
|
2024-05-21 11:24:53 +00:00
|
|
|
" kenderaan_kanan = carscrosseddown\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" cv2.line(image, (0, lineypos2), (width, lineypos2), (0, 0, 125), 5)\n",
|
|
|
|
" caridscrossed.append(currentcarsindex[i])\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # menampilkan jumlah mobil yang melintasi atas\n",
|
|
|
|
" cv2.putText(image, \"Mobil yang Melintasi Atas: \" + str(carscrossedup), (0, 15), cv2.FONT_HERSHEY_SIMPLEX, .5, (255, 255, 255),\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" 1)\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # menampilkan jumlah mobil yang melintasi bawah\n",
|
|
|
|
" cv2.putText(image, \"Mobil yang Melintasi Bawah: \" + str(carscrosseddown), (0, 30), cv2.FONT_HERSHEY_SIMPLEX, .5,\n",
|
2024-05-21 11:24:53 +00:00
|
|
|
" (255, 255, 0), 1)\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # # menampilkan jumlah total mobil yang terdeteksi\n",
|
|
|
|
" # cv2.putText(image, \"Total Mobil yang Terdeteksi: \" + str(len(carids)), (0, 60), cv2.FONT_HERSHEY_SIMPLEX, .5,\n",
|
|
|
|
" # (255, 255, 255), 1)\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # menampilkan frame saat ini dan total frame\n",
|
|
|
|
" cv2.putText(image, \"Frame: \" + str(framenumber) + ' dari ' + str(frames_count), (0, 45), cv2.FONT_HERSHEY_SIMPLEX,\n",
|
|
|
|
" .5, (255, 255, 255), 1)\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # menampilkan waktu yang sudah berlalu dan total waktu\n",
|
|
|
|
" cv2.putText(image, 'Waktu: ' + str(round(framenumber / fps, 2)) + ' detik dari ' + str(round(frames_count / fps, 2))\n",
|
2024-05-21 11:24:53 +00:00
|
|
|
" + ' detik', (0, 60), cv2.FONT_HERSHEY_SIMPLEX, .6, (255, 255, 0), 1)\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-06-10 05:47:41 +00:00
|
|
|
" # make them view side by side\n",
|
|
|
|
"\n",
|
|
|
|
" # make this view resize same as \n",
|
|
|
|
" # ratio = height / 640\n",
|
|
|
|
" frame = cv2.resize(frame, (int(width * ratio), int(height * ratio)))\n",
|
|
|
|
" cv2.imshow(\"Input\", frame)\n",
|
|
|
|
" cv2.moveWindow(\"Input\", 0, 0)\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" cv2.imshow(\"gray\", gray)\n",
|
|
|
|
" cv2.moveWindow(\"gray\", int(width * ratio), 0)\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-06-10 05:47:41 +00:00
|
|
|
" cv2.imshow(\"closing\", bins)\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" cv2.moveWindow(\"closing\", width, 0)\n",
|
|
|
|
"\n",
|
2024-06-10 05:47:41 +00:00
|
|
|
" cv2.imshow(\"Output\", image)\n",
|
|
|
|
" cv2.moveWindow(\"Output\", width, int(height * ratio))\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # cv2.imshow(\"opening\", opening)\n",
|
|
|
|
" # cv2.moveWindow(\"opening\", 0, int(height * ratio))\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # cv2.imshow(\"dilation\", dilation)\n",
|
|
|
|
" # cv2.moveWindow(\"dilation\", int(width * ratio), int(height * ratio))\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # cv2.imshow(\"binary\", bins)\n",
|
|
|
|
" # cv2.moveWindow(\"binary\", width, int(height * ratio))\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
|
|
|
"\n",
|
|
|
|
" # adds to framecount\n",
|
|
|
|
" framenumber = framenumber + 1\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" # Menunggu key dari user dalam milidetik, fps adalah frame per detik, dan 0xff adalah binary\n",
|
|
|
|
" # bahasa indonesia: Menunggu key dari user dalam milidetik\n",
|
|
|
|
" k = cv2.waitKey(int(1000/fps)) & 0xff \n",
|
|
|
|
" if k == 27: # bahasa indonesia: Jika key nya adalah 27 (ESC) maka break loop\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
" break\n",
|
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" else: # bahasa indonesia: Jika video selesai maka break loop\n",
|
|
|
|
"\n",
|
2024-05-06 00:26:54 +00:00
|
|
|
"\n",
|
2024-05-06 00:30:22 +00:00
|
|
|
" break\n",
|
|
|
|
"\n"
|
2024-05-06 00:26:54 +00:00
|
|
|
]
|
|
|
|
},
|
2024-05-21 11:24:53 +00:00
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-07-10 04:18:35 +00:00
|
|
|
"execution_count": 7,
|
2024-05-21 11:24:53 +00:00
|
|
|
"id": "d0ecd4ba-d8bd-4450-838a-c539dbe5c6b4",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"## Menutup Window OpenCV"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-07-10 04:18:35 +00:00
|
|
|
"execution_count": 8,
|
2024-05-21 11:24:53 +00:00
|
|
|
"id": "78a5686d-3899-4dac-a95c-cff992ac52ba",
|
|
|
|
"metadata": {},
|
2024-07-10 04:18:35 +00:00
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "stdout",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
|
|
|
"1\n",
|
|
|
|
"0\n"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
2024-05-21 11:24:53 +00:00
|
|
|
"source": [
|
|
|
|
"print(kenderaan_kiri)\n",
|
|
|
|
"print(kenderaan_kanan)"
|
|
|
|
]
|
|
|
|
},
|
2024-05-06 00:26:54 +00:00
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-07-10 04:18:35 +00:00
|
|
|
"execution_count": 9,
|
2024-05-06 00:26:54 +00:00
|
|
|
"id": "af84e6b4-dd55-447e-ac8c-a02a5f6f34be",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"cap.release()\n",
|
|
|
|
"cv2.destroyAllWindows()\n"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"metadata": {
|
|
|
|
"kernelspec": {
|
|
|
|
"display_name": "Python 3 (ipykernel)",
|
|
|
|
"language": "python",
|
|
|
|
"name": "python3"
|
|
|
|
},
|
|
|
|
"language_info": {
|
|
|
|
"codemirror_mode": {
|
|
|
|
"name": "ipython",
|
|
|
|
"version": 3
|
|
|
|
},
|
|
|
|
"file_extension": ".py",
|
|
|
|
"mimetype": "text/x-python",
|
|
|
|
"name": "python",
|
|
|
|
"nbconvert_exporter": "python",
|
|
|
|
"pygments_lexer": "ipython3",
|
|
|
|
"version": "3.10.10"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"nbformat": 4,
|
|
|
|
"nbformat_minor": 5
|
|
|
|
}
|