added kepadatan kenderaan

This commit is contained in:
kicap 2024-07-10 12:18:35 +08:00
parent 9273d712bb
commit 1f60d77958
3 changed files with 70 additions and 17 deletions

View File

@ -91,7 +91,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "1c53be2e-1fc0-46af-b8dd-84cd6b1dffdb",
"metadata": {},
"outputs": [],
@ -408,7 +408,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "d0ecd4ba-d8bd-4450-838a-c539dbe5c6b4",
"metadata": {},
"outputs": [],
@ -418,10 +418,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "78a5686d-3899-4dac-a95c-cff992ac52ba",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"0\n"
]
}
],
"source": [
"print(kenderaan_kiri)\n",
"print(kenderaan_kanan)"
@ -429,7 +438,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"id": "af84e6b4-dd55-447e-ac8c-a02a5f6f34be",
"metadata": {},
"outputs": [],

18
app.py
View File

@ -18,6 +18,7 @@ videonya = None
jumlah_kenderaan = 0
kenderaan_kiri = 0
kenderaan_kanan = 0
kenderaan_sekarang = 0
selesainya= False
MYSQL_HOST = os.getenv('MYSQL_HOST')
@ -62,7 +63,7 @@ async def generate_frames2(video, threshold,stat):
global jumlah_kenderaan
global kenderaan_kiri
global kenderaan_kanan
global cap,selesainya
global cap,selesainya, kenderaan_sekarang
jumlah_kenderaan = 0
kenderaan_kiri = 0
@ -125,7 +126,7 @@ async def generate_frames2(video, threshold,stat):
# cv2.drawContours(image, hull, -1, (0, 255, 0), 3)
# line created to stop counting contours, needed as cars in distance become one big contour
lineypos = 125
lineypos = 100
# cv2.line(image, (0, lineypos), (width, lineypos), (255, 0, 0), 5)
# line y position created to count contours
@ -133,7 +134,7 @@ async def generate_frames2(video, threshold,stat):
cv2.line(image, (0, lineypos2), (width, lineypos2), (0, 255, 0), 5)
# min area for contours in case a bunch of small noise contours are created
minarea = 175
minarea = 200
# max area for contours, can be quite large for buses
maxarea = 50000
@ -291,6 +292,8 @@ async def generate_frames2(video, threshold,stat):
currentcars = currentcars + 1 # adds another to current cars on screen
currentcarsindex.append(i) # adds car ids to current cars on screen
kenderaan_sekarang = currentcars
for i in range(currentcars): # loops through all current car ids on screen
# grabs centroid of certain carid for current frame
@ -417,6 +420,7 @@ async def index():
video = request.args.get('video', 'video/video.mp4')
videonya = video
the_threshold = request.args.get('threshold', 450)
minimal_kepadatan = request.args.get('minimal_kepadatan', 5)
threshold = int(the_threshold)
try:
@ -429,10 +433,10 @@ async def index():
conn.close()
if len(result) == 0:
return await render_template('index2.html', video=video, threshold=threshold, video_list=video_list, stat="Belum Ada Data", selesainya=selesainya)
return await render_template('index2.html', video=video, threshold=threshold, video_list=video_list, stat="Belum Ada Data", selesainya=selesainya, minimal_kepadatan=minimal_kepadatan)
else :
print(result[0])
return await render_template('index2.html', video=video, threshold=threshold, video_list=video_list, stat=result[0], selesainya=selesainya)
return await render_template('index2.html', video=video, threshold=threshold, video_list=video_list, stat=result[0], selesainya=selesainya, minimal_kepadatan=minimal_kepadatan)
except Exception as e:
@ -467,7 +471,7 @@ app.add_url_rule('/video_feed', 'video_feed', video_feed)
@app.route('/check_jumlah_kenderaan', methods=['GET'])
async def check_jumlah_kenderaan():
global jumlah_kenderaan , kenderaan_kiri, kenderaan_kanan ,videonya ,selesainya
global jumlah_kenderaan , kenderaan_kiri, kenderaan_kanan ,videonya ,selesainya , kenderaan_sekarang
if (videonya != None):
conn = await get_db_connection()
async with conn.cursor() as cursor:
@ -483,7 +487,7 @@ async def check_jumlah_kenderaan():
kenderaan_kiri = result[0][4]
kenderaan_kanan = result[0][5]
waktu_sekarang = result[0][3]
return jsonify({'jumlah_kenderaan': jumlah_kenderaan, 'kenderaan_kiri': kenderaan_kiri, 'kenderaan_kanan': kenderaan_kanan, 'waktu_sekarang':waktu_sekarang , selesainya: selesainya})
return jsonify({'jumlah_kenderaan': jumlah_kenderaan, 'kenderaan_kiri': kenderaan_kiri, 'kenderaan_kanan': kenderaan_kanan, 'waktu_sekarang':waktu_sekarang , "selesainya": selesainya , "kenderaan_sekarang": kenderaan_sekarang})
# return jsonify({'jumlah_kenderaan': jumlah_kenderaan, 'kenderaan_kiri': kenderaan_kiri, 'kenderaan_kanan': kenderaan_kanan})

View File

@ -98,7 +98,7 @@
<div class="col-lg-8 col-md-10 col-xs-12">
<div class="box-content">
<h4 class="box-title">Form Analisa Video</h4>
<h4 class="box-title">Form Analisa Video {{ minimal_kepadatan }}</h4>
<div class="card-content">
<div class="form-group">
<label for="video">Pilih Video:</label>
@ -111,6 +111,24 @@
</select>
</div>
<div class="form-group">
<label for="video">Minimal Kepadatan:</label>
<select class="form-control" id="minimal_kepadatan" name="minimal_kepadatan">
<option value="-Minimal Kepadatan-" disabled selected>-Pilih Minimal Kepadatan-
</option>
<option value="2" {% if minimal_kepadatan==2 %}selected{% endif %}>2</option>
<option value="3" {% if minimal_kepadatan==3 %}selected{% endif %}>3</option>
<option value="4" {% if minimal_kepadatan==4 %}selected{% endif %}>4</option>
<option value="5" {% if minimal_kepadatan==5 %}selected{% endif %}>5</option>
<option value="6" {% if minimal_kepadatan==6 %}selected{% endif %}>6</option>
<option value="7" {% if minimal_kepadatan==7 %}selected{% endif %}>7</option>
<option value="8" {% if minimal_kepadatan==8 %}selected{% endif %}>8</option>
<option value="9" {% if minimal_kepadatan==9 %}selected{% endif %}>9</option>
<option value="10" {% if minimal_kepadatan==10 %}selected{% endif %}>10</option>
</select>
</div>
<div class="form-group text-center">
<button type="button" class="btn btn-primary" onclick="olah_video()">Proses
Video</button>
@ -181,7 +199,7 @@
</div>
<div class="row small-spacing">
<div class="col-lg-4 col-md-4 col-xs-12">
<div class="col-lg-3 col-md-3 col-xs-12">
<div class="box-content">
<div class="statistics-box with-icon">
<!-- <i class="ico fa fa-car text-info"></i> -->
@ -191,7 +209,7 @@
</div>
<!-- /.box-content -->
</div>
<div class="col-lg-4 col-md-4 col-xs-12">
<div class="col-lg-3 col-md-3 col-xs-12">
<div class="box-content">
<div class="statistics-box with-icon">
<!-- <i class="ico fa fa-car text-info"></i> -->
@ -201,7 +219,8 @@
</div>
<!-- /.box-content -->
</div>
<div class="col-lg-4 col-md-4 col-xs-12">
<input type="hidden" id="minimal_value" value="{{ minimal_kepadatan }}">
<div class="col-lg-3 col-md-3 col-xs-12">
<div class="box-content">
<div class="statistics-box with-icon">
<!-- <i class="ico fa fa-car text-info"></i> -->
@ -211,6 +230,16 @@
</div>
<!-- /.box-content -->
</div>
<div class="col-lg-3 col-md-3 col-xs-12">
<div class="box-content">
<div class="statistics-box with-icon">
<!-- <i class="ico fa fa-car text-info"></i> -->
<h2 class="counter text-info" id="kepadatan">...</h2>
<p class="text">Kepadatan Kenderaan</p>
</div>
</div>
<!-- /.box-content -->
</div>
</div>
@ -246,11 +275,14 @@
<script src="assets/scripts/main.min.js"></script>
<script src="assets/color-switcher/color-switcher.min.js"></script>
<script>
var minimal_kepadatan = document.getElementById('minimal_value').value;
$("#minimal_kepadatan").val(minimal_kepadatan);
function olah_video() {
var video = document.getElementById('video').value;
var minimal_kepadatan = document.getElementById('minimal_kepadatan').value;
// var threshold = document.getElementById('threshold').value;
var threshold = 450;
window.location.href = '/?video=' + video + '&threshold=' + threshold;
window.location.href = '/?video=' + video + '&threshold=' + threshold + '&minimal_kepadatan=' + minimal_kepadatan;
}
function check_jumlah_kenderaan() {
@ -260,6 +292,14 @@
url: "/check_jumlah_kenderaan",
success: function (data) {
// console.log(data)
console.log(data.kenderaan_sekarang)
if (data.kenderaan_sekarang == undefined) {
document.getElementById('kepadatan').innerHTML = "Belum Diproses"
} else if (data.kenderaan_sekarang >= minimal_kepadatan) {
document.getElementById('kepadatan').innerHTML = "Padat"
} else {
document.getElementById('kepadatan').innerHTML = "Tidak Padat"
}
document.getElementById('kiri').innerHTML = data.kenderaan_kiri
document.getElementById('kanan').innerHTML = data.kenderaan_kanan