Sunil Sarolkar
commited on
Commit
·
65c0b8a
1
Parent(s):
82a7886
handled closure of opened file
Browse files
app.py
CHANGED
|
@@ -394,53 +394,85 @@ if app_mode =='About App':
|
|
| 394 |
|
| 395 |
''')
|
| 396 |
|
| 397 |
-
elif app_mode ==
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
)
|
| 402 |
-
mask = (test_files_df['Category']
|
| 403 |
-
test_files_df_category
|
| 404 |
-
|
| 405 |
-
cls = st.sidebar.selectbox(
|
| 406 |
-
'Choose Class',
|
| 407 |
np.sort(test_files_df_category['Class'].unique(), axis=-1, kind='mergesort')
|
| 408 |
)
|
| 409 |
-
mask = (test_files_df['Class']
|
| 410 |
-
filename = st.sidebar.selectbox(
|
| 411 |
-
'Choose File',
|
| 412 |
np.sort(test_files_df_category[mask]['Filename'].unique(), axis=-1, kind='mergesort')
|
| 413 |
)
|
| 414 |
-
|
|
|
|
|
|
|
|
|
|
| 415 |
if st.sidebar.button("Start", type="primary"):
|
| 416 |
-
|
| 417 |
-
|
| 418 |
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
)
|
| 424 |
-
current_test_df = testing_df[mask]
|
| 425 |
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
)
|
| 430 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 431 |
|
| 432 |
st.sidebar.markdown('---')
|
| 433 |
-
|
|
|
|
| 434 |
|
| 435 |
runtime_progress = st.empty()
|
|
|
|
| 436 |
with runtime_progress.container():
|
| 437 |
-
df1 = pd.DataFrame([['--',
|
|
|
|
| 438 |
my_table = st.table(df1)
|
|
|
|
| 439 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
view = st.empty()
|
|
|
|
| 441 |
st.markdown("<hr/>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
| 442 |
|
| 443 |
-
# ✅ download video
|
| 444 |
vid_file = hf_hub_download(
|
| 445 |
repo_id="sunilsarolkar/isl-test-data",
|
| 446 |
filename=f'test/{category}/{cls}/{filename}',
|
|
@@ -448,120 +480,187 @@ elif app_mode == 'Run on Test Videos':
|
|
| 448 |
)
|
| 449 |
|
| 450 |
vid = cv2.VideoCapture(vid_file)
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
|
| 395 |
''')
|
| 396 |
|
| 397 |
+
elif app_mode =='Run on Test Videos':
|
| 398 |
+
# placeholder = st.empty()
|
| 399 |
+
category = st.sidebar.selectbox('Choose Category',
|
| 400 |
+
np.sort(test_files_df['Category'].unique(), axis=-1, kind='mergesort'))
|
| 401 |
+
# print(category)
|
| 402 |
+
mask = (test_files_df['Category']==category)
|
| 403 |
+
test_files_df_category=test_files_df[mask]
|
| 404 |
+
cls = st.sidebar.selectbox('Choose Class',
|
|
|
|
|
|
|
| 405 |
np.sort(test_files_df_category['Class'].unique(), axis=-1, kind='mergesort')
|
| 406 |
)
|
| 407 |
+
mask = (test_files_df['Class']==cls)
|
| 408 |
+
filename = st.sidebar.selectbox('Choose File',
|
|
|
|
| 409 |
np.sort(test_files_df_category[mask]['Filename'].unique(), axis=-1, kind='mergesort')
|
| 410 |
)
|
| 411 |
+
# print(f'test/{category}/{cls}/{filename}')
|
| 412 |
+
# mask = (include_df['Filepath'].str.contains(key[0])) & (include_df['type']==key[2]) & (include_df['expression']==key[1])
|
| 413 |
+
# stframe = st.empty()
|
| 414 |
+
|
| 415 |
if st.sidebar.button("Start", type="primary"):
|
| 416 |
+
mask = (testing_df['FileName'] == filename) & (testing_df['Type']==category)& (testing_df['Expression']==cls)
|
| 417 |
+
# filtered_df = current_test_df.sort_
|
| 418 |
|
| 419 |
+
window_size=20
|
| 420 |
+
current_test_df=testing_df[mask]
|
| 421 |
+
X_test_filtered,y_test_filtered = create_timeseries_data(current_test_df,feature_columns_new,label_columns,window_size=window_size)
|
| 422 |
+
# y_filtered_encoded=to_categorical(y_test_filtered, num_classes=len(df['Expression_encoded'].unique()))
|
| 423 |
+
X_test_filtered=np.array(X_test_filtered)
|
|
|
|
| 424 |
|
| 425 |
+
# encoded_translation=model(frame.reshape(1,frame.shape[0],frame.shape[1]))
|
| 426 |
+
st.set_option('deprecation.showfileUploaderEncoding', False)
|
| 427 |
+
|
| 428 |
+
# use_webcam = st.sidebar.button('Use Webcam')
|
| 429 |
+
# record = st.sidebar.checkbox("Record Video")
|
| 430 |
+
# if record:
|
| 431 |
+
# st.checkbox("Recording", value=True)
|
| 432 |
+
|
| 433 |
+
st.sidebar.markdown('---')
|
| 434 |
+
st.markdown(
|
| 435 |
+
"""
|
| 436 |
+
<style>
|
| 437 |
+
[data-testid="stSidebar"][aria-expanded="true"] > div:first-child {
|
| 438 |
+
width: 400px;
|
| 439 |
+
}
|
| 440 |
+
[data-testid="stSidebar"][aria-expanded="false"] > div:first-child {
|
| 441 |
+
width: 400px;
|
| 442 |
+
margin-left: -400px;
|
| 443 |
+
}
|
| 444 |
+
</style>
|
| 445 |
+
""",
|
| 446 |
+
unsafe_allow_html=True,
|
| 447 |
+
)
|
| 448 |
|
| 449 |
st.sidebar.markdown('---')
|
| 450 |
+
|
| 451 |
+
st.markdown(' ## Output')
|
| 452 |
|
| 453 |
runtime_progress = st.empty()
|
| 454 |
+
|
| 455 |
with runtime_progress.container():
|
| 456 |
+
df1 = pd.DataFrame([['--','--']], columns=['Frames Processed','Detected Class'])
|
| 457 |
+
|
| 458 |
my_table = st.table(df1)
|
| 459 |
+
# kpi1, kpi2 = st.columns(2)
|
| 460 |
|
| 461 |
+
# with kpi1:
|
| 462 |
+
# st.markdown("**Frames Processed**")
|
| 463 |
+
# kpi1_text = st.markdown(f'0/{current_test_df.shape[0]}')
|
| 464 |
+
|
| 465 |
+
# with kpi2:
|
| 466 |
+
# st.markdown("**Detected Class**")
|
| 467 |
+
# kpi2_text = st.markdown("--")
|
| 468 |
+
|
| 469 |
view = st.empty()
|
| 470 |
+
|
| 471 |
st.markdown("<hr/>", unsafe_allow_html=True)
|
| 472 |
+
stframes = st.empty()#[st.empty() for _ in range(20)]
|
| 473 |
+
# video_file_buffer = st.sidebar.file_uploader("Upload a video", type=[ "mp4", "mov",'avi','asf', 'm4v' ])
|
| 474 |
+
# tfflie = tempfile.NamedTemporaryFile(delete=False)
|
| 475 |
|
|
|
|
| 476 |
vid_file = hf_hub_download(
|
| 477 |
repo_id="sunilsarolkar/isl-test-data",
|
| 478 |
filename=f'test/{category}/{cls}/{filename}',
|
|
|
|
| 480 |
)
|
| 481 |
|
| 482 |
vid = cv2.VideoCapture(vid_file)
|
| 483 |
+
|
| 484 |
+
ffprobe_result = ffprobe(vid_file)
|
| 485 |
+
info = json.loads(ffprobe_result.json)
|
| 486 |
+
videoinfo = [i for i in info["streams"] if i["codec_type"] == "video"][0]
|
| 487 |
+
input_fps = videoinfo["avg_frame_rate"]
|
| 488 |
+
# input_fps = float(input_fps[0])/float(input_fps[1])
|
| 489 |
+
input_pix_fmt = videoinfo["pix_fmt"]
|
| 490 |
+
input_vcodec = videoinfo["codec_name"]
|
| 491 |
+
postfix = info["format"]["format_name"].split(",")[0]
|
| 492 |
+
# print(f'input_vcodec-{input_vcodec}')
|
| 493 |
+
|
| 494 |
+
width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 495 |
+
height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 496 |
+
fps_input = int(vid.get(cv2.CAP_PROP_FPS))
|
| 497 |
+
|
| 498 |
+
#codec = cv2.VideoWriter_fourcc(*FLAGS.output_format)
|
| 499 |
+
# codec = cv2.VideoWriter_fourcc('V','P','0','9')
|
| 500 |
+
# out = cv2.VideoWriter('output1.mp4', codec, fps_input, (width, height))
|
| 501 |
+
|
| 502 |
+
# st.sidebar.text('Input Video')
|
| 503 |
+
# st.sidebar.video(tfflie.name)
|
| 504 |
+
fps = 0
|
| 505 |
+
i = 0
|
| 506 |
+
|
| 507 |
+
|
| 508 |
+
|
| 509 |
+
# cap = cv2.VideoCapture(video_file,)
|
| 510 |
+
totalFrames=int(vid.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 511 |
+
window_size=20
|
| 512 |
+
# print('current_test_df',current_test_df)
|
| 513 |
+
# print('totalFrames',totalFrames)
|
| 514 |
+
window=[]
|
| 515 |
+
|
| 516 |
+
|
| 517 |
+
prevTime = 0
|
| 518 |
+
postfix = info["format"]["format_name"].split(",")[0]
|
| 519 |
+
|
| 520 |
+
|
| 521 |
+
output_file = f"/tmp/output_{uuid.uuid4().hex}.{postfix}"
|
| 522 |
+
|
| 523 |
+
# height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 524 |
+
fps_input = int(vid.get(cv2.CAP_PROP_FPS))
|
| 525 |
+
|
| 526 |
+
#codec = cv2.VideoWriter_fourcc(*FLAGS.output_format)
|
| 527 |
+
|
| 528 |
+
# codec = cv2.VideoWriter_fourcc('m','p','4','v')
|
| 529 |
+
out = None
|
| 530 |
+
writer=None
|
| 531 |
+
weighted_avg_dict={}
|
| 532 |
+
|
| 533 |
+
idx=0
|
| 534 |
+
try:
|
| 535 |
+
for _, row in current_test_df.iterrows():#enumerate(file_df.rolling(window=20, step=20,min_periods=1)):
|
| 536 |
+
# print(f'captured frame#{idx}')
|
| 537 |
+
if not vid.isOpened():
|
| 538 |
+
st.error(f"Could not open video: {vid_file}")
|
| 539 |
+
else:
|
| 540 |
+
if(vid.isOpened()):
|
| 541 |
+
ret, frame = vid.read()
|
| 542 |
+
|
| 543 |
+
|
| 544 |
+
if len(window)<window_size:
|
| 545 |
+
canvas=util.drawStickmodel(frame,eval(row['bodypose_circles']),eval(row['bodypose_sticks']),eval(row['handpose_edges']),eval(row['handpose_peaks']))
|
| 546 |
+
canvas_with_plot=util.draw_bar_plot_below_image(canvas,{}, f'Prediction bar plot - Frame number {idx+1} [** no predictions]',canvas)
|
| 547 |
+
canvas_with_plot=util.draw_bar_plot_below_image(canvas_with_plot,weighted_avg_dict, f'Weighted avg - Frame number {idx+1} [** no predictions]',canvas)
|
| 548 |
+
canvas_with_plot=util.add_padding_to_bottom(canvas_with_plot,(255,255,255),100)# Adds padding at bottom
|
| 549 |
+
|
| 550 |
+
if writer is None:
|
| 551 |
+
input_framesize = canvas_with_plot.shape[:2]
|
| 552 |
+
writer = Writer(output_file, input_fps, input_framesize, input_pix_fmt,
|
| 553 |
+
input_vcodec)
|
| 554 |
+
|
| 555 |
+
# if out is None:
|
| 556 |
+
# out=cv2.VideoWriter(output_file, codec, fps_input, frame.shape[:2])
|
| 557 |
+
|
| 558 |
+
|
| 559 |
+
writer(canvas_with_plot)
|
| 560 |
+
# out.write(canvas)
|
| 561 |
+
with runtime_progress.container():
|
| 562 |
+
df1 = pd.DataFrame([[f'{idx+1}/{current_test_df.shape[0]}','<model will output after 20 frames>']], columns=['Frames Processed','Detected Class'])
|
| 563 |
+
|
| 564 |
+
my_table = st.table(df1)
|
| 565 |
+
window.append(frame)
|
| 566 |
+
# kpi1_text.write(f"<h1 style='text-align: center; color: red;'>{idx+1}/{current_test_df.shape[0]}</h1>", unsafe_allow_html=True)
|
| 567 |
+
# kpi2_text.write(f"<h1 style='text-align: center; color: red;'>--</h1>", unsafe_allow_html=True)
|
| 568 |
+
with view.container():
|
| 569 |
+
st.image(canvas_with_plot,channels = 'BGR',use_column_width=True)
|
| 570 |
+
else:
|
| 571 |
+
|
| 572 |
+
window[:-1] = window[1:]
|
| 573 |
+
window[-1]=frame
|
| 574 |
+
translation_model=get_translator_model()
|
| 575 |
+
# testing_df[]
|
| 576 |
+
|
| 577 |
+
encoded_translation = translation_model(X_test_filtered[idx-20].reshape(1,X_test_filtered[idx-20].shape[0],X_test_filtered[idx-20].shape[1]))
|
| 578 |
+
encoded_translation=encoded_translation[0].cpu().detach().numpy()
|
| 579 |
+
sorted_index=np.argsort(encoded_translation)[::-1]
|
| 580 |
+
maxindex=np.argmax(encoded_translation)
|
| 581 |
+
|
| 582 |
+
top_3_probs = encoded_translation.argsort()[-3:][::-1] # Get indices of top 3 probabilities (descending order)
|
| 583 |
+
top_3_categories = [expression_mapping[i] for i in top_3_probs] # Convert indices to category names (assuming class_names list exists)
|
| 584 |
+
top_3_values = encoded_translation[top_3_probs] # Get corresponding probabilities
|
| 585 |
+
# print(f'{idx} {encoded_translation[maxindex]:0.4f} {maxindex}-{expression_mapping[maxindex]} ')#{[(pi,encoded_translation[pi],expression_mapping[pi]) for pi in sorted_index]}
|
| 586 |
+
for category, prob in zip(top_3_categories, top_3_values):
|
| 587 |
+
if category not in frame_wise_outputs:
|
| 588 |
+
frame_wise_outputs[category]=[]
|
| 589 |
+
frame_wise_outputs[category].append(prob)
|
| 590 |
+
current_prob={}
|
| 591 |
+
|
| 592 |
+
for category, prob in zip(top_3_categories, top_3_values):
|
| 593 |
+
current_prob[category]=prob
|
| 594 |
+
|
| 595 |
+
for key in frame_wise_outputs:
|
| 596 |
+
weighted_avg_dict[key]=weighted_average(frame_wise_outputs[key],[len(frame_wise_outputs[key]) for i in range(len(frame_wise_outputs[key]))])
|
| 597 |
+
|
| 598 |
+
sorted_dict = dict(sorted(weighted_avg_dict.items(), key=lambda item: item[1], reverse=True))
|
| 599 |
+
canvas=util.drawStickmodel(frame,eval(row['bodypose_circles']),eval(row['bodypose_sticks']),eval(row['handpose_edges']),eval(row['handpose_peaks']))
|
| 600 |
+
canvas_with_plot=util.draw_bar_plot_below_image(canvas,current_prob, f'Prediction at frame window({idx-20+1}-{idx+1})',canvas)
|
| 601 |
+
canvas_with_plot=util.draw_bar_plot_below_image(canvas_with_plot,weighted_avg_dict, f'Weighted avg till window {idx+1}',canvas)
|
| 602 |
+
canvas_with_plot=util.add_padding_to_bottom(canvas_with_plot,(255,255,255),100)
|
| 603 |
+
writer(canvas_with_plot)
|
| 604 |
+
|
| 605 |
+
|
| 606 |
+
currTime = time.time()
|
| 607 |
+
fps = 1 / (currTime - prevTime)
|
| 608 |
+
prevTime = currTime
|
| 609 |
+
# out.write(frame)
|
| 610 |
+
# if record:
|
| 611 |
+
# #st.checkbox("Recording", value=True)
|
| 612 |
+
# out.write(frame)
|
| 613 |
+
#Dashboard
|
| 614 |
+
|
| 615 |
+
max_prob = float('-inf') # Initialize with negative infinity
|
| 616 |
+
max_key = None
|
| 617 |
+
|
| 618 |
+
for exp, prob in weighted_avg_dict.items():
|
| 619 |
+
if prob > max_prob:
|
| 620 |
+
max_prob = prob
|
| 621 |
+
max_key = exp
|
| 622 |
+
with runtime_progress.container():
|
| 623 |
+
df1 = pd.DataFrame([[f'{idx+1}/{current_test_df.shape[0]}',f'{max_key} ({max_prob*100:.2f}%)']], columns=['Frames Processed','Detected Class'])
|
| 624 |
+
my_table = st.table(df1)
|
| 625 |
+
# kpi1_text.write(f"<h1 style='text-align: center; color: red;'>{idx+1}/{current_test_df.shape[0]}</h1>", unsafe_allow_html=True)
|
| 626 |
+
# kpi2_text.write(f"<h1 style='text-align: center; color: red;'>{max_key} ({max_prob*100:.2f}%)</h1>", unsafe_allow_html=True)
|
| 627 |
+
# with placeholder.container():
|
| 628 |
+
# # st.write(weighted_avg_dict)
|
| 629 |
+
# # data = {
|
| 630 |
+
# # "I": 0.7350964583456516,
|
| 631 |
+
# # "Hello": 0.1078806109726429,
|
| 632 |
+
# # "you": 0.11776176246348768,
|
| 633 |
+
# # "you (plural)": 0.12685142129916568
|
| 634 |
+
# # }
|
| 635 |
+
|
| 636 |
+
# # Convert the dictionary to a Pandas DataFrame for easier plotting
|
| 637 |
+
# df = pd.DataFrame.from_dict(weighted_avg_dict, orient='index', columns=['Values'])
|
| 638 |
+
|
| 639 |
+
# # Create a bar chart with Streamlit
|
| 640 |
+
# st.bar_chart(df)
|
| 641 |
+
# frame = cv2.resize(frame,(0,0),fx = 0.8 , fy = 0.8)
|
| 642 |
+
# frame = image_resize(image = frame, width = 640)
|
| 643 |
+
with view.container():
|
| 644 |
+
st.image(canvas_with_plot,channels = 'BGR',use_column_width=True)
|
| 645 |
+
|
| 646 |
+
idx=idx+1
|
| 647 |
+
|
| 648 |
+
|
| 649 |
+
# st.text('Video Processed')
|
| 650 |
+
with view.container():
|
| 651 |
+
if writer is not None: # ✅ safeguard
|
| 652 |
+
writer.close()
|
| 653 |
+
output_video = open(output_file,'rb')
|
| 654 |
+
out_bytes = output_video.read()
|
| 655 |
+
st.video(out_bytes)
|
| 656 |
+
print(f'Output file - {output_file}')
|
| 657 |
+
else:
|
| 658 |
+
st.warning("No video was processed, writer is empty.")
|
| 659 |
+
# out.release()
|
| 660 |
+
|
| 661 |
+
print(f'Output file - {output_file}')
|
| 662 |
+
finally:
|
| 663 |
+
vid.release()
|
| 664 |
+
if writer is not None:
|
| 665 |
+
writer.close()
|
| 666 |
+
cv2.destroyAllWindows()
|