464 lines
22 KiB
Plaintext
464 lines
22 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "f90cb956-250b-454e-855e-fefcd0ff880a",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"## Import Library"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "93b77493-0a01-4421-b2a0-380991740ff6",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"import cv2\n",
|
|
"import pandas as pd\n",
|
|
"\n",
|
|
"jumlah_kenderaan = 0\n",
|
|
"kenderaan_kiri = 0\n",
|
|
"kenderaan_kanan = 0"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "8d8696ea-4af1-4aa9-96cb-ff4538242ab5",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"## Deklarasi Variable"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "80b4ff7c-1f3b-4e1d-896c-d88c0966f33e",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"6868.0 30.03550936578534 848 478\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"cap = cv2.VideoCapture('video/video.mp4')\n",
|
|
"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",
|
|
"# membuat data frame pandas dengan jumlah baris sama dengan jumlah frame\n",
|
|
"df = pd.DataFrame(index=range(int(frames_count)))\n",
|
|
"df.index.name = \"Frame\" # frame dalam bahasa indonesia\n",
|
|
"\n",
|
|
"framenumber = 0 # mencatat frame saat ini\n",
|
|
"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)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"id": "1c2cd208-3d36-4e1d-9376-5e1d223bd021",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"## Proses Video "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "1c53be2e-1fc0-46af-b8dd-84cd6b1dffdb",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"while True:\n",
|
|
"\n",
|
|
" ret, frame = cap.read() # impor gambar\n",
|
|
"\n",
|
|
" if ret: # jika ada frame lanjutkan kode\n",
|
|
"\n",
|
|
" image = cv2.resize(frame, (0, 0), None, ratio, ratio) # ubah ukuran gambar\n",
|
|
"\n",
|
|
" gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # konversi gambar ke warna abu-abu\n",
|
|
"\n",
|
|
" fgmask = fgbg.apply(gray) # menggunakan pengurangan latar belakang MOG2\n",
|
|
"\n",
|
|
" # 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",
|
|
" 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",
|
|
" # menggunakan konveks hull untuk membuat poligon kait dengan kontur\n",
|
|
" hull = [cv2.convexHull(c) for c in contours]\n",
|
|
"\n",
|
|
" # menggambar kontur\n",
|
|
" cv2.drawContours(image, hull, -1, (0, 255, 0), 3)\n",
|
|
"\n",
|
|
" # garis dibuat untuk menghentikan penghitungan kontur, diperlukan karena mobil jauh menjadi kontur satu\n",
|
|
" lineypos = 100\n",
|
|
" cv2.line(image, (0, lineypos), (width, lineypos), (255, 0, 0), 5)\n",
|
|
"\n",
|
|
" # garis y posisi dibuat untuk menghitung kontur\n",
|
|
" lineypos2 = 125\n",
|
|
" cv2.line(image, (0, lineypos2), (width, lineypos2), (0, 255, 0), 5)\n",
|
|
"\n",
|
|
" # area minimal untuk kontur agar tidak dihitung sebagai rumit\n",
|
|
" minarea = 400\n",
|
|
"\n",
|
|
" # area maksimal untuk kontur, dapat cukup besar untuk bus\n",
|
|
" maxarea = 40000\n",
|
|
"\n",
|
|
" # vektor untuk x dan y lokasi tengah kontur dalam frame saat ini\n",
|
|
" cxx = np.zeros(len(contours))\n",
|
|
" cyy = np.zeros(len(contours))\n",
|
|
"\n",
|
|
" for i in range(len(contours)): # melakukan iterasi pada semua kontur dalam frame saat ini\n",
|
|
"\n",
|
|
" # menggunakan hierarki untuk hanya menghitung kontur induk (kontur yang tidak berada dalam kontur lain)\n",
|
|
" if hierarchy[0, i, 3] == -1:\n",
|
|
"\n",
|
|
" area = cv2.contourArea(contours[i]) # menghitung luas kontur\n",
|
|
"\n",
|
|
" if minarea < area < maxarea: # menggunakan area sebagai garis pembatas untuk kontur\n",
|
|
"\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",
|
|
" if cy > lineypos: # menghapus kontur yang berada di atas garis (y dimulai dari atas)\n",
|
|
"\n",
|
|
" # mengambil titik koordinat untuk membuat kotak lingkaran\n",
|
|
" x, y, w, h = cv2.boundingRect(cnt)\n",
|
|
"\n",
|
|
" # membuat kotak lingkaran dari kontur\n",
|
|
" cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)\n",
|
|
"\n",
|
|
" # Menambahkan teks centroid untuk memverifikasi pada tahap selanjutnya\n",
|
|
" cv2.putText(image, str(cx) + \",\" + str(cy), (cx + 10, cy + 10), cv2.FONT_HERSHEY_SIMPLEX,\n",
|
|
" 0.3, (0, 0, 255), 1)\n",
|
|
"\n",
|
|
" cv2.drawMarker(image, (cx, cy), (0, 0, 255), cv2.MARKER_STAR, markerSize=5, thickness=1,\n",
|
|
" line_type=cv2.LINE_AA)\n",
|
|
"\n",
|
|
" # menambahkan centroid yang telah memenuhi kriteria ke dalam list centroid\n",
|
|
" cxx[i] = cx\n",
|
|
" cyy[i] = cy\n",
|
|
"\n",
|
|
" # menghapus nol dalam vector centroid yang tidak dihitung (centroid yang tidak dikirim ke dataframe)\n",
|
|
" cxx = cxx[cxx != 0]\n",
|
|
" cyy = cyy[cyy != 0]\n",
|
|
"\n",
|
|
" # list kosong untuk nanti mencatat indeks centroid yang dikirim ke dataframe\n",
|
|
" minx_index2 = []\n",
|
|
" miny_index2 = []\n",
|
|
"\n",
|
|
" # jumlah maksimum yang diizinkan untuk centroid dalam frame saat ini untuk dikaitkan dengan centroid dari frame sebelumnya\n",
|
|
" maxrad = 25\n",
|
|
"\n",
|
|
" # bagian berikut mengelola centroid dan mengasignasinya ke id mobil lama atau id mobil baru\n",
|
|
"\n",
|
|
" # jika terdapat centroid dalam area yang ditentukan\n",
|
|
" if len(cxx): # jika ada centroid dalam area yang ditentukan\n",
|
|
"\n",
|
|
" if not carids: # jika daftar carids kosong\n",
|
|
"\n",
|
|
" for i in range(len(cxx)): # melakukan loop sebanyak centroid yang ada\n",
|
|
"\n",
|
|
" carids.append(i) # menambahkan id mobil ke dalam daftar kosong\n",
|
|
" df[str(carids[i])] = \"\" # menambahkan kolom ke dalam dataframe berdasarkan id mobil\n",
|
|
"\n",
|
|
" # mengisi nilai centroid pada frame saat ini dan id mobil yang sesuai\n",
|
|
" df.at[int(framenumber), str(carids[i])] = [cxx[i], cyy[i]]\n",
|
|
"\n",
|
|
" totalcars = carids[i] + 1 # menambahkan 1 pada jumlah mobil\n",
|
|
"\n",
|
|
" else: # jika sudah ada id mobil\n",
|
|
"\n",
|
|
" dx = np.zeros((len(cxx), len(carids))) # array untuk menghitung deltanya\n",
|
|
" dy = np.zeros((len(cyy), len(carids))) # array untuk menghitung deltanya\n",
|
|
"\n",
|
|
" for i in range(len(cxx)): # melakukan loop sebanyak centroid yang ada\n",
|
|
"\n",
|
|
" for j in range(len(carids)): # melakukan loop sebanyak id mobil yang ada\n",
|
|
"\n",
|
|
" # mengambil centroid dari frame sebelumnya untuk id mobil tertentu\n",
|
|
" oldcxcy = df.iloc[int(framenumber - 1)][str(carids[j])]\n",
|
|
"\n",
|
|
" # mengambil centroid dari frame sekarang yang tidak selalu sesuai dengan centroid dari frame sebelumnya\n",
|
|
" curcxcy = np.array([cxx[i], cyy[i]])\n",
|
|
"\n",
|
|
" if not oldcxcy: # jika centroid dari frame sebelumnya kosong karena mobil keluar layar\n",
|
|
"\n",
|
|
" continue # lanjutkan ke id mobil selanjutnya\n",
|
|
"\n",
|
|
" else: # hitung deltanya untuk dibandingkan dengan centroid dari frame sekarang\n",
|
|
"\n",
|
|
" dx[i, j] = oldcxcy[0] - curcxcy[0]\n",
|
|
" dy[i, j] = oldcxcy[1] - curcxcy[1]\n",
|
|
"\n",
|
|
" for j in range(len(carids)): # melakukan loop sebanyak id mobil yang ada\n",
|
|
"\n",
|
|
" jumlahjumlah = np.abs(dx[:, j]) + np.abs(dy[:, j]) # menghitung jumlah delta wrt id mobil tertentu\n",
|
|
"\n",
|
|
" # 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",
|
|
"\n",
|
|
" # mengambil nilai delta untuk id mobil yang dipilih\n",
|
|
" deltadeltadx = dx[minx_index, j]\n",
|
|
" deltadeltady = dy[miny_index, j]\n",
|
|
"\n",
|
|
" 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",
|
|
"\n",
|
|
" continue # lanjutkan ke id mobil selanjutnya\n",
|
|
"\n",
|
|
" else:\n",
|
|
"\n",
|
|
" # 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",
|
|
"\n",
|
|
" # menambahkan centroid ke id mobil yang sudah ada\n",
|
|
" df.at[int(framenumber), str(carids[j])] = [cxx[minx_index], cyy[miny_index]]\n",
|
|
" minx_index2.append(minx_index) # menambahkan indeks centroid yang sudah ditambahkan ke id mobil lain\n",
|
|
" miny_index2.append(miny_index)\n",
|
|
"\n",
|
|
" 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",
|
|
"\n",
|
|
" currentcars = 0 # mobil yang ada di layar\n",
|
|
" currentcarsindex = [] # indeks id mobil yang ada di layar\n",
|
|
"\n",
|
|
" 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",
|
|
" if df.at[int(framenumber), str(carids[i])] != '':\n",
|
|
"\n",
|
|
" currentcars = currentcars + 1 # menambahkan mobil yang ada di layar\n",
|
|
" currentcarsindex.append(i) # menambahkan id mobil yang ada di layar\n",
|
|
"\n",
|
|
" for i in range(currentcars): # melakukan loops sebanyak jumlah mobil yang ada di layar\n",
|
|
"\n",
|
|
" # mengambil centroid untuk id mobil tertentu pada frame saat ini\n",
|
|
" curcent = df.iloc[int(framenumber)][str(carids[currentcarsindex[i]])]\n",
|
|
"\n",
|
|
" # mengambil centroid untuk id mobil tertentu pada frame sebelumnya\n",
|
|
" oldcent = df.iloc[int(framenumber - 1)][str(carids[currentcarsindex[i]])]\n",
|
|
"\n",
|
|
" if curcent: # jika ada centroid pada frame saat ini\n",
|
|
"\n",
|
|
" # Teks di layar untuk centroid saat ini\n",
|
|
" 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",
|
|
" # 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",
|
|
" if oldcent[1] >= lineypos2 and curcent[1] <= lineypos2 and carids[\n",
|
|
" currentcarsindex[i]] not in caridscrossed:\n",
|
|
"\n",
|
|
" carscrossedup = carscrossedup + 1\n",
|
|
" kenderaan_kiri = carscrossedup\n",
|
|
" cv2.line(image, (0, lineypos2), (width, lineypos2), (0, 0, 255), 5)\n",
|
|
" caridscrossed.append(\n",
|
|
" currentcarsindex[i]) # Tambahkan id mobil ke daftar mobil yang dihitung untuk mencegah penghitungan dua kali\n",
|
|
"\n",
|
|
" # 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",
|
|
" elif oldcent[1] <= lineypos2 and curcent[1] >= lineypos2 and carids[\n",
|
|
" currentcarsindex[i]] not in caridscrossed:\n",
|
|
"\n",
|
|
" carscrosseddown = carscrosseddown + 1\n",
|
|
" kenderaan_kanan = carscrosseddown\n",
|
|
" cv2.line(image, (0, lineypos2), (width, lineypos2), (0, 0, 125), 5)\n",
|
|
" caridscrossed.append(currentcarsindex[i])\n",
|
|
"\n",
|
|
" # 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",
|
|
" 1)\n",
|
|
"\n",
|
|
" # menampilkan jumlah mobil yang melintasi bawah\n",
|
|
" cv2.putText(image, \"Mobil yang Melintasi Bawah: \" + str(carscrosseddown), (0, 30), cv2.FONT_HERSHEY_SIMPLEX, .5,\n",
|
|
" (255, 255, 0), 1)\n",
|
|
"\n",
|
|
" # # 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",
|
|
"\n",
|
|
" # 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",
|
|
"\n",
|
|
" # 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",
|
|
" + ' detik', (0, 60), cv2.FONT_HERSHEY_SIMPLEX, .6, (255, 255, 0), 1)\n",
|
|
"\n",
|
|
" # 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",
|
|
"\n",
|
|
" cv2.imshow(\"gray\", gray)\n",
|
|
" cv2.moveWindow(\"gray\", int(width * ratio), 0)\n",
|
|
"\n",
|
|
" cv2.imshow(\"closing\", bins)\n",
|
|
" cv2.moveWindow(\"closing\", width, 0)\n",
|
|
"\n",
|
|
" cv2.imshow(\"Output\", image)\n",
|
|
" cv2.moveWindow(\"Output\", width, int(height * ratio))\n",
|
|
"\n",
|
|
" # cv2.imshow(\"opening\", opening)\n",
|
|
" # cv2.moveWindow(\"opening\", 0, int(height * ratio))\n",
|
|
"\n",
|
|
" # cv2.imshow(\"dilation\", dilation)\n",
|
|
" # cv2.moveWindow(\"dilation\", int(width * ratio), int(height * ratio))\n",
|
|
"\n",
|
|
" # cv2.imshow(\"binary\", bins)\n",
|
|
" # cv2.moveWindow(\"binary\", width, int(height * ratio))\n",
|
|
"\n",
|
|
"\n",
|
|
" # adds to framecount\n",
|
|
" framenumber = framenumber + 1\n",
|
|
"\n",
|
|
" # 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",
|
|
" break\n",
|
|
"\n",
|
|
" else: # bahasa indonesia: Jika video selesai maka break loop\n",
|
|
"\n",
|
|
"\n",
|
|
" break\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "d0ecd4ba-d8bd-4450-838a-c539dbe5c6b4",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"## Menutup Window OpenCV"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "78a5686d-3899-4dac-a95c-cff992ac52ba",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"print(kenderaan_kiri)\n",
|
|
"print(kenderaan_kanan)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"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
|
|
}
|